comparison src/goodjava/io/BufferedInputStream.java @ 1494:91c167099462

more io
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 May 2020 11:51:31 -0600
parents fe237d72b234
children
comparison
equal deleted inserted replaced
1493:471ef3e6a84e 1494:91c167099462
4 */ 4 */
5 5
6 package goodjava.io; 6 package goodjava.io;
7 7
8 import java.io.InputStream; 8 import java.io.InputStream;
9 import java.io.FilterInputStream;
10 import java.io.IOException; 9 import java.io.IOException;
11 10
12 /** 11 /**
13 * A <code>BufferedInputStream</code> adds 12 * A <code>BufferedInputStream</code> adds
14 * functionality to another input stream-namely, 13 * functionality to another input stream-namely,
20 * many bytes at a time. 19 * many bytes at a time.
21 * 20 *
22 * @author Arthur van Hoff 21 * @author Arthur van Hoff
23 * @since JDK1.0 22 * @since JDK1.0
24 */ 23 */
25 public final class BufferedInputStream extends FilterInputStream { 24 public final class BufferedInputStream extends NoMarkInputStream {
26 private final byte buf[]; 25 private final byte buf[];
27 26
28 /** 27 /**
29 * The index one greater than the index of the last valid byte in 28 * The index one greater than the index of the last valid byte in
30 * the buffer. 29 * the buffer.
81 buf = new byte[size]; 80 buf = new byte[size];
82 } 81 }
83 82
84 /** 83 /**
85 * Fills the buffer with more data. 84 * Fills the buffer with more data.
86 * Assumes that it is being called by a synchronized method.
87 * This method also assumes that all data has already been read in, 85 * This method also assumes that all data has already been read in,
88 * hence pos > count. 86 * hence pos > count.
89 */ 87 */
90 private void fill() throws IOException { 88 private void fill() throws IOException {
91 pos = 0; 89 pos = 0;
92 count = 0; 90 count = 0;
93 int n = super.read(buf, 0, buf.length); 91 int n = in.read(buf, 0, buf.length);
94 if (n > 0) 92 if (n > 0)
95 count = n; 93 count = n;
96 } 94 }
97 95
98 /** 96 /**
105 * @exception IOException if this input stream has been closed by 103 * @exception IOException if this input stream has been closed by
106 * invoking its {@link #close()} method, 104 * invoking its {@link #close()} method,
107 * or an I/O error occurs. 105 * or an I/O error occurs.
108 * @see java.io.FilterInputStream#in 106 * @see java.io.FilterInputStream#in
109 */ 107 */
110 public synchronized int read() throws IOException { 108 public int read() throws IOException {
111 if (pos >= count) { 109 if (pos >= count) {
112 fill(); 110 fill();
113 if (pos >= count) 111 if (pos >= count)
114 return -1; 112 return -1;
115 } 113 }
131 if( len == 0 ) 129 if( len == 0 )
132 return cnt; 130 return cnt;
133 off += cnt; 131 off += cnt;
134 } 132 }
135 if (len >= buf.length) { 133 if (len >= buf.length) {
136 return cnt + Math.max( 0, super.read(b, off, len) ); 134 return cnt + Math.max( 0, in.read(b, off, len) );
137 } 135 }
138 fill(); 136 fill();
139 if (count <= 0) 137 if (count <= 0)
140 return cnt; 138 return cnt;
141 System.arraycopy(buf, 0, b, off, len); 139 System.arraycopy(buf, 0, b, off, len);
142 pos += len; 140 pos += len;
143 return cnt + len; 141 return cnt + len;
144 } 142 }
145 143
146 public synchronized int read(byte[] b, int off, int len) throws IOException { 144 public int read(byte[] b, int off, int len) throws IOException {
147 if( len == 0 ) 145 if( len == 0 )
148 return 0; 146 return 0;
149 int n = read1(b,off,len); 147 int n = read1(b,off,len);
150 return n==0 ? -1 : n; 148 return n==0 ? -1 : n;
151 } 149 }
157 * @exception IOException if the stream does not support seek, 155 * @exception IOException if the stream does not support seek,
158 * or if this input stream has been closed by 156 * or if this input stream has been closed by
159 * invoking its {@link #close()} method, or an 157 * invoking its {@link #close()} method, or an
160 * I/O error occurs. 158 * I/O error occurs.
161 */ 159 */
162 public synchronized long skip(long n) throws IOException { 160 public long skip(long n) throws IOException {
163 if( n <= 0 ) 161 if( n <= 0 )
164 return 0; 162 return 0;
165 long skipped = 0; 163 long skipped = 0;
166 long avail = count - pos; 164 long avail = count - pos;
167 if( avail > 0 ) { 165 if( avail > 0 ) {
169 pos += skipped; 167 pos += skipped;
170 n -= skipped; 168 n -= skipped;
171 if( n == 0 ) 169 if( n == 0 )
172 return skipped; 170 return skipped;
173 } 171 }
174 return skipped + super.skip(n); 172 return skipped + in.skip(n);
175 } 173 }
176 174
177 /** 175 /**
178 * Returns an estimate of the number of bytes that can be read (or 176 * Returns an estimate of the number of bytes that can be read (or
179 * skipped over) from this input stream without blocking by the next 177 * skipped over) from this input stream without blocking by the next
189 * over) from this input stream without blocking. 187 * over) from this input stream without blocking.
190 * @exception IOException if this input stream has been closed by 188 * @exception IOException if this input stream has been closed by
191 * invoking its {@link #close()} method, 189 * invoking its {@link #close()} method,
192 * or an I/O error occurs. 190 * or an I/O error occurs.
193 */ 191 */
194 public synchronized int available() throws IOException { 192 public int available() throws IOException {
195 int n = count - pos; 193 int n = count - pos;
196 int avail = super.available(); 194 int avail = in.available();
197 return n > (Integer.MAX_VALUE - avail) 195 return n > (Integer.MAX_VALUE - avail)
198 ? Integer.MAX_VALUE 196 ? Integer.MAX_VALUE
199 : n + avail; 197 : n + avail;
200 } 198 }
201 199
202 public void mark(int readlimit) {}
203
204 public void reset() throws IOException {
205 throw new IOException("mark/reset not supported");
206 }
207
208 public boolean markSupported() {
209 return false;
210 }
211
212 } 200 }