|
|
@@ -8,7 +8,6 @@ package net.ranides.assira.io;
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.DataInputStream;
|
|
|
-import java.io.DataOutput;
|
|
|
import java.io.DataOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
@@ -19,11 +18,11 @@ import java.util.function.BiConsumer;
|
|
|
import java.util.function.Function;
|
|
|
import net.ranides.assira.ContractTesters;
|
|
|
import net.ranides.assira.collection.arrays.ArrayUtils;
|
|
|
+import net.ranides.assira.test.TAdapter;
|
|
|
import net.ranides.assira.text.Charsets;
|
|
|
import static org.junit.Assert.*;
|
|
|
import org.junit.Test;
|
|
|
import net.ranides.assira.text.StringUtils;
|
|
|
-import net.ranides.assira.trace.ExceptionUtils;
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
@@ -31,32 +30,36 @@ import net.ranides.assira.trace.ExceptionUtils;
|
|
|
*/
|
|
|
public class DataAdapterTest {
|
|
|
|
|
|
- // @todo (assira #0) DataBuffer implements DataInput, DataOutput
|
|
|
+ // @todo (assira #3) DataBuffer implements DataInput, DataOutput
|
|
|
|
|
|
@Test
|
|
|
public void testWriter() {
|
|
|
-
|
|
|
+ TAdapter<Writer, ByteArrayOutputStream> map = new TAdapter<>();
|
|
|
+
|
|
|
Function<Writer, String> mr = (ostream) -> {
|
|
|
- DataOutput odata = ((DataAdapter.DOWriter)ostream).odata;
|
|
|
- byte[] data = ((ByteArrayDataOutput)odata).getBytes();
|
|
|
- return new String(data, Charsets.UTF16BE);
|
|
|
+ return new String(map.get(ostream).toByteArray(), Charsets.UTF16BE);
|
|
|
};
|
|
|
ContractTesters.runner()
|
|
|
.debug(true)
|
|
|
- .param("output!", mr)
|
|
|
- .run(() -> DataAdapter.asWriter(new ByteArrayDataOutput()));
|
|
|
+ .param("writer.chars!", mr)
|
|
|
+ .run(map.map(()->new ByteArrayOutputStream(), a -> DataAdapter.asWriter(new DataOutputStream(a))));
|
|
|
assertTrue(true);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testOStream() {
|
|
|
+ TAdapter<OutputStream, ByteArrayOutputStream> map = new TAdapter<>();
|
|
|
+
|
|
|
Function<OutputStream, byte[]> mr = (ostream) -> {
|
|
|
- return ((ByteArrayDataOutput)((DataAdapter.DOStream)ostream).odata).getBytes();
|
|
|
+ return map.get(ostream).toByteArray();
|
|
|
};
|
|
|
|
|
|
ContractTesters.runner()
|
|
|
- .param("output!", mr)
|
|
|
- .run(() -> DataAdapter.asOutput(new ByteArrayDataOutput()));
|
|
|
+ .param("ostream.bytes!", mr)
|
|
|
+ .run(map.map(
|
|
|
+ () -> new ByteArrayOutputStream(),
|
|
|
+ a -> DataAdapter.asOutput(new DataOutputStream(a))
|
|
|
+ ));
|
|
|
assertTrue(true);
|
|
|
|
|
|
}
|
|
|
@@ -64,7 +67,7 @@ public class DataAdapterTest {
|
|
|
@Test
|
|
|
public void testInput() throws IOException {
|
|
|
Function<int[], InputStream> mr = (array) -> {
|
|
|
- return DataAdapter.asInput(new ByteArrayDataInput().appendBytes(array));
|
|
|
+ return DataAdapter.asInput(new DataInputStream(new ByteArrayInput().append(array)));
|
|
|
};
|
|
|
|
|
|
ContractTesters.runner()
|
|
|
@@ -74,51 +77,47 @@ public class DataAdapterTest {
|
|
|
|
|
|
@Test
|
|
|
public void testAsReader() throws IOException {
|
|
|
- Function<int[], Reader> mr = (array) -> {
|
|
|
- return DataAdapter.asReader(new ByteArrayDataInput().appendU16(array));
|
|
|
- };
|
|
|
- BiConsumer<Reader, String> ar = (r, value) -> {
|
|
|
- DataAdapter.DIReader ri = (DataAdapter.DIReader)r;
|
|
|
- ByteArrayDataInput di = (ByteArrayDataInput)ri.idata;
|
|
|
+ TAdapter<Reader, ByteArrayInput> map = new TAdapter<>();
|
|
|
+
|
|
|
+ BiConsumer<Reader, String> ar = (reader, value) -> {
|
|
|
+ ByteArrayInput di = map.get(reader);
|
|
|
di.appendU16(value);
|
|
|
};
|
|
|
ContractTesters.runner()
|
|
|
.ignore("ReaderTester.mark")
|
|
|
.param("reader.appender!", ar)
|
|
|
- .run(mr);
|
|
|
+ .run(map.map(array -> new ByteArrayInput().appendU16(array),
|
|
|
+ (a) -> DataAdapter.asReader(new DataInputStream(a))
|
|
|
+ ));
|
|
|
assertTrue(true);
|
|
|
}
|
|
|
|
|
|
- private static final class ByteArrayDataInput extends DataInputStream {
|
|
|
-
|
|
|
- public ByteArrayDataInput() {
|
|
|
- super(new ByteArrayInput());
|
|
|
- }
|
|
|
+ private static final class ByteArrayInput extends InputStream {
|
|
|
+
|
|
|
+ byte[] buffer = new byte[0];
|
|
|
+ int position = 0;
|
|
|
|
|
|
- public ByteArrayDataInput appendU16(int[] value) {
|
|
|
- return appendU16(StringUtils.fromCodePoints(value));
|
|
|
- }
|
|
|
|
|
|
- public ByteArrayDataInput appendU16(String value) {
|
|
|
- return appendBytes(value.getBytes(Charsets.UTF16BE));
|
|
|
+ public ByteArrayInput appendU16(int[] array) {
|
|
|
+ return appendU16(StringUtils.fromCodePoints(array));
|
|
|
}
|
|
|
+
|
|
|
+ public ByteArrayInput appendU16(String value) {
|
|
|
+ return append(value.getBytes(Charsets.UTF16BE));
|
|
|
+ }
|
|
|
|
|
|
- public ByteArrayDataInput appendBytes(int[] value) {
|
|
|
- return appendBytes(ints2bytes(value));
|
|
|
+ public ByteArrayInput append(int[] array) {
|
|
|
+ byte[] bytes = new byte[array.length];
|
|
|
+ for(int i=0; i<bytes.length; i++) {
|
|
|
+ bytes[i] = (byte)array[i];
|
|
|
+ }
|
|
|
+ return append(bytes);
|
|
|
}
|
|
|
|
|
|
- public ByteArrayDataInput appendBytes(byte[] value) {
|
|
|
- ByteArrayInput si = (ByteArrayInput)in;
|
|
|
- si.buffer = ArrayUtils.concat(si.buffer, value);
|
|
|
+ public ByteArrayInput append(byte[] value) {
|
|
|
+ buffer = ArrayUtils.concat(buffer, value);
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private static final class ByteArrayInput extends InputStream {
|
|
|
-
|
|
|
- byte[] buffer = new byte[0];
|
|
|
- int position = 0;
|
|
|
|
|
|
@Override
|
|
|
public int read() throws IOException {
|
|
|
@@ -127,30 +126,4 @@ public class DataAdapterTest {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private static final class ByteArrayDataOutput extends DataOutputStream {
|
|
|
-
|
|
|
- public ByteArrayDataOutput() {
|
|
|
- super(new ByteArrayOutputStream());
|
|
|
- }
|
|
|
-
|
|
|
- public byte[] getBytes() {
|
|
|
- try {
|
|
|
- flush();
|
|
|
- return ((ByteArrayOutputStream)out).toByteArray();
|
|
|
- } catch(IOException cause) {
|
|
|
- throw ExceptionUtils.rethrow(cause);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private static byte[] ints2bytes(int[] array) {
|
|
|
- byte[] ba = new byte[array.length];
|
|
|
- for(int i=0; i<ba.length; i++) {
|
|
|
- ba[i] = (byte)array[i];
|
|
|
- }
|
|
|
- return ba;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
}
|