< prev index next >

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

Print this page
rev 15365 : imported patch 8163517-Various-cleanup-in-java-io-code


  91     public synchronized int read() {
  92         return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
  93     }
  94 
  95     /**
  96      * Reads up to <code>len</code> bytes of data from this input stream
  97      * into an array of bytes.
  98      * <p>
  99      * The <code>read</code> method of
 100      * <code>StringBufferInputStream</code> cannot block. It copies the
 101      * low eight bits from the characters in this input stream's buffer into
 102      * the byte array argument.
 103      *
 104      * @param      b     the buffer into which the data is read.
 105      * @param      off   the start offset of the data.
 106      * @param      len   the maximum number of bytes read.
 107      * @return     the total number of bytes read into the buffer, or
 108      *             <code>-1</code> if there is no more data because the end of
 109      *             the stream has been reached.
 110      */

 111     public synchronized int read(byte b[], int off, int len) {
 112         if (b == null) {
 113             throw new NullPointerException();
 114         } else if ((off < 0) || (off > b.length) || (len < 0) ||
 115                    ((off + len) > b.length) || ((off + len) < 0)) {
 116             throw new IndexOutOfBoundsException();
 117         }
 118         if (pos >= count) {
 119             return -1;
 120         }
 121 
 122         int avail = count - pos;
 123         if (len > avail) {
 124             len = avail;
 125         }
 126         if (len <= 0) {
 127             return 0;
 128         }
 129         String  s = buffer;
 130         int cnt = len;
 131         while (--cnt >= 0) {
 132             b[off++] = (byte)s.charAt(pos++);
 133         }
 134 
 135         return len;
 136     }
 137 
 138     /**
 139      * Skips <code>n</code> bytes of input from this input stream. Fewer
 140      * bytes might be skipped if the end of the input stream is reached.
 141      *
 142      * @param      n   the number of bytes to be skipped.
 143      * @return     the actual number of bytes skipped.
 144      */
 145     public synchronized long skip(long n) {
 146         if (n < 0) {
 147             return 0;
 148         }
 149         if (n > count - pos) {
 150             n = count - pos;
 151         }
 152         pos += n;
 153         return n;
 154     }


  91     public synchronized int read() {
  92         return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
  93     }
  94 
  95     /**
  96      * Reads up to <code>len</code> bytes of data from this input stream
  97      * into an array of bytes.
  98      * <p>
  99      * The <code>read</code> method of
 100      * <code>StringBufferInputStream</code> cannot block. It copies the
 101      * low eight bits from the characters in this input stream's buffer into
 102      * the byte array argument.
 103      *
 104      * @param      b     the buffer into which the data is read.
 105      * @param      off   the start offset of the data.
 106      * @param      len   the maximum number of bytes read.
 107      * @return     the total number of bytes read into the buffer, or
 108      *             <code>-1</code> if there is no more data because the end of
 109      *             the stream has been reached.
 110      */
 111     @SuppressWarnings("deprecation")
 112     public synchronized int read(byte b[], int off, int len) {
 113         if (b == null) {
 114             throw new NullPointerException();
 115         } else if ((off < 0) || (off > b.length) || (len < 0) ||
 116                    ((off + len) > b.length) || ((off + len) < 0)) {
 117             throw new IndexOutOfBoundsException();
 118         }
 119         if (pos >= count) {
 120             return -1;
 121         }
 122 
 123         int avail = count - pos;
 124         if (len > avail) {
 125             len = avail;
 126         }
 127         if (len <= 0) {
 128             return 0;
 129         }
 130         buffer.getBytes(pos, pos + len, b, off);
 131         pos += len;




 132         return len;
 133     }
 134 
 135     /**
 136      * Skips <code>n</code> bytes of input from this input stream. Fewer
 137      * bytes might be skipped if the end of the input stream is reached.
 138      *
 139      * @param      n   the number of bytes to be skipped.
 140      * @return     the actual number of bytes skipped.
 141      */
 142     public synchronized long skip(long n) {
 143         if (n < 0) {
 144             return 0;
 145         }
 146         if (n > count - pos) {
 147             n = count - pos;
 148         }
 149         pos += n;
 150         return n;
 151     }
< prev index next >