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