[core] Fix bug in IOUtil.fromReader

This commit is contained in:
Andreas Dangel
2022-05-13 17:13:16 +02:00
parent 8addd05369
commit a9219b7967
2 changed files with 16 additions and 1 deletions

View File

@ -325,7 +325,7 @@ public final class IOUtil {
charBuffer.flip();
encoder.encode(charBuffer, byteBuffer, eof);
byteBuffer.flip();
charBuffer.flip();
charBuffer.compact();
}
if (byteBuffer.hasRemaining()) {

View File

@ -193,6 +193,21 @@ public class IOUtilTest {
}
}
@Test
public void testInputStreamFromReader2() throws IOException {
int size = 8192 + 8192 + 10;
char[] data = new char[size];
for (int i = 0; i < size; i++) {
data[i] = 'A';
}
data[8192] = 'ä'; // block size border - in UTF-8 these are two bytes. Decoding needs to take the bytes
// from previous block and new block
try (InputStream inputStream = IOUtil.fromReader(new StringReader(new String(data)))) {
byte[] bytes = IOUtil.toByteArray(inputStream);
Assert.assertEquals(new String(data), new String(bytes, StandardCharsets.UTF_8));
}
}
@Test
public void testCopyStream() throws IOException {
int size = 8192 + 8192 + 10;