Created
January 18, 2019 17:58
-
-
Save Stephan972/c9f791c472bc10fdb87183eb254268c2 to your computer and use it in GitHub Desktop.
All wire types work except CSV and READ_ANY.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>my</groupId> | |
<artifactId>ChronicleWireTest</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<properties> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>net.openhft</groupId> | |
<artifactId>chronicle-wire</artifactId> | |
<version>2.17.7</version> | |
</dependency> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.2</version> | |
<configuration> | |
<source>${java.version}</source> | |
<target>${java.version}</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment