Open Kilda Java Documentation
ObjectSerializer.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.messaging;
17 
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.ObjectInputStream;
24 import java.io.ObjectOutputStream;
25 
26 public interface ObjectSerializer extends AbstractSerializer {
27  String FILE_NAME = "target/serialization.txt";
28 
29  @Override
30  default Object deserialize() throws IOException, ClassNotFoundException {
31  FileInputStream fis = new FileInputStream(FILE_NAME);
32  BufferedInputStream bis = new BufferedInputStream(fis);
33  ObjectInputStream ois = new ObjectInputStream(bis);
34  Object obj = ois.readObject();
35  ois.close();
36  bis.close();
37  fis.close();
38  return obj;
39  }
40 
41  @Override
42  default void serialize(Object obj) throws IOException {
43  FileOutputStream fos = new FileOutputStream(FILE_NAME);
44  BufferedOutputStream bos = new BufferedOutputStream(fos);
45  ObjectOutputStream oos = new ObjectOutputStream(bos);
46  oos.writeObject(obj);
47  oos.close();
48  bos.close();
49  fos.close();
50  }
51 }