annotate src/goodjava/io/BufferedInputStream.java @ 1479:bd13aaeaf6d4

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 24 Apr 2020 10:52:54 -0600
parents 37e582f2e266
children 1f41e5921090
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 *
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 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 */
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 package goodjava.io;
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 import java.io.InputStream;
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 import java.io.FilterInputStream;
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 import java.io.IOException;
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 /**
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 * A <code>BufferedInputStream</code> adds
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 * functionality to another input stream-namely,
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 * the ability to buffer the input and to
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 * support the <code>mark</code> and <code>reset</code>
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 * methods. When the <code>BufferedInputStream</code>
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 * is created, an internal buffer array is
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 * created. As bytes from the stream are read
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 * or skipped, the internal buffer is refilled
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 * as necessary from the contained input stream,
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 * many bytes at a time. The <code>mark</code>
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 * operation remembers a point in the input
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 * stream and the <code>reset</code> operation
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 * causes all the bytes read since the most
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 * recent <code>mark</code> operation to be
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 * reread before new bytes are taken from
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 * the contained input stream.
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 *
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 * @author Arthur van Hoff
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 * @since JDK1.0
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 */
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
54 public class BufferedInputStream extends FilterInputStream {
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
56 private static int DEFAULT_BUFFER_SIZE = 8192;
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
58 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
59 * The maximum size of array to allocate.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
60 * Some VMs reserve some header words in an array.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
61 * Attempts to allocate larger arrays may result in
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
62 * OutOfMemoryError: Requested array size exceeds VM limit
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
63 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
64 private static int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
66 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
67 * The internal buffer array where the data is stored. When necessary,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
68 * it may be replaced by another array of
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
69 * a different size.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
70 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
71 protected volatile byte buf[];
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
73 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
74 * Atomic updater to provide compareAndSet for buf. This is
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
75 * necessary because closes can be asynchronous. We use nullness
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
76 * of buf[] as primary indicator that this stream is closed. (The
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
77 * "in" field is also nulled out on close.)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
78 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
79 private static final
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
80 AtomicReferenceFieldUpdater<BufferedInputStream, byte[]> bufUpdater =
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
81 AtomicReferenceFieldUpdater.newUpdater
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
82 (BufferedInputStream.class, byte[].class, "buf");
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
84 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
85 * The index one greater than the index of the last valid byte in
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
86 * the buffer.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
87 * This value is always
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
88 * in the range <code>0</code> through <code>buf.length</code>;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
89 * elements <code>buf[0]</code> through <code>buf[count-1]
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
90 * </code>contain buffered input data obtained
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
91 * from the underlying input stream.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
92 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
93 protected int count;
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
95 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
96 * The current position in the buffer. This is the index of the next
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
97 * character to be read from the <code>buf</code> array.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
98 * <p>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
99 * This value is always in the range <code>0</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
100 * through <code>count</code>. If it is less
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
101 * than <code>count</code>, then <code>buf[pos]</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
102 * is the next byte to be supplied as input;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
103 * if it is equal to <code>count</code>, then
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
104 * the next <code>read</code> or <code>skip</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
105 * operation will require more bytes to be
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
106 * read from the contained input stream.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
107 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
108 * @see java.io.BufferedInputStream#buf
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
109 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
110 protected int pos;
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
112 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
113 * The value of the <code>pos</code> field at the time the last
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
114 * <code>mark</code> method was called.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
115 * <p>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
116 * This value is always
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
117 * in the range <code>-1</code> through <code>pos</code>.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
118 * If there is no marked position in the input
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
119 * stream, this field is <code>-1</code>. If
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
120 * there is a marked position in the input
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
121 * stream, then <code>buf[markpos]</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
122 * is the first byte to be supplied as input
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
123 * after a <code>reset</code> operation. If
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
124 * <code>markpos</code> is not <code>-1</code>,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
125 * then all bytes from positions <code>buf[markpos]</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
126 * through <code>buf[pos-1]</code> must remain
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
127 * in the buffer array (though they may be
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
128 * moved to another place in the buffer array,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
129 * with suitable adjustments to the values
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
130 * of <code>count</code>, <code>pos</code>,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
131 * and <code>markpos</code>); they may not
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
132 * be discarded unless and until the difference
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
133 * between <code>pos</code> and <code>markpos</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
134 * exceeds <code>marklimit</code>.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
135 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
136 * @see java.io.BufferedInputStream#mark(int)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
137 * @see java.io.BufferedInputStream#pos
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
138 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
139 protected int markpos = -1;
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
141 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
142 * The maximum read ahead allowed after a call to the
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
143 * <code>mark</code> method before subsequent calls to the
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
144 * <code>reset</code> method fail.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
145 * Whenever the difference between <code>pos</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
146 * and <code>markpos</code> exceeds <code>marklimit</code>,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
147 * then the mark may be dropped by setting
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
148 * <code>markpos</code> to <code>-1</code>.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
149 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
150 * @see java.io.BufferedInputStream#mark(int)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
151 * @see java.io.BufferedInputStream#reset()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
152 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
153 protected int marklimit;
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
155 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
156 * Check to make sure that underlying input stream has not been
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
157 * nulled out due to close; if not return it;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
158 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
159 private InputStream getInIfOpen() throws IOException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
160 InputStream input = in;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
161 if (input == null)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
162 throw new IOException("Stream closed");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
163 return input;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
164 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
166 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
167 * Check to make sure that buffer has not been nulled out due to
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
168 * close; if not return it;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
169 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
170 private byte[] getBufIfOpen() throws IOException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
171 byte[] buffer = buf;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
172 if (buffer == null)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
173 throw new IOException("Stream closed");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
174 return buffer;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
175 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
177 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
178 * Creates a <code>BufferedInputStream</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
179 * and saves its argument, the input stream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
180 * <code>in</code>, for later use. An internal
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
181 * buffer array is created and stored in <code>buf</code>.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
182 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
183 * @param in the underlying input stream.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
184 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
185 public BufferedInputStream(InputStream in) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
186 this(in, DEFAULT_BUFFER_SIZE);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
187 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
189 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
190 * Creates a <code>BufferedInputStream</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
191 * with the specified buffer size,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
192 * and saves its argument, the input stream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
193 * <code>in</code>, for later use. An internal
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
194 * buffer array of length <code>size</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
195 * is created and stored in <code>buf</code>.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
196 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
197 * @param in the underlying input stream.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
198 * @param size the buffer size.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
199 * @exception IllegalArgumentException if {@code size <= 0}.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
200 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
201 public BufferedInputStream(InputStream in, int size) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
202 super(in);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
203 if (size <= 0) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
204 throw new IllegalArgumentException("Buffer size <= 0");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
205 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
206 buf = new byte[size];
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
207 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
208
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
209 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
210 * Fills the buffer with more data, taking into account
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
211 * shuffling and other tricks for dealing with marks.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
212 * Assumes that it is being called by a synchronized method.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
213 * This method also assumes that all data has already been read in,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
214 * hence pos > count.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
215 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
216 private void fill() throws IOException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
217 byte[] buffer = getBufIfOpen();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
218 if (markpos < 0)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
219 pos = 0; /* no mark: throw away the buffer */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
220 else if (pos >= buffer.length) /* no room left in buffer */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
221 if (markpos > 0) { /* can throw away early part of the buffer */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
222 int sz = pos - markpos;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
223 System.arraycopy(buffer, markpos, buffer, 0, sz);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
224 pos = sz;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
225 markpos = 0;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
226 } else if (buffer.length >= marklimit) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
227 markpos = -1; /* buffer got too big, invalidate mark */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
228 pos = 0; /* drop buffer contents */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
229 } else if (buffer.length >= MAX_BUFFER_SIZE) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
230 throw new OutOfMemoryError("Required array size too large");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
231 } else { /* grow buffer */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
232 int nsz = (pos <= MAX_BUFFER_SIZE - pos) ?
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
233 pos * 2 : MAX_BUFFER_SIZE;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
234 if (nsz > marklimit)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
235 nsz = marklimit;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
236 byte nbuf[] = new byte[nsz];
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
237 System.arraycopy(buffer, 0, nbuf, 0, pos);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
238 if (!bufUpdater.compareAndSet(this, buffer, nbuf)) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
239 // Can't replace buf if there was an async close.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
240 // Note: This would need to be changed if fill()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
241 // is ever made accessible to multiple threads.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
242 // But for now, the only way CAS can fail is via close.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
243 // assert buf == null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
244 throw new IOException("Stream closed");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
245 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
246 buffer = nbuf;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
247 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
248 count = pos;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
249 int n = getInIfOpen().read(buffer, pos, buffer.length - pos);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
250 if (n > 0)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
251 count = n + pos;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
252 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
253
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
254 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
255 * See
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
256 * the general contract of the <code>read</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
257 * method of <code>InputStream</code>.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
258 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
259 * @return the next byte of data, or <code>-1</code> if the end of the
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
260 * stream is reached.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
261 * @exception IOException if this input stream has been closed by
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
262 * invoking its {@link #close()} method,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
263 * or an I/O error occurs.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
264 * @see java.io.FilterInputStream#in
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
265 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
266 public synchronized int read() throws IOException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
267 if (pos >= count) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
268 fill();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
269 if (pos >= count)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
270 return -1;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
271 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
272 return getBufIfOpen()[pos++] & 0xff;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
273 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
274
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
275 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
276 * Read characters into a portion of an array, reading from the underlying
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
277 * stream at most once if necessary.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
278 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
279 private int read1(byte[] b, int off, int len) throws IOException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
280 int avail = count - pos;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
281 if (avail <= 0) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
282 /* If the requested length is at least as large as the buffer, and
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
283 if there is no mark/reset activity, do not bother to copy the
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
284 bytes into the local buffer. In this way buffered streams will
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
285 cascade harmlessly. */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
286 if (len >= getBufIfOpen().length && markpos < 0) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
287 return getInIfOpen().read(b, off, len);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
288 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
289 fill();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
290 avail = count - pos;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
291 if (avail <= 0) return -1;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
292 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
293 int cnt = (avail < len) ? avail : len;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
294 System.arraycopy(getBufIfOpen(), pos, b, off, cnt);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
295 pos += cnt;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
296 return cnt;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
297 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
298
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
299 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
300 * Reads bytes from this byte-input stream into the specified byte array,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
301 * starting at the given offset.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
302 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
303 * <p> This method implements the general contract of the corresponding
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
304 * <code>{@link InputStream#read(byte[], int, int) read}</code> method of
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
305 * the <code>{@link InputStream}</code> class. As an additional
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
306 * convenience, it attempts to read as many bytes as possible by repeatedly
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
307 * invoking the <code>read</code> method of the underlying stream. This
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
308 * iterated <code>read</code> continues until one of the following
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
309 * conditions becomes true: <ul>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
310 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
311 * <li> The specified number of bytes have been read,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
312 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
313 * <li> The <code>read</code> method of the underlying stream returns
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
314 * <code>-1</code>, indicating end-of-file, or
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
315 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
316 * <li> The <code>available</code> method of the underlying stream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
317 * returns zero, indicating that further input requests would block.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
318 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
319 * </ul> If the first <code>read</code> on the underlying stream returns
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
320 * <code>-1</code> to indicate end-of-file then this method returns
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
321 * <code>-1</code>. Otherwise this method returns the number of bytes
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
322 * actually read.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
323 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
324 * <p> Subclasses of this class are encouraged, but not required, to
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
325 * attempt to read as many bytes as possible in the same fashion.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
326 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
327 * @param b destination buffer.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
328 * @param off offset at which to start storing bytes.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
329 * @param len maximum number of bytes to read.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
330 * @return the number of bytes read, or <code>-1</code> if the end of
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
331 * the stream has been reached.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
332 * @exception IOException if this input stream has been closed by
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
333 * invoking its {@link #close()} method,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
334 * or an I/O error occurs.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
335 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
336 public synchronized int read(byte b[], int off, int len)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
337 throws IOException
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
338 {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
339 getBufIfOpen(); // Check for closed stream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
340 if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
341 throw new IndexOutOfBoundsException();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
342 } else if (len == 0) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
343 return 0;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
344 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
345
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
346 int n = 0;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
347 for (;;) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
348 int nread = read1(b, off + n, len - n);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
349 if (nread <= 0)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
350 return (n == 0) ? nread : n;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
351 n += nread;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
352 if (n >= len)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
353 return n;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
354 // if not closed but no bytes available, return
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
355 InputStream input = in;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
356 if (input != null && input.available() <= 0)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
357 return n;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
358 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
359 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
360
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
361 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
362 * See the general contract of the <code>skip</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
363 * method of <code>InputStream</code>.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
364 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
365 * @exception IOException if the stream does not support seek,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
366 * or if this input stream has been closed by
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
367 * invoking its {@link #close()} method, or an
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
368 * I/O error occurs.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
369 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
370 public synchronized long skip(long n) throws IOException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
371 getBufIfOpen(); // Check for closed stream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
372 if (n <= 0) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
373 return 0;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
374 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
375 long avail = count - pos;
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
376
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
377 if (avail <= 0) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
378 // If no mark position set then don't keep in buffer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
379 if (markpos <0)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
380 return getInIfOpen().skip(n);
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
381
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
382 // Fill in buffer to save bytes for reset
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
383 fill();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
384 avail = count - pos;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
385 if (avail <= 0)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
386 return 0;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
387 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
388
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
389 long skipped = (avail < n) ? avail : n;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
390 pos += skipped;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
391 return skipped;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
392 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
393
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
394 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
395 * Returns an estimate of the number of bytes that can be read (or
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
396 * skipped over) from this input stream without blocking by the next
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
397 * invocation of a method for this input stream. The next invocation might be
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
398 * the same thread or another thread. A single read or skip of this
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
399 * many bytes will not block, but may read or skip fewer bytes.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
400 * <p>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
401 * This method returns the sum of the number of bytes remaining to be read in
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
402 * the buffer (<code>count&nbsp;- pos</code>) and the result of calling the
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
403 * {@link java.io.FilterInputStream#in in}.available().
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
404 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
405 * @return an estimate of the number of bytes that can be read (or skipped
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
406 * over) from this input stream without blocking.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
407 * @exception IOException if this input stream has been closed by
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
408 * invoking its {@link #close()} method,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
409 * or an I/O error occurs.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
410 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
411 public synchronized int available() throws IOException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
412 int n = count - pos;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
413 int avail = getInIfOpen().available();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
414 return n > (Integer.MAX_VALUE - avail)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
415 ? Integer.MAX_VALUE
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
416 : n + avail;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
417 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
418
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
419 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
420 * See the general contract of the <code>mark</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
421 * method of <code>InputStream</code>.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
422 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
423 * @param readlimit the maximum limit of bytes that can be read before
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
424 * the mark position becomes invalid.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
425 * @see java.io.BufferedInputStream#reset()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
426 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
427 public synchronized void mark(int readlimit) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
428 marklimit = readlimit;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
429 markpos = pos;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
430 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
431
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
432 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
433 * See the general contract of the <code>reset</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
434 * method of <code>InputStream</code>.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
435 * <p>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
436 * If <code>markpos</code> is <code>-1</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
437 * (no mark has been set or the mark has been
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
438 * invalidated), an <code>IOException</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
439 * is thrown. Otherwise, <code>pos</code> is
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
440 * set equal to <code>markpos</code>.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
441 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
442 * @exception IOException if this stream has not been marked or,
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
443 * if the mark has been invalidated, or the stream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
444 * has been closed by invoking its {@link #close()}
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
445 * method, or an I/O error occurs.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
446 * @see java.io.BufferedInputStream#mark(int)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
447 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
448 public synchronized void reset() throws IOException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
449 getBufIfOpen(); // Cause exception if closed
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
450 if (markpos < 0)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
451 throw new IOException("Resetting to invalid mark");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
452 pos = markpos;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
453 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
454
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
455 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
456 * Tests if this input stream supports the <code>mark</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
457 * and <code>reset</code> methods. The <code>markSupported</code>
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
458 * method of <code>BufferedInputStream</code> returns
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
459 * <code>true</code>.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
460 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
461 * @return a <code>boolean</code> indicating if this stream type supports
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
462 * the <code>mark</code> and <code>reset</code> methods.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
463 * @see java.io.InputStream#mark(int)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
464 * @see java.io.InputStream#reset()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
465 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
466 public boolean markSupported() {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
467 return true;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
468 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
469
1479
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
470 /**
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
471 * Closes this input stream and releases any system resources
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
472 * associated with the stream.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
473 * Once the stream has been closed, further read(), available(), reset(),
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
474 * or skip() invocations will throw an IOException.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
475 * Closing a previously closed stream has no effect.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
476 *
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
477 * @exception IOException if an I/O error occurs.
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
478 */
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
479 public void close() throws IOException {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
480 byte[] buffer;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
481 while ( (buffer = buf) != null) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
482 if (bufUpdater.compareAndSet(this, buffer, null)) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
483 InputStream input = in;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
484 in = null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
485 if (input != null)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
486 input.close();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
487 return;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
488 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
489 // Else retry in case a new buf was CASed in fill()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
490 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1478
diff changeset
491 }
1478
37e582f2e266 clone BufferedInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
492 }