in codec/src/main/java/org/apache/mina/codec/textline/TextLineDecoder.java [264:324]
private String decodeNormal(Context ctx, ByteBuffer in) {
String decoded = null;
int matchCount = ctx.getMatchCount();
// Try to find a match
int oldPos = in.position();
int oldLimit = in.limit();
while (in.hasRemaining() && decoded == null) {
byte b = in.get();
if (delimBuf.get(matchCount) == b) {
matchCount++;
if (matchCount == delimBuf.limit()) {
// Found a match.
int pos = in.position();
in.limit(pos);
in.position(oldPos);
ctx.append(in);
in.limit(oldLimit);
in.position(pos);
try {
if (ctx.getOverflowLength() == 0) {
ByteBuffer buf = ctx.getBuffer();
buf.flip();
buf.limit(buf.limit() - matchCount);
CharsetDecoder decoder = ctx.getDecoder();
CharBuffer buffer = decoder.decode(buf);
decoded = new String(buffer.array());
} else {
int overflowLength = ctx.getOverflowLength();
throw new IllegalStateException("Line is too long: " + overflowLength);
}
} catch (CharacterCodingException cce) {
throw new ProtocolDecoderException(cce);
} finally {
ctx.reset();
}
oldPos = pos;
matchCount = 0;
}
} else {
// fix for DIRMINA-506 & DIRMINA-536
in.position(Math.max(0, in.position() - matchCount));
matchCount = 0;
}
}
// Put remainder to buf.
in.position(oldPos);
ctx.append(in);
ctx.setMatchCount(matchCount);
return decoded;
}