classlib: fix issue in InputStreamReader

This commit is contained in:
Alexey Andreev 2023-10-26 11:50:27 +02:00
parent 3ac0078e2a
commit c5768e07bc
2 changed files with 24 additions and 1 deletions

View File

@ -110,8 +110,11 @@ public class TInputStreamReader extends TReader {
if (!inBuffer.hasRemaining() && !fillReadBuffer()) { if (!inBuffer.hasRemaining() && !fillReadBuffer()) {
break; break;
} }
if (decoder.decode(inBuffer, outBuffer, streamEof).isOverflow()) { var result = decoder.decode(inBuffer, outBuffer, streamEof);
if (result.isOverflow()) {
break; break;
} else if (result.isUnderflow()) {
fillReadBuffer();
} }
} }
if (!inBuffer.hasRemaining() && streamEof && decoder.flush(outBuffer).isUnderflow()) { if (!inBuffer.hasRemaining() && streamEof && decoder.flush(outBuffer).isUnderflow()) {

View File

@ -17,8 +17,10 @@ package org.teavm.classlib.java.io;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.teavm.junit.TeaVMTestRunner; import org.teavm.junit.TeaVMTestRunner;
@ -78,4 +80,22 @@ public class InputStreamReaderTest {
assertEquals(str.charAt(i), chars[i]); assertEquals(str.charAt(i), chars[i]);
} }
} }
@Test
public void underflowWhileFillingBuffer() throws IOException {
var bos = new ByteArrayOutputStream();
bos.write(0x24);
for (var i = 0; i < 20000; ++i) {
bos.write(0xC2);
bos.write(0xA2);
}
var bis = new ByteArrayInputStream(bos.toByteArray());
var reader = new InputStreamReader(bis, StandardCharsets.UTF_8);
assertEquals(0x24, reader.read());
for (var i = 0; i < 20000; ++i) {
assertEquals(0xA2, reader.read());
}
assertEquals(-1, reader.read());
}
} }