@Test public void encode() throws Exception { Object bean = new TestBean("xiaoming", 20); byte[] data = new NettyObjectEncoder().encode(bean); Object obj = new NettyObjectDecoder().decode(data); assertTrue(obj instanceof TestBean); assertEquals(bean, obj); ByteBuffer buffer = new NettyObjectEncoder().encodeBuf(bean); obj = new NettyObjectDecoder().decode(buffer); assertTrue(obj instanceof TestBean); assertEquals(bean, obj); }
@Override public byte[] encode(Object object) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(LENGTH_PLACEHOLDER); try (ObjectOutputStream oos = createObjectOutputStream(bos)) { oos.writeObject(object); byte[] data = bos.toByteArray(); int length = data.length - LENGTH_PLACEHOLDER.length; data[0] = (byte) (length >>> 24); data[1] = (byte) (length >>> 16); data[2] = (byte) (length >>> 8); data[3] = (byte) length; return data; } }
NettyObjectEncoder extends ObjectEncoder { @Override public byte[] encode(Object object) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(LENGTH_PLACEHOLDER); try (ObjectOutputStream oos = createObjectOutputStream(bos)) { oos.writeObject(object); byte[] data = bos.toByteArray(); int length = data.length - LENGTH_PLACEHOLDER.length; data[0] = (byte) (length >>> 24); data[1] = (byte) (length >>> 16); data[2] = (byte) (length >>> 8); data[3] = (byte) length; return data; } } }
NettyObjectEncoder extends ObjectEncoder { @Override public byte[] encode(Object object) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(LENGTH_PLACEHOLDER); try (ObjectOutputStream oos = createObjectOutputStream(bos)) { oos.writeObject(object); byte[] data = bos.toByteArray(); int length = data.length - LENGTH_PLACEHOLDER.length; data[0] = (byte) (length >>> 24); data[1] = (byte) (length >>> 16); data[2] = (byte) (length >>> 8); data[3] = (byte) length; return data; } }  }
NettyObjectEncoder extends ObjectEncoder { @Override public byte[] encode(Object object) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(LENGTH_PLACEHOLDER); try (ObjectOutputStream oos = createObjectOutputStream(bos)) { oos.writeObject(object); byte[] data = bos.toByteArray(); int length = data.length - LENGTH_PLACEHOLDER.length; data[0] = (byte) (length >>> 24); data[1] = (byte) (length >>> 16); data[2] = (byte) (length >>> 8); data[3] = (byte) length; return data; } }  @Override byte[] encode(Object object); }
NettyObjectEncoder extends ObjectEncoder { @Override public byte[] encode(Object object) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(LENGTH_PLACEHOLDER); try (ObjectOutputStream oos = createObjectOutputStream(bos)) { oos.writeObject(object); byte[] data = bos.toByteArray(); int length = data.length - LENGTH_PLACEHOLDER.length; data[0] = (byte) (length >>> 24); data[1] = (byte) (length >>> 16); data[2] = (byte) (length >>> 8); data[3] = (byte) length; return data; } }  @Override byte[] encode(Object object);  }