Search in sources :

Example 61 with ObjectInputStream

use of java.io.ObjectInputStream in project XobotOS by xamarin.

the class SealedObject method getObject.

/**
     * Returns the wrapped object, decrypting it using the specified
     * cipher.
     *
     * @param c
     *            the cipher to decrypt the data.
     * @return the encapsulated object.
     * @throws IOException
     *             if deserialization fails.
     * @throws ClassNotFoundException
     *             if deserialization fails.
     * @throws IllegalBlockSizeException
     *             if the specified cipher is a block cipher and the length of
     *             the serialized data is not a multiple of the ciphers block
     *             size.
     * @throws BadPaddingException
     *             if the padding of the data does not match the padding scheme.
     */
public final Object getObject(Cipher c) throws IOException, ClassNotFoundException, IllegalBlockSizeException, BadPaddingException {
    if (c == null) {
        throw new NullPointerException("c == null");
    }
    byte[] serialized = c.doFinal(encryptedContent);
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serialized));
    return ois.readObject();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 62 with ObjectInputStream

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

the class NonSortedExecutableListTest method testSerializeDeserialize.

@Test
public void testSerializeDeserialize() throws IOException, ClassNotFoundException {
    l.add(action4);
    l.add(action3);
    l.add(action2);
    l.add(action1);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    l.writeExternal(oos);
    // this OOS stream needs to be flushed...
    oos.flush();
    ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bin);
    l = new ExecutableList<NonSortedExecutableListTest.AnExecutable>(false);
    l.readExternal(ois);
    Assert.assertEquals(4, l.size());
    Assert.assertEquals(action4, l.get(0));
    Assert.assertEquals(action3, l.get(1));
    Assert.assertEquals(action2, l.get(2));
    Assert.assertEquals(action1, l.get(3));
    Assert.assertFalse(l.get(0).afterDeserializeCalled);
    Assert.assertFalse(l.get(1).afterDeserializeCalled);
    Assert.assertFalse(l.get(2).afterDeserializeCalled);
    Assert.assertFalse(l.get(3).afterDeserializeCalled);
    l.afterDeserialize(null);
    Assert.assertTrue(l.get(0).afterDeserializeCalled);
    Assert.assertTrue(l.get(1).afterDeserializeCalled);
    Assert.assertTrue(l.get(2).afterDeserializeCalled);
    Assert.assertTrue(l.get(3).afterDeserializeCalled);
    Assert.assertEquals(action4, l.get(0));
    Assert.assertEquals(action3, l.get(1));
    Assert.assertEquals(action2, l.get(2));
    Assert.assertEquals(action1, l.get(3));
    // sort after deserializing; it should still have no affect
    l.sort();
    Assert.assertEquals(action4, l.get(0));
    Assert.assertEquals(action3, l.get(1));
    Assert.assertEquals(action2, l.get(2));
    Assert.assertEquals(action1, l.get(3));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 63 with ObjectInputStream

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

the class SortedExecutableListTest method testSerializeDeserialize.

@Test
public void testSerializeDeserialize() throws IOException, ClassNotFoundException {
    l.add(action4);
    l.add(action3);
    l.add(action2);
    l.add(action1);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    l.writeExternal(oos);
    // this OOS stream needs to be flushed...
    oos.flush();
    ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bin);
    l = new ExecutableList<SortedExecutableListTest.AnExecutable>();
    l.readExternal(ois);
    Assert.assertEquals(4, l.size());
    Assert.assertEquals(action4, l.get(0));
    Assert.assertEquals(action3, l.get(1));
    Assert.assertEquals(action2, l.get(2));
    Assert.assertEquals(action1, l.get(3));
    Assert.assertFalse(l.get(0).afterDeserializeCalled);
    Assert.assertFalse(l.get(1).afterDeserializeCalled);
    Assert.assertFalse(l.get(2).afterDeserializeCalled);
    Assert.assertFalse(l.get(3).afterDeserializeCalled);
    l.afterDeserialize(null);
    Assert.assertTrue(l.get(0).afterDeserializeCalled);
    Assert.assertTrue(l.get(1).afterDeserializeCalled);
    Assert.assertTrue(l.get(2).afterDeserializeCalled);
    Assert.assertTrue(l.get(3).afterDeserializeCalled);
    Assert.assertEquals(action4, l.get(0));
    Assert.assertEquals(action3, l.get(1));
    Assert.assertEquals(action2, l.get(2));
    Assert.assertEquals(action1, l.get(3));
    // sort after deserializing
    l.sort();
    Assert.assertEquals(action1, l.get(0));
    Assert.assertEquals(action2, l.get(1));
    Assert.assertEquals(action3, l.get(2));
    Assert.assertEquals(action4, l.get(3));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 64 with ObjectInputStream

use of java.io.ObjectInputStream 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 65 with ObjectInputStream

use of java.io.ObjectInputStream 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)

Aggregations

ObjectInputStream (java.io.ObjectInputStream)1041 ByteArrayInputStream (java.io.ByteArrayInputStream)667 ObjectOutputStream (java.io.ObjectOutputStream)427 ByteArrayOutputStream (java.io.ByteArrayOutputStream)354 IOException (java.io.IOException)341 FileInputStream (java.io.FileInputStream)152 Test (org.junit.Test)128 File (java.io.File)89 InputStream (java.io.InputStream)85 BufferedInputStream (java.io.BufferedInputStream)47 Serializable (java.io.Serializable)40 HashMap (java.util.HashMap)35 ArrayList (java.util.ArrayList)31 FileNotFoundException (java.io.FileNotFoundException)27 FileOutputStream (java.io.FileOutputStream)27 Test (org.testng.annotations.Test)26 Map (java.util.Map)25 EOFException (java.io.EOFException)21 GZIPInputStream (java.util.zip.GZIPInputStream)21 ObjectInput (java.io.ObjectInput)20