public static void testStringMarshalling() throws Exception { byte[] tmp = {'B', 'e', 'l', 'a'}; String str = new String(tmp); byte[] buf = Util.objectToByteBuffer(str); String str2 = (String) Util.objectFromByteBuffer(buf); assert str.equals(str2); tmp[1] = 'a'; str2 = (String) Util.objectFromByteBuffer(buf); assert str.equals(str2); }
/** * Uses custom serialization to create an object from the buffer of the message. Note that this is * dangerous when using your own classloader, e.g. inside of an application server ! Most likely, * JGroups will use the system classloader to deserialize the buffer into an object, whereas (for * example) a web application will want to use the webapp's classloader, resulting in a * ClassCastException. The recommended way is for the application to use their own serialization * and only pass byte[] buffer to JGroups. * * @return */ public final Object getObject() { try { return Util.objectFromByteBuffer(buf, offset, length); } catch (Exception ex) { throw new IllegalArgumentException(ex); } }
static void marshal(Object obj) throws Exception { byte[] buf = Util.objectToByteBuffer(obj); assert buf != null; assert buf.length > 0; Object obj2 = Util.objectFromByteBuffer(buf); System.out.println( "obj=" + obj + ", obj2=" + obj2 + " (type=" + obj.getClass().getName() + ", length=" + buf.length + " bytes)"); Assert.assertEquals(obj, obj2); if (obj instanceof Integer) { // test compressed ints and longs buf = new byte[10]; Bits.writeIntCompressed((int) obj, buf, 0); obj2 = Bits.readIntCompressed(buf, 0); assert obj.equals(obj2); } if (obj instanceof Long) { // test compressed ints and longs buf = new byte[10]; Bits.writeLongCompressed((long) obj, buf, 0); obj2 = Bits.readLongCompressed(buf, 0); assert obj.equals(obj2); } }
protected static void start(InetAddress dest, int port, String add_server, String remove_server) throws Throwable { try (Socket sock = new Socket(dest, port); DataInputStream in = new DataInputStream(sock.getInputStream()); DataOutputStream out = new DataOutputStream(sock.getOutputStream())) { CLIENT.RequestType type = add_server != null ? CLIENT.RequestType.add_server : CLIENT.RequestType.remove_server; out.writeByte((byte) type.ordinal()); byte[] buf = Util.stringToBytes(add_server != null ? add_server : remove_server); out.writeInt(buf.length); out.write(buf, 0, buf.length); type = CLIENT.RequestType.values()[in.readByte()]; if (type != CLIENT.RequestType.rsp) throw new IllegalStateException( String.format("expected type %s but got %s", CLIENT.RequestType.rsp, type)); int len = in.readInt(); if (len == 0) return; buf = new byte[len]; in.readFully(buf); Object response = Util.objectFromByteBuffer(buf); if (response instanceof Throwable) throw (Throwable) response; System.out.println("response = " + response); } catch (Exception ex) { ex.printStackTrace(); } }
private static void marshalString(int size) throws Exception { byte[] tmp = new byte[size]; String str = new String(tmp, 0, tmp.length); byte[] retval = Util.objectToByteBuffer(str); System.out.println("length=" + retval.length + " bytes"); String obj = (String) Util.objectFromByteBuffer(retval); System.out.println("read " + obj.length() + " string"); }
public void readFrom(DataInput in) throws Exception { type = Type.values()[in.readByte()]; // We can't use Util.readObject since it's size is limited to 2^15-1 try { short first = in.readShort(); if (first == -1) { object = Util.readGenericStreamable(in); } else { ByteBuffer bb = ByteBuffer.allocate(4); bb.putShort(first); bb.putShort(in.readShort()); int size = bb.getInt(0); byte[] bytes = new byte[size]; in.readFully(bytes, 0, size); object = Util.objectFromByteBuffer(bytes); } } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException("Exception encountered while serializing execution request", e); } request = in.readLong(); }
@SuppressWarnings("unchecked") public void testObjectToFromByteBuffer() throws Exception { byte[] buf; Address addr = Util.createRandomAddress(), addr2; List<String> list = new ArrayList<>(), list2; list.add("Bela"); list.add("Jeannette"); buf = Util.objectToByteBuffer(addr); addr2 = (Address) Util.objectFromByteBuffer(buf); System.out.println("addr=" + addr + ", addr2=" + addr2); Assert.assertEquals(addr, addr2); buf = Util.objectToByteBuffer(list); list2 = (List<String>) Util.objectFromByteBuffer(buf); System.out.println("list=" + list + ", list2=" + list2); Assert.assertEquals(list, list2); byte[] buffer = {'B', 'e', 'l', 'a', ' ', 'B', 'a', 'n'}; buf = Util.objectToByteBuffer(buffer); byte[] buffer2 = (byte[]) Util.objectFromByteBuffer(buf); assert buffer2 != null && buffer.length == buffer2.length; assert Arrays.equals(buffer, buffer2); Object obj = null; buf = Util.objectToByteBuffer(obj); assert buf != null; assert buf.length > 0; obj = Util.objectFromByteBuffer(buf); assert obj == null; Object[] values = { Boolean.TRUE, true, false, Boolean.FALSE, (byte) 22, new Byte("2"), '5', 3.14, 352.3f, 0, 100, 322649, Integer.MAX_VALUE, Integer.MIN_VALUE, 0L, 322649L, Long.MAX_VALUE - 50, Long.MAX_VALUE, Long.MIN_VALUE, (short) 22, Short.MAX_VALUE, Short.MIN_VALUE, "Bela Ban", new byte[] {'H', 'e', 'l', 'l', 'o'}, Util.generateArray(1024) }; for (int i = 0; i < values.length; i++) { Object value = values[i]; marshal(value); } }