Debian Codegraph @

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

raw
1 /*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14 package io.airlift.slice;
15
16 import java.io.EOFException;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.OutputStream;
20 import java.io.PushbackInputStream;
21
22 import static io.airlift.slice.Preconditions.checkNotNull;
23
24 public final class InputStreamSliceInput
25 extends SliceInput
26 {
27 private final PushbackInputStream pushbackInputStream;
28 private final CountingInputStream countingInputStream;
29 private final LittleEndianDataInputStream dataInputStream;
30
31 @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
32 public InputStreamSliceInput(InputStream inputStream)
33 {
34 checkNotNull(inputStream, "inputStream is null");
35 pushbackInputStream = new PushbackInputStream(inputStream);
36 countingInputStream = new CountingInputStream(pushbackInputStream);
37 dataInputStream = new LittleEndianDataInputStream(countingInputStream);
38 }
39
40 @Override
41 public long position()
42 {
43 return (int) countingInputStream.getCount();
44 }
45
46 @Override
47 public void setPosition(long position)
48 {
49 throw new UnsupportedOperationException();
50 }
51
52 @Override
53 public boolean isReadable()
54 {
55 try {
56 int value = pushbackInputStream.read();
57 if (value == -1) {
58 return false;
59 }
60 pushbackInputStream.unread(value);
61 return true;
62 }
63 catch (IOException e) {
64 throw new RuntimeIOException(e);
65 }
66 }
67
68 @Override
69 public int skipBytes(int n)
70 {
71 try {
72 return dataInputStream.skipBytes(n);
73 }
74 catch (IOException e) {
75 throw new RuntimeIOException(e);
76 }
77 }
78
79 @Override
80 public float readFloat()
81 {
82 try {
83 return dataInputStream.readFloat();
84 }
85 catch (EOFException e) {
86 throw new IndexOutOfBoundsException();
87 }
88 catch (IOException e) {
89 throw new RuntimeIOException(e);
90 }
91 }
92
93 @Override
94 public double readDouble()
95 {
96 try {
97 return dataInputStream.readDouble();
98 }
99 catch (EOFException e) {
100 throw new IndexOutOfBoundsException();
101 }
102 catch (IOException e) {
103 throw new RuntimeIOException(e);
104 }
105 }
106
107 @Override
108 public int readUnsignedByte()
109 {
110 try {
111 return dataInputStream.readUnsignedByte();
112 }
113 catch (IOException e) {
114 throw new RuntimeIOException(e);
115 }
116 }
117
118 @Override
119 public int readUnsignedShort()
120 {
121 try {
122 return dataInputStream.readUnsignedShort();
123 }
124 catch (EOFException e) {
125 throw new IndexOutOfBoundsException();
126 }
127 catch (IOException e) {
128 throw new RuntimeIOException(e);
129 }
130 }
131
132 @Override
133 public int readInt()
134 {
135 try {
136 return dataInputStream.readInt();
137 }
138 catch (EOFException e) {
139 throw new IndexOutOfBoundsException();
140 }
141 catch (IOException e) {
142 throw new RuntimeIOException(e);
143 }
144 }
145
146 @Override
147 public long readLong()
148 {
149 try {
150 return dataInputStream.readLong();
151 }
152 catch (EOFException e) {
153 throw new IndexOutOfBoundsException();
154 }
155 catch (IOException e) {
156 throw new RuntimeIOException(e);
157 }
158 }
159
160 @Override
161 public short readShort()
162 {
163 try {
164 return dataInputStream.readShort();
165 }
166 catch (EOFException e) {
167 throw new IndexOutOfBoundsException();
168 }
169 catch (IOException e) {
170 throw new RuntimeIOException(e);
171 }
172 }
173
174 @Override
175 public byte readByte()
176 {
177 try {
178 return dataInputStream.readByte();
179 }
180 catch (EOFException e) {
181 throw new IndexOutOfBoundsException();
182 }
183 catch (IOException e) {
184 throw new RuntimeIOException(e);
185 }
186 }
187
188 @Override
189 public boolean readBoolean()
190 {
191 try {
192 return dataInputStream.readBoolean();
193 }
194 catch (EOFException e) {
195 throw new IndexOutOfBoundsException();
196 }
197 catch (IOException e) {
198 throw new RuntimeIOException(e);
199 }
200 }
201
202 @Override
203 public int read()
204 {
205 try {
206 return dataInputStream.read();
207 }
208 catch (EOFException e) {
209 throw new IndexOutOfBoundsException();
210 }
211 catch (IOException e) {
212 throw new RuntimeIOException(e);
213 }
214 }
215
216 @Override
217 public int read(byte[] b, int off, int len)
218 {
219 try {
220 return dataInputStream.read(b, off, len);
221 }
222 catch (IOException e) {
223 throw new RuntimeIOException(e);
224 }
225 }
226
227 @Override
228 public long skip(long n)
229 {
230 try {
231 return dataInputStream.skip(n);
232 }
233 catch (IOException e) {
234 throw new RuntimeIOException(e);
235 }
236 }
237
238 @Override
239 public int available()
240 {
241 try {
242 return countingInputStream.available();
243 }
244 catch (IOException e) {
245 throw new RuntimeIOException(e);
246 }
247 }
248
249 @Override
250 public void close()
251 {
252 try {
253 dataInputStream.close();
254 }
255 catch (IOException e) {
256 throw new RuntimeIOException(e);
257 }
258 }
259
260 @Override
261 public void readBytes(byte[] destination, int destinationIndex, int length)
262 {
263 try {
264 while (length > 0) {
265 int bytesRead = dataInputStream.read(destination, destinationIndex, length);
266 if (bytesRead < 0) {
267 throw new IndexOutOfBoundsException("End of stream");
268 }
269 length -= bytesRead;
270 destinationIndex += bytesRead;
271 }
272 }
273 catch (IOException e) {
274 throw new RuntimeIOException(e);
275 }
276 }
277
278 @Override
279 public Slice readSlice(int length)
280 {
281 if (length == 0) {
282 return Slices.EMPTY_SLICE;
283 }
284 try {
285 Slice newSlice = Slices.allocate(length);
286 newSlice.setBytes(0, countingInputStream, length);
287 return newSlice;
288 }
289 catch (EOFException e) {
290 throw new IndexOutOfBoundsException();
291 }
292 catch (IOException e) {
293 throw new RuntimeIOException(e);
294 }
295 }
296
297 @Override
298 public void readBytes(Slice destination, int destinationIndex, int length)
299 {
300 try {
301 destination.setBytes(destinationIndex, countingInputStream, length);
302 }
303 catch (EOFException e) {
304 throw new IndexOutOfBoundsException();
305 }
306 catch (IOException e) {
307 throw new RuntimeIOException(e);
308 }
309 }
310
311 @Override
312 public void readBytes(OutputStream out, int length)
313 throws IOException
314 {
315 try {
316 SliceStreamUtils.copyStreamFully(this, out, length);
317 }
318 catch (EOFException e) {
319 // EOFException is thrown if this stream does not have the requested data available
320 throw new IndexOutOfBoundsException();
321 }
322 }
323 }