54 * will buffer the input from the specified file. Without buffering, each
55 * invocation of read() or readLine() could cause bytes to be read from the
56 * file, converted into characters, and then returned, which can be very
57 * inefficient.
58 *
59 * <p> Programs that use DataInputStreams for textual input can be localized by
60 * replacing each DataInputStream with an appropriate BufferedReader.
61 *
62 * @see FileReader
63 * @see InputStreamReader
64 * @see java.nio.file.Files#newBufferedReader
65 *
66 * @author Mark Reinhold
67 * @since 1.1
68 */
69
70 public class BufferedReader extends Reader {
71
72 private Reader in;
73
74 private char cb[];
75 private int nChars, nextChar;
76
77 private static final int INVALIDATED = -2;
78 private static final int UNMARKED = -1;
79 private int markedChar = UNMARKED;
80 private int readAheadLimit = 0; /* Valid only when markedChar > 0 */
81
82 /** If the next character is a line feed, skip it */
83 private boolean skipLF = false;
84
85 /** The skipLF flag when the mark was set */
86 private boolean markedSkipLF = false;
87
88 private static int defaultCharBufferSize = 8192;
89 private static int defaultExpectedLineLength = 80;
90
91 /**
92 * Creates a buffering character-input stream that uses an input buffer of
93 * the specified size.
94 *
129 int dst;
130 if (markedChar <= UNMARKED) {
131 /* No mark */
132 dst = 0;
133 } else {
134 /* Marked */
135 int delta = nextChar - markedChar;
136 if (delta >= readAheadLimit) {
137 /* Gone past read-ahead limit: Invalidate mark */
138 markedChar = INVALIDATED;
139 readAheadLimit = 0;
140 dst = 0;
141 } else {
142 if (readAheadLimit <= cb.length) {
143 /* Shuffle in the current buffer */
144 System.arraycopy(cb, markedChar, cb, 0, delta);
145 markedChar = 0;
146 dst = delta;
147 } else {
148 /* Reallocate buffer to accommodate read-ahead limit */
149 char ncb[] = new char[readAheadLimit];
150 System.arraycopy(cb, markedChar, ncb, 0, delta);
151 cb = ncb;
152 markedChar = 0;
153 dst = delta;
154 }
155 nextChar = nChars = delta;
156 }
157 }
158
159 int n;
160 do {
161 n = in.read(cb, dst, cb.length - dst);
162 } while (n == 0);
163 if (n > 0) {
164 nChars = dst + n;
165 nextChar = dst;
166 }
167 }
168
169 /**
257 * attempt to read as many characters as possible in the same fashion.
258 *
259 * <p> Ordinarily this method takes characters from this stream's character
260 * buffer, filling it from the underlying stream as necessary. If,
261 * however, the buffer is empty, the mark is not valid, and the requested
262 * length is at least as large as the buffer, then this method will read
263 * characters directly from the underlying stream into the given array.
264 * Thus redundant <code>BufferedReader</code>s will not copy data
265 * unnecessarily.
266 *
267 * @param cbuf Destination buffer
268 * @param off Offset at which to start storing characters
269 * @param len Maximum number of characters to read
270 *
271 * @return The number of characters read, or -1 if the end of the
272 * stream has been reached
273 *
274 * @exception IOException If an I/O error occurs
275 * @exception IndexOutOfBoundsException {@inheritDoc}
276 */
277 public int read(char cbuf[], int off, int len) throws IOException {
278 synchronized (lock) {
279 ensureOpen();
280 if ((off < 0) || (off > cbuf.length) || (len < 0) ||
281 ((off + len) > cbuf.length) || ((off + len) < 0)) {
282 throw new IndexOutOfBoundsException();
283 } else if (len == 0) {
284 return 0;
285 }
286
287 int n = read1(cbuf, off, len);
288 if (n <= 0) return n;
289 while ((n < len) && in.ready()) {
290 int n1 = read1(cbuf, off + n, len - n);
291 if (n1 <= 0) break;
292 n += n1;
293 }
294 return n;
295 }
296 }
297
|
54 * will buffer the input from the specified file. Without buffering, each
55 * invocation of read() or readLine() could cause bytes to be read from the
56 * file, converted into characters, and then returned, which can be very
57 * inefficient.
58 *
59 * <p> Programs that use DataInputStreams for textual input can be localized by
60 * replacing each DataInputStream with an appropriate BufferedReader.
61 *
62 * @see FileReader
63 * @see InputStreamReader
64 * @see java.nio.file.Files#newBufferedReader
65 *
66 * @author Mark Reinhold
67 * @since 1.1
68 */
69
70 public class BufferedReader extends Reader {
71
72 private Reader in;
73
74 private char[] cb;
75 private int nChars, nextChar;
76
77 private static final int INVALIDATED = -2;
78 private static final int UNMARKED = -1;
79 private int markedChar = UNMARKED;
80 private int readAheadLimit = 0; /* Valid only when markedChar > 0 */
81
82 /** If the next character is a line feed, skip it */
83 private boolean skipLF = false;
84
85 /** The skipLF flag when the mark was set */
86 private boolean markedSkipLF = false;
87
88 private static int defaultCharBufferSize = 8192;
89 private static int defaultExpectedLineLength = 80;
90
91 /**
92 * Creates a buffering character-input stream that uses an input buffer of
93 * the specified size.
94 *
129 int dst;
130 if (markedChar <= UNMARKED) {
131 /* No mark */
132 dst = 0;
133 } else {
134 /* Marked */
135 int delta = nextChar - markedChar;
136 if (delta >= readAheadLimit) {
137 /* Gone past read-ahead limit: Invalidate mark */
138 markedChar = INVALIDATED;
139 readAheadLimit = 0;
140 dst = 0;
141 } else {
142 if (readAheadLimit <= cb.length) {
143 /* Shuffle in the current buffer */
144 System.arraycopy(cb, markedChar, cb, 0, delta);
145 markedChar = 0;
146 dst = delta;
147 } else {
148 /* Reallocate buffer to accommodate read-ahead limit */
149 char[] ncb = new char[readAheadLimit];
150 System.arraycopy(cb, markedChar, ncb, 0, delta);
151 cb = ncb;
152 markedChar = 0;
153 dst = delta;
154 }
155 nextChar = nChars = delta;
156 }
157 }
158
159 int n;
160 do {
161 n = in.read(cb, dst, cb.length - dst);
162 } while (n == 0);
163 if (n > 0) {
164 nChars = dst + n;
165 nextChar = dst;
166 }
167 }
168
169 /**
257 * attempt to read as many characters as possible in the same fashion.
258 *
259 * <p> Ordinarily this method takes characters from this stream's character
260 * buffer, filling it from the underlying stream as necessary. If,
261 * however, the buffer is empty, the mark is not valid, and the requested
262 * length is at least as large as the buffer, then this method will read
263 * characters directly from the underlying stream into the given array.
264 * Thus redundant <code>BufferedReader</code>s will not copy data
265 * unnecessarily.
266 *
267 * @param cbuf Destination buffer
268 * @param off Offset at which to start storing characters
269 * @param len Maximum number of characters to read
270 *
271 * @return The number of characters read, or -1 if the end of the
272 * stream has been reached
273 *
274 * @exception IOException If an I/O error occurs
275 * @exception IndexOutOfBoundsException {@inheritDoc}
276 */
277 public int read(char[] cbuf, int off, int len) throws IOException {
278 synchronized (lock) {
279 ensureOpen();
280 if ((off < 0) || (off > cbuf.length) || (len < 0) ||
281 ((off + len) > cbuf.length) || ((off + len) < 0)) {
282 throw new IndexOutOfBoundsException();
283 } else if (len == 0) {
284 return 0;
285 }
286
287 int n = read1(cbuf, off, len);
288 if (n <= 0) return n;
289 while ((n < len) && in.ready()) {
290 int n1 = read1(cbuf, off + n, len - n);
291 if (n1 <= 0) break;
292 n += n1;
293 }
294 return n;
295 }
296 }
297
|