Path: blob/master/src/java.base/share/classes/java/io/InputStream.java
41152 views
/*1* Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627import java.util.ArrayList;28import java.util.Arrays;29import java.util.List;30import java.util.Objects;3132/**33* This abstract class is the superclass of all classes representing34* an input stream of bytes.35*36* <p> Applications that need to define a subclass of {@code InputStream}37* must always provide a method that returns the next byte of input.38*39* @author Arthur van Hoff40* @see java.io.BufferedInputStream41* @see java.io.ByteArrayInputStream42* @see java.io.DataInputStream43* @see java.io.FilterInputStream44* @see java.io.InputStream#read()45* @see java.io.OutputStream46* @see java.io.PushbackInputStream47* @since 1.048*/49public abstract class InputStream implements Closeable {5051// MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to52// use when skipping.53private static final int MAX_SKIP_BUFFER_SIZE = 2048;5455private static final int DEFAULT_BUFFER_SIZE = 8192;5657/**58* Constructor for subclasses to call.59*/60public InputStream() {}6162/**63* Returns a new {@code InputStream} that reads no bytes. The returned64* stream is initially open. The stream is closed by calling the65* {@code close()} method. Subsequent calls to {@code close()} have no66* effect.67*68* <p> While the stream is open, the {@code available()}, {@code read()},69* {@code read(byte[])}, {@code read(byte[], int, int)},70* {@code readAllBytes()}, {@code readNBytes(byte[], int, int)},71* {@code readNBytes(int)}, {@code skip(long)}, {@code skipNBytes(long)},72* and {@code transferTo()} methods all behave as if end of stream has been73* reached. After the stream has been closed, these methods all throw74* {@code IOException}.75*76* <p> The {@code markSupported()} method returns {@code false}. The77* {@code mark()} method does nothing, and the {@code reset()} method78* throws {@code IOException}.79*80* @return an {@code InputStream} which contains no bytes81*82* @since 1183*/84public static InputStream nullInputStream() {85return new InputStream() {86private volatile boolean closed;8788private void ensureOpen() throws IOException {89if (closed) {90throw new IOException("Stream closed");91}92}9394@Override95public int available () throws IOException {96ensureOpen();97return 0;98}99100@Override101public int read() throws IOException {102ensureOpen();103return -1;104}105106@Override107public int read(byte[] b, int off, int len) throws IOException {108Objects.checkFromIndexSize(off, len, b.length);109if (len == 0) {110return 0;111}112ensureOpen();113return -1;114}115116@Override117public byte[] readAllBytes() throws IOException {118ensureOpen();119return new byte[0];120}121122@Override123public int readNBytes(byte[] b, int off, int len)124throws IOException {125Objects.checkFromIndexSize(off, len, b.length);126ensureOpen();127return 0;128}129130@Override131public byte[] readNBytes(int len) throws IOException {132if (len < 0) {133throw new IllegalArgumentException("len < 0");134}135ensureOpen();136return new byte[0];137}138139@Override140public long skip(long n) throws IOException {141ensureOpen();142return 0L;143}144145@Override146public void skipNBytes(long n) throws IOException {147ensureOpen();148if (n > 0) {149throw new EOFException();150}151}152153@Override154public long transferTo(OutputStream out) throws IOException {155Objects.requireNonNull(out);156ensureOpen();157return 0L;158}159160@Override161public void close() throws IOException {162closed = true;163}164};165}166167/**168* Reads the next byte of data from the input stream. The value byte is169* returned as an {@code int} in the range {@code 0} to170* {@code 255}. If no byte is available because the end of the stream171* has been reached, the value {@code -1} is returned. This method172* blocks until input data is available, the end of the stream is detected,173* or an exception is thrown.174*175* <p> A subclass must provide an implementation of this method.176*177* @return the next byte of data, or {@code -1} if the end of the178* stream is reached.179* @throws IOException if an I/O error occurs.180*/181public abstract int read() throws IOException;182183/**184* Reads some number of bytes from the input stream and stores them into185* the buffer array {@code b}. The number of bytes actually read is186* returned as an integer. This method blocks until input data is187* available, end of file is detected, or an exception is thrown.188*189* <p> If the length of {@code b} is zero, then no bytes are read and190* {@code 0} is returned; otherwise, there is an attempt to read at191* least one byte. If no byte is available because the stream is at the192* end of the file, the value {@code -1} is returned; otherwise, at193* least one byte is read and stored into {@code b}.194*195* <p> The first byte read is stored into element {@code b[0]}, the196* next one into {@code b[1]}, and so on. The number of bytes read is,197* at most, equal to the length of {@code b}. Let <i>k</i> be the198* number of bytes actually read; these bytes will be stored in elements199* {@code b[0]} through {@code b[}<i>k</i>{@code -1]},200* leaving elements {@code b[}<i>k</i>{@code ]} through201* {@code b[b.length-1]} unaffected.202*203* <p> The {@code read(b)} method for class {@code InputStream}204* has the same effect as: <pre>{@code read(b, 0, b.length) }</pre>205*206* @param b the buffer into which the data is read.207* @return the total number of bytes read into the buffer, or208* {@code -1} if there is no more data because the end of209* the stream has been reached.210* @throws IOException If the first byte cannot be read for any reason211* other than the end of the file, if the input stream has been212* closed, or if some other I/O error occurs.213* @throws NullPointerException if {@code b} is {@code null}.214* @see java.io.InputStream#read(byte[], int, int)215*/216public int read(byte b[]) throws IOException {217return read(b, 0, b.length);218}219220/**221* Reads up to {@code len} bytes of data from the input stream into222* an array of bytes. An attempt is made to read as many as223* {@code len} bytes, but a smaller number may be read.224* The number of bytes actually read is returned as an integer.225*226* <p> This method blocks until input data is available, end of file is227* detected, or an exception is thrown.228*229* <p> If {@code len} is zero, then no bytes are read and230* {@code 0} is returned; otherwise, there is an attempt to read at231* least one byte. If no byte is available because the stream is at end of232* file, the value {@code -1} is returned; otherwise, at least one233* byte is read and stored into {@code b}.234*235* <p> The first byte read is stored into element {@code b[off]}, the236* next one into {@code b[off+1]}, and so on. The number of bytes read237* is, at most, equal to {@code len}. Let <i>k</i> be the number of238* bytes actually read; these bytes will be stored in elements239* {@code b[off]} through {@code b[off+}<i>k</i>{@code -1]},240* leaving elements {@code b[off+}<i>k</i>{@code ]} through241* {@code b[off+len-1]} unaffected.242*243* <p> In every case, elements {@code b[0]} through244* {@code b[off-1]} and elements {@code b[off+len]} through245* {@code b[b.length-1]} are unaffected.246*247* <p> The {@code read(b, off, len)} method248* for class {@code InputStream} simply calls the method249* {@code read()} repeatedly. If the first such call results in an250* {@code IOException}, that exception is returned from the call to251* the {@code read(b,} {@code off,} {@code len)} method. If252* any subsequent call to {@code read()} results in a253* {@code IOException}, the exception is caught and treated as if it254* were end of file; the bytes read up to that point are stored into255* {@code b} and the number of bytes read before the exception256* occurred is returned. The default implementation of this method blocks257* until the requested amount of input data {@code len} has been read,258* end of file is detected, or an exception is thrown. Subclasses are259* encouraged to provide a more efficient implementation of this method.260*261* @param b the buffer into which the data is read.262* @param off the start offset in array {@code b}263* at which the data is written.264* @param len the maximum number of bytes to read.265* @return the total number of bytes read into the buffer, or266* {@code -1} if there is no more data because the end of267* the stream has been reached.268* @throws IOException If the first byte cannot be read for any reason269* other than end of file, or if the input stream has been closed,270* or if some other I/O error occurs.271* @throws NullPointerException If {@code b} is {@code null}.272* @throws IndexOutOfBoundsException If {@code off} is negative,273* {@code len} is negative, or {@code len} is greater than274* {@code b.length - off}275* @see java.io.InputStream#read()276*/277public int read(byte b[], int off, int len) throws IOException {278Objects.checkFromIndexSize(off, len, b.length);279if (len == 0) {280return 0;281}282283int c = read();284if (c == -1) {285return -1;286}287b[off] = (byte)c;288289int i = 1;290try {291for (; i < len ; i++) {292c = read();293if (c == -1) {294break;295}296b[off + i] = (byte)c;297}298} catch (IOException ee) {299}300return i;301}302303/**304* The maximum size of array to allocate.305* Some VMs reserve some header words in an array.306* Attempts to allocate larger arrays may result in307* OutOfMemoryError: Requested array size exceeds VM limit308*/309private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;310311/**312* Reads all remaining bytes from the input stream. This method blocks until313* all remaining bytes have been read and end of stream is detected, or an314* exception is thrown. This method does not close the input stream.315*316* <p> When this stream reaches end of stream, further invocations of this317* method will return an empty byte array.318*319* <p> Note that this method is intended for simple cases where it is320* convenient to read all bytes into a byte array. It is not intended for321* reading input streams with large amounts of data.322*323* <p> The behavior for the case where the input stream is <i>asynchronously324* closed</i>, or the thread interrupted during the read, is highly input325* stream specific, and therefore not specified.326*327* <p> If an I/O error occurs reading from the input stream, then it may do328* so after some, but not all, bytes have been read. Consequently the input329* stream may not be at end of stream and may be in an inconsistent state.330* It is strongly recommended that the stream be promptly closed if an I/O331* error occurs.332*333* @implSpec334* This method invokes {@link #readNBytes(int)} with a length of335* {@link Integer#MAX_VALUE}.336*337* @return a byte array containing the bytes read from this input stream338* @throws IOException if an I/O error occurs339* @throws OutOfMemoryError if an array of the required size cannot be340* allocated.341*342* @since 9343*/344public byte[] readAllBytes() throws IOException {345return readNBytes(Integer.MAX_VALUE);346}347348/**349* Reads up to a specified number of bytes from the input stream. This350* method blocks until the requested number of bytes has been read, end351* of stream is detected, or an exception is thrown. This method does not352* close the input stream.353*354* <p> The length of the returned array equals the number of bytes read355* from the stream. If {@code len} is zero, then no bytes are read and356* an empty byte array is returned. Otherwise, up to {@code len} bytes357* are read from the stream. Fewer than {@code len} bytes may be read if358* end of stream is encountered.359*360* <p> When this stream reaches end of stream, further invocations of this361* method will return an empty byte array.362*363* <p> Note that this method is intended for simple cases where it is364* convenient to read the specified number of bytes into a byte array. The365* total amount of memory allocated by this method is proportional to the366* number of bytes read from the stream which is bounded by {@code len}.367* Therefore, the method may be safely called with very large values of368* {@code len} provided sufficient memory is available.369*370* <p> The behavior for the case where the input stream is <i>asynchronously371* closed</i>, or the thread interrupted during the read, is highly input372* stream specific, and therefore not specified.373*374* <p> If an I/O error occurs reading from the input stream, then it may do375* so after some, but not all, bytes have been read. Consequently the input376* stream may not be at end of stream and may be in an inconsistent state.377* It is strongly recommended that the stream be promptly closed if an I/O378* error occurs.379*380* @implNote381* The number of bytes allocated to read data from this stream and return382* the result is bounded by {@code 2*(long)len}, inclusive.383*384* @param len the maximum number of bytes to read385* @return a byte array containing the bytes read from this input stream386* @throws IllegalArgumentException if {@code length} is negative387* @throws IOException if an I/O error occurs388* @throws OutOfMemoryError if an array of the required size cannot be389* allocated.390*391* @since 11392*/393public byte[] readNBytes(int len) throws IOException {394if (len < 0) {395throw new IllegalArgumentException("len < 0");396}397398List<byte[]> bufs = null;399byte[] result = null;400int total = 0;401int remaining = len;402int n;403do {404byte[] buf = new byte[Math.min(remaining, DEFAULT_BUFFER_SIZE)];405int nread = 0;406407// read to EOF which may read more or less than buffer size408while ((n = read(buf, nread,409Math.min(buf.length - nread, remaining))) > 0) {410nread += n;411remaining -= n;412}413414if (nread > 0) {415if (MAX_BUFFER_SIZE - total < nread) {416throw new OutOfMemoryError("Required array size too large");417}418if (nread < buf.length) {419buf = Arrays.copyOfRange(buf, 0, nread);420}421total += nread;422if (result == null) {423result = buf;424} else {425if (bufs == null) {426bufs = new ArrayList<>();427bufs.add(result);428}429bufs.add(buf);430}431}432// if the last call to read returned -1 or the number of bytes433// requested have been read then break434} while (n >= 0 && remaining > 0);435436if (bufs == null) {437if (result == null) {438return new byte[0];439}440return result.length == total ?441result : Arrays.copyOf(result, total);442}443444result = new byte[total];445int offset = 0;446remaining = total;447for (byte[] b : bufs) {448int count = Math.min(b.length, remaining);449System.arraycopy(b, 0, result, offset, count);450offset += count;451remaining -= count;452}453454return result;455}456457/**458* Reads the requested number of bytes from the input stream into the given459* byte array. This method blocks until {@code len} bytes of input data have460* been read, end of stream is detected, or an exception is thrown. The461* number of bytes actually read, possibly zero, is returned. This method462* does not close the input stream.463*464* <p> In the case where end of stream is reached before {@code len} bytes465* have been read, then the actual number of bytes read will be returned.466* When this stream reaches end of stream, further invocations of this467* method will return zero.468*469* <p> If {@code len} is zero, then no bytes are read and {@code 0} is470* returned; otherwise, there is an attempt to read up to {@code len} bytes.471*472* <p> The first byte read is stored into element {@code b[off]}, the next473* one in to {@code b[off+1]}, and so on. The number of bytes read is, at474* most, equal to {@code len}. Let <i>k</i> be the number of bytes actually475* read; these bytes will be stored in elements {@code b[off]} through476* {@code b[off+}<i>k</i>{@code -1]}, leaving elements {@code b[off+}<i>k</i>477* {@code ]} through {@code b[off+len-1]} unaffected.478*479* <p> The behavior for the case where the input stream is <i>asynchronously480* closed</i>, or the thread interrupted during the read, is highly input481* stream specific, and therefore not specified.482*483* <p> If an I/O error occurs reading from the input stream, then it may do484* so after some, but not all, bytes of {@code b} have been updated with485* data from the input stream. Consequently the input stream and {@code b}486* may be in an inconsistent state. It is strongly recommended that the487* stream be promptly closed if an I/O error occurs.488*489* @param b the byte array into which the data is read490* @param off the start offset in {@code b} at which the data is written491* @param len the maximum number of bytes to read492* @return the actual number of bytes read into the buffer493* @throws IOException if an I/O error occurs494* @throws NullPointerException if {@code b} is {@code null}495* @throws IndexOutOfBoundsException If {@code off} is negative, {@code len}496* is negative, or {@code len} is greater than {@code b.length - off}497*498* @since 9499*/500public int readNBytes(byte[] b, int off, int len) throws IOException {501Objects.checkFromIndexSize(off, len, b.length);502503int n = 0;504while (n < len) {505int count = read(b, off + n, len - n);506if (count < 0)507break;508n += count;509}510return n;511}512513/**514* Skips over and discards {@code n} bytes of data from this input515* stream. The {@code skip} method may, for a variety of reasons, end516* up skipping over some smaller number of bytes, possibly {@code 0}.517* This may result from any of a number of conditions; reaching end of file518* before {@code n} bytes have been skipped is only one possibility.519* The actual number of bytes skipped is returned. If {@code n} is520* negative, the {@code skip} method for class {@code InputStream} always521* returns 0, and no bytes are skipped. Subclasses may handle the negative522* value differently.523*524* <p> The {@code skip} method implementation of this class creates a525* byte array and then repeatedly reads into it until {@code n} bytes526* have been read or the end of the stream has been reached. Subclasses are527* encouraged to provide a more efficient implementation of this method.528* For instance, the implementation may depend on the ability to seek.529*530* @param n the number of bytes to be skipped.531* @return the actual number of bytes skipped which might be zero.532* @throws IOException if an I/O error occurs.533* @see java.io.InputStream#skipNBytes(long)534*/535public long skip(long n) throws IOException {536long remaining = n;537int nr;538539if (n <= 0) {540return 0;541}542543int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);544byte[] skipBuffer = new byte[size];545while (remaining > 0) {546nr = read(skipBuffer, 0, (int)Math.min(size, remaining));547if (nr < 0) {548break;549}550remaining -= nr;551}552553return n - remaining;554}555556/**557* Skips over and discards exactly {@code n} bytes of data from this input558* stream. If {@code n} is zero, then no bytes are skipped.559* If {@code n} is negative, then no bytes are skipped.560* Subclasses may handle the negative value differently.561*562* <p> This method blocks until the requested number of bytes has been563* skipped, end of file is reached, or an exception is thrown.564*565* <p> If end of stream is reached before the stream is at the desired566* position, then an {@code EOFException} is thrown.567*568* <p> If an I/O error occurs, then the input stream may be569* in an inconsistent state. It is strongly recommended that the570* stream be promptly closed if an I/O error occurs.571*572* @implNote573* Subclasses are encouraged to provide a more efficient implementation574* of this method.575*576* @implSpec577* If {@code n} is zero or negative, then no bytes are skipped.578* If {@code n} is positive, the default implementation of this method579* invokes {@link #skip(long) skip()} repeatedly with its parameter equal580* to the remaining number of bytes to skip until the requested number581* of bytes has been skipped or an error condition occurs. If at any582* point the return value of {@code skip()} is negative or greater than the583* remaining number of bytes to be skipped, then an {@code IOException} is584* thrown. If {@code skip()} ever returns zero, then {@link #read()} is585* invoked to read a single byte, and if it returns {@code -1}, then an586* {@code EOFException} is thrown. Any exception thrown by {@code skip()}587* or {@code read()} will be propagated.588*589* @param n the number of bytes to be skipped.590* @throws EOFException if end of stream is encountered before the591* stream can be positioned {@code n} bytes beyond its position592* when this method was invoked.593* @throws IOException if the stream cannot be positioned properly or594* if an I/O error occurs.595* @see java.io.InputStream#skip(long)596*597* @since 12598*/599public void skipNBytes(long n) throws IOException {600while (n > 0) {601long ns = skip(n);602if (ns > 0 && ns <= n) {603// adjust number to skip604n -= ns;605} else if (ns == 0) { // no bytes skipped606// read one byte to check for EOS607if (read() == -1) {608throw new EOFException();609}610// one byte read so decrement number to skip611n--;612} else { // skipped negative or too many bytes613throw new IOException("Unable to skip exactly");614}615}616}617618/**619* Returns an estimate of the number of bytes that can be read (or skipped620* over) from this input stream without blocking, which may be 0, or 0 when621* end of stream is detected. The read might be on the same thread or622* another thread. A single read or skip of this many bytes will not block,623* but may read or skip fewer bytes.624*625* <p> Note that while some implementations of {@code InputStream} will626* return the total number of bytes in the stream, many will not. It is627* never correct to use the return value of this method to allocate628* a buffer intended to hold all data in this stream.629*630* <p> A subclass's implementation of this method may choose to throw an631* {@link IOException} if this input stream has been closed by invoking the632* {@link #close()} method.633*634* <p> The {@code available} method of {@code InputStream} always returns635* {@code 0}.636*637* <p> This method should be overridden by subclasses.638*639* @return an estimate of the number of bytes that can be read (or640* skipped over) from this input stream without blocking or641* {@code 0} when it reaches the end of the input stream.642* @throws IOException if an I/O error occurs.643*/644public int available() throws IOException {645return 0;646}647648/**649* Closes this input stream and releases any system resources associated650* with the stream.651*652* <p> The {@code close} method of {@code InputStream} does653* nothing.654*655* @throws IOException if an I/O error occurs.656*/657public void close() throws IOException {}658659/**660* Marks the current position in this input stream. A subsequent call to661* the {@code reset} method repositions this stream at the last marked662* position so that subsequent reads re-read the same bytes.663*664* <p> The {@code readlimit} arguments tells this input stream to665* allow that many bytes to be read before the mark position gets666* invalidated.667*668* <p> The general contract of {@code mark} is that, if the method669* {@code markSupported} returns {@code true}, the stream somehow670* remembers all the bytes read after the call to {@code mark} and671* stands ready to supply those same bytes again if and whenever the method672* {@code reset} is called. However, the stream is not required to673* remember any data at all if more than {@code readlimit} bytes are674* read from the stream before {@code reset} is called.675*676* <p> Marking a closed stream should not have any effect on the stream.677*678* <p> The {@code mark} method of {@code InputStream} does679* nothing.680*681* @param readlimit the maximum limit of bytes that can be read before682* the mark position becomes invalid.683* @see java.io.InputStream#reset()684*/685public synchronized void mark(int readlimit) {}686687/**688* Repositions this stream to the position at the time the689* {@code mark} method was last called on this input stream.690*691* <p> The general contract of {@code reset} is:692*693* <ul>694* <li> If the method {@code markSupported} returns695* {@code true}, then:696*697* <ul><li> If the method {@code mark} has not been called since698* the stream was created, or the number of bytes read from the stream699* since {@code mark} was last called is larger than the argument700* to {@code mark} at that last call, then an701* {@code IOException} might be thrown.702*703* <li> If such an {@code IOException} is not thrown, then the704* stream is reset to a state such that all the bytes read since the705* most recent call to {@code mark} (or since the start of the706* file, if {@code mark} has not been called) will be resupplied707* to subsequent callers of the {@code read} method, followed by708* any bytes that otherwise would have been the next input data as of709* the time of the call to {@code reset}. </ul>710*711* <li> If the method {@code markSupported} returns712* {@code false}, then:713*714* <ul><li> The call to {@code reset} may throw an715* {@code IOException}.716*717* <li> If an {@code IOException} is not thrown, then the stream718* is reset to a fixed state that depends on the particular type of the719* input stream and how it was created. The bytes that will be supplied720* to subsequent callers of the {@code read} method depend on the721* particular type of the input stream. </ul></ul>722*723* <p>The method {@code reset} for class {@code InputStream}724* does nothing except throw an {@code IOException}.725*726* @throws IOException if this stream has not been marked or if the727* mark has been invalidated.728* @see java.io.InputStream#mark(int)729* @see java.io.IOException730*/731public synchronized void reset() throws IOException {732throw new IOException("mark/reset not supported");733}734735/**736* Tests if this input stream supports the {@code mark} and737* {@code reset} methods. Whether or not {@code mark} and738* {@code reset} are supported is an invariant property of a739* particular input stream instance. The {@code markSupported} method740* of {@code InputStream} returns {@code false}.741*742* @return {@code true} if this stream instance supports the mark743* and reset methods; {@code false} otherwise.744* @see java.io.InputStream#mark(int)745* @see java.io.InputStream#reset()746*/747public boolean markSupported() {748return false;749}750751/**752* Reads all bytes from this input stream and writes the bytes to the753* given output stream in the order that they are read. On return, this754* input stream will be at end of stream. This method does not close either755* stream.756* <p>757* This method may block indefinitely reading from the input stream, or758* writing to the output stream. The behavior for the case where the input759* and/or output stream is <i>asynchronously closed</i>, or the thread760* interrupted during the transfer, is highly input and output stream761* specific, and therefore not specified.762* <p>763* If an I/O error occurs reading from the input stream or writing to the764* output stream, then it may do so after some bytes have been read or765* written. Consequently the input stream may not be at end of stream and766* one, or both, streams may be in an inconsistent state. It is strongly767* recommended that both streams be promptly closed if an I/O error occurs.768*769* @param out the output stream, non-null770* @return the number of bytes transferred771* @throws IOException if an I/O error occurs when reading or writing772* @throws NullPointerException if {@code out} is {@code null}773*774* @since 9775*/776public long transferTo(OutputStream out) throws IOException {777Objects.requireNonNull(out, "out");778long transferred = 0;779byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];780int read;781while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {782out.write(buffer, 0, read);783transferred += read;784}785return transferred;786}787}788789790