Guest User

Untitled

a guest
Jan 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. package the;
  2.  
  3. import static org.junit.Assert.assertEquals;
  4.  
  5. import java.io.Closeable;
  6. import java.io.DataInputStream;
  7. import java.io.DataOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import java.io.Serializable;
  12. import java.io.StreamCorruptedException;
  13. import java.net.ServerSocket;
  14. import java.net.Socket;
  15. import java.nio.ByteBuffer;
  16. import java.util.ArrayList;
  17. import java.util.Collection;
  18. import java.util.List;
  19.  
  20. import org.junit.Test;
  21. import org.junit.runner.RunWith;
  22. import org.junit.runners.Parameterized;
  23. import org.junit.runners.Parameterized.Parameters;
  24.  
  25. import net.openhft.chronicle.bytes.Bytes;
  26. import net.openhft.chronicle.core.util.ObjectUtils;
  27. import net.openhft.chronicle.wire.Wire;
  28. import net.openhft.chronicle.wire.WireType;
  29. import net.openhft.chronicle.wire.Wires;
  30.  
  31. @RunWith(Parameterized.class)
  32. public class WireToOutputStreamTest {
  33.  
  34. private WireType currentWireType;
  35.  
  36. public WireToOutputStreamTest(WireType currentWireType) {
  37. this.currentWireType = currentWireType;
  38. }
  39.  
  40. @Parameters(name = "{index}: {0}")
  41. public static Collection<WireType> data() {
  42. List<WireType> wireTypes = new ArrayList<>();
  43.  
  44. for (WireType wireType : WireType.values()) {
  45. if (wireType.isAvailable()) {
  46. wireTypes.add(wireType);
  47. }
  48. }
  49.  
  50. return wireTypes;
  51. }
  52.  
  53. @Test
  54. public void testVisSocket() throws IOException {
  55. ServerSocket ss = new ServerSocket(0);
  56. Socket s = new Socket("localhost", ss.getLocalPort());
  57. Socket s2 = ss.accept();
  58. WireToOutputStream wtos = new WireToOutputStream(currentWireType, s.getOutputStream());
  59.  
  60. Wire wire = wtos.getWire();
  61. AnObject ao = new AnObject();
  62. ao.value = 12345;
  63. ao.text = "Hello";
  64. //ao.timestamp1 = new Timestamp(1234567890);
  65. // write the type is needed.
  66. wire.getValueOut().typeLiteral(AnObject.class);
  67. Wires.writeMarshallable(ao, wire);
  68. wtos.flush();
  69.  
  70. InputStreamToWire istw = new InputStreamToWire(currentWireType, s2.getInputStream());
  71. Wire wire2 = istw.readOne();
  72. Class type = wire2.getValueIn().typeLiteral();
  73. Object ao2 = ObjectUtils.newInstance(type);
  74. Wires.readMarshallable(ao2, wire2, true);
  75. System.out.println(ao2);
  76. ss.close();
  77. s.close();
  78. s2.close();
  79. assertEquals(ao.toString(), ao2.toString());
  80. }
  81.  
  82. public static class AnObject implements Serializable {
  83. long value;
  84. String text;
  85.  
  86. // Timestamp timestamp1;
  87. // Timestamp timestamp=null;
  88.  
  89. @Override
  90. public String toString() {
  91. return "AnObject{" + "value=" + value + ", text='" + text + '\'' + '}';
  92. }
  93. }
  94.  
  95. public static class WireToOutputStream implements Closeable {
  96. private final Bytes<ByteBuffer> bytes = Bytes.elasticHeapByteBuffer(128);
  97. private final Wire wire;
  98. private final DataOutputStream dos;
  99.  
  100. public WireToOutputStream(WireType wireType, OutputStream os) {
  101. wire = wireType.apply(bytes);
  102. dos = new DataOutputStream(os);
  103. }
  104.  
  105. public Wire getWire() {
  106. wire.clear();
  107. return wire;
  108. }
  109.  
  110. public void flush() throws IOException {
  111. int length = Math.toIntExact(bytes.readRemaining());
  112. dos.writeInt(length);
  113. dos.write(bytes.underlyingObject().array(), 0, length);
  114. }
  115.  
  116. @Override
  117. public void close() throws IOException {
  118. try {
  119. dos.close();
  120. } finally {
  121. wire.clear();
  122. }
  123. }
  124. }
  125.  
  126. public static class InputStreamToWire implements Closeable {
  127. private final Bytes<ByteBuffer> bytes = Bytes.elasticHeapByteBuffer(128);
  128. private final Wire wire;
  129. private final DataInputStream dis;
  130.  
  131. public InputStreamToWire(WireType wireType, InputStream is) {
  132. wire = wireType.apply(bytes);
  133. dis = new DataInputStream(is);
  134. }
  135.  
  136. public Wire readOne() throws IOException {
  137. wire.clear();
  138. int length = dis.readInt();
  139. if (length < 0) {
  140. throw new StreamCorruptedException();
  141. }
  142. bytes.ensureCapacity(length);
  143. byte[] array = bytes.underlyingObject().array();
  144. dis.readFully(array, 0, length);
  145. bytes.readPositionRemaining(0, length);
  146. return wire;
  147. }
  148.  
  149. @Override
  150. public void close() throws IOException {
  151. try {
  152. dis.close();
  153. } finally {
  154. wire.clear();
  155. }
  156. }
  157. }
  158. }
Add Comment
Please, Sign In to add comment