Search in sources :

Example 21 with ObjectInput

use of java.io.ObjectInput in project wildfly by wildfly.

the class DsTestCase method testDatasourceSerialization.

@Test
public void testDatasourceSerialization() throws Exception {
    InitialContext context = new InitialContext();
    DataSource originalDs = (DataSource) context.lookup(JNDI_NAME);
    //serialize
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    DataSource ds;
    ObjectInput in = null;
    try {
        out = new ObjectOutputStream(bos);
        out.writeObject(originalDs);
        byte[] bytes = bos.toByteArray();
        //deserialize
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        try {
            in = new ObjectInputStream(bis);
            ds = (DataSource) in.readObject();
        } finally {
            try {
                bis.close();
            } catch (IOException ex) {
            // ignore close exception
            }
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
            // ignore close exception
            }
        }
        //use
        Connection conn = ds.getConnection();
        ResultSet rs = conn.prepareStatement("select 1").executeQuery();
        Assert.assertTrue(rs.next());
        conn.close();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException ex) {
        // ignore close exception
        }
        try {
            bos.close();
        } catch (IOException ex) {
        // ignore close exception
        }
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) InitialContext(javax.naming.InitialContext) WildFlyDataSource(org.jboss.as.connector.subsystems.datasources.WildFlyDataSource) DataSource(javax.sql.DataSource) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 22 with ObjectInput

use of java.io.ObjectInput in project gora by apache.

the class IOUtils method readObject.

public static Object readObject(DataInput in) throws ClassNotFoundException, IOException {
    if (in instanceof ObjectInput) {
        return ((ObjectInput) in).readObject();
    } else {
        if (in instanceof InputStream) {
            ObjectInput objIn = new ObjectInputStream((InputStream) in);
            Object obj = objIn.readObject();
            return obj;
        }
    }
    throw new IOException("cannot read from DataInput of instance:" + in.getClass());
}
Also used : ObjectInputStream(java.io.ObjectInputStream) ByteBufferInputStream(org.apache.avro.util.ByteBufferInputStream) InputStream(java.io.InputStream) ObjectInput(java.io.ObjectInput) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 23 with ObjectInput

use of java.io.ObjectInput in project ignite by apache.

the class HadoopSplitWrapperSelfTest method testSerialization.

/**
     * Tests serialization of wrapper and the wrapped native split.
     * @throws Exception If fails.
     */
public void testSerialization() throws Exception {
    FileSplit nativeSplit = new FileSplit(new Path("/path/to/file"), 100, 500, new String[] { "host1", "host2" });
    assertEquals("/path/to/file:100+500", nativeSplit.toString());
    HadoopSplitWrapper split = HadoopUtils.wrapSplit(10, nativeSplit, nativeSplit.getLocations());
    assertEquals("[host1, host2]", Arrays.toString(split.hosts()));
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(buf);
    out.writeObject(split);
    ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()));
    final HadoopSplitWrapper res = (HadoopSplitWrapper) in.readObject();
    assertEquals("/path/to/file:100+500", HadoopUtils.unwrapSplit(res).toString());
    GridTestUtils.assertThrows(log, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            res.hosts();
            return null;
        }
    }, AssertionError.class, null);
}
Also used : Path(org.apache.hadoop.fs.Path) ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileSplit(org.apache.hadoop.mapreduce.lib.input.FileSplit) ObjectOutputStream(java.io.ObjectOutputStream) HadoopSplitWrapper(org.apache.ignite.internal.processors.hadoop.HadoopSplitWrapper) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) ObjectInputStream(java.io.ObjectInputStream)

Example 24 with ObjectInput

use of java.io.ObjectInput in project ignite by apache.

the class KerberosHadoopFileSystemFactorySelfTest method checkSerialization.

/**
     * Serializes the factory,
     *
     * @param fac The facory to check.
     * @throws Exception If failed.
     */
private void checkSerialization(KerberosHadoopFileSystemFactory fac) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutput oo = new ObjectOutputStream(baos);
    oo.writeObject(fac);
    ObjectInput in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    KerberosHadoopFileSystemFactory fac2 = (KerberosHadoopFileSystemFactory) in.readObject();
    assertEquals(fac.getUri(), fac2.getUri());
    Assert.assertArrayEquals(fac.getConfigPaths(), fac2.getConfigPaths());
    assertEquals(fac.getKeyTab(), fac2.getKeyTab());
    assertEquals(fac.getKeyTabPrincipal(), fac2.getKeyTabPrincipal());
    assertEquals(fac.getReloginInterval(), fac2.getReloginInterval());
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectInput(java.io.ObjectInput) ObjectOutputStream(java.io.ObjectOutputStream) KerberosHadoopFileSystemFactory(org.apache.ignite.hadoop.fs.KerberosHadoopFileSystemFactory) ObjectInputStream(java.io.ObjectInputStream)

Example 25 with ObjectInput

use of java.io.ObjectInput in project jackrabbit by apache.

the class AsyncUploadCache method deserializeToBeDeleted.

/**
     * Deserialize {@link #toBeDeleted} from local file system.
     */
private synchronized void deserializeToBeDeleted() throws IOException, ClassNotFoundException {
    // use buffering
    InputStream fis = new FileInputStream(toBeDeletedUploads);
    InputStream buffer = new BufferedInputStream(fis);
    ObjectInput input = new ObjectInputStream(buffer);
    try {
        toBeDeleted = (Set<String>) input.readObject();
    } finally {
        input.close();
        IOUtils.closeQuietly(buffer);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ObjectInput(java.io.ObjectInput) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ObjectInput (java.io.ObjectInput)26 ObjectInputStream (java.io.ObjectInputStream)18 IOException (java.io.IOException)15 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ObjectOutput (java.io.ObjectOutput)10 InputStream (java.io.InputStream)7 ObjectOutputStream (java.io.ObjectOutputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 FileInputStream (java.io.FileInputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 RemoteException (java.rmi.RemoteException)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)3 File (java.io.File)3 MarshalException (java.rmi.MarshalException)3 UnmarshalException (java.rmi.UnmarshalException)3 Test (org.junit.Test)3 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)2 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ServerError (java.rmi.ServerError)2