How does the skip () method work in an InputStream?

InputStream is = new URL(someUrl).openStream(); long length = is.skip(Long.MAX_VALUE); 

When I call is.skip(Long.MAX_VALUE) , does the file load before returning the value or actually skip the given number of bytes (suppose the size is less than MAX_VALUE )?

+4
source share
5 answers

If you are tracking OpenJDK (assuming you have a network connection), you get SocketInputStream.skip(long)

 /** * Skips n bytes of input. * @param n the number of bytes to skip * @return the actual number of bytes skipped. * @exception IOException If an I/O error has occurred. */ public long skip(long numbytes) throws IOException { if (numbytes <= 0) { return 0; } long n = numbytes; int buflen = (int) Math.min(1024, n); byte data[] = new byte[buflen]; while (n > 0) { int r = read(data, 0, (int) Math.min((long) buflen, n)); if (r < 0) { break; } n -= r; } return numbytes - n; } 

It seems that all the data is read before the connection is closed (or you read 9 Exa-bytes;)

EDIT: The maximum read size of 1024 bytes is a bit surprising if 1.5K packets are pretty common (when sending a lot of data).

+7
source

From docs :

Skips and removes n bytes of data from the input stream.

The skip method can, for several reasons, end up with a fewer bytes, possibly 0. If n is negative, an IOException is thrown, although the skip method of the InputStream superclass does nothing. The actual number of bytes skipped is returned.

This method can skip more bytes than the rest in the backup file. This does not cause an exception, and the number of bytes skipped may include a number of bytes that were higher than the EOF of the backup file. Attempting to read from the stream after skipping through the end will result in -1 indicating the end of the file.

+2
source

Looking at the JDK6 implementation of an HTTP client , you donโ€™t see any special handling of the skip method. This means that (by default, assuming that you are not doing something complicated, for example, adjusting the request range header), you will read all these bytes in one piece at a time. The chunk size itself is variable (Peter Laurie makes a good case for 1024 bytes), but will be limited to MTU at the IP level, i.e. Usually about 1500 bytes.

In this code that was sent, the whole file will be downloaded. Unfortunately.

0
source

The Skip method skips pieces of bytes that you passed. For more information see http://www.roseindia.net/tutorial/java/corejava/zip/skip.html

0
source

Skips and removes n bytes of data from the input stream. The skip method may, for various reasons, result in passing some smaller number of bytes, possibly 0. The actual number of bytes skipped is returned.

for example see this: skip () example

0
source

Source: https://habr.com/ru/post/1393933/


All Articles