@Test(expected = EOFException.class) public void deserializeEmptyData() throws ClassNotFoundException, ClassCastException, IOException { ObjectSerialization.deserialize(new byte[0], Integer.class); }
@SuppressWarnings("unchecked") public static <T> T deserialize(final byte[] data, final Class<T> clazz) throws IOException, ClassNotFoundException, ClassCastException { requireNonNull(data); requireNonNull(clazz); try(ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data))) { final Object object = ois.readObject(); if(!object.getClass().equals(clazz)) { throw new ClassCastException("Object was of type " + object.getClass().getName() + ", but the expected type was " + clazz.getName() + "."); } return (T) object; } }
ObjectSerialization { @SuppressWarnings("unchecked") public static <T> T deserialize(final byte[] data, final Class<T> clazz) throws IOException, ClassNotFoundException, ClassCastException { requireNonNull(data); requireNonNull(clazz); try(ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data))) { final Object object = ois.readObject(); if(!object.getClass().equals(clazz)) { throw new ClassCastException("Object was of type " + object.getClass().getName() + ", but the expected type was " + clazz.getName() + "."); } return (T) object; } } }
ObjectSerialization { @SuppressWarnings("unchecked") public static <T> T deserialize(final byte[] data, final Class<T> clazz) throws IOException, ClassNotFoundException, ClassCastException { requireNonNull(data); requireNonNull(clazz); try(ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data))) { final Object object = ois.readObject(); if(!object.getClass().equals(clazz)) { throw new ClassCastException("Object was of type " + object.getClass().getName() + ", but the expected type was " + clazz.getName() + "."); } return (T) object; } }  }
ObjectSerialization { @SuppressWarnings("unchecked") public static <T> T deserialize(final byte[] data, final Class<T> clazz) throws IOException, ClassNotFoundException, ClassCastException { requireNonNull(data); requireNonNull(clazz); try(ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data))) { final Object object = ois.readObject(); if(!object.getClass().equals(clazz)) { throw new ClassCastException("Object was of type " + object.getClass().getName() + ", but the expected type was " + clazz.getName() + "."); } return (T) object; } }  static byte[] serialize(final T data); @SuppressWarnings("unchecked") static T deserialize(final byte[] data, final Class<T> clazz); }
ObjectSerialization { @SuppressWarnings("unchecked") public static <T> T deserialize(final byte[] data, final Class<T> clazz) throws IOException, ClassNotFoundException, ClassCastException { requireNonNull(data); requireNonNull(clazz); try(ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data))) { final Object object = ois.readObject(); if(!object.getClass().equals(clazz)) { throw new ClassCastException("Object was of type " + object.getClass().getName() + ", but the expected type was " + clazz.getName() + "."); } return (T) object; } }  static byte[] serialize(final T data); @SuppressWarnings("unchecked") static T deserialize(final byte[] data, final Class<T> clazz);  }