diff --git a/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index 850fef392a8..cc29ff6325d 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -117,22 +117,31 @@ public abstract class StreamInput extends InputStream { /** * Reads an int stored in variable-length format. Reads between one and - * five bytes. Smaller values take fewer bytes. Negative numbers are not - * supported. + * five bytes. Smaller values take fewer bytes. Negative numbers + * will always use all 5 bytes and are therefore better serialized + * using {@link #readInt} */ public int readVInt() throws IOException { byte b = readByte(); int i = b & 0x7F; - if ((b & 0x80) == 0) return i; + if ((b & 0x80) == 0) { + return i; + } b = readByte(); i |= (b & 0x7F) << 7; - if ((b & 0x80) == 0) return i; + if ((b & 0x80) == 0) { + return i; + } b = readByte(); i |= (b & 0x7F) << 14; - if ((b & 0x80) == 0) return i; + if ((b & 0x80) == 0) { + return i; + } b = readByte(); i |= (b & 0x7F) << 21; - if ((b & 0x80) == 0) return i; + if ((b & 0x80) == 0) { + return i; + } b = readByte(); assert (b & 0x80) == 0; return i | ((b & 0x7F) << 28); @@ -153,28 +162,44 @@ public abstract class StreamInput extends InputStream { public long readVLong() throws IOException { byte b = readByte(); long i = b & 0x7FL; - if ((b & 0x80) == 0) return i; + if ((b & 0x80) == 0) { + return i; + } b = readByte(); i |= (b & 0x7FL) << 7; - if ((b & 0x80) == 0) return i; + if ((b & 0x80) == 0) { + return i; + } b = readByte(); i |= (b & 0x7FL) << 14; - if ((b & 0x80) == 0) return i; + if ((b & 0x80) == 0) { + return i; + } b = readByte(); i |= (b & 0x7FL) << 21; - if ((b & 0x80) == 0) return i; + if ((b & 0x80) == 0) { + return i; + } b = readByte(); i |= (b & 0x7FL) << 28; - if ((b & 0x80) == 0) return i; + if ((b & 0x80) == 0) { + return i; + } b = readByte(); i |= (b & 0x7FL) << 35; - if ((b & 0x80) == 0) return i; + if ((b & 0x80) == 0) { + return i; + } b = readByte(); i |= (b & 0x7FL) << 42; - if ((b & 0x80) == 0) return i; + if ((b & 0x80) == 0) { + return i; + } b = readByte(); i |= (b & 0x7FL) << 49; - if ((b & 0x80) == 0) return i; + if ((b & 0x80) == 0) { + return i; + } b = readByte(); assert (b & 0x80) == 0; return i | ((b & 0x7FL) << 56); diff --git a/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index 70884098a3a..1997d22a5f4 100644 --- a/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -133,8 +133,9 @@ public abstract class StreamOutput extends OutputStream { /** * Writes an int in a variable-length format. Writes between one and - * five bytes. Smaller values take fewer bytes. Negative numbers are not - * supported. + * five bytes. Smaller values take fewer bytes. Negative numbers + * will always use all 5 bytes and are therefore better serialized + * using {@link #writeInt} */ public void writeVInt(int i) throws IOException { while ((i & ~0x7F) != 0) { @@ -153,11 +154,12 @@ public abstract class StreamOutput extends OutputStream { } /** - * Writes an long in a variable-length format. Writes between one and five + * Writes an long in a variable-length format. Writes between one and nine * bytes. Smaller values take fewer bytes. Negative numbers are not * supported. */ public void writeVLong(long i) throws IOException { + assert i >= 0; while ((i & ~0x7F) != 0) { writeByte((byte) ((i & 0x7f) | 0x80)); i >>>= 7;