comparison src/goodjava/io/BufferedInputStream.java @ 1489:fe237d72b234

improve BufferedInputStream
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 02 May 2020 16:28:24 -0600
parents 97740900c820
children 91c167099462
comparison
equal deleted inserted replaced
1488:af55cfad6e12 1489:fe237d72b234
118 118
119 /** 119 /**
120 * Read characters into a portion of an array, reading from the underlying 120 * Read characters into a portion of an array, reading from the underlying
121 * stream at most once if necessary. 121 * stream at most once if necessary.
122 */ 122 */
123 private int read1(byte[] b, int off, int len) throws IOException {
124 int cnt = 0;
125 int avail = count - pos;
126 if( avail > 0 ) {
127 cnt = (avail < len) ? avail : len;
128 System.arraycopy(buf, pos, b, off, cnt);
129 pos += cnt;
130 len -= cnt;
131 if( len == 0 )
132 return cnt;
133 off += cnt;
134 }
135 if (len >= buf.length) {
136 return cnt + Math.max( 0, super.read(b, off, len) );
137 }
138 fill();
139 if (count <= 0)
140 return cnt;
141 System.arraycopy(buf, 0, b, off, len);
142 pos += len;
143 return cnt + len;
144 }
145
123 public synchronized int read(byte[] b, int off, int len) throws IOException { 146 public synchronized int read(byte[] b, int off, int len) throws IOException {
124 int avail = count - pos; 147 if( len == 0 )
125 if (avail <= 0) { 148 return 0;
126 /* If the requested length is at least as large as the buffer, do not bother to copy the 149 int n = read1(b,off,len);
127 bytes into the local buffer. In this way buffered streams will 150 return n==0 ? -1 : n;
128 cascade harmlessly. */
129 if (len >= buf.length) {
130 return super.read(b, off, len);
131 }
132 fill();
133 avail = count - pos;
134 if (avail <= 0) return -1;
135 }
136 int cnt = (avail < len) ? avail : len;
137 System.arraycopy(buf, pos, b, off, cnt);
138 pos += cnt;
139 return cnt;
140 } 151 }
141 152
142 /** 153 /**
143 * See the general contract of the <code>skip</code> 154 * See the general contract of the <code>skip</code>
144 * method of <code>InputStream</code>. 155 * method of <code>InputStream</code>.
147 * or if this input stream has been closed by 158 * or if this input stream has been closed by
148 * invoking its {@link #close()} method, or an 159 * invoking its {@link #close()} method, or an
149 * I/O error occurs. 160 * I/O error occurs.
150 */ 161 */
151 public synchronized long skip(long n) throws IOException { 162 public synchronized long skip(long n) throws IOException {
163 if( n <= 0 )
164 return 0;
165 long skipped = 0;
152 long avail = count - pos; 166 long avail = count - pos;
153 if (avail <= 0) { 167 if( avail > 0 ) {
154 return super.skip(n); 168 skipped = (avail < n) ? avail : n;
155 } 169 pos += skipped;
156 long skipped = (avail < n) ? avail : n; 170 n -= skipped;
157 pos += skipped; 171 if( n == 0 )
158 return skipped; 172 return skipped;
173 }
174 return skipped + super.skip(n);
159 } 175 }
160 176
161 /** 177 /**
162 * Returns an estimate of the number of bytes that can be read (or 178 * Returns an estimate of the number of bytes that can be read (or
163 * skipped over) from this input stream without blocking by the next 179 * skipped over) from this input stream without blocking by the next