diff --git a/classlib/src/main/java/org/teavm/classlib/java/io/TReader.java b/classlib/src/main/java/org/teavm/classlib/java/io/TReader.java index c6dcaf0b4..73c600236 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/io/TReader.java +++ b/classlib/src/main/java/org/teavm/classlib/java/io/TReader.java @@ -18,8 +18,11 @@ package org.teavm.classlib.java.io; import java.io.IOException; import org.teavm.classlib.java.lang.TMath; import org.teavm.classlib.java.lang.TObject; +import org.teavm.classlib.java.lang.TReadable; +import org.teavm.classlib.java.nio.TCharBuffer; +import org.teavm.classlib.java.nio.TReadOnlyBufferException; -public abstract class TReader implements TCloseable { +public abstract class TReader implements TCloseable, TReadable { protected TObject lock; protected TReader() { @@ -41,6 +44,30 @@ public abstract class TReader implements TCloseable { public abstract int read(char[] cbuf, int off, int len) throws IOException; + @Override + public int read(TCharBuffer cb) throws IOException { + if (!cb.hasRemaining()) { + return 0; + } + if (cb.isReadOnly()) { + throw new TReadOnlyBufferException(); + } + int bytesRead; + if (cb.hasArray()) { + bytesRead = read(cb.array(), cb.position() + cb.arrayOffset(), cb.remaining()); + if (bytesRead > 0) { + cb.position(cb.position() + bytesRead); + } + } else { + var array = new char[cb.remaining()]; + bytesRead = read(array); + if (bytesRead > 0) { + cb.put(array, 0, bytesRead); + } + } + return bytesRead; + } + public long skip(long n) throws IOException { char[] buffer = new char[1024]; long skipped = 0; diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TReadable.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TReadable.java index d4ae68433..353beccb8 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TReadable.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TReadable.java @@ -15,9 +15,9 @@ */ package org.teavm.classlib.java.lang; -import org.teavm.classlib.java.io.TIOException; +import java.io.IOException; import org.teavm.classlib.java.nio.TCharBuffer; public interface TReadable { - int read(TCharBuffer cb) throws TIOException; + int read(TCharBuffer cb) throws IOException; } diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/TCharBuffer.java b/classlib/src/main/java/org/teavm/classlib/java/nio/TCharBuffer.java index 2f6a46a94..2dfd52a98 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/nio/TCharBuffer.java +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/TCharBuffer.java @@ -15,7 +15,8 @@ */ package org.teavm.classlib.java.nio; -import org.teavm.classlib.java.io.TIOException; +import java.io.IOException; +import java.util.Objects; import org.teavm.classlib.java.lang.TReadable; public abstract class TCharBuffer extends TBuffer implements Comparable, Appendable, @@ -46,13 +47,8 @@ public abstract class TCharBuffer extends TBuffer implements Comparable