Mercurial Hosting > luan
annotate src/goodjava/io/BufferedInputStream.java @ 1487:044a360c2300
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 01 May 2020 16:17:20 -0600 |
parents | 97740900c820 |
children | fe237d72b234 |
rev | line source |
---|---|
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 /* |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 */ |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 package goodjava.io; |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 import java.io.InputStream; |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 import java.io.FilterInputStream; |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 import java.io.IOException; |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 /** |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 * A <code>BufferedInputStream</code> adds |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 * functionality to another input stream-namely, |
1480 | 15 * the ability to buffer the input. When the <code>BufferedInputStream</code> |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 * is created, an internal buffer array is |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 * created. As bytes from the stream are read |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 * or skipped, the internal buffer is refilled |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 * as necessary from the contained input stream, |
1480 | 20 * many bytes at a time. |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 * |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 * @author Arthur van Hoff |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 * @since JDK1.0 |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 */ |
1480 | 25 public final class BufferedInputStream extends FilterInputStream { |
26 private final byte buf[]; | |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 |
1479 | 28 /** |
29 * The index one greater than the index of the last valid byte in | |
30 * the buffer. | |
31 * This value is always | |
32 * in the range <code>0</code> through <code>buf.length</code>; | |
33 * elements <code>buf[0]</code> through <code>buf[count-1] | |
34 * </code>contain buffered input data obtained | |
1480 | 35 * from the underlying input stream. |
1479 | 36 */ |
1480 | 37 private int count; |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 |
1479 | 39 /** |
40 * The current position in the buffer. This is the index of the next | |
41 * character to be read from the <code>buf</code> array. | |
42 * <p> | |
43 * This value is always in the range <code>0</code> | |
44 * through <code>count</code>. If it is less | |
45 * than <code>count</code>, then <code>buf[pos]</code> | |
46 * is the next byte to be supplied as input; | |
47 * if it is equal to <code>count</code>, then | |
48 * the next <code>read</code> or <code>skip</code> | |
49 * operation will require more bytes to be | |
50 * read from the contained input stream. | |
51 * | |
52 * @see java.io.BufferedInputStream#buf | |
53 */ | |
1480 | 54 private int pos; |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
55 |
1479 | 56 /** |
57 * Creates a <code>BufferedInputStream</code> | |
58 * and saves its argument, the input stream | |
59 * <code>in</code>, for later use. An internal | |
60 * buffer array is created and stored in <code>buf</code>. | |
61 * | |
62 * @param in the underlying input stream. | |
63 */ | |
64 public BufferedInputStream(InputStream in) { | |
1483 | 65 this(in, 8192); |
1479 | 66 } |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
67 |
1479 | 68 /** |
69 * Creates a <code>BufferedInputStream</code> | |
70 * with the specified buffer size, | |
71 * and saves its argument, the input stream | |
72 * <code>in</code>, for later use. An internal | |
73 * buffer array of length <code>size</code> | |
74 * is created and stored in <code>buf</code>. | |
75 * | |
76 * @param in the underlying input stream. | |
77 * @param size the buffer size. | |
78 */ | |
79 public BufferedInputStream(InputStream in, int size) { | |
80 super(in); | |
81 buf = new byte[size]; | |
82 } | |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
83 |
1479 | 84 /** |
1480 | 85 * Fills the buffer with more data. |
1479 | 86 * Assumes that it is being called by a synchronized method. |
87 * This method also assumes that all data has already been read in, | |
88 * hence pos > count. | |
89 */ | |
90 private void fill() throws IOException { | |
1480 | 91 pos = 0; |
92 count = 0; | |
93 int n = super.read(buf, 0, buf.length); | |
1479 | 94 if (n > 0) |
1480 | 95 count = n; |
1479 | 96 } |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
97 |
1479 | 98 /** |
99 * See | |
100 * the general contract of the <code>read</code> | |
101 * method of <code>InputStream</code>. | |
102 * | |
103 * @return the next byte of data, or <code>-1</code> if the end of the | |
104 * stream is reached. | |
105 * @exception IOException if this input stream has been closed by | |
106 * invoking its {@link #close()} method, | |
107 * or an I/O error occurs. | |
108 * @see java.io.FilterInputStream#in | |
109 */ | |
110 public synchronized int read() throws IOException { | |
111 if (pos >= count) { | |
112 fill(); | |
113 if (pos >= count) | |
114 return -1; | |
115 } | |
1480 | 116 return buf[pos++] & 0xff; |
1479 | 117 } |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
118 |
1479 | 119 /** |
120 * Read characters into a portion of an array, reading from the underlying | |
121 * stream at most once if necessary. | |
122 */ | |
1480 | 123 public synchronized int read(byte[] b, int off, int len) throws IOException { |
1479 | 124 int avail = count - pos; |
125 if (avail <= 0) { | |
1480 | 126 /* If the requested length is at least as large as the buffer, do not bother to copy the |
1479 | 127 bytes into the local buffer. In this way buffered streams will |
128 cascade harmlessly. */ | |
1480 | 129 if (len >= buf.length) { |
130 return super.read(b, off, len); | |
1479 | 131 } |
132 fill(); | |
133 avail = count - pos; | |
134 if (avail <= 0) return -1; | |
135 } | |
136 int cnt = (avail < len) ? avail : len; | |
1480 | 137 System.arraycopy(buf, pos, b, off, cnt); |
1479 | 138 pos += cnt; |
139 return cnt; | |
140 } | |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
141 |
1479 | 142 /** |
143 * See the general contract of the <code>skip</code> | |
144 * method of <code>InputStream</code>. | |
145 * | |
146 * @exception IOException if the stream does not support seek, | |
147 * or if this input stream has been closed by | |
148 * invoking its {@link #close()} method, or an | |
149 * I/O error occurs. | |
150 */ | |
151 public synchronized long skip(long n) throws IOException { | |
152 long avail = count - pos; | |
153 if (avail <= 0) { | |
1480 | 154 return super.skip(n); |
1479 | 155 } |
156 long skipped = (avail < n) ? avail : n; | |
157 pos += skipped; | |
158 return skipped; | |
159 } | |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
160 |
1479 | 161 /** |
162 * Returns an estimate of the number of bytes that can be read (or | |
163 * skipped over) from this input stream without blocking by the next | |
164 * invocation of a method for this input stream. The next invocation might be | |
165 * the same thread or another thread. A single read or skip of this | |
166 * many bytes will not block, but may read or skip fewer bytes. | |
167 * <p> | |
168 * This method returns the sum of the number of bytes remaining to be read in | |
169 * the buffer (<code>count - pos</code>) and the result of calling the | |
170 * {@link java.io.FilterInputStream#in in}.available(). | |
171 * | |
172 * @return an estimate of the number of bytes that can be read (or skipped | |
173 * over) from this input stream without blocking. | |
174 * @exception IOException if this input stream has been closed by | |
175 * invoking its {@link #close()} method, | |
176 * or an I/O error occurs. | |
177 */ | |
178 public synchronized int available() throws IOException { | |
179 int n = count - pos; | |
1480 | 180 int avail = super.available(); |
1479 | 181 return n > (Integer.MAX_VALUE - avail) |
182 ? Integer.MAX_VALUE | |
183 : n + avail; | |
184 } | |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
185 |
1480 | 186 public void mark(int readlimit) {} |
187 | |
188 public void reset() throws IOException { | |
189 throw new IOException("mark/reset not supported"); | |
1479 | 190 } |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
191 |
1479 | 192 public boolean markSupported() { |
1480 | 193 return false; |
1479 | 194 } |
1478
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
195 |
37e582f2e266
clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
196 } |