Search in sources :

Example 36 with ObjectOutput

use of java.io.ObjectOutput in project BetterBatteryStats by asksven.

the class ReferenceDto method serialize.

/**
 * Serialize using Java serialization
 * @return
 */
private byte[] serialize() {
    byte[] ret = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(this);
        ret = bos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return ret;
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 37 with ObjectOutput

use of java.io.ObjectOutput in project hibernate-orm by hibernate.

the class EntityManagerSerializationTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    EntityManager em = entityManagerFactory().createEntityManager();
    // em.getTransaction().begin();
    // em.setFlushMode( FlushModeType.NEVER );
    Cat cat = new Cat();
    cat.setAge(3);
    cat.setDateOfBirth(new Date());
    cat.setLength(22);
    cat.setName("Kitty");
    em.persist(cat);
    Item item = new Item();
    item.setName("Train Ticket");
    item.setDescr("Paris-London");
    em.persist(item);
    // em.getTransaction().commit();
    // em.getTransaction().begin();
    item.setDescr("Paris-Bruxelles");
    // em.getTransaction().commit();
    // fake the in container work
    ((HibernateEntityManager) em).getSession().disconnect();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(stream);
    out.writeObject(em);
    out.close();
    byte[] serialized = stream.toByteArray();
    stream.close();
    ByteArrayInputStream byteIn = new ByteArrayInputStream(serialized);
    ObjectInputStream in = new ObjectInputStream(byteIn);
    em = (EntityManager) in.readObject();
    in.close();
    byteIn.close();
    // fake the in container work
    em.getTransaction().begin();
    item = em.find(Item.class, item.getName());
    item.setDescr(item.getDescr() + "-Amsterdam");
    cat = (Cat) em.createQuery("select c from " + Cat.class.getName() + " c").getSingleResult();
    cat.setLength(34);
    em.flush();
    em.remove(item);
    em.remove(cat);
    em.flush();
    em.getTransaction().commit();
    em.close();
}
Also used : Item(org.hibernate.jpa.test.Item) HibernateEntityManager(org.hibernate.jpa.HibernateEntityManager) EntityManager(javax.persistence.EntityManager) ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) Cat(org.hibernate.jpa.test.Cat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Date(java.util.Date) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 38 with ObjectOutput

use of java.io.ObjectOutput in project hibernate-orm by hibernate.

the class EntityManagerFactorySerializationTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(stream);
    out.writeObject(entityManagerFactory());
    out.close();
    byte[] serialized = stream.toByteArray();
    stream.close();
    ByteArrayInputStream byteIn = new ByteArrayInputStream(serialized);
    ObjectInputStream in = new ObjectInputStream(byteIn);
    EntityManagerFactory serializedFactory = (EntityManagerFactory) in.readObject();
    in.close();
    byteIn.close();
    EntityManager em = serializedFactory.createEntityManager();
    // em.getTransaction().begin();
    // em.setFlushMode( FlushModeType.NEVER );
    Cat cat = new Cat();
    cat.setAge(3);
    cat.setDateOfBirth(new Date());
    cat.setLength(22);
    cat.setName("Kitty");
    em.persist(cat);
    Item item = new Item();
    item.setName("Train Ticket");
    item.setDescr("Paris-London");
    em.persist(item);
    // em.getTransaction().commit();
    // em.getTransaction().begin();
    item.setDescr("Paris-Bruxelles");
    // em.getTransaction().commit();
    // fake the in container work
    ((HibernateEntityManager) em).getSession().disconnect();
    stream = new ByteArrayOutputStream();
    out = new ObjectOutputStream(stream);
    out.writeObject(em);
    out.close();
    serialized = stream.toByteArray();
    stream.close();
    byteIn = new ByteArrayInputStream(serialized);
    in = new ObjectInputStream(byteIn);
    em = (EntityManager) in.readObject();
    in.close();
    byteIn.close();
    // fake the in container work
    em.getTransaction().begin();
    item = em.find(Item.class, item.getName());
    item.setDescr(item.getDescr() + "-Amsterdam");
    cat = (Cat) em.createQuery("select c from " + Cat.class.getName() + " c").getSingleResult();
    cat.setLength(34);
    em.flush();
    em.remove(item);
    em.remove(cat);
    em.flush();
    em.getTransaction().commit();
    em.close();
}
Also used : Item(org.hibernate.jpa.test.Item) HibernateEntityManager(org.hibernate.jpa.HibernateEntityManager) EntityManager(javax.persistence.EntityManager) ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityManagerFactory(javax.persistence.EntityManagerFactory) Cat(org.hibernate.jpa.test.Cat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Date(java.util.Date) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 39 with ObjectOutput

use of java.io.ObjectOutput in project mssql-jdbc by Microsoft.

the class NativeMSSQLDataSourceTest method testSerial.

private SQLServerDataSource testSerial(SQLServerDataSource ds) throws IOException, ClassNotFoundException {
    try (java.io.ByteArrayOutputStream outputStream = new java.io.ByteArrayOutputStream();
        java.io.ObjectOutput objectOutput = new java.io.ObjectOutputStream(outputStream)) {
        objectOutput.writeObject(ds);
        objectOutput.flush();
        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray()))) {
            SQLServerDataSource dtn;
            dtn = (SQLServerDataSource) in.readObject();
            return dtn;
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ISQLServerDataSource(com.microsoft.sqlserver.jdbc.ISQLServerDataSource) SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 40 with ObjectOutput

use of java.io.ObjectOutput in project mlib by myshzzx.

the class FilesUtil method writeObjectToFile.

/**
 * 将数据写入到文件.
 *
 * @param file 文件
 * @param obj  要写入的对象.
 */
public static void writeObjectToFile(File file, Object obj) throws IOException {
    file.getAbsoluteFile().getParentFile().mkdirs();
    File writeFile = getWriteFile(file);
    try (ObjectOutput out = new ObjectOutputStream(new FileOutputStream(writeFile))) {
        out.writeObject(obj);
    }
    file.delete();
    if (!writeFile.renameTo(file))
        throw new IOException("rename file error. " + writeFile.getAbsolutePath() + " -> " + file.getAbsolutePath());
    log.debug("write to file: " + file.getAbsolutePath());
}
Also used : ObjectOutput(java.io.ObjectOutput) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File)

Aggregations

ObjectOutput (java.io.ObjectOutput)105 ObjectOutputStream (java.io.ObjectOutputStream)78 ByteArrayOutputStream (java.io.ByteArrayOutputStream)64 IOException (java.io.IOException)53 ObjectInput (java.io.ObjectInput)26 Test (org.junit.Test)24 ObjectInputStream (java.io.ObjectInputStream)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 WorkingMemory (org.drools.core.WorkingMemory)13 FileOutputStream (java.io.FileOutputStream)12 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)12 Pattern (org.drools.core.rule.Pattern)12 Consequence (org.drools.core.spi.Consequence)12 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)12 OutputStream (java.io.OutputStream)11 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 Declaration (org.drools.core.rule.Declaration)8 IntrospectionException (java.beans.IntrospectionException)7 File (java.io.File)7 InvalidRuleException (org.drools.core.rule.InvalidRuleException)7