Debian Codegraph @

src/com/drew/lang/StreamReader.java

raw symbols SCIP
1 /*
2 * Copyright 2002-2019 Drew Noakes and contributors
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 * More information about this project is available at:
17 *
18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21
22 package com.drew.lang;
23
24 import com.drew.lang.annotations.NotNull;
25
26 import java.io.EOFException;
27 import java.io.IOException;
28 import java.io.InputStream;
29
30 /**
31 *
32 * @author Drew Noakes https://drewnoakes.com
33 */
34 public class StreamReader extends SequentialReader
35 {
36 @NotNull
37 private final InputStream _stream;
38
39 private long _pos;
40
41 @Override
42 public long getPosition()
43 {
44 return _pos;
45 }
46
47 @SuppressWarnings("ConstantConditions")
48 public StreamReader(@NotNull InputStream stream)
49 {
50 if (stream == null)
51 throw new NullPointerException();
52
53 _stream = stream;
54 _pos = 0;
55 }
56
57 @Override
58 public byte getByte() throws IOException
59 {
60 int value = _stream.read();
61 if (value == -1)
62 throw new EOFException("End of data reached.");
63 _pos++;
64 return (byte)value;
65 }
66
67 @NotNull
68 @Override
69 public byte[] getBytes(int count) throws IOException
70 {
71 try {
72 byte[] bytes = new byte[count];
73 getBytes(bytes, 0, count);
74 return bytes;
75 } catch (OutOfMemoryError e) {
76 throw new EOFException("End of data reached.");
77 }
78
79 }
80
81 @Override
82 public void getBytes(@NotNull byte[] buffer, int offset, int count) throws IOException
83 {
84 int totalBytesRead = 0;
85 while (totalBytesRead != count)
86 {
87 final int bytesRead = _stream.read(buffer, offset + totalBytesRead, count - totalBytesRead);
88 if (bytesRead == -1)
89 throw new EOFException("End of data reached.");
90 totalBytesRead += bytesRead;
91 assert(totalBytesRead <= count);
92 }
93 _pos += totalBytesRead;
94 }
95
96 @Override
97 public void skip(long n) throws IOException
98 {
99 if (n < 0)
100 throw new IllegalArgumentException("n must be zero or greater.");
101
102 long skippedCount = skipInternal(n);
103
104 if (skippedCount != n)
105 throw new EOFException(String.format("Unable to skip. Requested %d bytes but only %d remained.", n, skippedCount));
106 }
107
108 @Override
109 public boolean trySkip(long n) throws IOException
110 {
111 if (n < 0)
112 throw new IllegalArgumentException("n must be zero or greater.");
113
114 return skipInternal(n) == n;
115 }
116
117 @Override
118 public int available() {
119 try {
120 return _stream.available();
121 } catch (IOException e) {
122 return 0;
123 }
124 }
125
126 private long skipInternal(long n) throws IOException
127 {
128 // It seems that for some streams, such as BufferedInputStream, that skip can return
129 // some smaller number than was requested. So loop until we either skip enough, or
130 // InputStream.skip returns zero.
131 //
132 // See http://stackoverflow.com/questions/14057720/robust-skipping-of-data-in-a-java-io-inputstream-and-its-subtypes
133 //
134 long skippedTotal = 0;
135 while (skippedTotal != n) {
136 long skipped = _stream.skip(n - skippedTotal);
137 skippedTotal += skipped;
138 if (skipped == 0)
139 break;
140 }
141 _pos += skippedTotal;
142 return skippedTotal;
143 }
144 }