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)); }
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; }
/** * Streams all members (dest and src addresses, buffer and headers) to the output stream. * * @param out * @throws Exception */ public void writeTo(DataOutput out) throws Exception { byte leading = 0; if (dest_addr != null) leading = Util.setFlag(leading, DEST_SET); if (src_addr != null) leading = Util.setFlag(leading, SRC_SET); if (buf != null) leading = Util.setFlag(leading, BUF_SET); // 1. write the leading byte first out.write(leading); // 2. the flags (e.g. OOB, LOW_PRIO), skip the transient flags out.writeShort(flags); // 3. dest_addr if (dest_addr != null) Util.writeAddress(dest_addr, out); // 4. src_addr if (src_addr != null) Util.writeAddress(src_addr, out); // 5. buf if (buf != null) { out.writeInt(length); out.write(buf, offset, length); } // 6. headers int size = headers.size(); out.writeShort(size); final short[] ids = headers.getRawIDs(); final Header[] hdrs = headers.getRawHeaders(); for (int i = 0; i < ids.length; i++) { if (ids[i] > 0) { out.writeShort(ids[i]); writeHeader(hdrs[i], out); } } }
public void writeTo(DataOutput out) throws Exception { out.writeByte(type); boolean isMergeView = view != null && view instanceof MergeView; out.writeBoolean(isMergeView); Util.writeStreamable(view, out); Util.writeAddress(mbr, out); Util.writeAddresses(mbrs, out); Util.writeStreamable(join_rsp, out); Util.writeStreamable(my_digest, out); Util.writeStreamable(merge_id, out); out.writeBoolean(merge_rejected); out.writeBoolean(useFlushIfPresent); }
/** * Writes the message to the output stream, but excludes the dest and src addresses unless the src * address given as argument is different from the message's src address * * @param src * @param out * @throws Exception */ public void writeToNoAddrs(Address src, DataOutputStream out) throws Exception { byte leading = 0; boolean write_src_addr = src == null || src_addr != null && !src_addr.equals(src); if (write_src_addr) leading = Util.setFlag(leading, SRC_SET); if (buf != null) leading = Util.setFlag(leading, BUF_SET); // 1. write the leading byte first out.write(leading); // 2. the flags (e.g. OOB, LOW_PRIO) out.writeShort(flags); // 4. src_addr if (write_src_addr) Util.writeAddress(src_addr, out); // 5. buf if (buf != null) { out.writeInt(length); out.write(buf, offset, length); } // 6. headers int size = headers.size(); out.writeShort(size); final short[] ids = headers.getRawIDs(); final Header[] hdrs = headers.getRawHeaders(); for (int i = 0; i < ids.length; i++) { if (ids[i] > 0) { out.writeShort(ids[i]); writeHeader(hdrs[i], out); } } }