Pass through source name

This commit is contained in:
Ben Hamilton 2017-01-30 14:16:33 -08:00
parent 0f52b7c0d9
commit 499e44b03a
2 changed files with 18 additions and 10 deletions

View File

@ -60,6 +60,7 @@ public class TestCharStreams {
assertEquals(5, s.size());
assertEquals(0, s.index());
assertEquals("hello", s.toString());
assertEquals(p.toString(), s.getSourceName());
}
@Test
@ -70,6 +71,7 @@ public class TestCharStreams {
assertEquals(7, s.size());
assertEquals(0, s.index());
assertEquals("hello \uD83C\uDF0E", s.toString());
assertEquals(p.toString(), s.getSourceName());
}
@Test
@ -102,10 +104,11 @@ public class TestCharStreams {
Files.write(p, "hello".getBytes(StandardCharsets.UTF_8));
try (SeekableByteChannel c = Files.newByteChannel(p)) {
CodePointCharStream s = CharStreams.createWithUTF8Channel(
c, 4096, CodingErrorAction.REPLACE);
c, 4096, CodingErrorAction.REPLACE, "foo");
assertEquals(5, s.size());
assertEquals(0, s.index());
assertEquals("hello", s.toString());
assertEquals("foo", s.getSourceName());
}
}
@ -115,10 +118,11 @@ public class TestCharStreams {
Files.write(p, "hello \uD83C\uDF0E".getBytes(StandardCharsets.UTF_8));
try (SeekableByteChannel c = Files.newByteChannel(p)) {
CodePointCharStream s = CharStreams.createWithUTF8Channel(
c, 4096, CodingErrorAction.REPLACE);
c, 4096, CodingErrorAction.REPLACE, "foo");
assertEquals(7, s.size());
assertEquals(0, s.index());
assertEquals("hello \uD83C\uDF0E", s.toString());
assertEquals("foo", s.getSourceName());
}
}
@ -130,7 +134,7 @@ public class TestCharStreams {
Files.write(p, toWrite);
try (SeekableByteChannel c = Files.newByteChannel(p)) {
CodePointCharStream s = CharStreams.createWithUTF8Channel(
c, 4096, CodingErrorAction.REPLACE);
c, 4096, CodingErrorAction.REPLACE, "foo");
assertEquals(3, s.size());
assertEquals(0, s.index());
assertEquals("\uFFFD\uFFFD\uFFFD", s.toString());
@ -144,7 +148,7 @@ public class TestCharStreams {
Files.write(p, toWrite);
try (SeekableByteChannel c = Files.newByteChannel(p)) {
thrown.expect(CharacterCodingException.class);
CharStreams.createWithUTF8Channel(c, 4096, CodingErrorAction.REPORT);
CharStreams.createWithUTF8Channel(c, 4096, CodingErrorAction.REPORT, "foo");
}
}
@ -158,7 +162,8 @@ public class TestCharStreams {
// Note this buffer size ensures the SMP code point
// straddles the boundary of two buffers
8,
CodingErrorAction.REPLACE);
CodingErrorAction.REPLACE,
"foo");
assertEquals(7, s.size());
assertEquals(0, s.index());
assertEquals("hello \uD83C\uDF0E", s.toString());

View File

@ -49,7 +49,7 @@ public final class CharStreams {
stringIdx += Character.charCount(codePoint);
}
codePointBuffer.flip();
return new CodePointCharStream(codePointBuffer);
return new CodePointCharStream(codePointBuffer, IntStream.UNKNOWN_SOURCE_NAME);
}
public static CodePointCharStream createWithUTF8(Path path) throws IOException {
@ -57,7 +57,8 @@ public final class CharStreams {
return createWithUTF8Channel(
channel,
DEFAULT_BUFFER_SIZE,
CodingErrorAction.REPLACE);
CodingErrorAction.REPLACE,
path.toString());
}
}
@ -66,14 +67,16 @@ public final class CharStreams {
return createWithUTF8Channel(
channel,
DEFAULT_BUFFER_SIZE,
CodingErrorAction.REPLACE);
CodingErrorAction.REPLACE,
IntStream.UNKNOWN_SOURCE_NAME);
}
}
public static CodePointCharStream createWithUTF8Channel(
ReadableByteChannel channel,
int bufferSize,
CodingErrorAction decodingErrorAction
CodingErrorAction decodingErrorAction,
String sourceName
) throws IOException {
ByteBuffer utf8BytesIn = ByteBuffer.allocateDirect(bufferSize);
IntBuffer codePointsOut = IntBuffer.allocate(bufferSize);
@ -91,6 +94,6 @@ public final class CharStreams {
}
codePointsOut.limit(codePointsOut.position());
codePointsOut.flip();
return new CodePointCharStream(codePointsOut);
return new CodePointCharStream(codePointsOut, sourceName);
}
}