View Javadoc
1   package com.github.davidmoten.rx.buffertofile;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.ByteArrayOutputStream;
5   import java.io.DataInput;
6   import java.io.DataOutput;
7   import java.io.IOException;
8   import java.io.ObjectInputStream;
9   import java.io.ObjectOutputStream;
10  
11  public final class DataSerializers {
12  
13      private DataSerializers() {
14          // prevent instantiation
15      }
16  
17      public static DataSerializer<String> string() {
18          return StringHolder.INSTANCE;
19      }
20  
21      private static final class StringHolder {
22          final static DataSerializer<String> INSTANCE = new DataSerializer<String>() {
23  
24              @Override
25              public void serialize(DataOutput output, String t) throws IOException {
26                  output.writeUTF(t);
27              }
28  
29              @Override
30              public String deserialize(DataInput input) throws IOException {
31                  return input.readUTF();
32              }
33  
34              @Override
35              public int size() {
36                  return 0;
37              }
38          };
39      }
40  
41      public static DataSerializer<Integer> integer() {
42          return IntegerHolder.INSTANCE;
43      }
44  
45      private static final class IntegerHolder {
46          final static DataSerializer<Integer> INSTANCE = new DataSerializer<Integer>() {
47  
48              @Override
49              public void serialize(DataOutput output, Integer t) throws IOException {
50                  output.writeInt(t);
51              }
52  
53              @Override
54              public Integer deserialize(DataInput input) throws IOException {
55                  return input.readInt();
56              }
57  
58              @Override
59              public int size() {
60                  return 4;
61              }
62          };
63      }
64  
65      public static DataSerializer<byte[]> byteArray() {
66          return ByteArrayHolder.INSTANCE;
67      }
68  
69      private static final class ByteArrayHolder {
70          final static DataSerializer<byte[]> INSTANCE = new DataSerializer<byte[]>() {
71  
72              @Override
73              public void serialize(DataOutput output, byte[] bytes) throws IOException {
74                  output.writeInt(bytes.length);
75                  output.write(bytes);
76              }
77  
78              @Override
79              public byte[] deserialize(DataInput input) throws IOException {
80                  int length = input.readInt();
81                  byte[] bytes = new byte[length];
82                  input.readFully(bytes);
83                  return bytes;
84              }
85  
86              @Override
87              public int size() {
88                  return 0;
89              }
90          };
91      }
92  
93      @SuppressWarnings("unchecked")
94      public static <T> DataSerializer<T> javaIO() {
95          return (DataSerializer<T>) JavaIOHolder.INSTANCE;
96      }
97  
98      private static final class JavaIOHolder {
99  
100         final static DataSerializer<Object> INSTANCE = new DataSerializer<Object>() {
101 
102             @Override
103             public void serialize(DataOutput output, Object object) throws IOException {
104                 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
105                 ObjectOutputStream oos = new ObjectOutputStream(bytes);
106                 oos.writeObject(object);
107                 oos.close();
108                 byte[] array = bytes.toByteArray();
109                 output.writeInt(array.length);
110                 output.write(array);
111             }
112 
113             @Override
114             public Object deserialize(DataInput input) throws IOException {
115                 int length = input.readInt();
116                 byte[] array = new byte[length];
117                 input.readFully(array);
118                 ObjectInputStream ois = null;
119                 try {
120                     ois = new ObjectInputStream(new ByteArrayInputStream(array));
121                     return ois.readObject();
122                 } catch (ClassNotFoundException e) {
123                     throw new RuntimeException(e);
124                 } finally {
125                     if (ois != null) {
126                         try {
127                             ois.close();
128                         } catch (IOException e) {
129                             // ignore
130                         }
131                     }
132                 }
133             }
134 
135             @Override
136             public int size() {
137                 return 0;
138             }
139         };
140     }
141 
142 }