Mercurial Hosting > luan
comparison src/goodjava/io/BufferedInputStream.java @ 1483:97740900c820
minor
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sat, 25 Apr 2020 10:31:49 -0600 |
| parents | 1f41e5921090 |
| children | fe237d72b234 |
comparison
equal
deleted
inserted
replaced
| 1482:4c409291090f | 1483:97740900c820 |
|---|---|
| 21 * | 21 * |
| 22 * @author Arthur van Hoff | 22 * @author Arthur van Hoff |
| 23 * @since JDK1.0 | 23 * @since JDK1.0 |
| 24 */ | 24 */ |
| 25 public final class BufferedInputStream extends FilterInputStream { | 25 public final class BufferedInputStream extends FilterInputStream { |
| 26 private static int DEFAULT_BUFFER_SIZE = 8192; | |
| 27 private final byte buf[]; | 26 private final byte buf[]; |
| 28 private boolean isClosed = false; | |
| 29 | 27 |
| 30 /** | 28 /** |
| 31 * The index one greater than the index of the last valid byte in | 29 * The index one greater than the index of the last valid byte in |
| 32 * the buffer. | 30 * the buffer. |
| 33 * This value is always | 31 * This value is always |
| 53 * | 51 * |
| 54 * @see java.io.BufferedInputStream#buf | 52 * @see java.io.BufferedInputStream#buf |
| 55 */ | 53 */ |
| 56 private int pos; | 54 private int pos; |
| 57 | 55 |
| 58 private void checkClosed() throws IOException { | |
| 59 if (isClosed) | |
| 60 throw new IOException("Stream closed"); | |
| 61 } | |
| 62 | |
| 63 /** | 56 /** |
| 64 * Creates a <code>BufferedInputStream</code> | 57 * Creates a <code>BufferedInputStream</code> |
| 65 * and saves its argument, the input stream | 58 * and saves its argument, the input stream |
| 66 * <code>in</code>, for later use. An internal | 59 * <code>in</code>, for later use. An internal |
| 67 * buffer array is created and stored in <code>buf</code>. | 60 * buffer array is created and stored in <code>buf</code>. |
| 68 * | 61 * |
| 69 * @param in the underlying input stream. | 62 * @param in the underlying input stream. |
| 70 */ | 63 */ |
| 71 public BufferedInputStream(InputStream in) { | 64 public BufferedInputStream(InputStream in) { |
| 72 this(in, DEFAULT_BUFFER_SIZE); | 65 this(in, 8192); |
| 73 } | 66 } |
| 74 | 67 |
| 75 /** | 68 /** |
| 76 * Creates a <code>BufferedInputStream</code> | 69 * Creates a <code>BufferedInputStream</code> |
| 77 * with the specified buffer size, | 70 * with the specified buffer size, |
| 113 * invoking its {@link #close()} method, | 106 * invoking its {@link #close()} method, |
| 114 * or an I/O error occurs. | 107 * or an I/O error occurs. |
| 115 * @see java.io.FilterInputStream#in | 108 * @see java.io.FilterInputStream#in |
| 116 */ | 109 */ |
| 117 public synchronized int read() throws IOException { | 110 public synchronized int read() throws IOException { |
| 118 checkClosed(); | |
| 119 if (pos >= count) { | 111 if (pos >= count) { |
| 120 fill(); | 112 fill(); |
| 121 if (pos >= count) | 113 if (pos >= count) |
| 122 return -1; | 114 return -1; |
| 123 } | 115 } |
| 127 /** | 119 /** |
| 128 * Read characters into a portion of an array, reading from the underlying | 120 * Read characters into a portion of an array, reading from the underlying |
| 129 * stream at most once if necessary. | 121 * stream at most once if necessary. |
| 130 */ | 122 */ |
| 131 public synchronized int read(byte[] b, int off, int len) throws IOException { | 123 public synchronized int read(byte[] b, int off, int len) throws IOException { |
| 132 checkClosed(); | |
| 133 int avail = count - pos; | 124 int avail = count - pos; |
| 134 if (avail <= 0) { | 125 if (avail <= 0) { |
| 135 /* If the requested length is at least as large as the buffer, do not bother to copy the | 126 /* If the requested length is at least as large as the buffer, do not bother to copy the |
| 136 bytes into the local buffer. In this way buffered streams will | 127 bytes into the local buffer. In this way buffered streams will |
| 137 cascade harmlessly. */ | 128 cascade harmlessly. */ |
| 156 * or if this input stream has been closed by | 147 * or if this input stream has been closed by |
| 157 * invoking its {@link #close()} method, or an | 148 * invoking its {@link #close()} method, or an |
| 158 * I/O error occurs. | 149 * I/O error occurs. |
| 159 */ | 150 */ |
| 160 public synchronized long skip(long n) throws IOException { | 151 public synchronized long skip(long n) throws IOException { |
| 161 checkClosed(); | |
| 162 long avail = count - pos; | 152 long avail = count - pos; |
| 163 if (avail <= 0) { | 153 if (avail <= 0) { |
| 164 return super.skip(n); | 154 return super.skip(n); |
| 165 } | 155 } |
| 166 long skipped = (avail < n) ? avail : n; | 156 long skipped = (avail < n) ? avail : n; |
| 201 | 191 |
| 202 public boolean markSupported() { | 192 public boolean markSupported() { |
| 203 return false; | 193 return false; |
| 204 } | 194 } |
| 205 | 195 |
| 206 /** | |
| 207 * Closes this input stream and releases any system resources | |
| 208 * associated with the stream. | |
| 209 * Once the stream has been closed, further read(), available(), reset(), | |
| 210 * or skip() invocations will throw an IOException. | |
| 211 * Closing a previously closed stream has no effect. | |
| 212 * | |
| 213 * @exception IOException if an I/O error occurs. | |
| 214 */ | |
| 215 public synchronized void close() throws IOException { | |
| 216 isClosed = true; | |
| 217 super.close(); | |
| 218 } | |
| 219 } | 196 } |
