boolean firstPiece = (current_position == 0);
// This is the pieceSize accomadating for the unprepended ellipsis
int maxByteCount = (firstPiece ? pieceSize : pieceSize - 3);
ByteArray piece = new ByteArray(text.substring(current_position, current_position + chars_pulled));
while(piece.length() > maxByteCount) {
// Estimate that we need to remove half the overflow, since most unicode chars are 2-bytes
int deltaEstimate = (piece.length() - pieceSize + 1) / 2;
// Don't remove less than 1 character
if(deltaEstimate < 1)
deltaEstimate = 1;
Out.debug(getClass(), "deltaEstimate=" + deltaEstimate);
chars_pulled -= deltaEstimate;
piece = new ByteArray(text.substring(current_position, current_position + chars_pulled));
}
boolean lastPiece = (current_position + chars_pulled >= text.length());
if(!lastPiece) {
// This is not the last piece; append ellipsis
chars_pulled -= 3;
piece = new ByteArray(text.substring(current_position, current_position + chars_pulled)).concat("...".getBytes());
}
if(!firstPiece) {
// This is not the first piece; prepend ellipsis
piece = new ByteArray("...".getBytes()).concat(piece);
}
// Cryptos
if(enabledCryptos != 0)
piece = GenericCrypto.encode(piece, enabledCryptos);