From 7062ceedb0ed0d781bdad0002057abcf50b0c92d Mon Sep 17 00:00:00 2001 From: Nick Hill Date: Fri, 12 Oct 2018 21:24:40 -0700 Subject: [PATCH] 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 --- .../io/netty/buffer/ByteBufInputStream.java | 22 +++++-------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/buffer/src/main/java/io/netty/buffer/ByteBufInputStream.java b/buffer/src/main/java/io/netty/buffer/ByteBufInputStream.java index b7b67d5408..038cd8db45 100644 --- a/buffer/src/main/java/io/netty/buffer/ByteBufInputStream.java +++ b/buffer/src/main/java/io/netty/buffer/ByteBufInputStream.java @@ -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);