| 1 | |
|
| 2 | |
package org.galagosearch.core.index; |
| 3 | |
|
| 4 | |
import java.io.IOException; |
| 5 | |
import java.io.OutputStream; |
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
public class CompressedByteBuffer { |
| 13 | |
byte[] values; |
| 14 | |
int position; |
| 15 | |
|
| 16 | 356 | public CompressedByteBuffer() { |
| 17 | 356 | clear(); |
| 18 | 356 | } |
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
public void addRaw(int value) { |
| 27 | 1504 | if (position >= values.length) { |
| 28 | 12 | byte[] nValues = new byte[values.length * 2]; |
| 29 | 12 | System.arraycopy(values, 0, nValues, 0, values.length); |
| 30 | 12 | values = nValues; |
| 31 | |
} |
| 32 | |
|
| 33 | 1504 | values[position] = (byte) value; |
| 34 | 1504 | position += 1; |
| 35 | 1504 | } |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public void add(long i) { |
| 42 | 1112 | if (i < 1 << 7) { |
| 43 | 1056 | addRaw((int) (i | 0x80)); |
| 44 | 56 | } else if (i < 1 << 14) { |
| 45 | 48 | addRaw((int) (i >> 0) & 0x7f); |
| 46 | 48 | addRaw((int) ((i >> 7) & 0x7f) | 0x80); |
| 47 | 8 | } else if (i < 1 << 21) { |
| 48 | 8 | addRaw((int) (i >> 0) & 0x7f); |
| 49 | 8 | addRaw((int) (i >> 7) & 0x7f); |
| 50 | 8 | addRaw((int) ((i >> 14) & 0x7f) | 0x80); |
| 51 | |
} else { |
| 52 | 0 | while (i >= 1 << 7) { |
| 53 | 0 | addRaw((int) (i & 0x7f)); |
| 54 | 0 | i >>= 7; |
| 55 | |
} |
| 56 | |
|
| 57 | 0 | addRaw((int) (i | 0x80)); |
| 58 | |
} |
| 59 | 1112 | } |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public void addFloat(float value) { |
| 66 | 64 | int bits = Float.floatToIntBits(value); |
| 67 | |
|
| 68 | 64 | addRaw((bits >>> 24) & 0xFF); |
| 69 | 64 | addRaw((bits >>> 16) & 0xFF); |
| 70 | 64 | addRaw((bits >>> 8) & 0xFF); |
| 71 | 64 | addRaw(bits & 0xFF); |
| 72 | 64 | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
public void add(CompressedByteBuffer other) { |
| 81 | 48 | int totalLength = other.length() + length(); |
| 82 | 48 | byte[] newValues = new byte[totalLength]; |
| 83 | |
|
| 84 | 48 | System.arraycopy(values, 0, newValues, 0, position); |
| 85 | 48 | System.arraycopy(other.values, 0, newValues, position, other.position); |
| 86 | 48 | values = newValues; |
| 87 | 48 | position = totalLength; |
| 88 | 48 | } |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
public void clear() { |
| 95 | 596 | values = new byte[16]; |
| 96 | 596 | position = 0; |
| 97 | 596 | } |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
public byte[] getBytes() { |
| 106 | 128 | return values; |
| 107 | |
} |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
public int length() { |
| 113 | 2780 | return position; |
| 114 | |
} |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
public void write(OutputStream stream) throws IOException { |
| 120 | 236 | stream.write(values, 0, position); |
| 121 | 236 | } |
| 122 | |
} |
| 123 | |
|