View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.helyx.basics4me.io;
19  
20  import java.io.IOException;
21  import java.io.Reader;
22  
23  
24  /**
25   * BufferedReader is a buffered character input reader. Buffering allows reading
26   * from character streams more efficiently. If the default size of the buffer is
27   * not practical, another size may be specified. Reading a character from a
28   * Reader class usually involves reading a character from its Stream or
29   * subsequent Reader. It is advisable to wrap a BufferedReader around those
30   * Readers whose read operations may have high latency. For example, the
31   * following code
32   * 
33   * <pre>
34   * BufferedReader inReader = new BufferedReader(new FileReader(&quot;file.java&quot;));
35   * </pre>
36   * 
37   * will buffer input for the file <code>file.java</code>.
38   * 
39   * @since 1.1
40   */
41  public class BufferedReader extends Reader {
42  
43      private Reader in;
44  
45      private char[] buf;
46  
47      private int marklimit = -1;
48  
49      private int count;
50  
51      private int markpos = -1;
52  
53      private int pos;
54  
55      /**
56       * Constructs a new BufferedReader on the Reader <code>in</code>. The
57       * default buffer size (8K) is allocated and all reads can now be filtered
58       * through this BufferedReader.
59       * 
60       * @param in
61       *            the Reader to buffer reads on.
62       */
63      public BufferedReader(Reader in) {
64          super(in);
65          this.in = in;
66          buf = new char[8192];
67      }
68  
69      /**
70       * Constructs a new BufferedReader on the Reader <code>in</code>. The
71       * buffer size is specified by the parameter <code>size</code> and all
72       * reads can now be filtered through this BufferedReader.
73       * 
74       * @param in
75       *            the Reader to buffer reads on.
76       * @param size
77       *            the size of buffer to allocate.
78       * @throws IllegalArgumentException
79       *             if the size is <= 0
80       */
81      public BufferedReader(Reader in, int size) {
82          super(in);
83          if (size <= 0) {
84              throw new IllegalArgumentException("Size is negative"); //$NON-NLS-1$
85          }
86          this.in = in;
87          buf = new char[size];
88      }
89  
90      /**
91       * Close the Reader. This implementation closes the Reader being filtered
92       * and releases the buffer used by this reader. If this BufferedReader has
93       * already been closed, nothing is done.
94       * 
95       * @throws IOException
96       *             If an error occurs attempting to close this BufferedReader.
97       */
98      public void close() throws IOException {
99          synchronized (lock) {
100             if (!isClosed()) {
101                 in.close();
102                 buf = null;
103             }
104         }
105     }
106 
107     private int fillbuf() throws IOException {
108         if (markpos == -1 || (pos - markpos >= marklimit)) {
109             /* Mark position not set or exceeded readlimit */
110             int result = in.read(buf, 0, buf.length);
111             if (result > 0) {
112                 markpos = -1;
113                 pos = 0;
114                 count = result == -1 ? 0 : result;
115             }
116             return result;
117         }
118         if (markpos == 0 && marklimit > buf.length) {
119             /* Increase buffer size to accommodate the readlimit */
120             int newLength = buf.length * 2;
121             if (newLength > marklimit) {
122                 newLength = marklimit;
123             }
124             char[] newbuf = new char[newLength];
125             System.arraycopy(buf, 0, newbuf, 0, buf.length);
126             buf = newbuf;
127         } else if (markpos > 0) {
128             System.arraycopy(buf, markpos, buf, 0, buf.length - markpos);
129         }
130 
131         /* Set the new position and mark position */
132         pos -= markpos;
133         count = markpos = 0;
134         int charsread = in.read(buf, pos, buf.length - pos);
135         count = charsread == -1 ? pos : pos + charsread;
136         return charsread;
137     }
138 
139     /**
140      * Answer a boolean indicating whether or not this BufferedReader is closed.
141      * 
142      * @return <code>true</code> if this reader is closed, <code>false</code>
143      *         otherwise
144      */
145     private boolean isClosed() {
146         return buf == null;
147     }
148 
149     /**
150      * Set a Mark position in this BufferedReader. The parameter
151      * <code>readLimit</code> indicates how many characters can be read before
152      * a mark is invalidated. Sending reset() will reposition the reader back to
153      * the marked position provided <code>readLimit</code> has not been
154      * surpassed.
155      * 
156      * @param readlimit
157      *            an int representing how many characters must be read before
158      *            invalidating the mark.
159      * 
160      * @throws IOException
161      *             If an error occurs attempting mark this BufferedReader.
162      * @throws IllegalArgumentException
163      *             If readlimit is < 0
164      */
165     public void mark(int readlimit) throws IOException {
166         if (readlimit < 0) {
167             throw new IllegalArgumentException();
168         }
169         synchronized (lock) {
170             if (isClosed()) {
171                 throw new IOException("Reader is closed"); //$NON-NLS-1$
172             }
173             marklimit = readlimit;
174             markpos = pos;
175         }
176     }
177 
178     /**
179      * Answers a boolean indicating whether or not this Reader supports mark()
180      * and reset(). This implementation answers <code>true</code>.
181      * 
182      * @return <code>true</code> if mark() and reset() are supported,
183      *         <code>false</code> otherwise
184      */
185     public boolean markSupported() {
186         return true;
187     }
188 
189     /**
190      * Reads a single character from this reader and returns the result as an
191      * int. The 2 higher-order characters are set to 0. If the end of reader was
192      * encountered then return -1. This implementation either returns a
193      * character from the buffer or if there are no characters available, fill
194      * the buffer then return a character or -1.
195      * 
196      * @return the character read or -1 if end of reader.
197      * 
198      * @throws IOException
199      *             If the BufferedReader is already closed or some other IO
200      *             error occurs.
201      */
202     public int read() throws IOException {
203         synchronized (lock) {
204             if (isClosed()) {
205                 throw new IOException("Reader is closed"); //$NON-NLS-1$
206             }
207             /* Are there buffered characters available? */
208             if (pos < count || fillbuf() != -1) {
209                 return buf[pos++];
210             }
211             return -1;
212         }
213     }
214 
215     /**
216      * Reads at most <code>length</code> characters from this BufferedReader
217      * and stores them at <code>offset</code> in the character array
218      * <code>buffer</code>. Returns the number of characters actually read or
219      * -1 if the end of reader was encountered. If all the buffered characters
220      * have been used, a mark has not been set, and the requested number of
221      * characters is larger than this Readers buffer size, this implementation
222      * bypasses the buffer and simply places the results directly into
223      * <code>buffer</code>.
224      * 
225      * @param buffer
226      *            character array to store the read characters
227      * @param offset
228      *            offset in buf to store the read characters
229      * @param length
230      *            maximum number of characters to read
231      * @return number of characters read or -1 if end of reader.
232      * 
233      * @throws IOException
234      *             If the BufferedReader is already closed or some other IO
235      *             error occurs.
236      */
237     public int read(char[] buffer, int offset, int length) throws IOException {
238         synchronized (lock) {
239             if (isClosed()) {
240                 throw new IOException("Reader is closed"); //$NON-NLS-1$
241             }
242             if (offset < 0 || offset > buffer.length - length || length < 0) {
243                 throw new IndexOutOfBoundsException();
244             }
245             if (length == 0) {
246                 return 0;
247             }
248             int required;
249             if (pos < count) {
250                 /* There are bytes available in the buffer. */
251                 int copylength = count - pos >= length ? length : count - pos;
252                 System.arraycopy(buf, pos, buffer, offset, copylength);
253                 pos += copylength;
254                 if (copylength == length || !in.ready()) {
255                     return copylength;
256                 }
257                 offset += copylength;
258                 required = length - copylength;
259             } else {
260                 required = length;
261             }
262 
263             while (true) {
264                 int read;
265                 /*
266                  * If we're not marked and the required size is greater than the
267                  * buffer, simply read the bytes directly bypassing the buffer.
268                  */
269                 if (markpos == -1 && required >= buf.length) {
270                     read = in.read(buffer, offset, required);
271                     if (read == -1) {
272                         return required == length ? -1 : length - required;
273                     }
274                 } else {
275                     if (fillbuf() == -1) {
276                         return required == length ? -1 : length - required;
277                     }
278                     read = count - pos >= required ? required : count - pos;
279                     System.arraycopy(buf, pos, buffer, offset, read);
280                     pos += read;
281                 }
282                 required -= read;
283                 if (required == 0) {
284                     return length;
285                 }
286                 if (!in.ready()) {
287                     return length - required;
288                 }
289                 offset += read;
290             }
291         }
292     }
293 
294     /**
295      * Answers a <code>String</code> representing the next line of text
296      * available in this BufferedReader. A line is represented by 0 or more
297      * characters followed by <code>'\n'</code>, <code>'\r'</code>,
298      * <code>'\r\n'</code> or end of stream. The <code>String</code> does not
299      * include the newline sequence. 
300      * In EBCDIC systems, a new line can also be represented by the 
301      * <code>'&#92;u0085'</code> (NEL) character.
302      * 
303      * @return the contents of the line or null if no characters were read
304      *         before end of stream.
305      * 
306      * @throws IOException
307      *             If the BufferedReader is already closed or some other IO
308      *             error occurs.
309      */
310     public String readLine() throws IOException {
311         synchronized (lock) {
312             if (isClosed()) {
313                 throw new IOException("Reader is closed"); //$NON-NLS-1$
314             }
315             /* Are there buffered characters available? */
316             if ((pos >= count) && (fillbuf() == -1)) {
317                 return null;
318             }
319             for (int charPos = pos; charPos < count; charPos++) {
320                 char ch = buf[charPos];
321                 if ((ch > '\r') && (ch != '\u0085')) {
322                     continue;
323                 }
324                 if (ch == '\n') {
325                     String res = new String(buf, pos, charPos - pos);
326                     pos = charPos + 1;
327                     return res;
328                 } else if (ch == '\r') {
329                     String res = new String(buf, pos, charPos - pos);
330                     pos = charPos + 1;
331                     if (((pos < count) || (fillbuf() != -1))
332                             && (buf[pos] == '\n')) {
333                         pos++;
334                     }
335                     return res;
336                 } else if (ch == '\u0085') {
337                     /* Also handle the EBCDIC NEL character */
338                     String res = new String(buf, pos, charPos - pos);
339                     pos = charPos + 1;
340                     return res;
341                 }
342             }
343 
344             char eol = '\0';
345             StringBuffer result = new StringBuffer(80);
346             /* Typical Line Length */
347 
348             result.append(buf, pos, count - pos);
349             pos = count;
350             while (true) {
351                 /* Are there buffered characters available? */
352                 if (pos >= count) {
353                     if (eol == '\n') {
354                         return result.toString();
355                     }
356                     // attempt to fill buffer
357                     if (fillbuf() == -1) {
358                         // characters or null.
359                         return result.length() > 0 || eol != '\0' ? result
360                                 .toString() : null;
361                     }
362                 }
363                 for (int charPos = pos; charPos < count; charPos++) {
364                     if (eol == '\0') {
365                         if ((buf[charPos] == '\n' || buf[charPos] == '\r') || (buf[charPos] == '\u0085')) {
366                             eol = buf[charPos];
367                         }
368                     } else if (eol == '\r' && (buf[charPos] == '\n')) {
369                         if (charPos > pos) {
370                             result.append(buf, pos, charPos - pos - 1);
371                         }
372                         pos = charPos + 1;
373                         return result.toString();
374                     } else if (eol != '\0') {
375                         if (charPos > pos) {
376                             result.append(buf, pos, charPos - pos - 1);
377                         }
378                         pos = charPos;
379                         return result.toString();
380                     }
381                 }
382                 if (eol == '\0') {
383                     result.append(buf, pos, count - pos);
384                 } else {
385                     result.append(buf, pos, count - pos - 1);
386                 }
387                 pos = count;
388             }
389         }
390 
391     }
392 
393     /**
394      * Answers a <code>boolean</code> indicating whether or not this Reader is
395      * ready to be read without blocking. If the result is <code>true</code>,
396      * the next <code>read()</code> will not block. If the result is
397      * <code>false</code> this Reader may or may not block when
398      * <code>read()</code> is sent.
399      * 
400      * @return <code>true</code> if the receiver will not block when
401      *         <code>read()</code> is called, <code>false</code> if unknown
402      *         or blocking will occur.
403      * 
404      * @throws IOException
405      *             If the BufferedReader is already closed or some other IO
406      *             error occurs.
407      */
408     public boolean ready() throws IOException {
409         synchronized (lock) {
410             if (isClosed()) {
411                 throw new IOException("Reader is closed"); //$NON-NLS-1$
412             }
413             return ((count - pos) > 0) || in.ready();
414         }
415     }
416 
417     /**
418      * Reset this BufferedReader's position to the last <code>mark()</code>
419      * location. Invocations of <code>read()/skip()</code> will occur from
420      * this new location. If this Reader was not marked, throw IOException.
421      * 
422      * @throws IOException
423      *             If a problem occurred, the receiver does not support
424      *             <code>mark()/reset()</code>, or no mark has been set.
425      */
426     public void reset() throws IOException {
427         synchronized (lock) {
428             if (isClosed()) {
429                 throw new IOException("Reader is closed"); //$NON-NLS-1$
430             }
431             if (markpos == -1) {
432                 throw new IOException("Mark pos is not setted"); //$NON-NLS-1$
433             }
434             pos = markpos;
435         }
436     }
437 
438     /**
439      * Skips <code>amount</code> number of characters in this Reader.
440      * Subsequent <code>read()</code>'s will not return these characters
441      * unless <code>reset()</code> is used. Skipping characters may invalidate
442      * a mark if marklimit is surpassed.
443      * 
444      * @param amount
445      *            the maximum number of characters to skip.
446      * @return the number of characters actually skipped.
447      * 
448      * @throws IOException
449      *             If the BufferedReader is already closed or some other IO
450      *             error occurs.
451      * @throws IllegalArgumentException
452      *             If amount is negative
453      */
454     public long skip(long amount) throws IOException {
455         if (amount < 0) {
456             throw new IllegalArgumentException();
457         }
458         synchronized (lock) {
459             if (isClosed()) {
460                 throw new IOException("Reader is closed"); //$NON-NLS-1$
461             }
462             if (amount < 1) {
463                 return 0;
464             }
465             if (count - pos >= amount) {
466                 pos += amount;
467                 return amount;
468             }
469 
470             long read = count - pos;
471             pos = count;
472             while (read < amount) {
473                 if (fillbuf() == -1) {
474                     return read;
475                 }
476                 if (count - pos >= amount - read) {
477                     pos += amount - read;
478                     return amount;
479                 }
480                 // Couldn't get all the characters, skip what we read
481                 read += (count - pos);
482                 pos = count;
483             }
484             return amount;
485         }
486     }
487 }