Why doesn't the Javadoc for java.util.Scanner describe under what conditions it blocks? -
Why doesn't the Javadoc for java.util.Scanner describe under what conditions it blocks? -
the javadoc java.util.scanner notes that:
"both hasnext
, next
methods may block waiting farther input. whether hasnext
method blocks has no connection whether or not associated next
method block.".
in descriptions of various has*
, next*
methods, notes "may block while waiting input scan". page mention under conditions these methods may block, in spite of knowledge beingness prerequisite using them.
my question hence, why doesn't javadoc describe under conditions scanner's methods may block? there legitimate reason omitting information, or case of poor documentation?
now you've mentioned it, pretty vague. although, explaining mean digging implementation details.
the scanner
relies on underlying stream. scanner#next()
throw nosuchelementexception
whenever underlying stream's read()
method returns -1
upon reading:
(implementation summary)
public void next() { if(needinput) readinput(); else throwexception(); } void readinput() { int n = 0; seek { n = underlyingstream.read(buf); } grab (ioexception ioe) { lastexception = ioe; n = -1; //error happened } if (n == -1) underlyingstreamclosed = true; } void throwexception() { if (underlyingstreamclosed) throw new nosuchelementexception(); else throw new inputmismatchexception(); }
for example, lets @ inputstream
without scanner. inputstream#read()
blocks when called, until info comes in. using inputstream
such system.in
cause scanner
block, since inputstream#read()
blocks (the implementation written in native code, if you're really interested, can find file it's located in , @ implmenetation yourself). applies stream extends inputstream
, yet doesn't override read()
alter it's implementation. example, objectoutputstream
, dataoutputstream
fileinputstream
, however, override read()
(also native implmenetation). calling fileinputstream#read()
homecoming -1
when effort read, yet there nil there (although documentation method may block, have yet come across case). purely due implementation of fileinputstream#read()
, shouldn't worry about.
long story short: relies on underlying stream. become familiar streams, , you'll set. way see it, if you're worried stream blocks or not, @ implementations (i find when subclasses don't override read()
method, tend block), test them out yourself, or online. documentation should tell you, although said, have yet come across moment when fileinputstream#read()
has blocked (could help me reproduce situation, if documentation says?)
java java.util.scanner javadoc
Comments
Post a Comment