public static <T extends Streamable> T readStreamable(Supplier<T> factory, DataInput in) throws Exception { T retval=null; if(!in.readBoolean()) return null; retval=factory.get(); retval.readFrom(in); return retval; }
public static void writeStreamable(Streamable obj,DataOutput out) throws Exception { if(obj == null) { out.writeBoolean(false); return; } out.writeBoolean(true); obj.writeTo(out); }
@Deprecated public static <T extends Streamable> T readStreamable(Class<T> clazz,DataInput in) throws Exception { T retval=null; if(!in.readBoolean()) return null; retval=clazz.newInstance(); retval.readFrom(in); return retval; }
public static <T extends Streamable> void write(T[] array, DataOutput out) throws Exception { Bits.writeInt(array != null? array.length : 0, out); if(array == null) return; for(T el: array) el.writeTo(out); }
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 static void writeGenericStreamable(Streamable obj, DataOutput out) throws Exception { short magic_number; String classname; if(obj == null) { out.write(0); return; } out.write(1); magic_number=ClassConfigurator.getMagicNumber(obj.getClass()); out.writeShort(magic_number); if(magic_number == -1) { classname=obj.getClass().getName(); out.writeUTF(classname); } obj.writeTo(out); // write the contents }
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; }
public static byte[] streamableToByteBuffer(Streamable obj) throws Exception { int expected_size=obj instanceof SizeStreamable? ((SizeStreamable)obj).serializedSize() : 512; final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size); obj.writeTo(out); return Arrays.copyOf(out.buffer(), out.position()); }
public static <T extends Streamable> T[] read(Class<T> clazz, DataInput in) throws Exception { int size=Bits.readInt(in); if(size == 0) return null; T[] retval=(T[])Array.newInstance(clazz, size); for(int i=0; i < retval.length; i++) { retval[i]=clazz.newInstance(); retval[i].readFrom(in); } return retval; }
protected static Buffer streamableToBuffer(byte req_or_rsp, byte type, Streamable obj) throws Exception { int expected_size=obj instanceof SizeStreamable? ((SizeStreamable)obj).serializedSize() : 100; ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size); out.writeByte(req_or_rsp); out.writeByte(type); obj.writeTo(out); return new Buffer(out.buffer(), 0, out.position()); }
public static <T extends Streamable> T readGenericStreamable(DataInput in, ClassLoader loader) throws Exception { T retval=null; int b=in.readByte(); if(b == 0) return null; short magic_number=in.readShort(); Class<?> clazz; if(magic_number != -1) { retval=ClassConfigurator.create(magic_number); } else { String classname=in.readUTF(); clazz=ClassConfigurator.get(classname, loader); retval=(T)clazz.newInstance(); } retval.readFrom(in); return retval; }
public static void writeStreamable(Streamable obj,DataOutput out) throws Exception { if(obj == null) { out.writeBoolean(false); return; } out.writeBoolean(true); obj.writeTo(out); }
public static <T extends Streamable> T readStreamable(Supplier<T> factory, DataInput in) throws Exception { T retval=null; if(!in.readBoolean()) return null; retval=factory.get(); retval.readFrom(in); return retval; }
public static void writeStreamable(Streamable obj, DataOutputStream out) throws IOException { if(obj == null) { out.writeBoolean(false); return; } out.writeBoolean(true); obj.writeTo(out); }
public static Streamable readStreamable(Class clazz, DataInputStream in) throws IOException, IllegalAccessException, InstantiationException { Streamable retval=null; if(in.readBoolean() == false) return null; retval=(Streamable)clazz.newInstance(); retval.readFrom(in); return retval; }
public static byte[] streamableToByteBuffer(Streamable obj) throws Exception { byte[] result=null; synchronized(out_stream) { out_stream.reset(); DataOutputStream out=new DataOutputStream(out_stream); obj.writeTo(out); result=out_stream.toByteArray(); out.close(); } return result; }
@Deprecated public static <T extends Streamable> T readStreamable(Class<T> clazz,DataInput in) throws Exception { T retval=null; if(!in.readBoolean()) return null; retval=clazz.newInstance(); retval.readFrom(in); return retval; }
public static int sizeOf(Streamable inst) { byte[] data; ByteArrayOutputStream output; DataOutputStream out; try { output=new ByteArrayOutputStream(); out=new DataOutputStream(output); inst.writeTo(out); out.flush(); data=output.toByteArray(); return data.length; } catch(Exception ex) { return -1; } }
public static Streamable streamableFromByteBuffer(Class cl, byte[] buffer) throws Exception { if(buffer == null) return null; Streamable retval=null; ByteArrayInputStream in_stream=new ByteArrayInputStream(buffer); DataInputStream in=new DataInputStream(in_stream); // changed Nov 29 2004 (bela) retval=(Streamable)cl.newInstance(); retval.readFrom(in); in.close(); return retval; }
public static <T extends Streamable> void write(T[] array, DataOutput out) throws Exception { Bits.writeInt(array != null? array.length : 0, out); if(array == null) return; for(T el: array) el.writeTo(out); }