Supponendo di se bloccherà " vuoi dire che vuoi sapere quando buggera '
Dai un'occhiata a dove l'ingresso è assegnato nel metodo hasNextLine
String result = findWithinHorizon(linePattern(), 0);
Ora, date un'occhiata al findWithinHorizon
metodo
public String findWithinHorizon(Pattern pattern, int horizon) {
ensureOpen();
if (pattern == null)
throw new NullPointerException();
if (horizon < 0)
throw new IllegalArgumentException("horizon < 0");
clearCaches();
// Search for the pattern
while (true) { //it may block here if it never break
String token = findPatternInBuffer(pattern, horizon);
if (token != null) {
matchValid = true;
return token;
}
if (needInput)
readInput();
else
break; // up to end of input
}
return null;
}
Come si può vedere, è in loop all'infinito fino alla fine viene raggiunto, o fino a quando non riesce a leggere.
findPatternInBuffer
è un metodo privato della classe Scanner
che tenta di leggere l'input.
private String findPatternInBuffer(Pattern pattern, int horizon) {
matchValid = false;
matcher.usePattern(pattern);
int bufferLimit = buf.limit();
int horizonLimit = -1;
int searchLimit = bufferLimit;
if (horizon > 0) {
horizonLimit = position + horizon;
if (horizonLimit < bufferLimit)
searchLimit = horizonLimit;
}
matcher.region(position, searchLimit);
if (matcher.find()) {
if (matcher.hitEnd() && (!sourceClosed)) {
// The match may be longer if didn't hit horizon or real end
if (searchLimit != horizonLimit) {
// Hit an artificial end; try to extend the match
needInput = true;
return null;
}
// The match could go away depending on what is next
if ((searchLimit == horizonLimit) && matcher.requireEnd()) {
// Rare case: we hit the end of input and it happens
// that it is at the horizon and the end of input is
// required for the match.
needInput = true;
return null;
}
}
// Did not hit end, or hit real end, or hit horizon
position = matcher.end();
return matcher.group();
}
if (sourceClosed)
return null;
// If there is no specified horizon, or if we have not searched
// to the specified horizon yet, get more input
if ((horizon == 0) || (searchLimit != horizonLimit))
needInput = true;
return null;
}
ho postato l'intero metodo per darvi una migliore idea di quello che volevo dire da "riescono a leggere".
fonte
2015-03-24 14:27:43
Questa è una buona risposta e la spiegazione del perché funziona come fa Ma evita la vera domanda. – aioobe
@aioobe Non credo che eviti la domanda. Quando crei un'istanza di Scanner, sai da quale fonte riceve l'input, quindi sai se potrebbe bloccare o meno. Se ti viene data un'istanza di Scanner senza conoscere la sua fonte di input, non puoi sapere se bloccherà. – Eran
@aloobe: hai cancellato la tua risposta così velocemente che non ho potuto postare questo commento di follow-up qui, ma volevo notare che la tua idea generale sembra valida - avremmo solo bisogno di una migliore classe wrapper che effettivamente usasse ['InputStream. disponibile() '] (http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available%28%29) per verificare quanti caratteri può guardare avanti senza bloccare. (Naturalmente, ciò funzionerà in pratica solo se lo stream che stai leggendo implementa effettivamente un utile metodo 'available()', l'implementazione predefinita restituisce sempre zero.) –