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
}
}
}
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());
}
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);
}
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());
}
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);
}
}
Aggregations