Simplify ByteBufInputStream.readLine() logic (#8380)

Motivation:

While looking at the nice optimization done in
https://github.com/netty/netty/pull/8347 I couldn't help noticing the
logic could be simplified further. Apologies if this is just my OCD and
inappropriate!

Modifications:

Reduce amount of code used for ByteBufInputStream.readLine()

Result:

Slightly smaller and simpler code
This commit is contained in:
Nick Hill 2018-10-12 21:24:40 -07:00 committed by Norman Maurer
parent adb4ce1f31
commit 7062ceedb0

View File

@ -245,19 +245,15 @@ public class ByteBufInputStream extends InputStream implements DataInput {
@Override @Override
public String readLine() throws IOException { public String readLine() throws IOException {
if (!buffer.isReadable()) {
return null;
}
if (lineBuf != null) { if (lineBuf != null) {
lineBuf.setLength(0); lineBuf.setLength(0);
} }
boolean anyChar = false; loop: do {
loop: while (true) {
if (!buffer.isReadable()) {
return toStringIfAnyChar(lineBuf, anyChar);
}
int c = buffer.readUnsignedByte(); int c = buffer.readUnsignedByte();
anyChar = true;
switch (c) { switch (c) {
case '\n': case '\n':
break loop; break loop;
@ -274,19 +270,11 @@ public class ByteBufInputStream extends InputStream implements DataInput {
} }
lineBuf.append((char) c); lineBuf.append((char) c);
} }
} } while (buffer.isReadable());
return lineBuf != null && lineBuf.length() > 0 ? lineBuf.toString() : StringUtil.EMPTY_STRING; return lineBuf != null && lineBuf.length() > 0 ? lineBuf.toString() : StringUtil.EMPTY_STRING;
} }
private static String toStringIfAnyChar(StringBuilder lineBuf, boolean anyChars) {
if (anyChars) {
return lineBuf != null && lineBuf.length() > 0 ? lineBuf.toString() : StringUtil.EMPTY_STRING;
} else {
return null;
}
}
@Override @Override
public long readLong() throws IOException { public long readLong() throws IOException {
checkAvailable(8); checkAvailable(8);