< prev index next >

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

Print this page
rev 15329 : imported patch 8163518-Integer-overflow-in-StringBufferInputStream-read-byte-int-int


 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         if (pos + len > count) {
 122             len = count - pos;


 123         }
 124         if (len <= 0) {
 125             return 0;
 126         }
 127         String  s = buffer;
 128         int cnt = len;
 129         while (--cnt >= 0) {
 130             b[off++] = (byte)s.charAt(pos++);
 131         }
 132 
 133         return len;
 134     }
 135 
 136     /**
 137      * Skips <code>n</code> bytes of input from this input stream. Fewer
 138      * bytes might be skipped if the end of the input stream is reached.
 139      *
 140      * @param      n   the number of bytes to be skipped.
 141      * @return     the actual number of bytes skipped.
 142      */




 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      */


< prev index next >