--- old/src/share/classes/java/util/Scanner.java 2020-06-02 19:17:56.107134760 +0100 +++ new/src/share/classes/java/util/Scanner.java 2020-06-02 19:17:56.017133502 +0100 @@ -25,16 +25,15 @@ package java.util; -import java.nio.file.Path; -import java.nio.file.Files; -import java.util.regex.*; import java.io.*; import java.math.*; import java.nio.*; import java.nio.channels.*; import java.nio.charset.*; +import java.nio.file.Path; +import java.nio.file.Files; import java.text.*; -import java.util.Locale; +import java.util.regex.*; import sun.misc.LRUCache; @@ -995,8 +994,9 @@ } // Finds the specified pattern in the buffer up to horizon. - // Returns a match for the specified input pattern. - private String findPatternInBuffer(Pattern pattern, int horizon) { + // Returns true if the specified input pattern was matched, + // and leaves the matcher field with the current match state. + private boolean findPatternInBuffer(Pattern pattern, int horizon) { matchValid = false; matcher.usePattern(pattern); int bufferLimit = buf.limit(); @@ -1014,7 +1014,7 @@ if (searchLimit != horizonLimit) { // Hit an artificial end; try to extend the match needInput = true; - return null; + return false; } // The match could go away depending on what is next if ((searchLimit == horizonLimit) && matcher.requireEnd()) { @@ -1022,27 +1022,28 @@ // that it is at the horizon and the end of input is // required for the match. needInput = true; - return null; + return false; } } // Did not hit end, or hit real end, or hit horizon position = matcher.end(); - return matcher.group(); + return true; } if (sourceClosed) - return null; + return false; // 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; + return false; } - // Returns a match for the specified input pattern anchored at - // the current position - private String matchPatternInBuffer(Pattern pattern) { + // Attempts to match a pattern anchored at the current position. + // Returns true if the specified input pattern was matched, + // and leaves the matcher field with the current match state. + private boolean matchPatternInBuffer(Pattern pattern) { matchValid = false; matcher.usePattern(pattern); matcher.region(position, buf.limit()); @@ -1050,18 +1051,18 @@ if (matcher.hitEnd() && (!sourceClosed)) { // Get more input and try again needInput = true; - return null; + return false; } position = matcher.end(); - return matcher.group(); + return true; } if (sourceClosed) - return null; + return false; // Read more to find pattern needInput = true; - return null; + return false; } // Throws if the scanner is closed @@ -1593,8 +1594,7 @@ int endPosition = 0; saveState(); while (true) { - String token = findPatternInBuffer(separatorPattern(), 0); - if (token != null) { + if (findPatternInBuffer(separatorPattern(), 0)) { endPosition = matcher.start(); break; // up to next newline } @@ -1676,10 +1676,9 @@ // Search for the pattern while (true) { - String token = findPatternInBuffer(pattern, horizon); - if (token != null) { + if (findPatternInBuffer(pattern, horizon)) { matchValid = true; - return token; + return matcher.group(); } if (needInput) readInput(); @@ -1720,8 +1719,7 @@ // Search for the pattern while (true) { - String token = matchPatternInBuffer(pattern); - if (token != null) { + if (matchPatternInBuffer(pattern)) { matchValid = true; position = matcher.end(); return this;