public static void testWriteView() throws Exception { List<Address> members = new ArrayList<>(); View v; Address a1 = Util.createRandomAddress(); Address a2 = Util.createRandomAddress(); Address a4 = Util.createRandomAddress(); ViewId vid = new ViewId(a1, 12345); members.add(a1); members.add(a2); members.add(a4); v = new View(vid, members); ByteArrayOutputStream outstream = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(outstream); Util.writeGenericStreamable(v, dos); Util.writeStreamable(v, dos); dos.close(); byte[] buf = outstream.toByteArray(); ByteArrayInputStream instream = new ByteArrayInputStream(buf); DataInputStream dis = new DataInputStream(instream); View v2 = (View) Util.readGenericStreamable(dis); Assert.assertEquals(v, v2); v2 = (View) Util.readStreamable(View.class, dis); Assert.assertEquals(v, v2); }
public static void testWriteNullAddress() throws Exception { Address a1 = null; ByteArrayOutputStream outstream = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(outstream); Util.writeAddress(a1, dos); dos.close(); byte[] buf = outstream.toByteArray(); ByteArrayInputStream instream = new ByteArrayInputStream(buf); DataInputStream dis = new DataInputStream(instream); assert Util.readAddress(dis) == null; }
public static void testWriteByteBuffer() throws Exception { byte[] buf = new byte[1024], tmp; for (int i = 0; i < buf.length; i++) buf[i] = 0; ByteArrayOutputStream outstream = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(outstream); Util.writeByteBuffer(buf, dos); dos.close(); tmp = outstream.toByteArray(); ByteArrayInputStream instream = new ByteArrayInputStream(tmp); DataInputStream dis = new DataInputStream(instream); byte[] buf2 = Util.readByteBuffer(dis); assert buf2 != null; Assert.assertEquals(buf.length, buf2.length); }
public static void testWriteString() throws Exception { String s1 = "Bela Ban", s2 = "Michelle Ban"; ByteArrayOutputStream outstream = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(outstream); Bits.writeString(s1, dos); Bits.writeString(s2, dos); dos.close(); byte[] buf = outstream.toByteArray(); ByteArrayInputStream instream = new ByteArrayInputStream(buf); DataInputStream dis = new DataInputStream(instream); String s3 = Bits.readString(dis); String s4 = Bits.readString(dis); Assert.assertEquals(s1, s3); Assert.assertEquals(s2, s4); }
public static void testWriteStreamable() throws Exception { Message m = new Message(null, null, "Hello"); ViewId vid2 = new ViewId(Util.createRandomAddress(), 35623); ByteArrayOutputStream outstream = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(outstream); Util.writeGenericStreamable(m, dos); Util.writeGenericStreamable(vid2, dos); dos.close(); byte[] buf = outstream.toByteArray(); ByteArrayInputStream instream = new ByteArrayInputStream(buf); DataInputStream dis = new DataInputStream(instream); Message m2 = (Message) Util.readGenericStreamable(dis); ViewId v3 = (ViewId) Util.readGenericStreamable(dis); assert m2.getBuffer() != null; Assert.assertEquals(m.getLength(), m2.getLength()); assert v3 != null; }
protected void getStateFrom( JChannel channel, Protocol prot, String stack, String ch, DataOutputStream out) throws Exception { ByteArrayDataOutputStream output = new ByteArrayDataOutputStream(1024); OutputStreamAdapter out_ad = new OutputStreamAdapter(output); Event evt = new Event(Event.STATE_TRANSFER_OUTPUTSTREAM, out_ad); if (channel != null) channel.up(evt); else prot.up(evt); int len = output.position(); if (len > 0) { Bits.writeString(stack, out); Bits.writeString(ch, out); out.writeInt(len); out.write(output.buffer(), 0, len); log.trace("%s: fetched %d bytes from %s:%s", local_addr, len, stack, ch); } }
/** * \ Draw saved State (color dots on panel) on WhiteBoard * * @param outstream * @throws IOException */ public void writeState(OutputStream outstream) throws IOException { if (state == null) return; synchronized (state) { DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(outstream)); // DataOutputStream dos=new DataOutputStream(outstream); dos.writeInt(state.size()); for (Map.Entry<Point, Color> entry : state.entrySet()) { Point point = entry.getKey(); Color col = entry.getValue(); dos.writeInt(point.x); dos.writeInt(point.x); dos.writeInt(col.getRGB()); } dos.flush(); System.out.println("wrote " + state.size() + " elements"); } }
public static void testWriteAddress() throws Exception { Address a1 = Util.createRandomAddress(); Address a2 = Util.createRandomAddress(); Address a4 = Util.createRandomAddress(); ByteArrayOutputStream outstream = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(outstream); Util.writeAddress(a1, dos); Util.writeAddress(a2, dos); Util.writeAddress(a4, dos); dos.close(); byte[] buf = outstream.toByteArray(); ByteArrayInputStream instream = new ByteArrayInputStream(buf); DataInputStream dis = new DataInputStream(instream); Assert.assertEquals(a1, Util.readAddress(dis)); Assert.assertEquals(a2, Util.readAddress(dis)); Assert.assertEquals(a4, Util.readAddress(dis)); }