Deserializer { public <T extends Serializable> T readObject() throws ClassNotFoundException { byte tcByte = _buffer[_index++]; if (tcByte == SerializationConstants.TC_BOOLEAN) { return (T)Boolean.valueOf(readBoolean()); } else if (tcByte == SerializationConstants.TC_BYTE) { return (T)Byte.valueOf(readByte()); } else if (tcByte == SerializationConstants.TC_CHARACTER) { return (T)Character.valueOf(readChar()); } else if (tcByte == SerializationConstants.TC_CLASS) { String contextName = readString(); String className = readString(); return (T)ClassResolverUtil.resolve( className, ClassLoaderPool.getClassLoader(contextName)); } else if (tcByte == SerializationConstants.TC_DOUBLE) { return (T)Double.valueOf(readDouble()); } else if (tcByte == SerializationConstants.TC_FLOAT) { return (T)Float.valueOf(readFloat()); } else if (tcByte == SerializationConstants.TC_INTEGER) { return (T)Integer.valueOf(readInt()); } else if (tcByte == SerializationConstants.TC_LONG) { return (T)Long.valueOf(readLong()); } else if (tcByte == SerializationConstants.TC_NULL) { return null; } else if (tcByte == SerializationConstants.TC_SHORT) { return (T)Short.valueOf(readShort()); } else if (tcByte == SerializationConstants.TC_STRING) { return (T)readString(); } else if (tcByte == SerializationConstants.TC_OBJECT) { try { ObjectInputStream objectInputStream = new ProtectedAnnotatedObjectInputStream( new BufferInputStream()); return (T)objectInputStream.readObject(); } catch (IOException ioException) { throw new RuntimeException(ioException); } } throw new IllegalStateException("Unkown TC code " + tcByte); } Deserializer(ByteBuffer byteBuffer); boolean readBoolean(); byte readByte(); char readChar(); double readDouble(); float readFloat(); int readInt(); long readLong(); T readObject(); short readShort(); String readString();  }
@Test public void testReadObjectBoolean() throws ClassNotFoundException { ByteBuffer byteBuffer = ByteBuffer.allocate(2); byteBuffer.put(SerializationConstants.TC_BOOLEAN); byteBuffer.put((byte)1); byteBuffer.flip(); Deserializer deserializer = new Deserializer(byteBuffer); Object object = deserializer.readObject(); Assert.assertTrue(object instanceof Boolean); Assert.assertSame(Boolean.TRUE, object); }
@Test public void testReadObjectByte() throws ClassNotFoundException { ByteBuffer byteBuffer = ByteBuffer.allocate(2); byteBuffer.put(SerializationConstants.TC_BYTE); byteBuffer.put((byte)101); byteBuffer.flip(); Deserializer deserializer = new Deserializer(byteBuffer); Object object = deserializer.readObject(); Assert.assertTrue(object instanceof Byte); Assert.assertSame(Byte.valueOf((byte)101), object); }
@Test public void testReadObjectCharacter() throws ClassNotFoundException { ByteBuffer byteBuffer = ByteBuffer.allocate(3); byteBuffer.put(SerializationConstants.TC_CHARACTER); byteBuffer.putChar('a'); byteBuffer.flip(); Deserializer deserializer = new Deserializer(byteBuffer); Object object = deserializer.readObject(); Assert.assertTrue(object instanceof Character); Assert.assertSame('a', object); }
@Test public void testReadObjectClassWithBlankContextName() throws Exception { Class<?> clazz = getClass(); String className = clazz.getName(); ByteBuffer byteBuffer = ByteBuffer.allocate(className.length() + 11); byteBuffer.put(SerializationConstants.TC_CLASS); byteBuffer.put((byte)1); byteBuffer.putInt(0); byteBuffer.put((byte)1); byteBuffer.putInt(className.length()); byteBuffer.put(className.getBytes(StringPool.UTF8)); byteBuffer.flip(); Deserializer deserializer = new Deserializer(byteBuffer); ClassLoaderPool.register(StringPool.BLANK, clazz.getClassLoader()); try { Assert.assertSame(clazz, deserializer.readObject()); } finally { ClassLoaderPool.unregister(clazz.getClassLoader()); } }
@Test public void testReadObjectClassWithNullContextName() throws Exception { Class<?> clazz = getClass(); String className = clazz.getName(); String contextName = StringPool.NULL; ByteBuffer byteBuffer = ByteBuffer.allocate( className.length() + contextName.length() + 11); byteBuffer.put(SerializationConstants.TC_CLASS); byteBuffer.put((byte)1); byteBuffer.putInt(contextName.length()); byteBuffer.put(contextName.getBytes(StringPool.UTF8)); byteBuffer.put((byte)1); byteBuffer.putInt(className.length()); byteBuffer.put(className.getBytes(StringPool.UTF8)); byteBuffer.flip(); Deserializer deserializer = new Deserializer(byteBuffer); Class<?> readClass = deserializer.readObject(); Assert.assertSame(clazz, readClass); }
@Test public void testReadObjectDouble() throws ClassNotFoundException { ByteBuffer byteBuffer = ByteBuffer.allocate(9); byteBuffer.put(SerializationConstants.TC_DOUBLE); byteBuffer.putDouble(17.58D); byteBuffer.flip(); Deserializer deserializer = new Deserializer(byteBuffer); Object object = deserializer.readObject(); Assert.assertTrue(object instanceof Double); Assert.assertEquals(17.58D, object); }
@Test public void testReadObjectFloat() throws ClassNotFoundException { ByteBuffer byteBuffer = ByteBuffer.allocate(5); byteBuffer.put(SerializationConstants.TC_FLOAT); byteBuffer.putFloat(17.58F); byteBuffer.flip(); Deserializer deserializer = new Deserializer(byteBuffer); Object object = deserializer.readObject(); Assert.assertTrue(object instanceof Float); Assert.assertEquals(17.58F, object); }
@Test public void testReadObjectInteger() throws ClassNotFoundException { ByteBuffer byteBuffer = ByteBuffer.allocate(5); byteBuffer.put(SerializationConstants.TC_INTEGER); byteBuffer.putInt(101); byteBuffer.flip(); Deserializer deserializer = new Deserializer(byteBuffer); Object object = deserializer.readObject(); Assert.assertTrue(object instanceof Integer); Assert.assertSame(101, object); }
@Test public void testReadObjectLong() throws ClassNotFoundException { ByteBuffer byteBuffer = ByteBuffer.allocate(9); byteBuffer.put(SerializationConstants.TC_LONG); byteBuffer.putLong(101); byteBuffer.flip(); Deserializer deserializer = new Deserializer(byteBuffer); Object object = deserializer.readObject(); Assert.assertTrue(object instanceof Long); Assert.assertSame(Long.valueOf(101), object); }
@Test public void testReadObjectNull() throws ClassNotFoundException { ByteBuffer byteBuffer = ByteBuffer.allocate(1); byteBuffer.put(SerializationConstants.TC_NULL); byteBuffer.flip(); Deserializer deserializer = new Deserializer(byteBuffer); Object object = deserializer.readObject(); Assert.assertNull(object); }
@Test public void testReadObjectOrdinary() throws Exception { UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream(); unsyncByteArrayOutputStream.write(SerializationConstants.TC_OBJECT); ObjectOutputStream objectOutputStream = new AnnotatedObjectOutputStream( unsyncByteArrayOutputStream); Date date = new Date(123456); objectOutputStream.writeObject(date); ByteBuffer byteBuffer = unsyncByteArrayOutputStream.unsafeGetByteBuffer(); Deserializer deserializer = new Deserializer(byteBuffer); Object object = deserializer.readObject(); Assert.assertTrue(object instanceof Date); Assert.assertEquals(date, object); }
@Test public void testReadObjectOrdinaryNPE() throws Exception { UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream(); unsyncByteArrayOutputStream.write(SerializationConstants.TC_OBJECT); unsyncByteArrayOutputStream.write(1); unsyncByteArrayOutputStream.write(new byte[4]); ObjectOutputStream objectOutputStream = new ObjectOutputStream( unsyncByteArrayOutputStream); Date date = new Date(123456); objectOutputStream.writeObject(date); ByteBuffer byteBuffer = unsyncByteArrayOutputStream.unsafeGetByteBuffer(); byteBuffer.put(6, (byte)0xFF); Deserializer deserializer = new Deserializer(byteBuffer); try { deserializer.readObject(); Assert.fail(); } catch (RuntimeException runtimeException) { Assert.assertTrue( runtimeException.getCause() instanceof StreamCorruptedException); } }
@Test public void testReadObjectShort() throws ClassNotFoundException { ByteBuffer byteBuffer = ByteBuffer.allocate(3); byteBuffer.put(SerializationConstants.TC_SHORT); byteBuffer.putShort((short)101); byteBuffer.flip(); Deserializer deserializer = new Deserializer(byteBuffer); Object object = deserializer.readObject(); Assert.assertTrue(object instanceof Short); Assert.assertSame((short)101, object); }
@Test public void testReadObjectString() throws ClassNotFoundException { String asciiString = "abcdefghijklmn"; byte[] buffer = new byte[asciiString.length() + 6]; buffer[0] = SerializationConstants.TC_STRING; buffer[1] = 1; BigEndianCodec.putInt(buffer, 2, asciiString.length()); for (int i = 0; i < asciiString.length(); i++) { buffer[6 + i] = (byte)asciiString.charAt(i); } ByteBuffer byteBuffer = ByteBuffer.wrap(buffer); Deserializer deserializer = new Deserializer(byteBuffer); Object object = deserializer.readObject(); Assert.assertTrue(object instanceof String); Assert.assertEquals(asciiString, object); String nonasciiString = "非ASCII Code中文测试"; buffer = new byte[(nonasciiString.length() * 2) + 6]; buffer[0] = SerializationConstants.TC_STRING; buffer[1] = 0; BigEndianCodec.putInt(buffer, 2, nonasciiString.length()); for (int i = 0; i < nonasciiString.length(); i++) { BigEndianCodec.putChar( buffer, 6 + (i * 2), nonasciiString.charAt(i)); } byteBuffer = ByteBuffer.wrap(buffer); deserializer = new Deserializer(byteBuffer); object = deserializer.readObject(); Assert.assertTrue(object instanceof String); Assert.assertEquals(nonasciiString, object); }
@Test public void testReadObjectUnknowTCCode() throws ClassNotFoundException { ByteBuffer byteBuffer = ByteBuffer.allocate(1); byteBuffer.put((byte)12); Deserializer deserializer = new Deserializer(byteBuffer); try { deserializer.readObject(); } catch (IllegalStateException illegalStateException) { Assert.assertEquals( "Unkown TC code 12", illegalStateException.getMessage()); } }