Index: src/test/java/tests/api/java/util/ScannerTest.java =================================================================== --- src/test/java/tests/api/java/util/ScannerTest.java (revision 471600) +++ src/test/java/tests/api/java/util/ScannerTest.java (working copy) @@ -2784,6 +2784,37 @@ /** * @throws IOException + * @tests java.util.Scanner#hasNextByte(int) + */ + public void test_hasNextByteI_cache() throws IOException{ + s = new Scanner("123 45"); + assertTrue(s.hasNextByte(8)); + assertEquals(83, s.nextByte()); + assertEquals(45, s.nextByte()); + + s = new Scanner("123 45"); + assertTrue(s.hasNextByte(10)); + assertTrue(s.hasNextByte(8)); + assertEquals(83, s.nextByte()); + assertEquals(45, s.nextByte()); + + s = new Scanner("-123 -45"); + assertTrue(s.hasNextByte(8)); + assertEquals(-123, s.nextInt()); + assertEquals(-45, s.nextByte()); + + s = new Scanner("123 45"); + assertTrue(s.hasNextByte()); + s.close(); + try { + s.nextByte(); + fail("Should throw IllegalStateException"); + } catch (IllegalStateException e) { + // expected + } + } + /** + * @throws IOException * @tests java.util.Scanner#hasNextByte() */ public void test_hasNextByte() throws IOException { @@ -3041,6 +3072,38 @@ /** * @throws IOException + * @tests java.util.Scanner#hasNextBigInteger(int) + */ + public void test_hasNextBigIntegerI_cache() throws IOException { + s = new Scanner("123 123456789123456789"); + assertTrue(s.hasNextBigInteger(16)); + assertEquals(new BigInteger("291"), s.nextBigInteger()); + assertEquals(new BigInteger("123456789123456789"), s.nextBigInteger()); + + s = new Scanner("123456789123456789 456"); + assertTrue(s.hasNextBigInteger(16)); + assertTrue(s.hasNextBigInteger(10)); + assertEquals(new BigInteger("123456789123456789"), s.nextBigInteger()); + assertEquals(new BigInteger("456"), s.nextBigInteger()); + + s = new Scanner("-123 -123456789123456789"); + assertTrue(s.hasNextBigInteger(8)); + assertEquals(-123, s.nextShort()); + assertEquals(new BigInteger("-123456789123456789"), s.nextBigInteger()); + + s = new Scanner("123 456"); + assertTrue(s.hasNextBigInteger()); + s.close(); + try { + s.nextBigInteger(); + fail("Should throw IllegalStateException"); + } catch (IllegalStateException e) { + // expected + } + } + + /** + * @throws IOException * @tests java.util.Scanner#hasNextBigInteger() */ public void test_hasNextBigInteger() throws IOException { @@ -3403,6 +3466,16 @@ assertEquals(-456, s.nextInt()); assertTrue(s.hasNextShort(16)); assertEquals(-789, s.nextInt()); + + s = new Scanner("123 456"); + assertTrue(s.hasNextInt()); + s.close(); + try { + s.nextInt(); + fail("Should throw IllegalStateException"); + } catch (IllegalStateException e) { + // expected + } } /** * @throws IOException @@ -3690,6 +3763,17 @@ assertEquals("123-", s.next()); assertTrue(s.hasNextFloat()); assertEquals((float)-123.0, s.nextFloat()); + + s = new Scanner("+123.4 -456.7"); + s.useLocale(Locale.ENGLISH); + assertTrue(s.hasNextFloat()); + s.close(); + try{ + s.nextFloat(); + fail("Should throw IllegalStateException"); + }catch(IllegalStateException e){ + //expected + } } @@ -4057,6 +4141,16 @@ assertEquals(-456, s.nextShort()); assertTrue(s.hasNextInt(16)); assertEquals(-789, s.nextShort()); + + s = new Scanner("123 456"); + assertTrue(s.hasNextShort()); + s.close(); + try { + s.nextShort(); + fail("Should throw IllegalStateException"); + } catch (IllegalStateException e) { + // expected + } } /** @@ -4228,7 +4322,41 @@ assertTrue(s.hasNextLong(10)); assertEquals(-123, s.nextLong(10)); } + + /** + * @throws IOException + * @tests java.util.Scanner#hasNextLong(int) + */ + public void test_hasNextLongI_cache() throws IOException { + s = new Scanner("123 456"); + assertTrue(s.hasNextLong(16)); + assertEquals(291, s.nextLong()); + assertEquals(456, s.nextLong()); + s = new Scanner("123 456"); + assertTrue(s.hasNextLong(16)); + assertTrue(s.hasNextLong(8)); + assertEquals(83, s.nextLong()); + assertEquals(456, s.nextLong()); + + s = new Scanner("-123 -456 -789"); + assertTrue(s.hasNextLong(8)); + assertEquals(-123, s.nextInt()); + assertEquals(-456, s.nextLong()); + assertTrue(s.hasNextShort(16)); + assertEquals(-789, s.nextLong()); + + s = new Scanner("123 456"); + assertTrue(s.hasNextLong()); + s.close(); + try { + s.nextLong(); + fail("Should throw IllegalStateException"); + } catch (IllegalStateException e) { + // expected + } + } + /** * @throws IOException * @tests java.util.Scanner#hasNextLong() @@ -4495,6 +4623,17 @@ s.useLocale(Locale.ENGLISH); assertTrue(s.hasNextDouble()); assertEquals(-123.4, s.nextDouble()); + + s = new Scanner("+123.4 -456.7"); + s.useLocale(Locale.ENGLISH); + assertTrue(s.hasNextDouble()); + s.close(); + try{ + s.nextDouble(); + fail("Should throw IllegalStateException"); + }catch(IllegalStateException e){ + //expected + } } /** Index: src/main/java/java/util/Scanner.java =================================================================== --- src/main/java/java/util/Scanner.java (revision 471600) +++ src/main/java/java/util/Scanner.java (working copy) @@ -620,7 +620,7 @@ String floatString = matcher.group(); floatString = removeLocaleInfoFromFloat(floatString); try { - new BigDecimal(floatString); + cacheHasNextValue = new BigDecimal(floatString); isBigDecimalValue = true; } catch (NumberFormatException e) { matchSuccessful = false; @@ -662,7 +662,7 @@ String intString = matcher.group(); intString = removeLocaleInfo(intString, DataType.INT); try { - new BigInteger(intString, radix); + cacheHasNextValue = new BigInteger(intString, radix); isBigIntegerValue = true; } catch (NumberFormatException e) { matchSuccessful = false; @@ -718,7 +718,7 @@ String intString = matcher.group(); intString = removeLocaleInfo(intString, DataType.INT); try { - Byte.parseByte(intString, radix); + cacheHasNextValue = Byte.parseByte(intString, radix); isByteValue = true; } catch (NumberFormatException e) { matchSuccessful = false; @@ -743,7 +743,7 @@ String floatString = matcher.group(); floatString = removeLocaleInfoFromFloat(floatString); try { - Double.parseDouble(floatString); + cacheHasNextValue = Double.parseDouble(floatString); isDoubleValue = true; } catch (NumberFormatException e) { matchSuccessful = false; @@ -768,7 +768,7 @@ String floatString = matcher.group(); floatString = removeLocaleInfoFromFloat(floatString); try { - Float.parseFloat(floatString); + cacheHasNextValue = Float.parseFloat(floatString); isFloatValue = true; } catch (NumberFormatException e) { matchSuccessful = false; @@ -889,7 +889,7 @@ String intString = matcher.group(); intString = removeLocaleInfo(intString, DataType.INT); try { - Long.parseLong(intString, radix); + cacheHasNextValue = Long.parseLong(intString, radix); isLongValue = true; } catch (NumberFormatException e) { matchSuccessful = false; @@ -1089,6 +1089,13 @@ * BigDecimal */ public BigDecimal nextBigDecimal() { + checkClosed(); + Object obj = cacheHasNextValue; + cacheHasNextValue = null; + if (obj != null && obj instanceof BigDecimal) { + findStartIndex = cachehasNextIndex; + return (BigDecimal) obj; + } Pattern floatPattern = getFloatPattern(); String floatString = next(floatPattern); floatString = removeLocaleInfoFromFloat(floatString); @@ -1155,6 +1162,13 @@ * BigInteger, or it is out of range */ public BigInteger nextBigInteger(int radix) { + checkClosed(); + Object obj = cacheHasNextValue; + cacheHasNextValue = null; + if (obj != null && obj instanceof BigInteger) { + findStartIndex = cachehasNextIndex; + return (BigInteger) obj; + } Pattern integerPattern = getIntegerPattern(radix); String intString = next(integerPattern); intString = removeLocaleInfo(intString, DataType.INT); @@ -1241,6 +1255,13 @@ * value, or it is out of range */ public byte nextByte(int radix) { + checkClosed(); + Object obj = cacheHasNextValue; + cacheHasNextValue = null; + if (obj != null && obj instanceof Byte) { + findStartIndex = cachehasNextIndex; + return (Byte) obj; + } Pattern integerPattern = getIntegerPattern(radix); String intString = next(integerPattern); intString = removeLocaleInfo(intString, DataType.INT); @@ -1284,6 +1305,13 @@ * value */ public double nextDouble() { + checkClosed(); + Object obj = cacheHasNextValue; + cacheHasNextValue = null; + if (obj != null && obj instanceof Double) { + findStartIndex = cachehasNextIndex; + return (Double) obj; + } Pattern floatPattern = getFloatPattern(); String floatString = next(floatPattern); floatString = removeLocaleInfoFromFloat(floatString); @@ -1325,6 +1353,13 @@ * value */ public float nextFloat() { + checkClosed(); + Object obj = cacheHasNextValue; + cacheHasNextValue = null; + if (obj != null && obj instanceof Float) { + findStartIndex = cachehasNextIndex; + return (Float) obj; + } Pattern floatPattern = getFloatPattern(); String floatString = next(floatPattern); floatString = removeLocaleInfoFromFloat(floatString); @@ -1390,6 +1425,7 @@ * value */ public int nextInt(int radix) { + checkClosed(); Object obj = cacheHasNextValue; cacheHasNextValue = null; if (obj != null && obj instanceof Integer) { @@ -1511,6 +1547,13 @@ * value, or it is out of range */ public long nextLong(int radix) { + checkClosed(); + Object obj = cacheHasNextValue; + cacheHasNextValue = null; + if (obj != null && obj instanceof Long) { + findStartIndex = cachehasNextIndex; + return (Long) obj; + } Pattern integerPattern = getIntegerPattern(radix); String intString = next(integerPattern); intString = removeLocaleInfo(intString, DataType.INT); @@ -1577,6 +1620,7 @@ * value, or it is out of range */ public short nextShort(int radix) { + checkClosed(); Object obj = cacheHasNextValue; cacheHasNextValue = null; if (obj != null && obj instanceof Short) {