public static <T extends Streamable> T streamableFromByteBuffer(Class<? extends Streamable> cl,byte[] buffer,int offset,int length) throws Exception { if(buffer == null) return null; DataInput in=new ByteArrayDataInputStream(buffer,offset,length); T retval=(T)cl.newInstance(); retval.readFrom(in); return retval; }
public ByteArrayDataInputStream position(int pos) { this.pos=checkBounds(pos); return this; }
protected static Message readMessage(byte[] buf, int offset, int length) throws Exception { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset, length); short ver=in.readShort(); byte flags=in.readByte(); // final boolean multicast=(flags & (byte)2) == (byte)2; Message msg=new Message(false); // don't create headers, readFrom() will do this msg.readFrom(in); return msg; }
public String readUTF() throws IOException { int utflen=readUnsignedShort(); byte[] bytearr=new byte[utflen]; char[] chararr=new char[utflen]; int chararr_count=0; readFully(bytearr,0,utflen);
/** Reads the message's contents from an input stream, but skips the buffer and instead returns the * position (offset) at which the buffer starts */ public int readFromSkipPayload(ByteArrayDataInputStream in) throws Exception { // 1. read the leading byte first byte leading=in.readByte(); // 2. the flags flags=in.readShort(); // 3. dest_addr if(Util.isFlagSet(leading, DEST_SET)) dest_addr=Util.readAddress(in); // 4. src_addr if(Util.isFlagSet(leading, SRC_SET)) src_addr=Util.readAddress(in); // 5. headers int len=in.readShort(); headers=createHeaders(len); for(int i=0; i < len; i++) { short id=in.readShort(); Header hdr=readHeader(in).setProtId(id); this.headers[i]=hdr; } // 6. buf if(!Util.isFlagSet(leading, BUF_SET)) return -1; length=in.readInt(); return in.position(); }
@Override public void receive(Address sender, byte[] buf, int offset, int length) { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset, length); GossipType type; try { type=GossipType.values()[in.readByte()]; in.position(Global.BYTE_SIZE); ByteArrayDataInputStream input=new ByteArrayDataInputStream(buf, offset, length); GossipData data=new GossipData(); data.readFrom(input);
public double readDouble() throws IOException { return Double.longBitsToDouble(readLong()); }
public float readFloat() throws IOException { return Float.intBitsToFloat(readInt()); }
public void readFully(byte[] b) throws IOException { readFully(b, 0, b.length); }
/** Reads the message's contents from an input stream, but skips the buffer and instead returns the * position (offset) at which the buffer starts */ public int readFromSkipPayload(ByteArrayDataInputStream in) throws Exception { // 1. read the leading byte first byte leading=in.readByte(); // 2. the flags flags=in.readShort(); // 3. dest_addr if(Util.isFlagSet(leading, DEST_SET)) dest_addr=Util.readAddress(in); // 4. src_addr if(Util.isFlagSet(leading, SRC_SET)) src_addr=Util.readAddress(in); // 5. headers int len=in.readShort(); headers=createHeaders(len); for(int i=0; i < len; i++) { short id=in.readShort(); Header hdr=readHeader(in).setProtId(id); this.headers[i]=hdr; } // 6. buf if(!Util.isFlagSet(leading, BUF_SET)) return -1; length=in.readInt(); return in.position(); }
protected static Message readMessage(byte[] buf, int offset, int length) throws Exception { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset, length); short ver=in.readShort(); byte flags=in.readByte(); // final boolean multicast=(flags & (byte)2) == (byte)2; Message msg=new Message(false); // don't create headers, readFrom() will do this msg.readFrom(in); return msg; }
@Override public void receive(Address sender, byte[] buf, int offset, int length) { ByteArrayDataInputStream in=new ByteArrayDataInputStream(buf, offset, length); GossipType type; try { type=GossipType.values()[in.readByte()]; in.position(Global.BYTE_SIZE); ByteArrayDataInputStream input=new ByteArrayDataInputStream(buf, offset, length); GossipData data=new GossipData(); data.readFrom(input);
public int read() throws IOException { return input.read(); }
public String readUTF() throws IOException { int utflen=readUnsignedShort(); byte[] bytearr=new byte[utflen]; char[] chararr=new char[utflen]; int chararr_count=0; readFully(bytearr,0,utflen);
public double readDouble() throws IOException { return Double.longBitsToDouble(readLong()); }
public float readFloat() throws IOException { return Float.intBitsToFloat(readInt()); }
public void readFully(byte[] b) throws IOException { readFully(b, 0, b.length); }
public static <T extends Streamable> T streamableFromByteBuffer(Supplier<T> factory, byte[] buffer, int offset, int length) throws Exception { if(buffer == null) return null; DataInput in=new ByteArrayDataInputStream(buffer,offset,length); T retval=factory.get(); retval.readFrom(in); return retval; }