@Test public void testWritePackedLong() throws IOException { for (int i = -64; i <= 63; ++i) { StringBuilder sb = new StringBuilder(); writeLong(dos(sb), i); Assert.assertEquals("i="+i, 2, sb.length()); } for (int i = -4096; i <= 4095; ++i) { if (i < -64 || i > 63) { StringBuilder sb = new StringBuilder(); writeLong(dos(sb), i); Assert.assertEquals("i="+i, 4, sb.length()); } } for (int i = -1048576; i <= 1048575; ++i) { if (i < -4096 || i > 4095) { StringBuilder sb = new StringBuilder(); writeLong(dos(sb), i); Assert.assertEquals("i="+i, 6, sb.length()); } } }
public static void writeLong(DataOutput out, long l) throws IOException { if (l <= 63 && l >= -64) { out.writeByte(((int) l) << 1 | 0x01); return; } final int cBits = 65 - Long.numberOfLeadingZeros(Math.max(l, ~l)); int i = (int) l; if (((1L << cBits) & 0x3E3E00L) != 0) { if (cBits <= 13) { out.writeShort(0b010_00000000 | (i & 0x1F00) << 3 | (i & 0x00FF)); } else { out.writeByte(0b110 | (i & 0x1F0000) >>> 13); out.writeShort(i); } return; } int cBytes = (cBits + 7) >>> 3; out.writeByte((cBytes - 1) << 2); switch (cBytes) { case 1: out.writeByte(i); break; case 2: out.writeShort(i); break; case 3: out.writeByte(i >>> 16); out.writeShort(i); break; case 4: out.writeInt(i); break; case 5: out.writeByte((int) (l >>> 32)); out.writeInt(i); break; case 6: out.writeShort((int) (l >>> 32)); out.writeInt(i); break; case 7: out.writeByte((int) (l >>> 48)); out.writeShort((int) (l >>> 32)); out.writeInt(i); break; case 8: out.writeLong(l); break; default: throw new IllegalStateException("n=" + l); } }
PackedInteger implements Comparable<PackedInteger> { public static void writeLong(DataOutput out, long l) throws IOException { if (l <= 63 && l >= -64) { out.writeByte(((int) l) << 1 | 0x01); return; } final int cBits = 65 - Long.numberOfLeadingZeros(Math.max(l, ~l)); int i = (int) l; if (((1L << cBits) & 0x3E3E00L) != 0) { if (cBits <= 13) { out.writeShort(0b010_00000000 | (i & 0x1F00) << 3 | (i & 0x00FF)); } else { out.writeByte(0b110 | (i & 0x1F0000) >>> 13); out.writeShort(i); } return; } int cBytes = (cBits + 7) >>> 3; out.writeByte((cBytes - 1) << 2); switch (cBytes) { case 1: out.writeByte(i); break; case 2: out.writeShort(i); break; case 3: out.writeByte(i >>> 16); out.writeShort(i); break; case 4: out.writeInt(i); break; case 5: out.writeByte((int) (l >>> 32)); out.writeInt(i); break; case 6: out.writeShort((int) (l >>> 32)); out.writeInt(i); break; case 7: out.writeByte((int) (l >>> 48)); out.writeShort((int) (l >>> 32)); out.writeInt(i); break; case 8: out.writeLong(l); break; default: throw new IllegalStateException("n=" + l); } } }
PackedInteger implements Comparable<PackedInteger> { public static void writeLong(DataOutput out, long l) throws IOException { if (l <= 63 && l >= -64) { out.writeByte(((int) l) << 1 | 0x01); return; } final int cBits = 65 - Long.numberOfLeadingZeros(Math.max(l, ~l)); int i = (int) l; if (((1L << cBits) & 0x3E3E00L) != 0) { if (cBits <= 13) { out.writeShort(0b010_00000000 | (i & 0x1F00) << 3 | (i & 0x00FF)); } else { out.writeByte(0b110 | (i & 0x1F0000) >>> 13); out.writeShort(i); } return; } int cBytes = (cBits + 7) >>> 3; out.writeByte((cBytes - 1) << 2); switch (cBytes) { case 1: out.writeByte(i); break; case 2: out.writeShort(i); break; case 3: out.writeByte(i >>> 16); out.writeShort(i); break; case 4: out.writeInt(i); break; case 5: out.writeByte((int) (l >>> 32)); out.writeInt(i); break; case 6: out.writeShort((int) (l >>> 32)); out.writeInt(i); break; case 7: out.writeByte((int) (l >>> 48)); out.writeShort((int) (l >>> 32)); out.writeInt(i); break; case 8: out.writeLong(l); break; default: throw new IllegalStateException("n=" + l); } } PackedInteger();  PackedInteger(long lVal);  PackedInteger(BigInteger bigint);  PackedInteger(DataInput in); }
PackedInteger implements Comparable<PackedInteger> { public static void writeLong(DataOutput out, long l) throws IOException { if (l <= 63 && l >= -64) { out.writeByte(((int) l) << 1 | 0x01); return; } final int cBits = 65 - Long.numberOfLeadingZeros(Math.max(l, ~l)); int i = (int) l; if (((1L << cBits) & 0x3E3E00L) != 0) { if (cBits <= 13) { out.writeShort(0b010_00000000 | (i & 0x1F00) << 3 | (i & 0x00FF)); } else { out.writeByte(0b110 | (i & 0x1F0000) >>> 13); out.writeShort(i); } return; } int cBytes = (cBits + 7) >>> 3; out.writeByte((cBytes - 1) << 2); switch (cBytes) { case 1: out.writeByte(i); break; case 2: out.writeShort(i); break; case 3: out.writeByte(i >>> 16); out.writeShort(i); break; case 4: out.writeInt(i); break; case 5: out.writeByte((int) (l >>> 32)); out.writeInt(i); break; case 6: out.writeShort((int) (l >>> 32)); out.writeInt(i); break; case 7: out.writeByte((int) (l >>> 48)); out.writeShort((int) (l >>> 32)); out.writeInt(i); break; case 8: out.writeLong(l); break; default: throw new IllegalStateException("n=" + l); } } PackedInteger();  PackedInteger(long lVal);  PackedInteger(BigInteger bigint);  PackedInteger(DataInput in); static PackedInteger valueOf(long lVal); int getSignedByteSize(); int getUnsignedByteSize(); boolean isBig(); boolean checkRange(long nLo, long nHi); boolean isNegative(); int getInt(); long getLong(); void setLong(long lVal); BigInteger getBigInteger(); void setBigInteger(BigInteger bigint); PackedInteger negate(); PackedInteger complement(); PackedInteger previous(); PackedInteger next(); PackedInteger add(PackedInteger that); PackedInteger sub(PackedInteger that); PackedInteger mul(PackedInteger that); PackedInteger div(PackedInteger that); PackedInteger mod(PackedInteger that); PackedInteger[] divrem(PackedInteger that); PackedInteger and(PackedInteger that); PackedInteger or(PackedInteger that); PackedInteger xor(PackedInteger that); PackedInteger shl(PackedInteger that); PackedInteger shl(int count); PackedInteger shr(PackedInteger that); PackedInteger shr(int count); PackedInteger ushr(PackedInteger that); PackedInteger ushr(int count); int cmp(PackedInteger that); String toString(int radix); void readObject(DataInput in); void writeObject(DataOutput out); void verifyUninitialized(); void verifyInitialized(); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); @Override int compareTo(PackedInteger that); static void writeLong(DataOutput out, long l); static long readLong(DataInput in); static int packedLength(byte[] ab, int of); static int packedLength(long n); static long unpackInt(byte[] ab, int of); }
PackedInteger implements Comparable<PackedInteger> { public static void writeLong(DataOutput out, long l) throws IOException { if (l <= 63 && l >= -64) { out.writeByte(((int) l) << 1 | 0x01); return; } final int cBits = 65 - Long.numberOfLeadingZeros(Math.max(l, ~l)); int i = (int) l; if (((1L << cBits) & 0x3E3E00L) != 0) { if (cBits <= 13) { out.writeShort(0b010_00000000 | (i & 0x1F00) << 3 | (i & 0x00FF)); } else { out.writeByte(0b110 | (i & 0x1F0000) >>> 13); out.writeShort(i); } return; } int cBytes = (cBits + 7) >>> 3; out.writeByte((cBytes - 1) << 2); switch (cBytes) { case 1: out.writeByte(i); break; case 2: out.writeShort(i); break; case 3: out.writeByte(i >>> 16); out.writeShort(i); break; case 4: out.writeInt(i); break; case 5: out.writeByte((int) (l >>> 32)); out.writeInt(i); break; case 6: out.writeShort((int) (l >>> 32)); out.writeInt(i); break; case 7: out.writeByte((int) (l >>> 48)); out.writeShort((int) (l >>> 32)); out.writeInt(i); break; case 8: out.writeLong(l); break; default: throw new IllegalStateException("n=" + l); } } PackedInteger();  PackedInteger(long lVal);  PackedInteger(BigInteger bigint);  PackedInteger(DataInput in); static PackedInteger valueOf(long lVal); int getSignedByteSize(); int getUnsignedByteSize(); boolean isBig(); boolean checkRange(long nLo, long nHi); boolean isNegative(); int getInt(); long getLong(); void setLong(long lVal); BigInteger getBigInteger(); void setBigInteger(BigInteger bigint); PackedInteger negate(); PackedInteger complement(); PackedInteger previous(); PackedInteger next(); PackedInteger add(PackedInteger that); PackedInteger sub(PackedInteger that); PackedInteger mul(PackedInteger that); PackedInteger div(PackedInteger that); PackedInteger mod(PackedInteger that); PackedInteger[] divrem(PackedInteger that); PackedInteger and(PackedInteger that); PackedInteger or(PackedInteger that); PackedInteger xor(PackedInteger that); PackedInteger shl(PackedInteger that); PackedInteger shl(int count); PackedInteger shr(PackedInteger that); PackedInteger shr(int count); PackedInteger ushr(PackedInteger that); PackedInteger ushr(int count); int cmp(PackedInteger that); String toString(int radix); void readObject(DataInput in); void writeObject(DataOutput out); void verifyUninitialized(); void verifyInitialized(); @Override int hashCode(); @Override boolean equals(Object obj); @Override String toString(); @Override int compareTo(PackedInteger that); static void writeLong(DataOutput out, long l); static long readLong(DataInput in); static int packedLength(byte[] ab, int of); static int packedLength(long n); static long unpackInt(byte[] ab, int of); static final PackedInteger ZERO; static final PackedInteger ONE; static final PackedInteger NEG_ONE; static final PackedInteger SINT1_MIN; static final PackedInteger SINT2_MIN; static final PackedInteger SINT4_MIN; static final PackedInteger SINT8_MIN; static final PackedInteger SINT16_MIN; static final PackedInteger SINT32_MIN; static final PackedInteger SINT1_MAX; static final PackedInteger SINT2_MAX; static final PackedInteger SINT4_MAX; static final PackedInteger SINT8_MAX; static final PackedInteger SINT16_MAX; static final PackedInteger SINT32_MAX; static final PackedInteger UINT1_MAX; static final PackedInteger UINT2_MAX; static final PackedInteger UINT4_MAX; static final PackedInteger UINT8_MAX; static final PackedInteger UINT16_MAX; static final PackedInteger UINT32_MAX; }