Style Correction
Style Correction
This commit is contained in:
parent
e0ca514118
commit
98528e849e
@ -9,25 +9,23 @@ import java.io.Serializable;
|
|||||||
|
|
||||||
public class SerializationUtils {
|
public class SerializationUtils {
|
||||||
|
|
||||||
public static <T extends Serializable> byte[] serialize(T obj)
|
public static <T extends Serializable> byte[] serialize(T obj) throws IOException {
|
||||||
throws IOException {
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
oos.writeObject(obj);
|
||||||
oos.writeObject(obj);
|
oos.close();
|
||||||
oos.close();
|
return baos.toByteArray();
|
||||||
return baos.toByteArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends Serializable> T deserialize(byte[] b, Class<T> cl)
|
public static <T extends Serializable> T deserialize(byte[] b, Class<T> cl)
|
||||||
throws IOException, ClassNotFoundException {
|
throws IOException, ClassNotFoundException {
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream(b);
|
ByteArrayInputStream bais = new ByteArrayInputStream(b);
|
||||||
ObjectInputStream ois = new ObjectInputStream(bais);
|
ObjectInputStream ois = new ObjectInputStream(bais);
|
||||||
Object o = ois.readObject();
|
Object o = ois.readObject();
|
||||||
return cl.cast(o);
|
return cl.cast(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isSerializable(Class<?> it) {
|
public static boolean isSerializable(Class<?> it) {
|
||||||
return it.isPrimitive() || it.isInterface() || Serializable.class.isAssignableFrom(it);
|
return it.isPrimitive() || it.isInterface() || Serializable.class.isAssignableFrom(it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +68,8 @@ public class SerializationUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSerializingAndDeserializingUsingCustomSerializationUtils_ThenObjectIsTheSame() throws IOException, ClassNotFoundException {
|
public void whenSerializingAndDeserializingUsingCustomSerializationUtils_ThenObjectIsTheSame()
|
||||||
|
throws IOException, ClassNotFoundException {
|
||||||
Person p = new Person();
|
Person p = new Person();
|
||||||
p.setAge(20);
|
p.setAge(20);
|
||||||
p.setName("Joe");
|
p.setName("Joe");
|
||||||
@ -79,7 +80,7 @@ public class SerializationUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSerializingUsingCustomSerializationUtils_ThanOk(){
|
public void whenSerializingUsingCustomSerializationUtils_ThanOk() {
|
||||||
assertFalse(com.baeldung.util.SerializationUtils.isSerializable(Address.class));
|
assertFalse(com.baeldung.util.SerializationUtils.isSerializable(Address.class));
|
||||||
assertTrue(com.baeldung.util.SerializationUtils.isSerializable(Person.class));
|
assertTrue(com.baeldung.util.SerializationUtils.isSerializable(Person.class));
|
||||||
assertTrue(com.baeldung.util.SerializationUtils.isSerializable(Integer.class));
|
assertTrue(com.baeldung.util.SerializationUtils.isSerializable(Integer.class));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user