Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package the;
- import static org.junit.Assert.assertEquals;
- import java.io.Closeable;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.Serializable;
- import java.io.StreamCorruptedException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.nio.ByteBuffer;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.junit.runners.Parameterized;
- import org.junit.runners.Parameterized.Parameters;
- import net.openhft.chronicle.bytes.Bytes;
- import net.openhft.chronicle.core.util.ObjectUtils;
- import net.openhft.chronicle.wire.Wire;
- import net.openhft.chronicle.wire.WireType;
- import net.openhft.chronicle.wire.Wires;
- @RunWith(Parameterized.class)
- public class WireToOutputStreamTest {
- private WireType currentWireType;
- public WireToOutputStreamTest(WireType currentWireType) {
- this.currentWireType = currentWireType;
- }
- @Parameters(name = "{index}: {0}")
- public static Collection<WireType> data() {
- List<WireType> wireTypes = new ArrayList<>();
- for (WireType wireType : WireType.values()) {
- if (wireType.isAvailable()) {
- wireTypes.add(wireType);
- }
- }
- return wireTypes;
- }
- @Test
- public void testVisSocket() throws IOException {
- ServerSocket ss = new ServerSocket(0);
- Socket s = new Socket("localhost", ss.getLocalPort());
- Socket s2 = ss.accept();
- WireToOutputStream wtos = new WireToOutputStream(currentWireType, s.getOutputStream());
- Wire wire = wtos.getWire();
- AnObject ao = new AnObject();
- ao.value = 12345;
- ao.text = "Hello";
- //ao.timestamp1 = new Timestamp(1234567890);
- // write the type is needed.
- wire.getValueOut().typeLiteral(AnObject.class);
- Wires.writeMarshallable(ao, wire);
- wtos.flush();
- InputStreamToWire istw = new InputStreamToWire(currentWireType, s2.getInputStream());
- Wire wire2 = istw.readOne();
- Class type = wire2.getValueIn().typeLiteral();
- Object ao2 = ObjectUtils.newInstance(type);
- Wires.readMarshallable(ao2, wire2, true);
- System.out.println(ao2);
- ss.close();
- s.close();
- s2.close();
- assertEquals(ao.toString(), ao2.toString());
- }
- public static class AnObject implements Serializable {
- long value;
- String text;
- // Timestamp timestamp1;
- // Timestamp timestamp=null;
- @Override
- public String toString() {
- return "AnObject{" + "value=" + value + ", text='" + text + '\'' + '}';
- }
- }
- public static class WireToOutputStream implements Closeable {
- private final Bytes<ByteBuffer> bytes = Bytes.elasticHeapByteBuffer(128);
- private final Wire wire;
- private final DataOutputStream dos;
- public WireToOutputStream(WireType wireType, OutputStream os) {
- wire = wireType.apply(bytes);
- dos = new DataOutputStream(os);
- }
- public Wire getWire() {
- wire.clear();
- return wire;
- }
- public void flush() throws IOException {
- int length = Math.toIntExact(bytes.readRemaining());
- dos.writeInt(length);
- dos.write(bytes.underlyingObject().array(), 0, length);
- }
- @Override
- public void close() throws IOException {
- try {
- dos.close();
- } finally {
- wire.clear();
- }
- }
- }
- public static class InputStreamToWire implements Closeable {
- private final Bytes<ByteBuffer> bytes = Bytes.elasticHeapByteBuffer(128);
- private final Wire wire;
- private final DataInputStream dis;
- public InputStreamToWire(WireType wireType, InputStream is) {
- wire = wireType.apply(bytes);
- dis = new DataInputStream(is);
- }
- public Wire readOne() throws IOException {
- wire.clear();
- int length = dis.readInt();
- if (length < 0) {
- throw new StreamCorruptedException();
- }
- bytes.ensureCapacity(length);
- byte[] array = bytes.underlyingObject().array();
- dis.readFully(array, 0, length);
- bytes.readPositionRemaining(0, length);
- return wire;
- }
- @Override
- public void close() throws IOException {
- try {
- dis.close();
- } finally {
- wire.clear();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment