< prev index next >

src/java.base/share/classes/java/io/CharArrayReader.java

Print this page




  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 /**
  29  * This class implements a character buffer that can be used as a
  30  * character-input stream.
  31  *
  32  * @author      Herb Jellinek
  33  * @since       1.1
  34  */
  35 public class CharArrayReader extends Reader {
  36     /** The character buffer. */
  37     protected char buf[];
  38 
  39     /** The current buffer position. */
  40     protected int pos;
  41 
  42     /** The position of mark in buffer. */
  43     protected int markedPos = 0;
  44 
  45     /**
  46      *  The index of the end of this buffer.  There is not valid
  47      *  data at or beyond this index.
  48      */
  49     protected int count;
  50 
  51     /**
  52      * Creates a CharArrayReader from the specified array of chars.
  53      * @param buf       Input buffer (not copied)
  54      */
  55     public CharArrayReader(char buf[]) {
  56         this.buf = buf;
  57         this.pos = 0;
  58         this.count = buf.length;
  59     }
  60 
  61     /**
  62      * Creates a CharArrayReader from the specified array of chars.
  63      *
  64      * <p> The resulting reader will start reading at the given
  65      * {@code offset}.  The total number of {@code char} values that can be
  66      * read from this reader will be either {@code length} or
  67      * {@code buf.length-offset}, whichever is smaller.
  68      *
  69      * @throws IllegalArgumentException
  70      *         If {@code offset} is negative or greater than
  71      *         {@code buf.length}, or if {@code length} is negative, or if
  72      *         the sum of these two values is negative.
  73      *
  74      * @param buf       Input buffer (not copied)
  75      * @param offset    Offset of the first char to read
  76      * @param length    Number of chars to read
  77      */
  78     public CharArrayReader(char buf[], int offset, int length) {
  79         if ((offset < 0) || (offset > buf.length) || (length < 0) ||
  80             ((offset + length) < 0)) {
  81             throw new IllegalArgumentException();
  82         }
  83         this.buf = buf;
  84         this.pos = offset;
  85         this.count = Math.min(offset + length, buf.length);
  86         this.markedPos = offset;
  87     }
  88 
  89     /** Checks to make sure that the stream has not been closed */
  90     private void ensureOpen() throws IOException {
  91         if (buf == null)
  92             throw new IOException("Stream closed");
  93     }
  94 
  95     /**
  96      * Reads a single character.
  97      *
  98      * @exception   IOException  If an I/O error occurs


 101         synchronized (lock) {
 102             ensureOpen();
 103             if (pos >= count)
 104                 return -1;
 105             else
 106                 return buf[pos++];
 107         }
 108     }
 109 
 110     /**
 111      * Reads characters into a portion of an array.
 112      * @param b  Destination buffer
 113      * @param off  Offset at which to start storing characters
 114      * @param len   Maximum number of characters to read
 115      * @return  The actual number of characters read, or -1 if
 116      *          the end of the stream has been reached
 117      *
 118      * @exception   IOException  If an I/O error occurs
 119      * @exception   IndexOutOfBoundsException {@inheritDoc}
 120      */
 121     public int read(char b[], int off, int len) throws IOException {
 122         synchronized (lock) {
 123             ensureOpen();
 124             if ((off < 0) || (off > b.length) || (len < 0) ||
 125                 ((off + len) > b.length) || ((off + len) < 0)) {
 126                 throw new IndexOutOfBoundsException();
 127             } else if (len == 0) {
 128                 return 0;
 129             }
 130 
 131             if (pos >= count) {
 132                 return -1;
 133             }
 134 
 135             int avail = count - pos;
 136             if (len > avail) {
 137                 len = avail;
 138             }
 139             if (len <= 0) {
 140                 return 0;
 141             }




  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 /**
  29  * This class implements a character buffer that can be used as a
  30  * character-input stream.
  31  *
  32  * @author      Herb Jellinek
  33  * @since       1.1
  34  */
  35 public class CharArrayReader extends Reader {
  36     /** The character buffer. */
  37     protected char[] buf;
  38 
  39     /** The current buffer position. */
  40     protected int pos;
  41 
  42     /** The position of mark in buffer. */
  43     protected int markedPos = 0;
  44 
  45     /**
  46      *  The index of the end of this buffer.  There is not valid
  47      *  data at or beyond this index.
  48      */
  49     protected int count;
  50 
  51     /**
  52      * Creates a CharArrayReader from the specified array of chars.
  53      * @param buf       Input buffer (not copied)
  54      */
  55     public CharArrayReader(char[] buf) {
  56         this.buf = buf;
  57         this.pos = 0;
  58         this.count = buf.length;
  59     }
  60 
  61     /**
  62      * Creates a CharArrayReader from the specified array of chars.
  63      *
  64      * <p> The resulting reader will start reading at the given
  65      * {@code offset}.  The total number of {@code char} values that can be
  66      * read from this reader will be either {@code length} or
  67      * {@code buf.length-offset}, whichever is smaller.
  68      *
  69      * @throws IllegalArgumentException
  70      *         If {@code offset} is negative or greater than
  71      *         {@code buf.length}, or if {@code length} is negative, or if
  72      *         the sum of these two values is negative.
  73      *
  74      * @param buf       Input buffer (not copied)
  75      * @param offset    Offset of the first char to read
  76      * @param length    Number of chars to read
  77      */
  78     public CharArrayReader(char[] buf, int offset, int length) {
  79         if ((offset < 0) || (offset > buf.length) || (length < 0) ||
  80             ((offset + length) < 0)) {
  81             throw new IllegalArgumentException();
  82         }
  83         this.buf = buf;
  84         this.pos = offset;
  85         this.count = Math.min(offset + length, buf.length);
  86         this.markedPos = offset;
  87     }
  88 
  89     /** Checks to make sure that the stream has not been closed */
  90     private void ensureOpen() throws IOException {
  91         if (buf == null)
  92             throw new IOException("Stream closed");
  93     }
  94 
  95     /**
  96      * Reads a single character.
  97      *
  98      * @exception   IOException  If an I/O error occurs


 101         synchronized (lock) {
 102             ensureOpen();
 103             if (pos >= count)
 104                 return -1;
 105             else
 106                 return buf[pos++];
 107         }
 108     }
 109 
 110     /**
 111      * Reads characters into a portion of an array.
 112      * @param b  Destination buffer
 113      * @param off  Offset at which to start storing characters
 114      * @param len   Maximum number of characters to read
 115      * @return  The actual number of characters read, or -1 if
 116      *          the end of the stream has been reached
 117      *
 118      * @exception   IOException  If an I/O error occurs
 119      * @exception   IndexOutOfBoundsException {@inheritDoc}
 120      */
 121     public int read(char[] b, int off, int len) throws IOException {
 122         synchronized (lock) {
 123             ensureOpen();
 124             if ((off < 0) || (off > b.length) || (len < 0) ||
 125                 ((off + len) > b.length) || ((off + len) < 0)) {
 126                 throw new IndexOutOfBoundsException();
 127             } else if (len == 0) {
 128                 return 0;
 129             }
 130 
 131             if (pos >= count) {
 132                 return -1;
 133             }
 134 
 135             int avail = count - pos;
 136             if (len > avail) {
 137                 len = avail;
 138             }
 139             if (len <= 0) {
 140                 return 0;
 141             }


< prev index next >