A small doc change to reflect StreamOutput.writeVInt() does support negative numbers but not efficiently. StreamOutput.writeVLong & StreamInput.readVLong really support it.

This is to better describe the current situation. We probably want to normalize these methods and potentially add optimization/support for -1 values.
This commit is contained in:
Boaz Leskes 2013-06-25 14:13:44 +02:00
parent 4c0b10aec7
commit 99cb26fa02
2 changed files with 44 additions and 17 deletions

View File

@ -117,22 +117,31 @@ public abstract class StreamInput extends InputStream {
/** /**
* Reads an int stored in variable-length format. Reads between one and * Reads an int stored in variable-length format. Reads between one and
* five bytes. Smaller values take fewer bytes. Negative numbers are not * five bytes. Smaller values take fewer bytes. Negative numbers
* supported. * will always use all 5 bytes and are therefore better serialized
* using {@link #readInt}
*/ */
public int readVInt() throws IOException { public int readVInt() throws IOException {
byte b = readByte(); byte b = readByte();
int i = b & 0x7F; int i = b & 0x7F;
if ((b & 0x80) == 0) return i; if ((b & 0x80) == 0) {
return i;
}
b = readByte(); b = readByte();
i |= (b & 0x7F) << 7; i |= (b & 0x7F) << 7;
if ((b & 0x80) == 0) return i; if ((b & 0x80) == 0) {
return i;
}
b = readByte(); b = readByte();
i |= (b & 0x7F) << 14; i |= (b & 0x7F) << 14;
if ((b & 0x80) == 0) return i; if ((b & 0x80) == 0) {
return i;
}
b = readByte(); b = readByte();
i |= (b & 0x7F) << 21; i |= (b & 0x7F) << 21;
if ((b & 0x80) == 0) return i; if ((b & 0x80) == 0) {
return i;
}
b = readByte(); b = readByte();
assert (b & 0x80) == 0; assert (b & 0x80) == 0;
return i | ((b & 0x7F) << 28); return i | ((b & 0x7F) << 28);
@ -153,28 +162,44 @@ public abstract class StreamInput extends InputStream {
public long readVLong() throws IOException { public long readVLong() throws IOException {
byte b = readByte(); byte b = readByte();
long i = b & 0x7FL; long i = b & 0x7FL;
if ((b & 0x80) == 0) return i; if ((b & 0x80) == 0) {
return i;
}
b = readByte(); b = readByte();
i |= (b & 0x7FL) << 7; i |= (b & 0x7FL) << 7;
if ((b & 0x80) == 0) return i; if ((b & 0x80) == 0) {
return i;
}
b = readByte(); b = readByte();
i |= (b & 0x7FL) << 14; i |= (b & 0x7FL) << 14;
if ((b & 0x80) == 0) return i; if ((b & 0x80) == 0) {
return i;
}
b = readByte(); b = readByte();
i |= (b & 0x7FL) << 21; i |= (b & 0x7FL) << 21;
if ((b & 0x80) == 0) return i; if ((b & 0x80) == 0) {
return i;
}
b = readByte(); b = readByte();
i |= (b & 0x7FL) << 28; i |= (b & 0x7FL) << 28;
if ((b & 0x80) == 0) return i; if ((b & 0x80) == 0) {
return i;
}
b = readByte(); b = readByte();
i |= (b & 0x7FL) << 35; i |= (b & 0x7FL) << 35;
if ((b & 0x80) == 0) return i; if ((b & 0x80) == 0) {
return i;
}
b = readByte(); b = readByte();
i |= (b & 0x7FL) << 42; i |= (b & 0x7FL) << 42;
if ((b & 0x80) == 0) return i; if ((b & 0x80) == 0) {
return i;
}
b = readByte(); b = readByte();
i |= (b & 0x7FL) << 49; i |= (b & 0x7FL) << 49;
if ((b & 0x80) == 0) return i; if ((b & 0x80) == 0) {
return i;
}
b = readByte(); b = readByte();
assert (b & 0x80) == 0; assert (b & 0x80) == 0;
return i | ((b & 0x7FL) << 56); return i | ((b & 0x7FL) << 56);

View File

@ -133,8 +133,9 @@ public abstract class StreamOutput extends OutputStream {
/** /**
* Writes an int in a variable-length format. Writes between one and * Writes an int in a variable-length format. Writes between one and
* five bytes. Smaller values take fewer bytes. Negative numbers are not * five bytes. Smaller values take fewer bytes. Negative numbers
* supported. * will always use all 5 bytes and are therefore better serialized
* using {@link #writeInt}
*/ */
public void writeVInt(int i) throws IOException { public void writeVInt(int i) throws IOException {
while ((i & ~0x7F) != 0) { 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 * bytes. Smaller values take fewer bytes. Negative numbers are not
* supported. * supported.
*/ */
public void writeVLong(long i) throws IOException { public void writeVLong(long i) throws IOException {
assert i >= 0;
while ((i & ~0x7F) != 0) { while ((i & ~0x7F) != 0) {
writeByte((byte) ((i & 0x7f) | 0x80)); writeByte((byte) ((i & 0x7f) | 0x80));
i >>>= 7; i >>>= 7;