mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
classlib: make Reader class implement Readable interface
This commit is contained in:
parent
b9f5e9be1c
commit
cd38447057
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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<TCharBuffer>, Appendable,
|
||||
|
@ -46,13 +47,8 @@ public abstract class TCharBuffer extends TBuffer implements Comparable<TCharBuf
|
|||
}
|
||||
|
||||
@Override
|
||||
public int read(TCharBuffer target) throws TIOException {
|
||||
if (target == null) {
|
||||
throw new NullPointerException("Target is null");
|
||||
}
|
||||
if (target.isReadOnly()) {
|
||||
throw new TReadOnlyBufferException();
|
||||
}
|
||||
public int read(TCharBuffer target) throws IOException {
|
||||
Objects.requireNonNull(target);
|
||||
if (!hasRemaining()) {
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,8 @@ import static org.junit.Assert.assertTrue;
|
|||
import static org.junit.Assert.fail;
|
||||
import java.io.CharArrayReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.ReadOnlyBufferException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.teavm.junit.TeaVMTestRunner;
|
||||
|
@ -149,4 +151,35 @@ public class CharArrayReaderTest {
|
|||
assertEquals("Failed to skip correct number of chars", 5L, skipped);
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user