Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.tradefed.util;
     17 
     18 import java.io.File;
     19 import java.io.FileInputStream;
     20 import java.io.FileOutputStream;
     21 import java.io.IOException;
     22 import java.io.ObjectInputStream;
     23 import java.io.ObjectOutputStream;
     24 import java.io.Serializable;
     25 
     26 /** Utility to serialize/deserialize an object that implements {@link Serializable}. */
     27 public class SerializationUtil {
     28 
     29     /**
     30      * Serialize a object that implements {@link Serializable}.
     31      *
     32      * @param o the object to serialize.
     33      * @return the {@link File} where the object was serialized.
     34      * @throws IOException if serialization fails.
     35      */
     36     public static File serialize(Serializable o) throws IOException {
     37         File tmpSerialized = FileUtil.createTempFile("serial-util", ".ser");
     38         FileOutputStream fileOut = null;
     39         ObjectOutputStream out = null;
     40         try {
     41             fileOut = new FileOutputStream(tmpSerialized);
     42             out = new ObjectOutputStream(fileOut);
     43             out.writeObject(o);
     44         } catch (IOException e) {
     45             // catch in order to clean up the tmp file.
     46             FileUtil.deleteFile(tmpSerialized);
     47             throw e;
     48         } finally {
     49             StreamUtil.close(out);
     50             StreamUtil.close(fileOut);
     51         }
     52         return tmpSerialized;
     53     }
     54 
     55     /**
     56      * Deserialize an object that was serialized using {@link #serialize(Serializable)}.
     57      *
     58      * @param serializedFile the file where the object was serialized.
     59      * @param deleteFile true if the serialized file should be deleted once deserialized.
     60      * @return the Object deserialized.
     61      * @throws IOException if the deserialization fails.
     62      */
     63     public static Object deserialize(File serializedFile, boolean deleteFile) throws IOException {
     64         FileInputStream fileIn = null;
     65         ObjectInputStream in = null;
     66         try {
     67             fileIn = new FileInputStream(serializedFile);
     68             in = new ObjectInputStream(fileIn);
     69             return in.readObject();
     70         } catch (ClassNotFoundException cnfe) {
     71             throw new RuntimeException(cnfe);
     72         } finally {
     73             StreamUtil.close(in);
     74             StreamUtil.close(fileIn);
     75             if (deleteFile) {
     76                 FileUtil.deleteFile(serializedFile);
     77             }
     78         }
     79     }
     80 }
     81