Skip to content

Commit 0da806d

Browse files
author
alafighting
committed
增加更多的DiskConverter实现
1 parent 726ff55 commit 0da806d

File tree

7 files changed

+135
-26
lines changed

7 files changed

+135
-26
lines changed

app/src/main/java/com/im4j/kakacache/rxjava/demo/GithubRepoEntity.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import com.im4j.kakacache.rxjava.common.utils.MemorySizeOf;
44

5-
import java.io.Serializable;
6-
75
/**
86
* @version alafighting 2016-07
97
*/
10-
public class GithubRepoEntity implements MemorySizeOf.SizeOf, Serializable {
8+
public class GithubRepoEntity implements MemorySizeOf.SizeOf {
119

1210
private String id;
1311
private String name;

library/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ dependencies {
3333
compile 'com.squareup.retrofit2:retrofit:2.1.0'
3434
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
3535
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
36+
compile 'com.esotericsoftware:kryo:4.0.0'
3637
}

library/src/main/java/com/im4j/kakacache/rxjava/KakaCache.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44

5+
import com.esotericsoftware.kryo.Kryo;
56
import com.google.gson.GsonBuilder;
67
import com.google.gson.JsonDeserializationContext;
78
import com.google.gson.JsonDeserializer;
@@ -12,6 +13,7 @@
1213
import com.im4j.kakacache.rxjava.common.utils.LogUtils;
1314
import com.im4j.kakacache.rxjava.common.utils.Utils;
1415
import com.im4j.kakacache.rxjava.core.CacheCore;
16+
import com.im4j.kakacache.rxjava.core.disk.converter.KryoDiskConverter;
1517
import com.im4j.kakacache.rxjava.core.disk.converter.SerializableDiskConverter;
1618
import com.im4j.kakacache.rxjava.core.disk.journal.LRUDiskJournal;
1719
import com.im4j.kakacache.rxjava.core.disk.storage.FileDiskStorage;
@@ -74,7 +76,7 @@ public static RxCacheManager manager() {
7476
coreBuilder.disk(new FileDiskStorage(storageDir));
7577
coreBuilder.diskJournal(new LRUDiskJournal(liteOrm));
7678
coreBuilder.diskMax(30 * 1024 * 1024, 10 * 1000);
77-
coreBuilder.diskConverter(new SerializableDiskConverter());
79+
coreBuilder.diskConverter(new KryoDiskConverter(new Kryo()));
7880
CacheCore core = coreBuilder.create();
7981
cacheManager = new RxCacheManager(core, DEFAULT_EXPIRES);
8082
}

library/src/main/java/com/im4j/kakacache/rxjava/common/utils/Utils.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ public static <T> T checkNotNull(T obj) {
3939
}
4040
return obj;
4141
}
42-
/**
43-
* 不为空
44-
*/
45-
public static <T> T checkNotNull(T obj, String message) {
46-
if (obj == null) {
47-
throw new NullException(message);
48-
}
49-
return obj;
50-
}
5142

5243
/**
5344
* 不小于0
@@ -61,10 +52,7 @@ public static long checkNotLessThanZero(long number) {
6152
}
6253

6354
public static boolean isEmpty(String str) {
64-
if (str == null || str.isEmpty()) {
65-
return true;
66-
}
67-
return false;
55+
return str == null || str.isEmpty();
6856
}
6957

7058

@@ -226,15 +214,6 @@ public static File getStorageCacheDir(Context context) {
226214
+ context.getPackageName() + SEPARATOR + "cache" + SEPARATOR);
227215
}
228216
static File getDataCacheDir(Context context) {
229-
// String dataDir = "/data";
230-
//
231-
// File path = Environment.getDataDirectory();
232-
// if (path != null) {
233-
// dataDir = path.getAbsolutePath();
234-
// }
235-
// return new File(dataDir + SEPARATOR + "data" + SEPARATOR
236-
// + context.getPackageName() + SEPARATOR + "cache" + SEPARATOR);
237-
238217
return context.getCacheDir();
239218
}
240219

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.im4j.kakacache.rxjava.core.disk.converter;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonIOException;
5+
import com.google.gson.JsonSyntaxException;
6+
import com.im4j.kakacache.rxjava.common.utils.LogUtils;
7+
import com.im4j.kakacache.rxjava.common.utils.Utils;
8+
import com.im4j.kakacache.rxjava.core.disk.sink.Sink;
9+
import com.im4j.kakacache.rxjava.core.disk.source.ReaderSource;
10+
import com.im4j.kakacache.rxjava.core.disk.source.Source;
11+
12+
import java.io.IOException;
13+
import java.lang.reflect.Type;
14+
15+
/**
16+
* GSON-数据转换器
17+
* @version alafighting 2016-07
18+
*/
19+
public class GsonDiskConverter implements IDiskConverter {
20+
private Gson gson;
21+
22+
public GsonDiskConverter(Gson gson) {
23+
this.gson = gson;
24+
}
25+
26+
@Override
27+
public Object load(Source source, Type type) {
28+
Object value = null;
29+
ReaderSource readerSource = new ReaderSource(source);
30+
try {
31+
value = gson.fromJson(readerSource, type);
32+
} catch (JsonIOException e) {
33+
LogUtils.log(e);
34+
} catch (JsonSyntaxException e) {
35+
LogUtils.log(e);
36+
} finally {
37+
Utils.close(readerSource);
38+
}
39+
return value;
40+
}
41+
42+
@Override
43+
public void writer(Sink sink, Object data) {
44+
try {
45+
String json = gson.toJson(data);
46+
byte[] bytes = json.getBytes();
47+
sink.write(bytes, 0, bytes.length);
48+
sink.flush();
49+
} catch (JsonIOException e) {
50+
LogUtils.log(e);
51+
} catch (JsonSyntaxException e) {
52+
LogUtils.log(e);
53+
} catch (IOException e) {
54+
LogUtils.log(e);
55+
}
56+
}
57+
58+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.im4j.kakacache.rxjava.core.disk.converter;
2+
3+
import com.esotericsoftware.kryo.Kryo;
4+
import com.esotericsoftware.kryo.io.Input;
5+
import com.esotericsoftware.kryo.io.Output;
6+
import com.im4j.kakacache.rxjava.common.utils.Utils;
7+
import com.im4j.kakacache.rxjava.core.disk.sink.BasicSink;
8+
import com.im4j.kakacache.rxjava.core.disk.sink.Sink;
9+
import com.im4j.kakacache.rxjava.core.disk.source.BasicSource;
10+
import com.im4j.kakacache.rxjava.core.disk.source.Source;
11+
12+
import java.lang.reflect.Type;
13+
14+
/**
15+
* Kryo-数据转换器
16+
* @version alafighting 2016-07
17+
*/
18+
public class KryoDiskConverter implements IDiskConverter {
19+
private Kryo kryo;
20+
21+
public KryoDiskConverter(Kryo kryo) {
22+
this.kryo = kryo;
23+
}
24+
25+
@Override
26+
public Object load(Source source, Type type) {
27+
Object value = null;
28+
Input input = null;
29+
try {
30+
input = new Input(new BasicSource(source));
31+
value = kryo.readClassAndObject(input);
32+
} finally {
33+
Utils.close(input);
34+
}
35+
return value;
36+
}
37+
38+
@Override
39+
public void writer(Sink sink, Object data) {
40+
Output output = null;
41+
try {
42+
output = new Output(new BasicSink(sink));
43+
kryo.writeClassAndObject(output, data);
44+
} finally {
45+
Utils.close(output);
46+
}
47+
}
48+
49+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.im4j.kakacache.rxjava.core.disk.source;
2+
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
6+
/**
7+
* @version alafighting 2016-07
8+
*/
9+
public class ReaderSource extends InputStreamReader implements Source {
10+
11+
private final Source source;
12+
13+
public ReaderSource(Source source) {
14+
super(new BasicSource(source));
15+
this.source = source;
16+
}
17+
18+
@Override
19+
public int read(byte[] buffer, int offset, int byteCount) throws IOException {
20+
return source.read(buffer, offset, byteCount);
21+
}
22+
}

0 commit comments

Comments
 (0)