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.ByteArrayInputStream; 19 import java.io.ByteArrayOutputStream; 20 import java.io.File; 21 import java.io.FileInputStream; 22 import java.io.FileOutputStream; 23 import java.io.IOException; 24 import java.io.ObjectInputStream; 25 import java.io.ObjectOutputStream; 26 import java.io.Serializable; 27 import java.util.Base64; 28 29 /** Utility to serialize/deserialize an object that implements {@link Serializable}. */ 30 public class SerializationUtil { 31 32 /** 33 * Serialize a object that implements {@link Serializable}. 34 * 35 * @param o the object to serialize. 36 * @return the {@link File} where the object was serialized. 37 * @throws IOException if serialization fails. 38 */ serialize(Serializable o)39 public static File serialize(Serializable o) throws IOException { 40 File tmpSerialized = FileUtil.createTempFile("serial-util", ".ser"); 41 FileOutputStream fileOut = null; 42 ObjectOutputStream out = null; 43 try { 44 fileOut = new FileOutputStream(tmpSerialized); 45 out = new ObjectOutputStream(fileOut); 46 out.writeObject(o); 47 } catch (IOException e) { 48 // catch in order to clean up the tmp file. 49 FileUtil.deleteFile(tmpSerialized); 50 throw e; 51 } finally { 52 StreamUtil.close(out); 53 StreamUtil.close(fileOut); 54 } 55 return tmpSerialized; 56 } 57 58 /** 59 * Serialize and object into a base64 encoded string. 60 * 61 * @param o the object to serialize. 62 * @return the {@link String} where the object was serialized. 63 * @throws IOException if serialization fails. 64 */ serializeToString(Serializable o)65 public static String serializeToString(Serializable o) throws IOException { 66 ByteArrayOutputStream byteOut = null; 67 ObjectOutputStream out = null; 68 try { 69 byteOut = new ByteArrayOutputStream(); 70 out = new ObjectOutputStream(byteOut); 71 out.writeObject(o); 72 return Base64.getEncoder().encodeToString(byteOut.toByteArray()); 73 } finally { 74 StreamUtil.close(out); 75 StreamUtil.close(byteOut); 76 } 77 } 78 79 /** 80 * Deserialize an object that was serialized using {@link #serializeToString(Serializable)}. 81 * 82 * @param serialized the base64 string where the object was serialized. 83 * @return the Object deserialized. 84 * @throws IOException if the deserialization fails. 85 */ deserialize(String serialized)86 public static Object deserialize(String serialized) throws IOException { 87 ByteArrayInputStream bais = null; 88 ObjectInputStream in = null; 89 try { 90 bais = new ByteArrayInputStream(Base64.getDecoder().decode(serialized)); 91 in = new ObjectInputStream(bais); 92 return in.readObject(); 93 } catch (ClassNotFoundException cnfe) { 94 throw new RuntimeException(cnfe); 95 } finally { 96 StreamUtil.close(in); 97 StreamUtil.close(bais); 98 } 99 } 100 101 /** 102 * Deserialize an object that was serialized using {@link #serialize(Serializable)}. 103 * 104 * @param serializedFile the file where the object was serialized. 105 * @param deleteFile true if the serialized file should be deleted once deserialized. 106 * @return the Object deserialized. 107 * @throws IOException if the deserialization fails. 108 */ deserialize(File serializedFile, boolean deleteFile)109 public static Object deserialize(File serializedFile, boolean deleteFile) throws IOException { 110 FileInputStream fileIn = null; 111 ObjectInputStream in = null; 112 try { 113 fileIn = new FileInputStream(serializedFile); 114 in = new ObjectInputStream(fileIn); 115 return in.readObject(); 116 } catch (ClassNotFoundException cnfe) { 117 throw new RuntimeException(cnfe); 118 } finally { 119 StreamUtil.close(in); 120 StreamUtil.close(fileIn); 121 if (deleteFile) { 122 FileUtil.deleteFile(serializedFile); 123 } 124 } 125 } 126 } 127