1 /*
2 * Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
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
147 * end of file is detected, or an exception is thrown. Subclasses are encouraged
148 * to provide a more efficient implementation of this method.
149 *
150 * @param b the buffer into which the data is read.
151 * @param off the start offset in array <code>b</code>
152 * at which the data is written.
153 * @param len the maximum number of bytes to read.
154 * @return the total number of bytes read into the buffer, or
155 * <code>-1</code> if there is no more data because the end of
156 * the stream has been reached.
157 * @exception IOException If the first byte cannot be read for any reason
158 * other than end of file, or if the input stream has been closed, or if
159 * some other I/O error occurs.
160 * @exception NullPointerException If <code>b</code> is <code>null</code>.
161 * @exception IndexOutOfBoundsException If <code>off</code> is negative,
162 * <code>len</code> is negative, or <code>len</code> is greater than
163 * <code>b.length - off</code>
164 * @see java.io.InputStream#read()
165 */
166 public int read(byte b[], int off, int len) throws IOException {
167 if (b == null) {
168 throw new NullPointerException();
169 } else if (off < 0 || len < 0 || len > b.length - off) {
170 throw new IndexOutOfBoundsException();
171 } else if (len == 0) {
172 return 0;
173 }
174
175 int c = read();
176 if (c == -1) {
177 return -1;
178 }
179 b[off] = (byte)c;
180
181 int i = 1;
182 try {
183 for (; i < len ; i++) {
184 c = read();
185 if (c == -1) {
186 break;
187 }
188 b[off + i] = (byte)c;
189 }
190 } catch (IOException ee) {
191 }
285 *
286 * <p> If an I/O error occurs reading from the input stream, then it may do
287 * so after some, but not all, bytes of {@code b} have been updated with
288 * data from the input stream. Consequently the input stream and {@code b}
289 * may be in an inconsistent state. It is strongly recommended that the
290 * stream be promptly closed if an I/O error occurs.
291 *
292 * @param b the byte array into which the data is read
293 * @param off the start offset in {@code b} at which the data is written
294 * @param len the maximum number of bytes to read
295 * @return the actual number of bytes read into the buffer
296 * @throws IOException if an I/O error occurs
297 * @throws NullPointerException if {@code b} is {@code null}
298 * @throws IndexOutOfBoundsException If {@code off} is negative, {@code len}
299 * is negative, or {@code len} is greater than {@code b.length - off}
300 *
301 * @since 9
302 */
303 public int readNBytes(byte[] b, int off, int len) throws IOException {
304 Objects.requireNonNull(b);
305 if (off < 0 || len < 0 || len > b.length - off)
306 throw new IndexOutOfBoundsException();
307 int n = 0;
308 while (n < len) {
309 int count = read(b, off + n, len - n);
310 if (count < 0)
311 break;
312 n += count;
313 }
314 return n;
315 }
316
317 /**
318 * Skips over and discards <code>n</code> bytes of data from this input
319 * stream. The <code>skip</code> method may, for a variety of reasons, end
320 * up skipping over some smaller number of bytes, possibly <code>0</code>.
321 * This may result from any of a number of conditions; reaching end of file
322 * before <code>n</code> bytes have been skipped is only one possibility.
323 * The actual number of bytes skipped is returned. If {@code n} is
324 * negative, the {@code skip} method for class {@code InputStream} always
325 * returns 0, and no bytes are skipped. Subclasses may handle the negative
326 * value differently.
|
1 /*
2 * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
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
147 * end of file is detected, or an exception is thrown. Subclasses are encouraged
148 * to provide a more efficient implementation of this method.
149 *
150 * @param b the buffer into which the data is read.
151 * @param off the start offset in array <code>b</code>
152 * at which the data is written.
153 * @param len the maximum number of bytes to read.
154 * @return the total number of bytes read into the buffer, or
155 * <code>-1</code> if there is no more data because the end of
156 * the stream has been reached.
157 * @exception IOException If the first byte cannot be read for any reason
158 * other than end of file, or if the input stream has been closed, or if
159 * some other I/O error occurs.
160 * @exception NullPointerException If <code>b</code> is <code>null</code>.
161 * @exception IndexOutOfBoundsException If <code>off</code> is negative,
162 * <code>len</code> is negative, or <code>len</code> is greater than
163 * <code>b.length - off</code>
164 * @see java.io.InputStream#read()
165 */
166 public int read(byte b[], int off, int len) throws IOException {
167 Objects.requireNonNull(b);
168 Objects.checkFromIndexSize(off, len, b.length);
169 if (len == 0) {
170 return 0;
171 }
172
173 int c = read();
174 if (c == -1) {
175 return -1;
176 }
177 b[off] = (byte)c;
178
179 int i = 1;
180 try {
181 for (; i < len ; i++) {
182 c = read();
183 if (c == -1) {
184 break;
185 }
186 b[off + i] = (byte)c;
187 }
188 } catch (IOException ee) {
189 }
283 *
284 * <p> If an I/O error occurs reading from the input stream, then it may do
285 * so after some, but not all, bytes of {@code b} have been updated with
286 * data from the input stream. Consequently the input stream and {@code b}
287 * may be in an inconsistent state. It is strongly recommended that the
288 * stream be promptly closed if an I/O error occurs.
289 *
290 * @param b the byte array into which the data is read
291 * @param off the start offset in {@code b} at which the data is written
292 * @param len the maximum number of bytes to read
293 * @return the actual number of bytes read into the buffer
294 * @throws IOException if an I/O error occurs
295 * @throws NullPointerException if {@code b} is {@code null}
296 * @throws IndexOutOfBoundsException If {@code off} is negative, {@code len}
297 * is negative, or {@code len} is greater than {@code b.length - off}
298 *
299 * @since 9
300 */
301 public int readNBytes(byte[] b, int off, int len) throws IOException {
302 Objects.requireNonNull(b);
303 Objects.checkFromIndexSize(off, len, b.length);
304
305 int n = 0;
306 while (n < len) {
307 int count = read(b, off + n, len - n);
308 if (count < 0)
309 break;
310 n += count;
311 }
312 return n;
313 }
314
315 /**
316 * Skips over and discards <code>n</code> bytes of data from this input
317 * stream. The <code>skip</code> method may, for a variety of reasons, end
318 * up skipping over some smaller number of bytes, possibly <code>0</code>.
319 * This may result from any of a number of conditions; reaching end of file
320 * before <code>n</code> bytes have been skipped is only one possibility.
321 * The actual number of bytes skipped is returned. If {@code n} is
322 * negative, the {@code skip} method for class {@code InputStream} always
323 * returns 0, and no bytes are skipped. Subclasses may handle the negative
324 * value differently.
|