View Javadoc
1   package com.github.davidmoten.rx2.internal.flowable.buffertofile;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.ByteArrayOutputStream;
5   import java.io.IOException;
6   import java.io.ObjectInputStream;
7   import java.io.ObjectOutputStream;
8   import java.io.Serializable;
9   
10  import com.github.davidmoten.rx2.buffertofile.Serializer;
11  
12  public final class SerializerJavaIO<T extends Serializable> implements Serializer<T> {
13  
14      @Override
15      public byte[] serialize(Serializable t) throws IOException {
16          ByteArrayOutputStream bos = new ByteArrayOutputStream();
17          ObjectOutputStream oos = new ObjectOutputStream(bos);
18          oos.writeObject(t);
19          bos.close();
20          return bos.toByteArray();
21      }
22  
23      @Override
24      public T deserialize(byte[] bytes) throws ClassNotFoundException, IOException {
25          ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
26          ObjectInputStream ois = null;
27          try {
28              ois = new ObjectInputStream(bis);
29              @SuppressWarnings("unchecked")
30              T t = (T) ois.readObject();
31              return t;
32          } finally {
33              if (ois != null) {
34                  ois.close();
35              }
36          }
37      }
38  
39  }