diff options
Diffstat (limited to 'plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedIOUtil.java')
-rw-r--r-- | plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedIOUtil.java | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedIOUtil.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedIOUtil.java index f92ee469a3..a0a4847b53 100644 --- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedIOUtil.java +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ExtendedIOUtil.java @@ -56,10 +56,100 @@ public final class ExtendedIOUtil private static final int CHARACTER_BUFFER_SIZE = 4096; + private static final byte[] EMPTY_BYTE_ARRAY = {}; + private ExtendedIOUtil() { } + /** + * @since 3.7 + */ + public static void writeVarInt(DataOutput out, int v) throws IOException + { + while ((v & ~0x7f) != 0) + { + out.writeByte((byte)(0x80 | v & 0x7f)); + v >>>= 7; + } + + out.writeByte((byte)v); + } + + /** + * @since 3.7 + */ + public static int readVarInt(DataInput in) throws IOException + { + int b = in.readByte(); + if (b >= 0) + { + return b; + } + + int v = b & 0x7f; + b = in.readByte(); + if (b >= 0) + { + return v | b << 7; + } + + v |= (b & 0x7f) << 7; + b = in.readByte(); + if (b >= 0) + { + return v | b << 14; + } + + v |= (b & 0x7f) << 14; + b = in.readByte(); + if (b >= 0) + { + return v | b << 21; + } + + v |= (b & 0x7f) << 21; + b = in.readByte(); + return v | b << 28; + } + + /** + * @since 3.7 + */ + public static void writeVarLong(DataOutput out, long v) throws IOException + { + while ((v & ~0x7f) != 0) + { + out.writeByte((byte)(v & 0x7f | 0x80)); + v >>>= 7; + } + + out.writeByte((byte)v); + } + + /** + * @since 3.7 + */ + public static long readVarLong(DataInput in) throws IOException + { + long v = in.readByte(); + if (v >= 0) + { + return v; + } + + v &= 0x7f; + for (int s = 7;; s += 7) + { + long b = in.readByte(); + v |= (b & 0x7f) << s; + if (b >= 0) + { + return v; + } + } + } + public static void writeByteArray(DataOutput out, byte[] b) throws IOException { if (b != null) @@ -81,6 +171,11 @@ public final class ExtendedIOUtil return null; } + if (length == 0) + { + return EMPTY_BYTE_ARRAY; + } + byte[] b; try { |