Example #1
0
 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);
 }
Example #2
0
  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);
    }
  }
Example #3
0
 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");
 }
Example #4
0
  @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);
    }
  }