static int readInt(BufferedSource source) throws IOException { try { long result = source.readDecimalLong(); String line = source.readUtf8LineStrict(); if (result < 0 || result > Integer.MAX_VALUE || !line.isEmpty()) { throw new IOException("expected an int but was \"" + result + line + "\""); } return (int) result; } catch (NumberFormatException e) { throw new IOException(e.getMessage()); } }
static int readInt(BufferedSource source) throws IOException { try { long result = source.readDecimalLong(); String line = source.readUtf8LineStrict(); if (result < 0 || result > Integer.MAX_VALUE || !line.isEmpty()) { throw new IOException("expected an int but was \"" + result + line + "\""); } return (int) result; } catch (NumberFormatException e) { throw new IOException(e.getMessage()); } }
private static int readInt(BufferedSource source) throws IOException { try { long result = source.readDecimalLong(); String line = source.readUtf8LineStrict(); if (result < 0 || result > Integer.MAX_VALUE || !line.isEmpty()) { throw new IOException("expected an int but was \"" + result + line + "\""); } return (int) result; } catch (NumberFormatException e) { throw new IOException(e.getMessage()); } }
@Test public void longDecimalStringTooLongThrows() throws IOException { try { sink.writeUtf8("12345678901234567890"); // Too many digits. sink.emit(); source.readDecimalLong(); fail(); } catch (NumberFormatException e) { assertEquals("Number too large: 12345678901234567890", e.getMessage()); } }
@Test public void longDecimalStringTooShortThrows() throws IOException { try { sink.writeUtf8(" "); sink.emit(); source.readDecimalLong(); fail(); } catch (NumberFormatException e) { assertEquals("Expected leading [0-9] or '-' character but was 0x20", e.getMessage()); } }
@Test public void longDecimalStringTooHighThrows() throws IOException { try { sink.writeUtf8("9223372036854775808"); // Right size but cannot fit. sink.emit(); source.readDecimalLong(); fail(); } catch (NumberFormatException e) { assertEquals("Number too large: 9223372036854775808", e.getMessage()); } }
@Test public void longDecimalStringTooLowThrows() throws IOException { try { sink.writeUtf8("-9223372036854775809"); // Right size but cannot fit. sink.emit(); source.readDecimalLong(); fail(); } catch (NumberFormatException e) { assertEquals("Number too large: -9223372036854775809", e.getMessage()); } }
@Test public void longDecimalEmptyThrows() throws IOException { try { sink.writeUtf8(""); sink.emit(); source.readDecimalLong(); fail(); } catch (EOFException expected) { } }
private void assertLongDecimalString(String s, long expected) throws IOException { sink.writeUtf8(s); sink.writeUtf8("zzz"); sink.emit(); long actual = source.readDecimalLong(); assertEquals(s + " --> " + expected, expected, actual); assertEquals("zzz", source.readUtf8()); }
@Test public void longDecimalStringAcrossSegment() throws IOException { sink.writeUtf8(repeat('a', SEGMENT_SIZE - 8)).writeUtf8("1234567890123456"); sink.writeUtf8("zzz"); sink.emit(); source.skip(SEGMENT_SIZE - 8); assertEquals(1234567890123456L, source.readDecimalLong()); assertEquals("zzz", source.readUtf8()); }
static int readInt(BufferedSource source) throws IOException { try { long result = source.readDecimalLong(); String line = source.readUtf8LineStrict(); if (result < 0 || result > Integer.MAX_VALUE || !line.isEmpty()) { throw new IOException("expected an int but was \"" + result + line + "\""); } return (int) result; } catch (NumberFormatException e) { throw new IOException(e.getMessage()); } }
private static int readInt(BufferedSource source) throws IOException { try { long result = source.readDecimalLong(); String line = source.readUtf8LineStrict(); if (result < 0 || result > Integer.MAX_VALUE || !line.isEmpty()) { throw new IOException("expected an int but was \"" + result + line + "\""); } return (int) result; } catch (NumberFormatException e) { throw new IOException(e.getMessage()); } }
private static int readInt(BufferedSource source) throws IOException { try { long result = source.readDecimalLong(); String line = source.readUtf8LineStrict(); if (result < 0 || result > Integer.MAX_VALUE || !line.isEmpty()) { throw new IOException("expected an int but was \"" + result + line + "\""); } return (int) result; } catch (NumberFormatException e) { throw new IOException(e.getMessage()); } }
private static int readInt(BufferedSource source) throws IOException { try { long result = source.readDecimalLong(); String line = source.readUtf8LineStrict(); if (result < 0 || result > Integer.MAX_VALUE || !line.isEmpty()) { throw new IOException("expected an int but was \"" + result + line + "\""); } return (int) result; } catch (NumberFormatException e) { throw new IOException(e.getMessage()); } }
static int readInt(BufferedSource source) throws IOException { try { long result = source.readDecimalLong(); String line = source.readUtf8LineStrict(); if (result < 0 || result > Integer.MAX_VALUE || !line.isEmpty()) { throw new IOException("expected an int but was \"" + result + line + "\""); } return (int) result; } catch (NumberFormatException e) { throw new IOException(e.getMessage()); } }