[JBoss JIRA] (SHRINKWRAP-461) SeekableInMemoryByteChannel returns incorrect value for EOF
by Resurgam Immortius (JIRA)
[ https://issues.jboss.org/browse/SHRINKWRAP-461?page=com.atlassian.jira.pl... ]
Resurgam Immortius updated SHRINKWRAP-461:
------------------------------------------
Description:
The NIO2 API's SeekableInMemoryByteChannel currently returns 0 when a read requests data either at the end of the data or past the end of the data.
The API for SeekableByteChannel ( http://docs.oracle.com/javase/7/docs/api/java/nio/channels/SeekableByteCh... ) states the return value should be -1 if EOF has been reached. This behavior is confirmed through testing of FileChannelImpl.
Java's sun.nio.cs.StreamDecoder.readBytes() expects this behavior, throwing an IOException if zero bytes are read.
I encountered this issue using GSON to write and then read a json file from a ShrinkWrap archive as part of a unit test. At the end of reading a json file, GSON checks that the EOF has been reached, and at this point a read returns 0 and causes an exception in sun.nio.cs.StreamDecoder.readBytes().
A unit test showing the issue:
{code:java}
@Test
public void test() throws IOException {
JavaArchive homeArchive = ShrinkWrap.create(JavaArchive.class);
FileSystem vfs = ShrinkWrapFileSystems.newFileSystem(homeArchive);
Path path = vfs.getPath("out.json");
try (BufferedWriter writer = Files.newBufferedWriter(path, Charset.forName("UTF-8"))) {
new GsonBuilder().create().toJson("Test", writer);
}
try (BufferedReader reader = Files.newBufferedReader(path, Charset.forName("UTF-8"))) {
assertEquals("Test", new GsonBuilder().create().fromJson(reader), String.class));
}
}
{code}
was:
The NIO2 API's SeekableInMemoryByteChannel currently returns 0 when a read requests data either at the end of the data or past the end of the data.
The API for SeekableByteChannel ( http://docs.oracle.com/javase/7/docs/api/java/nio/channels/SeekableByteCh... ) states the return value should be -1 if EOF has been reached. This behavior is confirmed through testing of FileChannelImpl.
Java's sun.nio.cs.StreamDecoder.readBytes() expects this behavior, throwing an IOException if zero bytes are read.
I encountered this issue using GSON to write and then read a json file from a ShrinkWrap archive as part of a unit test. At the end of reading a json file, GSON checks that the EOF has been reached, and at this point a read returns 0 and causes an exception in sun.nio.cs.StreamDecoder.readBytes().
A unit test showing the issue:
{code:java}
@Test
public void test() throws IOException {
JavaArchive homeArchive = ShrinkWrap.create(JavaArchive.class);
FileSystem vfs = ShrinkWrapFileSystems.newFileSystem(homeArchive);
Path path = vfs.getPath("out.json");
new GsonBuilder().create().toJson("Test", Files.newBufferedWriter(path, Charset.forName("UTF-8")));
assertEquals("Test", new GsonBuilder().create().fromJson(Files.newBufferedReader(path, Charset.forName("UTF-8")), String.class));
}
{code}
> SeekableInMemoryByteChannel returns incorrect value for EOF
> -----------------------------------------------------------
>
> Key: SHRINKWRAP-461
> URL: https://issues.jboss.org/browse/SHRINKWRAP-461
> Project: ShrinkWrap
> Issue Type: Bug
> Components: api
> Affects Versions: 1.1.2
> Environment: Window 7, Java 7
> Reporter: Resurgam Immortius
>
> The NIO2 API's SeekableInMemoryByteChannel currently returns 0 when a read requests data either at the end of the data or past the end of the data.
> The API for SeekableByteChannel ( http://docs.oracle.com/javase/7/docs/api/java/nio/channels/SeekableByteCh... ) states the return value should be -1 if EOF has been reached. This behavior is confirmed through testing of FileChannelImpl.
> Java's sun.nio.cs.StreamDecoder.readBytes() expects this behavior, throwing an IOException if zero bytes are read.
> I encountered this issue using GSON to write and then read a json file from a ShrinkWrap archive as part of a unit test. At the end of reading a json file, GSON checks that the EOF has been reached, and at this point a read returns 0 and causes an exception in sun.nio.cs.StreamDecoder.readBytes().
> A unit test showing the issue:
> {code:java}
> @Test
> public void test() throws IOException {
> JavaArchive homeArchive = ShrinkWrap.create(JavaArchive.class);
> FileSystem vfs = ShrinkWrapFileSystems.newFileSystem(homeArchive);
> Path path = vfs.getPath("out.json");
> try (BufferedWriter writer = Files.newBufferedWriter(path, Charset.forName("UTF-8"))) {
> new GsonBuilder().create().toJson("Test", writer);
> }
> try (BufferedReader reader = Files.newBufferedReader(path, Charset.forName("UTF-8"))) {
> assertEquals("Test", new GsonBuilder().create().fromJson(reader), String.class));
> }
> }
> {code}
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira