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:
parent
adb4ce1f31
commit
7062ceedb0
@ -245,19 +245,15 @@ public class ByteBufInputStream extends InputStream implements DataInput {
|
||||
|
||||
@Override
|
||||
public String readLine() throws IOException {
|
||||
if (!buffer.isReadable()) {
|
||||
return null;
|
||||
}
|
||||
if (lineBuf != null) {
|
||||
lineBuf.setLength(0);
|
||||
}
|
||||
|
||||
boolean anyChar = false;
|
||||
|
||||
loop: while (true) {
|
||||
if (!buffer.isReadable()) {
|
||||
return toStringIfAnyChar(lineBuf, anyChar);
|
||||
}
|
||||
|
||||
loop: do {
|
||||
int c = buffer.readUnsignedByte();
|
||||
anyChar = true;
|
||||
switch (c) {
|
||||
case '\n':
|
||||
break loop;
|
||||
@ -274,19 +270,11 @@ public class ByteBufInputStream extends InputStream implements DataInput {
|
||||
}
|
||||
lineBuf.append((char) c);
|
||||
}
|
||||
}
|
||||
} while (buffer.isReadable());
|
||||
|
||||
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
|
||||
public long readLong() throws IOException {
|
||||
checkAvailable(8);
|
||||
|
Loading…
Reference in New Issue
Block a user