How do I know which thread supports Java searches

java.io.InputStream.skip () says: "Throws: IOException - if the stream does not support the search or some other I / O error occurs."

How do I know which searches are supported?

when google I find Seekable, but I see that a simple FileInputStream, ByteArrayInputStream ... also supports skip (), I mean, does not give an IOException; they do not apply to searches.

+6
source share
3 answers

The only way to know for sure is to read javadocs for any thread you are interested in. The inheritance hierarchy is pretty bad, but it's an old class.

Edit: I just read javadoc, and although it seems that InputStream itself implements it (with a naive read / reject implementation), it says

"Subclasses are encouraged to provide a more efficient implementation of this method. For example, an implementation may depend on the ability to search."

Now, instead of throwing an IOException if the search is not supported, subclasses can always use the default implementation. However, most likely, due to backward compatibility, this strange design artifact was abandoned.

+6
source

An InputStream is by definition not searchable. You can only skip forward, not backward in the stream (the only exception is to rewind the stream with reset (), it only works in threads that support the label).

As for the skip () method, skipping is always possible, because the InputStream class already implements it in the general case by simply reading and discarding bytes. A separate subclass of InputStream can implement it in different ways (more efficient for a particular type).

Throw comment: "IOException - if the stream does not support the search, or some other I / O error occurs." It is misleading because it implies that there may be streams that categorically do not allow bytes to be skipped (which does not make sense, since skips are semantically the same as read + discard).

+2
source

You can move the position of FileInputStream using FileInputStream.getChannel.position(long) . Skip for streams, it is different from positioning, you cannot return with it, this is for random access devices (HDD)

+1
source

All Articles