Search in sources :

Example 46 with ObjectOutputStream

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;
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File)

Example 47 with ObjectOutputStream

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 48 with ObjectOutputStream

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;
}
Also used : ObjectOutput(java.io.ObjectOutput) DecimalPrecision(siena.core.DecimalPrecision) Json(siena.Json) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Field(java.lang.reflect.Field) SienaException(siena.SienaException) Polymorphic(siena.core.Polymorphic)

Example 49 with ObjectOutputStream

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;
}
Also used : ObjectOutput(java.io.ObjectOutput) DecimalPrecision(siena.core.DecimalPrecision) Json(siena.Json) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Field(java.lang.reflect.Field) SienaException(siena.SienaException) Polymorphic(siena.core.Polymorphic)

Example 50 with ObjectOutputStream

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));
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultExchange(org.apache.camel.impl.DefaultExchange) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) DefaultExchangeHolder(org.apache.camel.impl.DefaultExchangeHolder) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

ObjectOutputStream (java.io.ObjectOutputStream)1102 ByteArrayOutputStream (java.io.ByteArrayOutputStream)760 ObjectInputStream (java.io.ObjectInputStream)428 ByteArrayInputStream (java.io.ByteArrayInputStream)375 IOException (java.io.IOException)358 FileOutputStream (java.io.FileOutputStream)161 Test (org.junit.Test)161 File (java.io.File)96 BufferedOutputStream (java.io.BufferedOutputStream)52 ObjectOutput (java.io.ObjectOutput)47 OutputStream (java.io.OutputStream)37 HashMap (java.util.HashMap)37 ArrayList (java.util.ArrayList)33 FileInputStream (java.io.FileInputStream)25 InputStream (java.io.InputStream)23 FileNotFoundException (java.io.FileNotFoundException)22 Serializable (java.io.Serializable)17 GZIPOutputStream (java.util.zip.GZIPOutputStream)16 Test (org.testng.annotations.Test)15 NotSerializableException (java.io.NotSerializableException)14