This project has retired. For details please refer to its
Attic page.
ExtendedByteArrayDataOutput xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.utils;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.DataOutput;
22 import java.io.DataOutputStream;
23 import java.io.IOException;
24 import java.util.Arrays;
25
26
27
28
29
30
31 public class ExtendedByteArrayDataOutput extends ByteArrayOutputStream
32 implements ExtendedDataOutput {
33
34 private static final int DEFAULT_BYTES = 32;
35
36 private final DataOutput dataOutput;
37
38
39
40
41
42
43 public ExtendedByteArrayDataOutput(byte[] buf) {
44 if (buf == null) {
45 this.buf = new byte[DEFAULT_BYTES];
46 } else {
47 this.buf = buf;
48 }
49 dataOutput = new DataOutputStream(this);
50 }
51
52
53
54
55
56
57
58 public ExtendedByteArrayDataOutput(byte[] buf, int pos) {
59 this(buf);
60 this.count = pos;
61 }
62
63
64
65
66
67 public ExtendedByteArrayDataOutput() {
68 this(DEFAULT_BYTES);
69 }
70
71
72
73
74
75
76
77
78 public ExtendedByteArrayDataOutput(int size) {
79 if (size < 0) {
80 throw new IllegalArgumentException("Negative initial size: " +
81 size);
82 }
83 buf = new byte[size];
84 dataOutput = new DataOutputStream(this);
85 }
86
87 @Override
88 public void writeBoolean(boolean v) throws IOException {
89 dataOutput.writeBoolean(v);
90 }
91
92 @Override
93 public void writeByte(int v) throws IOException {
94 dataOutput.writeByte(v);
95 }
96
97 @Override
98 public void writeShort(int v) throws IOException {
99 dataOutput.writeShort(v);
100 }
101
102 @Override
103 public void writeChar(int v) throws IOException {
104 dataOutput.writeChar(v);
105 }
106
107 @Override
108 public void writeInt(int v) throws IOException {
109 dataOutput.writeInt(v);
110 }
111
112 @Override
113 public void writeLong(long v) throws IOException {
114 dataOutput.writeLong(v);
115 }
116
117 @Override
118 public void writeFloat(float v) throws IOException {
119 dataOutput.writeFloat(v);
120 }
121
122 @Override
123 public void writeDouble(double v) throws IOException {
124 dataOutput.writeDouble(v);
125 }
126
127 @Override
128 public void writeBytes(String s) throws IOException {
129 dataOutput.writeBytes(s);
130 }
131
132 @Override
133 public void writeChars(String s) throws IOException {
134 dataOutput.writeChars(s);
135 }
136
137 @Override
138 public void writeUTF(String s) throws IOException {
139 dataOutput.writeUTF(s);
140 }
141
142 @Override
143 public void ensureWritable(int minSize) {
144 if ((count + minSize) > buf.length) {
145 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, count + minSize));
146 }
147 }
148
149 @Override
150 public void skipBytes(int bytesToSkip) {
151 ensureWritable(bytesToSkip);
152 count += bytesToSkip;
153 }
154
155 @Override
156 public void writeInt(int position, int value) {
157 if (position + 4 > count) {
158 throw new IndexOutOfBoundsException(
159 "writeIntOnPosition: Tried to write int to position " + position +
160 " but current length is " + count);
161 }
162 buf[position] = (byte) ((value >>> 24) & 0xFF);
163 buf[position + 1] = (byte) ((value >>> 16) & 0xFF);
164 buf[position + 2] = (byte) ((value >>> 8) & 0xFF);
165 buf[position + 3] = (byte) ((value >>> 0) & 0xFF);
166 }
167
168 @Override
169 public byte[] toByteArray(int offset, int length) {
170 if (offset + length > count) {
171 throw new IndexOutOfBoundsException(String.format("Offset: %d + " +
172 "Length: %d exceeds the size of buf : %d", offset, length, count));
173 }
174 return Arrays.copyOfRange(buf, offset, length);
175 }
176
177 @Override
178 public byte[] getByteArray() {
179 return buf;
180 }
181
182 @Override
183 public int getPos() {
184 return count;
185 }
186 }