in codec/src/main/java/org/apache/mina/codec/textline/TextLineDecoder.java [188:257]
private String decodeAuto(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();
boolean matched = false;
switch (b) {
case '\r':
// Might be Mac, but we don't auto-detect Mac EOL
// to avoid confusion.
matchCount++;
break;
case '\n':
// UNIX
matchCount++;
matched = true;
break;
default:
matchCount = 0;
}
if (matched) {
// 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 = buffer.toString();
} else {
int overflowPosition = ctx.getOverflowLength();
throw new IllegalStateException("Line is too long: " + overflowPosition);
}
} catch (CharacterCodingException cce) {
throw new ProtocolDecoderException(cce);
} finally {
ctx.reset();
}
oldPos = pos;
matchCount = 0;
}
}
// Put remainder to buf.
in.position(oldPos);
ctx.append(in);
ctx.setMatchCount(matchCount);
return decoded;
}