| 1 | |
|
| 2 | |
package org.galagosearch.tupleflow; |
| 3 | |
|
| 4 | |
import java.io.DataOutput; |
| 5 | |
import java.io.IOException; |
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | 2 | public class VByteOutput implements DataOutput { |
| 12 | |
DataOutput output; |
| 13 | |
|
| 14 | |
|
| 15 | 4 | public VByteOutput(DataOutput output) { |
| 16 | 4 | this.output = output; |
| 17 | 4 | } |
| 18 | |
|
| 19 | |
public void writeUTF(String string) throws IOException { |
| 20 | 0 | writeInt(string.length()); |
| 21 | 0 | output.writeUTF(string); |
| 22 | 0 | } |
| 23 | |
|
| 24 | |
public void writeBytes(String string) throws IOException { |
| 25 | 0 | writeInt(string.length()); |
| 26 | 0 | output.writeBytes(string); |
| 27 | 0 | } |
| 28 | |
|
| 29 | |
public void writeChars(String string) throws IOException { |
| 30 | 0 | writeInt(string.length()); |
| 31 | 0 | output.writeChars(string); |
| 32 | 0 | } |
| 33 | |
|
| 34 | |
public void writeString(String string) throws IOException { |
| 35 | 4 | byte[] bytes = Utility.makeBytes(string); |
| 36 | 4 | writeInt(bytes.length); |
| 37 | 4 | write(bytes); |
| 38 | 4 | } |
| 39 | |
|
| 40 | |
public void write(byte[] b, int i, int i0) throws IOException { |
| 41 | 0 | output.write(b, i, i0); |
| 42 | 0 | } |
| 43 | |
|
| 44 | |
public void write(byte[] b) throws IOException { |
| 45 | 4 | output.write(b); |
| 46 | 4 | } |
| 47 | |
|
| 48 | |
public void write(int i) throws IOException { |
| 49 | 4 | assert i >= 0; |
| 50 | |
|
| 51 | 4 | if (i < 1 << 7) { |
| 52 | 4 | output.writeByte((i | 0x80)); |
| 53 | 0 | } else if (i < 1 << 14) { |
| 54 | 0 | output.writeByte((i >> 0) & 0x7f); |
| 55 | 0 | output.writeByte(((i >> 7) & 0x7f) | 0x80); |
| 56 | 0 | } else if (i < 1 << 21) { |
| 57 | 0 | output.writeByte((i >> 0) & 0x7f); |
| 58 | 0 | output.writeByte((i >> 7) & 0x7f); |
| 59 | 0 | output.writeByte(((i >> 14) & 0x7f) | 0x80); |
| 60 | 0 | } else if (i < 1 << 28) { |
| 61 | 0 | output.writeByte((i >> 0) & 0x7f); |
| 62 | 0 | output.writeByte((i >> 7) & 0x7f); |
| 63 | 0 | output.writeByte((i >> 14) & 0x7f); |
| 64 | 0 | output.writeByte(((i >> 21) & 0x7f) | 0x80); |
| 65 | |
} else { |
| 66 | 0 | output.writeByte((i >> 0) & 0x7f); |
| 67 | 0 | output.writeByte((i >> 7) & 0x7f); |
| 68 | 0 | output.writeByte((i >> 14) & 0x7f); |
| 69 | 0 | output.writeByte((i >> 21) & 0x7f); |
| 70 | 0 | output.writeByte(((i >> 28) & 0x7f) | 0x80); |
| 71 | |
} |
| 72 | 4 | } |
| 73 | |
|
| 74 | |
public void writeByte(int i) throws IOException { |
| 75 | 0 | write(i); |
| 76 | 0 | } |
| 77 | |
|
| 78 | |
public void writeChar(int i) throws IOException { |
| 79 | 0 | write(i); |
| 80 | 0 | } |
| 81 | |
|
| 82 | |
public void writeInt(int i) throws IOException { |
| 83 | 4 | write(i); |
| 84 | 4 | } |
| 85 | |
|
| 86 | |
public void writeShort(int i) throws IOException { |
| 87 | 0 | write(i); |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
public void writeBoolean(boolean b) throws IOException { |
| 91 | 0 | if (b) { |
| 92 | 0 | write(1); |
| 93 | |
} else { |
| 94 | 0 | write(0); |
| 95 | |
} |
| 96 | 0 | } |
| 97 | |
|
| 98 | |
public void writeLong(long i) throws IOException { |
| 99 | 0 | assert i >= 0; |
| 100 | |
|
| 101 | 0 | if (i < 1 << 7) { |
| 102 | 0 | output.writeByte((int) (i | 0x80)); |
| 103 | 0 | } else if (i < 1 << 14) { |
| 104 | 0 | output.writeByte((int) (i >> 0) & 0x7f); |
| 105 | 0 | output.writeByte((int) ((i >> 7) & 0x7f) | 0x80); |
| 106 | 0 | } else if (i < 1 << 21) { |
| 107 | 0 | output.writeByte((int) (i >> 0) & 0x7f); |
| 108 | 0 | output.writeByte((int) (i >> 7) & 0x7f); |
| 109 | 0 | output.writeByte((int) ((i >> 14) & 0x7f) | 0x80); |
| 110 | |
} else { |
| 111 | 0 | while (i >= 1 << 7) { |
| 112 | 0 | output.writeByte((int) (i & 0x7f)); |
| 113 | 0 | i >>= 7; |
| 114 | |
} |
| 115 | |
|
| 116 | 0 | output.writeByte((int) (i | 0x80)); |
| 117 | |
} |
| 118 | 0 | } |
| 119 | |
|
| 120 | |
public void writeDouble(double d) throws IOException { |
| 121 | 0 | output.writeLong(Double.doubleToRawLongBits(d)); |
| 122 | 0 | } |
| 123 | |
|
| 124 | |
public void writeFloat(float f) throws IOException { |
| 125 | 0 | output.writeInt(Float.floatToRawIntBits(f)); |
| 126 | 0 | } |
| 127 | |
} |