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 ByteArrayDataOutputStream position(int pos) {this.pos=checkBounds(pos); return this;} public int position() {return pos;}
public void writeUTF(String str) { int strlen=str != null? str.length() : 0; if(strlen > 0) ensureCapacity(strlen *2 + 2); writeShort(-1); return; write(bytearr, 0, utflen+2);
public void writeBytes(String s) { int len=s != null? s.length() : 0; if(len > 0) ensureCapacity(len); for(int i = 0 ; i < len ; i++) write((byte)s.charAt(i)); }
public void writeChars(String s) { int len=s != null? s.length() : 0; if(len > 0) ensureCapacity(len *2); // 2 bytes per char for(int i = 0 ; i < len ; i++) { int v = s.charAt(i); writeChar(v); } }
public static Buffer exceptionToBuffer(Throwable t) throws Exception { ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(512, true); exceptionToStream(t, out); return out.getBuffer(); }
/** * Serializes/Streams an object into a byte buffer. * The object has to implement interface Serializable or Externalizable or Streamable. */ public static byte[] objectToByteBuffer(Object obj) throws Exception { if(obj == null) return TYPE_NULL_ARRAY; if(obj instanceof Streamable) { int expected_size=obj instanceof SizeStreamable? ((SizeStreamable)obj).serializedSize() : 512; final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size, true); out.write(TYPE_STREAMABLE); writeGenericStreamable((Streamable)obj,out); return Arrays.copyOf(out.buf,out.position()); } Byte type=PRIMITIVE_TYPES.get(obj.getClass()); if(type == null) { // will throw an exception if object is not serializable final ByteArrayDataOutputStream out_stream=new ByteArrayDataOutputStream(512, true); out_stream.write(TYPE_SERIALIZABLE); try(ObjectOutputStream out=new ObjectOutputStream(new OutputStreamAdapter(out_stream))) { out.writeObject(obj); out.flush(); return Arrays.copyOf(out_stream.buffer(), out_stream.position()); } } return marshalPrimitiveType(type, obj); }
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()); }
protected Object _down(Message msg) throws Exception { Address dest=msg.dest(); setSourceAddress(msg); // very important !! listToBuffer() will fail with a null src address !! int size=(int)msg.size(); ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(size + Global.INT_SIZE); out.writeInt(size); msg.writeTo(out); if(dest != null) // unicast sendTo(dest, out.buffer(), 0, out.position()); else { // multicast Collection<Address> dests=view != null? view.getMembers(): addr_table.keySet(); for(Address dst: dests) { try { sendTo(dst, out.buffer(), 0, out.position()); } catch(Throwable t) { log.error("failed sending multicast message to " + dst, t); } } } return null; }
protected Buffer writeMessage(final Message msg) throws Exception { dos.position(0); byte flags=0; dos.writeShort(Version.version); // write the version if(msg.getDest() == null) flags+=(byte)2; dos.writeByte(flags); msg.writeTo(dos); return new Buffer(dos.buffer(), 0, dos.position()); } }
protected void sendLocalAddress(Address local_addr) throws Exception { try { int addr_size=local_addr.serializedSize(); int expected_size=cookie.length + Global.SHORT_SIZE*2 + addr_size; ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size +2); out.write(cookie, 0, cookie.length); out.writeShort(Version.version); out.writeShort(addr_size); // address size local_addr.writeTo(out); ByteBuffer buf=out.getByteBuffer(); send(buf, false); updateLastAccessed(); } catch(Exception ex) { close(); throw ex; } }
ByteArrayDataOutputStream dos=new ByteArrayDataOutputStream((int)(msg.size())); byte flags=0; dos.writeShort(Version.version); // write the version if(msg.getDest() == null) flags+=(byte)2; dos.writeByte(flags); msg.writeTo(dos); Buffer buffer=dos.getBuffer();
protected void sendMessageList(final Address dest, final Address src, final List<Message> list) { try { Util.writeMessageList(dest, src, transport.cluster_name.chars(), list, output, dest == null, transport.getId()); transport.doSend(output.buffer(), 0, output.position(), dest); } catch(SocketException | SocketTimeoutException sock_ex) { log.debug(Util.getMessage("FailureSendingMsgBundle"), transport.localAddress(),sock_ex); } catch(Throwable e) { log.error(Util.getMessage("FailureSendingMsgBundle"), transport.localAddress(), e); } }
output.position(0); Util.writeMessageListHeader(dest, msg.src(), cluster_name, 1, output, dest == null); int size_pos=output.position() - Global.INT_SIZE; int num_msgs=marshalMessagesToSameDestination(dest, buf, i, write_index, max_bundle_size); sent_msgs+=num_msgs; if(num_msgs > 1) { int current_pos=output.position(); output.position(size_pos); output.writeInt(num_msgs); output.position(current_pos); transport.doSend(output.buffer(), 0, output.position(), dest); if(transport.statsEnabled()) transport.incrBatchesSent(num_msgs);
public static Buffer messageToByteBuffer(Message msg) throws Exception { ByteArrayDataOutputStream out=new ByteArrayDataOutputStream((int)msg.size()+1); out.writeBoolean(msg != null); if(msg != null) msg.writeTo(out); return out.getBuffer(); }
public static Buffer marshal(LazyRemovalCache<Address,IpAddress> addrs) { final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(512); try { int size=addrs != null? addrs.size() : 0; out.writeInt(size); if(size > 0) { for(Map.Entry<Address,LazyRemovalCache.Entry<IpAddress>> entry: addrs.entrySet()) { Address key=entry.getKey(); IpAddress val=entry.getValue().getVal(); Util.writeAddress(key, out); Util.writeStreamable(val, out); } } return out.getBuffer(); } catch(Exception ex) { return null; } }
protected static Buffer marshal(final View view, final Digest digest) { try { int expected_size=Global.SHORT_SIZE; if(view != null) expected_size+=view.serializedSize(); boolean write_addrs=writeAddresses(view, digest); if(digest != null) expected_size=(int)digest.serializedSize(write_addrs); final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size +10); out.writeShort(determineFlags(view, digest)); if(view != null) view.writeTo(out); if(digest != null) digest.writeTo(out, write_addrs); return out.getBuffer(); } catch(Exception ex) { return null; } }
public static Buffer objectToBuffer(Object obj) throws Exception { if(obj == null) return new Buffer(TYPE_NULL_ARRAY); if(obj instanceof Streamable) { int expected_size=obj instanceof SizeStreamable? ((SizeStreamable)obj).serializedSize() : 512; final ByteArrayDataOutputStream out=new ByteArrayDataOutputStream(expected_size, true); out.write(TYPE_STREAMABLE); writeGenericStreamable((Streamable)obj,out); return out.getBuffer(); } Byte type=PRIMITIVE_TYPES.get(obj.getClass()); if(type == null) { // will throw an exception if object is not serializable final ByteArrayDataOutputStream out_stream=new ByteArrayDataOutputStream(512, true); out_stream.write(TYPE_SERIALIZABLE); try(ObjectOutputStream out=new ObjectOutputStream(new OutputStreamAdapter(out_stream))) { out.writeObject(obj); out.flush(); return out_stream.getBuffer(); } } return new Buffer(marshalPrimitiveType(type, obj)); }
@Test public void testSeriazilabeToAndFromByteArray() throws IOException { Map<String, String> attributesMap = new HashMap<>(); attributesMap.put(REGION_NAME, "myRegion"); attributesMap.put(TABLE_NAME, "myTable"); attributesMap.put(PDX_NAME, "myPdx"); attributesMap.put(DATA_SOURCE_NAME, "myDatasource"); attributesMap.put(SYNCHRONOUS_NAME, "false"); attributesMap.put(ID_NAME, "myId"); DescribeMappingResult result = new DescribeMappingResult(attributesMap); DescribeMappingResult newResult = new DescribeMappingResult(); ByteArrayDataOutputStream output = new ByteArrayDataOutputStream(); ByteArrayDataInputStream input; result.toData(output); input = new ByteArrayDataInputStream(output.buffer()); newResult.fromData(input); Map<String, String> newAttributeMap = newResult.getAttributeMap(); assertThat(attributesMap).isEqualTo(newAttributeMap); }