This is because of the reason, that mark() and reset() work together as you can see in the documentation.
public void mark(int readlimit):
Marks the current position in this input stream. A subsequent call to the reset
method repositions this stream at the last marked
position so that subsequent reads re-read the same bytes.
If you have multiple threads which share the same InputStream, it could lead to problems, if these two methods wouldn't be synchronized.
Update to comment
java.io.InputStream
is an abstract class so I think that the synchronized is more for the classes that inherits InputStream as a hint. The methods mark()
and reset()
will only be used, if markSupported()
returns true. And in the class java.io.InputStream#markSupported()
returns false.
/**
* Tests if this input stream supports the <code>mark</code> and
* <code>reset</code> methods. Whether or not <code>mark</code> and
* <code>reset</code> are supported is an invariant property of a
* particular input stream instance. The <code>markSupported</code> method
* of <code>InputStream</code> returns <code>false</code>.
*
* @return <code>true</code> if this stream instance supports the mark
* and reset methods; <code>false</code> otherwise.
* @see java.io.InputStream#mark(int)
* @see java.io.InputStream#reset()
*/
public boolean markSupported() {
return false;
}
InputStream
subclass authors. Furthermore, it's very hard to imagine a realistic scenario when sharing a stream between two or more threads would be useful at all. (In fact it's hard to imagine a scenario where it wouldn't lead to disaster.) – Alexalexaread(byte[],int,int)
swallowsIOException
s… – Ploceskip()
is quite peculiar too. And the fact thatInputStream
isn't an interface in the first place, given the uselessness of the skeleton implementations provided. – AlexalexaBufferedInputStream
. It eventually declares allread
methods,skip
,mark
andreset
assynchronized
, ensuring indeed a consistent thread-safe behavior across all these operations. Then it provides an optimizedclose
operation using CAS. I’m wondering what the authors think how often a stream gets closed, typically, compared to all the other operations named above… – PloceOutputStreamWriter
, you'll be impressed by performance as well. It works slow like a hell and sources are not available. – Utgardlokiclose()
could be the result of some kind of mass "clean-up" process when theCloseable
interface was introduced. – AlexalexaCloseable
interface and theAtomicReferenceFieldUpdater
were introduced with Java 5. Still, that “cleanup” looks more like an unnecessary complication. Ironically,FileInputStream.close
usessynchronized
… – Ploce