use of java.io.ObjectOutputStream in project leakcanary by square.
the class DisplayLeakService method saveResult.
private boolean saveResult(HeapDump heapDump, AnalysisResult result) {
File resultFile = new File(heapDump.heapDumpFile.getParentFile(), heapDump.heapDumpFile.getName() + ".result");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(resultFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(heapDump);
oos.writeObject(result);
return true;
} catch (IOException e) {
CanaryLog.d(e, "Could not save leak analysis result to disk.");
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ignored) {
}
}
}
return false;
}
use of java.io.ObjectOutputStream in project spring-security by spring-projects.
the class SecuredAnnotationDrivenBeanDefinitionParserTests method serializeAndDeserialize.
private Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.flush();
baos.flush();
byte[] bytes = baos.toByteArray();
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(is);
Object o2 = ois.readObject();
return o2;
}
use of java.io.ObjectOutputStream in project siena by mandubian.
the class JdbcPersistenceManager method addParameters.
protected int addParameters(Object obj, List<Field> fields, PreparedStatement ps, int i) throws SQLException {
for (Field field : fields) {
Class<?> type = field.getType();
if (ClassInfo.isModel(type) && !ClassInfo.isEmbedded(field)) {
JdbcClassInfo ci = JdbcClassInfo.getClassInfo(type);
Object rel = Util.readField(obj, field);
for (Field f : ci.keys) {
if (rel != null) {
Object value = Util.readField(rel, f);
if (value instanceof Json)
value = ((Json) value).toString();
setParameter(ps, i++, value);
} else {
setParameter(ps, i++, null);
}
}
} else {
Object value = Util.readField(obj, field);
if (value != null) {
if (Json.class.isAssignableFrom(type)) {
value = ((Json) value).toString();
} else if (field.getAnnotation(Embedded.class) != null) {
value = JsonSerializer.serialize(value).toString();
} else if (field.getAnnotation(Polymorphic.class) != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out;
try {
out = new ObjectOutputStream(bos);
out.writeObject(value);
out.close();
} catch (IOException e) {
throw new SienaException(e);
}
value = bos.toByteArray();
} else if (Enum.class.isAssignableFrom(type)) {
value = value.toString();
} else if (BigDecimal.class == type) {
DecimalPrecision ann = field.getAnnotation(DecimalPrecision.class);
if (ann == null) {
value = (BigDecimal) value;
} else {
switch(ann.storageType()) {
case DOUBLE:
value = ((BigDecimal) value).doubleValue();
break;
case STRING:
value = ((BigDecimal) value).toPlainString();
break;
case NATIVE:
value = (BigDecimal) value;
break;
}
}
}
}
setParameter(ps, i++, value);
}
}
return i;
}
use of java.io.ObjectOutputStream in project siena by mandubian.
the class GoogleSqlPersistenceManager method addParameters.
@Override
protected int addParameters(Object obj, List<Field> fields, PreparedStatement ps, int i) throws SQLException {
for (Field field : fields) {
Class<?> type = field.getType();
if (ClassInfo.isModel(type) && !ClassInfo.isEmbedded(field)) {
JdbcClassInfo ci = JdbcClassInfo.getClassInfo(type);
Object rel = Util.readField(obj, field);
for (Field f : ci.keys) {
if (rel != null) {
Object value = Util.readField(rel, f);
if (value instanceof Json)
value = ((Json) value).toString();
setParameter(ps, i++, value, f);
} else {
setParameter(ps, i++, null, f);
}
}
} else {
Object value = Util.readField(obj, field);
if (value != null) {
if (Json.class.isAssignableFrom(type)) {
value = ((Json) value).toString();
} else if (field.getAnnotation(Embedded.class) != null) {
value = JsonSerializer.serialize(value).toString();
} else if (field.getAnnotation(Polymorphic.class) != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out;
try {
out = new ObjectOutputStream(bos);
out.writeObject(value);
out.close();
} catch (IOException e) {
throw new SienaException(e);
}
value = bos.toByteArray();
} else if (Enum.class.isAssignableFrom(type)) {
value = value.toString();
} else if (BigDecimal.class == type) {
DecimalPrecision ann = field.getAnnotation(DecimalPrecision.class);
if (ann == null) {
value = (BigDecimal) value;
} else {
switch(ann.storageType()) {
case DOUBLE:
value = ((BigDecimal) value).doubleValue();
break;
case STRING:
value = ((BigDecimal) value).toPlainString();
break;
case NATIVE:
value = (BigDecimal) value;
break;
}
}
}
}
setParameter(ps, i++, value, field);
}
}
return i;
}
use of java.io.ObjectOutputStream in project camel by apache.
the class MyObject method deserialize.
@Test
public void deserialize() throws IOException, ClassNotFoundException {
CamelContext context = new DefaultCamelContext();
final DefaultExchange exchange = new DefaultExchange(context);
final List<MyObject> objects = new ArrayList<>();
final MyObject o = new MyObject("leb", "hello".getBytes());
objects.add(o);
exchange.getIn().setBody(objects);
final DefaultExchangeHolder deh = DefaultExchangeHolder.marshal(exchange);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(deh);
oos.flush();
final byte[] serialized = baos.toByteArray();
final ObjectInputStream bis = new ClassLoadingAwareObjectInputStream(context, new ByteArrayInputStream(serialized));
final DefaultExchangeHolder deserialized = (DefaultExchangeHolder) bis.readObject();
final DefaultExchange exchange2 = new DefaultExchange(context);
DefaultExchangeHolder.unmarshal(exchange2, deserialized);
List<MyObject> receivedObjects = exchange2.getIn().getBody(List.class);
assertEquals(1, receivedObjects.size());
assertEquals(o, receivedObjects.get(0));
}
Aggregations