Home | History | Annotate | Download | only in utils
      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 
     17 package com.android.server.backup.utils;
     18 
     19 import java.io.BufferedOutputStream;
     20 import java.io.DataInputStream;
     21 import java.io.DataOutputStream;
     22 import java.io.File;
     23 import java.io.FileInputStream;
     24 import java.io.FileOutputStream;
     25 import java.io.IOException;
     26 
     27 /**
     28  * Provides an interface for serializing an object to a file and deserializing it back again.
     29  *
     30  * <p>Serialization logic is implemented as a {@link DataStreamCodec}.
     31  *
     32  * @param <T> The type of object to serialize / deserialize.
     33  */
     34 public final class DataStreamFileCodec<T> {
     35     private final File mFile;
     36     private final DataStreamCodec<T> mCodec;
     37 
     38     /**
     39      * Constructs an instance to serialize to or deserialize from the given file, with the given
     40      * serialization / deserialization strategy.
     41      */
     42     public DataStreamFileCodec(File file, DataStreamCodec<T> codec) {
     43         mFile = file;
     44         mCodec = codec;
     45     }
     46 
     47     /**
     48      * Deserializes a {@code T} from the file, automatically closing input streams.
     49      *
     50      * @return The deserialized object.
     51      * @throws IOException if an IO error occurred.
     52      */
     53     public T deserialize() throws IOException {
     54         try (
     55             FileInputStream fileInputStream = new FileInputStream(mFile);
     56             DataInputStream dataInputStream = new DataInputStream(fileInputStream)
     57         ) {
     58             return mCodec.deserialize(dataInputStream);
     59         }
     60     }
     61 
     62     /**
     63      * Serializes {@code t} to the file, automatically flushing and closing output streams.
     64      *
     65      * @param t The object to serialize.
     66      * @throws IOException if an IO error occurs.
     67      */
     68     public void serialize(T t) throws IOException {
     69         try (
     70             FileOutputStream fileOutputStream = new FileOutputStream(mFile);
     71             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
     72             DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream)
     73         ) {
     74             mCodec.serialize(t, dataOutputStream);
     75             dataOutputStream.flush();
     76         }
     77     }
     78 }
     79