📄 compressednumber.java
字号:
out.write((int) ( (value >>> 24) & 0xff)); out.write((int) ( (value >>> 16) & 0xff)); out.write((int) ( (value >>> 8) & 0xff)); out.write((int) ( (value ) & 0xff)); return 8; } /** Read a long previously written by writeLong(). @exception IOException an exception was thrown by a method on in. */ public static final long readLong(DataInput in) throws IOException { int int_value = in.readUnsignedByte(); if ((int_value & ~0x3f) == 0) { // test for small case first - assuming this is usual case. // this is stored in 2 bytes. return((int_value << 8) | in.readUnsignedByte()); } else if ((int_value & 0x80) == 0) { // value is stored in 4 bytes. only use low 6 bits from 1st byte. return( ((int_value & 0x3f) << 24) | (in.readUnsignedByte() << 16) | (in.readUnsignedByte() << 8) | (in.readUnsignedByte())); } else { // value is stored in 8 bytes. only use low 7 bits from 1st byte. return( (((long) (int_value & 0x7f) ) << 56) | (((long) in.readUnsignedByte()) << 48) | (((long) in.readUnsignedByte()) << 40) | (((long) in.readUnsignedByte()) << 32) | (((long) in.readUnsignedByte()) << 24) | (((long) in.readUnsignedByte()) << 16) | (((long) in.readUnsignedByte()) << 8) | (((long) in.readUnsignedByte()) )); } } /** Read a long previously written by writeLong(). @exception IOException an exception was thrown by a method on in. */ public static final long readLong(InputStream in) throws IOException { int int_value = InputStreamUtil.readUnsignedByte(in); if ((int_value & ~0x3f) == 0) { // test for small case first - assuming this is usual case. // this is stored in 2 bytes. return((int_value << 8) | InputStreamUtil.readUnsignedByte(in)); } else if ((int_value & 0x80) == 0) { // value is stored in 4 bytes. only use low 6 bits from 1st byte. return( ((int_value & 0x3f) << 24) | (InputStreamUtil.readUnsignedByte(in) << 16) | (InputStreamUtil.readUnsignedByte(in) << 8) | (InputStreamUtil.readUnsignedByte(in) )); } else { // value is stored in 8 bytes. only use low 7 bits from 1st byte. long value = int_value; return( (((long) (value & 0x7f) ) << 56) | (((long) InputStreamUtil.readUnsignedByte(in)) << 48) | (((long) InputStreamUtil.readUnsignedByte(in)) << 40) | (((long) InputStreamUtil.readUnsignedByte(in)) << 32) | (((long) InputStreamUtil.readUnsignedByte(in)) << 24) | (((long) InputStreamUtil.readUnsignedByte(in)) << 16) | (((long) InputStreamUtil.readUnsignedByte(in)) << 8) | (((long) InputStreamUtil.readUnsignedByte(in)) )); } } public static final long readLong( byte[] data, int offset) { int int_value = data[offset++]; if ((int_value & ~0x3f) == 0) { // test for small case first - assuming this is usual case. // this is stored in 2 bytes. return((int_value << 8) | (data[offset] & 0xff)); } else if ((int_value & 0x80) == 0) { // value is stored in 4 bytes. only use low 6 bits from 1st byte. return( ((int_value & 0x3f) << 24) | ((data[offset++] & 0xff) << 16) | ((data[offset++] & 0xff) << 8) | ((data[offset] & 0xff) )); } else { // value is stored in 8 bytes. only use low 6 bits from 1st byte. return( (((long) (int_value & 0x7f)) << 56) | (((long) (data[offset++] & 0xff)) << 48) | (((long) (data[offset++] & 0xff)) << 40) | (((long) (data[offset++] & 0xff)) << 32) | (((long) (data[offset++] & 0xff)) << 24) | (((long) (data[offset++] & 0xff)) << 16) | (((long) (data[offset++] & 0xff)) << 8) | (((long) (data[offset] & 0xff)) )); } } /** Skip a long previously written by writeLong(). @exception IOException an exception was thrown by a method on in. */ public static final int skipLong(DataInput in) throws IOException { long value = in.readUnsignedByte(); if ((value & 0x80) == 0x80) { in.skipBytes(7); return 8; } if ((value & 0x40) == 0x40) { in.skipBytes(3); return 4; } in.skipBytes(1); return 2; } /** Skip a long previously written by writeLong(). @exception IOException an exception was thrown by a method on in. */ public static final int skipLong(InputStream in) throws IOException { int value = InputStreamUtil.readUnsignedByte(in); int skipBytes; if ((value & 0x80) == 0x80) { skipBytes = 7; } else if ((value & 0x40) == 0x40) { skipBytes = 3; } else skipBytes = 1; if (in.skip(skipBytes) != skipBytes) throw new EOFException(); return skipBytes + 1; } public static final int sizeLong(long value) { if (value <= 0x3fff) { return 2; } if (value <= 0x3fffffff) { return 4; } return 8; }// /* FOR TESTING// ***************************************************** private static byte[] holder = new byte[8]; private static ArrayOutputStream aos = new ArrayOutputStream(holder); private static DataOutput out = new DataOutputStream(aos); private static ArrayInputStream ais = new ArrayInputStream(holder); private static DataInput in = new DataInputStream(ais); private static InputStream in_stream = ais; private static short checkInt(int i, short oldLength) throws IOException { aos.setPosition(0); int length = CompressedNumber.writeInt(out, i); if (length != oldLength) { System.out.println("changing length to " + length + " at value " + i + " 0x" + Integer.toHexString(i)); oldLength = (short) length; } int writtenBytes = aos.getPosition(); if (writtenBytes != length) { System.out.println("MISMATCH written bytes expected " + length + " got " + writtenBytes); System.exit(1); } if (length != CompressedNumber.sizeInt(i)) { System.out.println("MISMATCH sizeInt() bytes expected " + length + " got " + CompressedNumber.sizeInt(i)); System.exit(1); } ais.setPosition(0); int value = CompressedNumber.readInt(in); if (value != i) { System.out.println("MISMATCH value readInt(DataInput) expected " + i + " got " + value); System.exit(1); } ais.setPosition(0); value = ais.readCompressedInt(); if (value != i) { System.out.println("MISMATCH value readInt(DataInput) expected " + i + " got " + value); System.exit(1); } ais.setPosition(0); value = CompressedNumber.readInt(in_stream); if (value != i) { System.out.println("MISMATCH value in readInt(InputStream) expected " + i + " got " + value); System.exit(1); } value = CompressedNumber.readInt(holder, 0); if (value != i) { System.out.println( "MISMATCH frome readInt(byte[], offset) value expected " + i + " got " + value); System.exit(1); } ais.setPosition(0); int skipLength = CompressedNumber.skipInt(in); if (skipLength != length) { System.out.println("MISMATCH skip length expected " + length + " got " + skipLength); System.exit(1); } int value_plus_int_length = readIntAndReturnIntPlusOverhead(holder, 0); if (value_plus_int_length != (length + i + 1)) { System.out.println("MISMATCH readIntAndReturnIntPlusOverhead() return expected " + (length + i) + " got " + value_plus_int_length); System.exit(1); } int skipPosition = ais.getPosition(); if (skipPosition != length) { System.out.println("MISMATCH skip position expected " + length + " got " + skipPosition); System.exit(1); } return oldLength; } private static short checkLong(long i, short oldLength) throws IOException { aos.setPosition(0); int length = CompressedNumber.writeLong(out, i); if (length != oldLength) { System.out.println("changing length to " + length + " at value " + i + " 0x" + Long.toHexString(i)); oldLength = (short) length; } int writtenBytes = aos.getPosition(); if (writtenBytes != length) { System.out.println("MISMATCH written bytes expected " + length + " got " + writtenBytes); System.exit(1); } if (length != CompressedNumber.sizeLong(i)) { System.out.println("MISMATCH sizeLong() bytes expected " + length + " got " + CompressedNumber.sizeLong(i)); System.exit(1); } long value = CompressedNumber.readLong(holder, 0); if (value != i) { for (int j = 0; j < 8; j++) { System.out.println(Integer.toHexString((int) holder[j])); } System.out.println( "MISMATCH in readLong(byte[], offset) value expected " + Long.toHexString(i) + " got " + value); System.exit(1); } ais.setPosition(0); value = CompressedNumber.readLong(in_stream); if (value != i) { for (int j = 0; j < 8; j++) { System.out.println(Integer.toHexString((int) holder[j])); } System.out.println("MISMATCH value in readLong(InputStream) expected " + Long.toHexString(i) + " got " + value); System.exit(1); } ais.setPosition(0); value = ais.readCompressedLong(); if (value != i) { for (int j = 0; j < 8; j++) { System.out.println(Integer.toHexString((int) holder[j])); } System.out.println("MISMATCH value in readLong(InputStream) expected " + Long.toHexString(i) + " got " + value); System.exit(1); } ais.setPosition(0); value = CompressedNumber.readLong(in); if (value != i) { for (int j = 0; j < 8; j++) { System.out.println(Integer.toHexString((int) holder[j])); } System.out.println("MISMATCH value in readLong(DataInput) expected " + Long.toHexString(i) + " got " + value); System.exit(1); } ais.setPosition(0); int skipLength = CompressedNumber.skipLong(in); if (skipLength != length) { System.out.println("MISMATCH skip length expected " + length + " got " + skipLength); System.exit(1); } int skipPosition = ais.getPosition(); if (skipPosition != length) { System.out.println("MISMATCH skip position expected " + length + " got " + skipPosition); System.exit(1); } return oldLength; } public static void main(String[] args) throws IOException { short oldLength = -1; System.out.println("** Testing Int"); oldLength = checkInt(0, oldLength); oldLength = checkInt(1, oldLength); oldLength = checkInt(2, oldLength); oldLength = checkInt(0x3f - 4, oldLength); oldLength = checkInt(0x3f - 3, oldLength); oldLength = checkInt(0x3f - 2, oldLength); oldLength = checkInt(0x3f - 1, oldLength); oldLength = checkInt(0x3f , oldLength); oldLength = checkInt(0x3f + 1, oldLength); oldLength = checkInt(0x3f + 2, oldLength); oldLength = checkInt(0x3f + 3, oldLength); oldLength = checkInt(0x3f + 4, oldLength); oldLength = checkInt(0x3f80 - 4, oldLength); oldLength = checkInt(0x3f80 - 3, oldLength); oldLength = checkInt(0x3f80 - 2, oldLength); oldLength = checkInt(0x3f80 - 1, oldLength); oldLength = checkInt(0x3f80 , oldLength); oldLength = checkInt(0x3f80 + 1, oldLength); oldLength = checkInt(0x3f80 + 2, oldLength); oldLength = checkInt(0x3f80 + 3, oldLength); oldLength = checkInt(0x3f80 + 4, oldLength); oldLength = checkInt(0x3fff - 4, oldLength); oldLength = checkInt(0x3fff - 3, oldLength); oldLength = checkInt(0x3fff - 2, oldLength); oldLength = checkInt(0x3fff - 1, oldLength); oldLength = checkInt(0x3fff , oldLength); oldLength = checkInt(0x3fff + 1, oldLength); oldLength = checkInt(0x3fff + 2, oldLength); oldLength = checkInt(0x3fff + 3, oldLength); oldLength = checkInt(0x3fff + 4, oldLength); oldLength = checkInt(Integer.MAX_VALUE - 4, oldLength); oldLength = checkInt(Integer.MAX_VALUE - 3, oldLength); oldLength = checkInt(Integer.MAX_VALUE - 2, oldLength); oldLength = checkInt(Integer.MAX_VALUE - 1, oldLength); oldLength = checkInt(Integer.MAX_VALUE , oldLength); oldLength = -1; for (int i = 0; i < 0xf0000; i++) { oldLength = checkInt(i, oldLength); } // takes 30 minutes to run. // // for (int i = 0; i < Integer.MAX_VALUE; i++) // { // if (i % 0x00800000 == 0) // System.out.println("checking: " + i); // // oldLength = checkInt(i, oldLength); // } System.out.println("** Testing Long"); oldLength = -1; for (int i = 0; i < 0xf0000; i++) { oldLength = checkLong(i, oldLength); } oldLength = -1; oldLength = checkLong(0, oldLength); oldLength = checkLong(1, oldLength); oldLength = checkLong(2, oldLength); oldLength = checkLong(0x3fff - 2, oldLength); oldLength = checkLong(0x3fff - 1, oldLength); oldLength = checkLong(0x3fff , oldLength); oldLength = checkLong(0x3fff + 1, oldLength); oldLength = checkLong(0x3fff + 2, oldLength); oldLength = checkLong(0x3fffffff - 4, oldLength); oldLength = checkLong(0x3fffffff - 3, oldLength); oldLength = checkLong(0x3fffffff - 2, oldLength); oldLength = checkLong(0x3fffffff - 1, oldLength); oldLength = checkLong(0x3fffffff , oldLength); oldLength = checkLong(0x3fffffff + 1, oldLength); oldLength = checkLong(0x3fffffff + 2, oldLength); oldLength = checkLong(0x3fffffff + 3, oldLength); oldLength = checkLong(0x3fffffff + 4, oldLength); oldLength = checkLong(0x70000000 - 2, oldLength); oldLength = checkLong(0x70000000 - 1, oldLength); oldLength = checkLong(0x70000000 , oldLength); oldLength = checkLong(0x70000000 + 1, oldLength); oldLength = checkLong(0x70000000 + 2, oldLength); oldLength = checkLong(Long.MAX_VALUE - 2, oldLength); oldLength = checkLong(Long.MAX_VALUE - 1, oldLength); oldLength = checkLong(Long.MAX_VALUE , oldLength); }// ********************************************************/}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -