Skip to content

Commit 10c22c8

Browse files
committed
Merge pull request square#104 from square/jwilson_1227_eof
Provide context when readUtf8LineStrict throws.
2 parents 492904d + 0ebea21 commit 10c22c8

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

okio/src/main/java/okio/Buffer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,12 @@ public byte getByte(long pos) {
489489

490490
@Override public String readUtf8LineStrict() throws EOFException {
491491
long newline = indexOf((byte) '\n');
492-
if (newline == -1) throw new EOFException();
492+
if (newline == -1) {
493+
Buffer data = new Buffer();
494+
copyTo(data, 0, Math.min(32, size));
495+
throw new EOFException("\\n not found: size=" + size()
496+
+ " content=" + data.readByteString().hex() + "...");
497+
}
493498
return readUtf8Line(newline);
494499
}
495500

okio/src/main/java/okio/RealBufferedSource.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,12 @@ public RealBufferedSource(Source source) {
194194

195195
@Override public String readUtf8LineStrict() throws IOException {
196196
long newline = indexOf((byte) '\n');
197-
if (newline == -1L) throw new EOFException();
197+
if (newline == -1L) {
198+
Buffer data = new Buffer();
199+
buffer.copyTo(data, 0, Math.min(32, buffer.size()));
200+
throw new EOFException("\\n not found: size=" + buffer.size()
201+
+ " content=" + data.readByteString().hex() + "...");
202+
}
198203
return buffer.readUtf8Line(newline);
199204
}
200205

okio/src/test/java/okio/ReadUtf8LineTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ public static List<Object[]> parameters() {
9090
source.readUtf8LineStrict();
9191
fail();
9292
} catch (EOFException expected) {
93+
assertEquals("\\n not found: size=0 content=...", expected.getMessage());
94+
}
95+
}
96+
97+
@Test public void eofExceptionProvidesLimitedContent() throws IOException {
98+
data.writeUtf8("aaaaaaaabbbbbbbbccccccccdddddddde");
99+
try {
100+
source.readUtf8LineStrict();
101+
fail();
102+
} catch (EOFException expected) {
103+
assertEquals("\\n not found: size=33 content=616161616161616162626262626262626363636363636363"
104+
+ "6464646464646464...", expected.getMessage());
93105
}
94106
}
95107

0 commit comments

Comments
 (0)