Debian Codegraph @

src/main/java/io/airlift/slice/LittleEndianDataInputStream.java

raw
1 /*
2 * Copyright (C) 2007 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package io.airlift.slice;
17
18 import java.io.DataInput;
19 import java.io.DataInputStream;
20 import java.io.EOFException;
21 import java.io.FilterInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import static io.airlift.slice.Preconditions.checkNotNull;
26
27 /**
28 * An implementation of {@link java.io.DataInput} that uses little-endian byte ordering
29 * for reading {@code short}, {@code int}, {@code float}, {@code double}, and
30 * {@code long} values.
31 * <p/>
32 * <b>Note:</b> This class intentionally violates the specification of its
33 * supertype {@code DataInput}, which explicitly requires big-endian byte order.
34 *
35 * @author Chris Nokleberg
36 * @author Keith Bottner
37 * @since 8.0
38 */
39 final class LittleEndianDataInputStream
40 extends FilterInputStream
41 implements DataInput
42 {
43 /**
44 * Creates a {@code LittleEndianDataInputStream} that wraps the given stream.
45 *
46 * @param in the stream to delegate to
47 */
48 public LittleEndianDataInputStream(InputStream in)
49 {
50 super(checkNotNull(in, "in is null"));
51 }
52
53 /**
54 * This method will throw an {@link UnsupportedOperationException}.
55 */
56 @Override
57 public String readLine()
58 {
59 throw new UnsupportedOperationException("readLine is not supported");
60 }
61
62 @Override
63 public void readFully(byte[] b)
64 throws IOException
65 {
66 readFully(b, 0, b.length);
67 }
68
69 @Override
70 public void readFully(byte[] b, int offset, int length)
71 throws IOException
72 {
73 checkNotNull(b, "byte array is null");
74 if (length < 0) {
75 throw new IndexOutOfBoundsException("length is negative");
76 }
77
78 int total = 0;
79 while (total < length) {
80 int result = read(b, offset + total, length - total);
81 if (result == -1) {
82 break;
83 }
84 total += result;
85 }
86
87 if (total != length) {
88 throw new EOFException("reached end of stream after reading " + total + " bytes; " + length + " bytes expected");
89 }
90 }
91
92 @Override
93 public int skipBytes(int n)
94 throws IOException
95 {
96 return (int) in.skip(n);
97 }
98
99 @Override
100 public int readUnsignedByte()
101 throws IOException
102 {
103 int b1 = in.read();
104 if (0 > b1) {
105 throw new EOFException();
106 }
107
108 return b1;
109 }
110
111 /**
112 * Reads an unsigned {@code short} as specified by
113 * {@link java.io.DataInputStream#readUnsignedShort()}, except using little-endian
114 * byte order.
115 *
116 * @return the next two bytes of the input stream, interpreted as an
117 * unsigned 16-bit integer in little-endian byte order
118 * @throws java.io.IOException if an I/O error occurs
119 */
120 @Override
121 public int readUnsignedShort()
122 throws IOException
123 {
124 byte b1 = readAndCheckByte();
125 byte b2 = readAndCheckByte();
126
127 return intFromBytes(b1, b2, (byte) 0, (byte) 0);
128 }
129
130 /**
131 * Reads an integer as specified by {@link java.io.DataInputStream#readInt()}, except
132 * using little-endian byte order.
133 *
134 * @return the next four bytes of the input stream, interpreted as an
135 * {@code int} in little-endian byte order
136 * @throws java.io.IOException if an I/O error occurs
137 */
138 @Override
139 public int readInt()
140 throws IOException
141 {
142 byte b1 = readAndCheckByte();
143 byte b2 = readAndCheckByte();
144 byte b3 = readAndCheckByte();
145 byte b4 = readAndCheckByte();
146
147 return intFromBytes(b1, b2, b3, b4);
148 }
149
150 /**
151 * Reads a {@code long} as specified by {@link java.io.DataInputStream#readLong()},
152 * except using little-endian byte order.
153 *
154 * @return the next eight bytes of the input stream, interpreted as a
155 * {@code long} in little-endian byte order
156 * @throws java.io.IOException if an I/O error occurs
157 */
158 @Override
159 public long readLong()
160 throws IOException
161 {
162 byte b1 = readAndCheckByte();
163 byte b2 = readAndCheckByte();
164 byte b3 = readAndCheckByte();
165 byte b4 = readAndCheckByte();
166 byte b5 = readAndCheckByte();
167 byte b6 = readAndCheckByte();
168 byte b7 = readAndCheckByte();
169 byte b8 = readAndCheckByte();
170
171 return longFromBytes(b1, b2, b3, b4, b5, b6, b7, b8);
172 }
173
174 /**
175 * Reads a {@code float} as specified by {@link java.io.DataInputStream#readFloat()},
176 * except using little-endian byte order.
177 *
178 * @return the next four bytes of the input stream, interpreted as a
179 * {@code float} in little-endian byte order
180 * @throws java.io.IOException if an I/O error occurs
181 */
182 @Override
183 public float readFloat()
184 throws IOException
185 {
186 return Float.intBitsToFloat(readInt());
187 }
188
189 /**
190 * Reads a {@code double} as specified by
191 * {@link java.io.DataInputStream#readDouble()}, except using little-endian byte
192 * order.
193 *
194 * @return the next eight bytes of the input stream, interpreted as a
195 * {@code double} in little-endian byte order
196 * @throws java.io.IOException if an I/O error occurs
197 */
198 @Override
199 public double readDouble()
200 throws IOException
201 {
202 return Double.longBitsToDouble(readLong());
203 }
204
205 @Override
206 public String readUTF()
207 throws IOException
208 {
209 return new DataInputStream(in).readUTF();
210 }
211
212 /**
213 * Reads a {@code short} as specified by {@link java.io.DataInputStream#readShort()},
214 * except using little-endian byte order.
215 *
216 * @return the next two bytes of the input stream, interpreted as a
217 * {@code short} in little-endian byte order.
218 * @throws java.io.IOException if an I/O error occurs.
219 */
220 @Override
221 public short readShort()
222 throws IOException
223 {
224 return (short) readUnsignedShort();
225 }
226
227 /**
228 * Reads a char as specified by {@link java.io.DataInputStream#readChar()}, except
229 * using little-endian byte order.
230 *
231 * @return the next two bytes of the input stream, interpreted as a
232 * {@code char} in little-endian byte order
233 * @throws java.io.IOException if an I/O error occurs
234 */
235 @Override
236 public char readChar()
237 throws IOException
238 {
239 return (char) readUnsignedShort();
240 }
241
242 @Override
243 public byte readByte()
244 throws IOException
245 {
246 return (byte) readUnsignedByte();
247 }
248
249 @Override
250 public boolean readBoolean()
251 throws IOException
252 {
253 return readUnsignedByte() != 0;
254 }
255
256 /**
257 * Reads a byte from the input stream checking that the end of file (EOF)
258 * has not been encountered.
259 *
260 * @return byte read from input
261 * @throws java.io.IOException if an error is encountered while reading
262 * @throws java.io.EOFException if the end of file (EOF) is encountered.
263 */
264 private byte readAndCheckByte()
265 throws IOException
266 {
267 int b1 = in.read();
268
269 if (-1 == b1) {
270 throw new EOFException();
271 }
272
273 return (byte) b1;
274 }
275
276 private static int intFromBytes(byte b1, byte b2, byte b3, byte b4)
277 {
278 return b4 << 24 |
279 (b3 & 0xFF) << 16 |
280 (b2 & 0xFF) << 8 |
281 (b1 & 0xFF);
282 }
283
284 private static long longFromBytes(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8)
285 {
286 return (b8 & 0xFFL) << 56
287 | (b7 & 0xFFL) << 48
288 | (b6 & 0xFFL) << 40
289 | (b5 & 0xFFL) << 32
290 | (b4 & 0xFFL) << 24
291 | (b3 & 0xFFL) << 16
292 | (b2 & 0xFFL) << 8
293 | (b1 & 0xFFL);
294 }
295 }