下面列出了java.io.ObjectOutputStream#writeUnshared ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void run() {
try {
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(cachePath));
List<LearningInstance> instances;
while ((instances = data.getLearningInstance()).size() > 0) {
for (LearningInstance ins : instances) {
cnt++;
outputStream.writeUnshared(ins);
outputStream.reset();
if (cnt % 1000000 == 0) {
logger.info("Cached {} instances.", cnt);
}
}
}
outputStream.flush();
outputStream.close();
} catch (IOException e) {
logger.error(e.getMessage());
throw new BadRequestException(e);
}
}
/**
* Java serialization
*
* @param o
* @throws IOException
*/
private void writeObject(ObjectOutputStream o) throws IOException {
o.defaultWriteObject();
if (isLeaf()) {
o.writeUnshared(elementIds.toArray());
}
}
@Override
public byte[] serializeObject(String key, Object object) throws CacheException {
try {
ByteArrayOutputStream oo = new ByteArrayOutputStream();
ObjectOutputStream oo2 = new ObjectOutputStream(oo);
oo2.writeUnshared(object);
oo2.close();
return oo.toByteArray();
} catch (IOException | SecurityException err) {
throw new CacheException(err);
}
}
@Override
public String serialize(Object value) {
if (value instanceof String) {
// BlazingCache is made for string keys!
// if you are not using strings then implement a custom serializer
String key = (String) value;
if (!key.startsWith("$")) {
return key;
}
}
try {
ByteArrayOutputStream oo = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(oo);
o.writeUnshared(value);
o.flush();
return "$" + Base64.getEncoder().encodeToString(oo.toByteArray());
} catch (java.io.NotSerializableException notSerializable) {
// VERY BAD CASE (the TCK for instance), Usually keys are serializable or the client uses a custom serializer
String alreadyCached = notSerializableKeys.get(value);
if (alreadyCached != null) {
return alreadyCached;
}
String id = value.getClass().getName() + "_" + System.identityHashCode(value);
notSerializableKeys.put(value, id);
return id;
} catch (IOException err) {
throw new CacheException(err);
}
}
public static void writeObject2File(String file, Object obj) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
oos.writeUnshared(obj);
oos.close();
}
public static void writeObj2FileWithTs(String file, Object obj) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(file));
oos.writeUnshared(obj);
oos.close();
}
private void writeObject(ObjectOutputStream o) throws IOException {
o.writeUnshared(word);
o.writeUnshared(fourierValues);
o.writeInt(pos);
}