ParcelPersistentUtils.java 4.83 KB
/*
 *  Copyright (c)  2016.  wugian
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.gimi.common.cinema.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Parcel;
import android.util.Log;
import com.mstar.android.samba.SmbDevice;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wugian on 2016/11/17
 */
public class ParcelPersistentUtils {

    @TargetApi(Build.VERSION_CODES.FROYO)
    public static void saveSmbList(Context context, List<SmbDevice> result) {
        File file = context.getExternalCacheDir();
        if (!file.exists()) {
            file.mkdirs();
        }
        try {
            DiskLruCache cache = DiskLruCache.open(file, 0, 1, 10 * 1024 * 1024);
            DiskLruCache.Editor editor = cache.edit("smb_list");
            OutputStream outputStream = editor.newOutputStream(0);
            BufferedOutputStream bos = new BufferedOutputStream(outputStream);
            Parcel parcel = Parcel.obtain();
            parcel.writeInt(result.size());
            parcel.writeTypedList(result);

            bos.write(parcel.marshall());
            bos.flush();
            bos.close();
            outputStream.flush();
            outputStream.close();
            editor.commit();
            cache.flush();
            Log.d("lovely", "╟  write file success!");
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("lovely", "╟  write file failed!");
        }
    }

    @TargetApi(Build.VERSION_CODES.FROYO)
    public static List<SmbDevice> loadSmbList(Context context) {
        File file = context.getExternalCacheDir();
        if (!file.exists()) {
            file.mkdirs();
        }
        try {
            DiskLruCache cache = DiskLruCache.open(file, 0, 1, 10 * 1024 * 1024);
            DiskLruCache.Snapshot snapshot = null;
            snapshot = cache.get("smb_list");
            if (snapshot == null) {
                Log.d("lovely", "╟  cache not exist!");
                return null;
            }
            InputStream inputStream = snapshot.getInputStream(0);
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            Parcel parcel = Parcel.obtain();
            parcel.unmarshall(bytes, 0, bytes.length);
            parcel.setDataPosition(0);
            int size = parcel.readInt();
            List<SmbDevice> list = new ArrayList<>(size);
            parcel.readTypedList(list, SmbDevice.CREATOR);
            inputStream.close();
            snapshot.close();
            Log.d("lovely", "╟  read file success!");
            return list;
            //cache.close();
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("lovely", "╟  read file failed!");
            return null;
        }
    }

    public static void writeObjectToFile(Object obj) {
        File file = new File("/sdcard/result.dat");
        FileOutputStream out;
        try {
            out = new FileOutputStream(file);
            ObjectOutputStream objOut = new ObjectOutputStream(out);
            objOut.writeObject(obj);
            objOut.flush();
            objOut.close();
            Log.d("lovely", "╟  write object success!");
        } catch (IOException e) {
            Log.d("lovely", "╟  write object failed!");
            e.printStackTrace();
        }
    }

    private Object readObjectFromFile() {
        Object temp = null;
        File file = new File("/sdcard/result.dat");
        if (!file.exists()) {
            return null;
        }
        FileInputStream in;
        try {
            in = new FileInputStream(file);
            ObjectInputStream objIn = new ObjectInputStream(in);
            temp = objIn.readObject();
            objIn.close();
            System.out.println("read object success!");
        } catch (IOException e) {
            System.out.println("read object failed");
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return temp;
    }
}