ParcelPersistentUtils.java
4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* 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;
}
}