classlib: make Reader class implement Readable interface

This commit is contained in:
Alexey Andreev 2023-08-10 19:24:30 +02:00
parent b9f5e9be1c
commit cd38447057
4 changed files with 67 additions and 11 deletions

View File

@ -18,8 +18,11 @@ package org.teavm.classlib.java.io;
import java.io.IOException; import java.io.IOException;
import org.teavm.classlib.java.lang.TMath; import org.teavm.classlib.java.lang.TMath;
import org.teavm.classlib.java.lang.TObject; 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 TObject lock;
protected TReader() { protected TReader() {
@ -41,6 +44,30 @@ public abstract class TReader implements TCloseable {
public abstract int read(char[] cbuf, int off, int len) throws IOException; 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 { public long skip(long n) throws IOException {
char[] buffer = new char[1024]; char[] buffer = new char[1024];
long skipped = 0; long skipped = 0;

View File

@ -15,9 +15,9 @@
*/ */
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import org.teavm.classlib.java.io.TIOException; import java.io.IOException;
import org.teavm.classlib.java.nio.TCharBuffer; import org.teavm.classlib.java.nio.TCharBuffer;
public interface TReadable { public interface TReadable {
int read(TCharBuffer cb) throws TIOException; int read(TCharBuffer cb) throws IOException;
} }

View File

@ -15,7 +15,8 @@
*/ */
package org.teavm.classlib.java.nio; 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; import org.teavm.classlib.java.lang.TReadable;
public abstract class TCharBuffer extends TBuffer implements Comparable<TCharBuffer>, Appendable, public abstract class TCharBuffer extends TBuffer implements Comparable<TCharBuffer>, Appendable,
@ -46,13 +47,8 @@ public abstract class TCharBuffer extends TBuffer implements Comparable<TCharBuf
} }
@Override @Override
public int read(TCharBuffer target) throws TIOException { public int read(TCharBuffer target) throws IOException {
if (target == null) { Objects.requireNonNull(target);
throw new NullPointerException("Target is null");
}
if (target.isReadOnly()) {
throw new TReadOnlyBufferException();
}
if (!hasRemaining()) { if (!hasRemaining()) {
return -1; return -1;
} }

View File

@ -21,6 +21,8 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.CharArrayReader; import java.io.CharArrayReader;
import java.io.IOException; import java.io.IOException;
import java.nio.CharBuffer;
import java.nio.ReadOnlyBufferException;
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;
@ -149,4 +151,35 @@ public class CharArrayReaderTest {
assertEquals("Failed to skip correct number of chars", 5L, skipped); assertEquals("Failed to skip correct number of chars", 5L, skipped);
assertEquals("Skip skipped wrong chars", 'W', cr.read()); assertEquals("Skip skipped wrong chars", 'W', cr.read());
} }
@Test
public void readIntoBuffer() throws IOException {
cr = new CharArrayReader(hw);
var buffer = CharBuffer.allocate(100);
assertEquals(10, cr.read(buffer));
buffer.flip();
assertEquals("HelloWorld", buffer.toString());
buffer.limit(20);
assertEquals(0, buffer.get(10));
cr = new CharArrayReader(hw);
var array = new char[100];
buffer = CharBuffer.wrap(array, 10, 80);
assertEquals(10, cr.read(buffer));
assertEquals("HelloWorld", new String(array, 10, 10));
cr = new CharArrayReader(hw);
buffer = CharBuffer.allocate(5);
assertEquals(5, cr.read(buffer));
buffer.flip();
assertEquals("Hello", buffer.toString());
cr = new CharArrayReader(hw);
try {
cr.read(CharBuffer.allocate(10).asReadOnlyBuffer());
fail("Expected exception not thrown");
} catch (ReadOnlyBufferException e) {
// ok
}
}
} }