use of java.io.ObjectOutput in project compss by bsc-wdc.
the class Serializer method serializeBinary.
/**
* Serializes an objects using the default java serializer
*
* @param o
* object to be serialized
* @throws IOException
* Error writing the byte stream
*/
private static byte[] serializeBinary(Object o) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(o);
return bos.toByteArray();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException ex) {
// No need to handle such exception
}
try {
bos.close();
} catch (IOException ex) {
// No need to handle such exception
}
}
}
use of java.io.ObjectOutput in project mssql-jdbc by Microsoft.
the class NativeMSSQLDataSourceTest method testSerialization.
@Test
public void testSerialization() throws IOException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutput objectOutput = new ObjectOutputStream(outputStream)) {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setLogWriter(new PrintWriter(new ByteArrayOutputStream()));
objectOutput.writeObject(ds);
objectOutput.flush();
}
}
use of java.io.ObjectOutput in project candlepin by candlepin.
the class EntitlerJobTest method serialize.
private void serialize(Object obj) throws IOException {
ObjectOutput out = new ObjectOutputStream(new FileOutputStream("obj.ser"));
out.writeObject(obj);
out.close();
}
use of java.io.ObjectOutput in project teiid by teiid.
the class BufferFrontedFileStoreCache method add.
@SuppressWarnings("unchecked")
@Override
public boolean add(CacheEntry entry, Serializer s) {
if (LogManager.isMessageToBeRecorded(LogConstants.CTX_BUFFER_MGR, MessageLevel.DETAIL)) {
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_BUFFER_MGR, "adding object", s.getId(), entry.getId());
}
boolean newEntry = false;
InodeBlockManager blockManager = null;
boolean hasPermit = false;
PhysicalInfo info = null;
boolean success = false;
int memoryBlocks = this.maxMemoryBlocks;
try {
Map<Long, PhysicalInfo> map = physicalMapping.get(s.getId());
if (map == null) {
// already removed
return true;
}
info = map.get(entry.getId());
if (info == null) {
synchronized (map) {
info = map.get(entry.getId());
if (info == null) {
newEntry = true;
if (!map.containsKey(entry.getId())) {
// already removed
return true;
}
info = new PhysicalInfo(s.getId(), entry.getId(), EMPTY_ADDRESS, readAttempts.get(), entry.getSizeEstimate());
info.adding = true;
map.put(entry.getId(), info);
}
}
}
if (!newEntry) {
synchronized (info) {
if (info.adding) {
// someone else is responsible for adding this cache entry
return false;
}
if (!shouldPlaceInMemoryBuffer(info)) {
// safe to remove from tier 1
return true;
}
info.adding = true;
// second chance re-add to the cache, we assume that serialization would be faster than a disk read
if (info.memoryBlockCount != 0) {
memoryBlocks = info.memoryBlockCount;
}
}
}
checkForLowMemory();
memoryWritePermits.acquire(memoryBlocks);
hasPermit = true;
blockManager = getBlockManager(s.getId(), entry.getId(), EMPTY_ADDRESS);
BlockOutputStream bos = new BlockOutputStream(blockManager, memoryBlocks);
bos.writeLong(s.getId());
bos.writeLong(entry.getId());
ObjectOutput dos = new ObjectOutputStream(bos);
s.serialize(entry.getObject(), dos);
dos.close();
// synchronized to ensure proper cleanup from a concurrent removal
synchronized (map) {
if (physicalMapping.containsKey(s.getId()) && map.containsKey(entry.getId())) {
synchronized (info) {
// set the size first, since it may raise an exceptional condition
info.setSize(bos.getBytesWritten());
info.inode = blockManager.getInode();
memoryBufferEntries.add(info);
}
success = true;
}
}
} catch (Throwable e) {
if (e == PhysicalInfo.sizeChanged) {
// entries are mutable after adding, the original should be removed shortly so just ignore
// $NON-NLS-1$ //$NON-NLS-2$
LogManager.logDetail(LogConstants.CTX_BUFFER_MGR, "Object ", entry.getId(), " changed size since first persistence, keeping the original.");
} else if (e == BlockOutputStream.exceededMax) {
final long[] size = new long[1];
try {
ObjectOutput dos = new ObjectOutputStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
size[0]++;
}
});
s.serialize(entry.getObject(), dos);
} catch (IOException e1) {
}
if (!newEntry && memoryBlocks < maxMemoryBlocks) {
// entries are mutable after adding, the original should be removed shortly so just ignore
// $NON-NLS-1$ //$NON-NLS-2$
LogManager.logDetail(LogConstants.CTX_BUFFER_MGR, "Object ", entry.getId(), " changed size since first persistence, keeping the original.");
} else {
LogManager.logError(LogConstants.CTX_BUFFER_MGR, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30001, entry.getId(), s.getId(), entry.getSizeEstimate(), size[0], s.describe(entry.getObject())));
}
} else {
LogManager.logError(LogConstants.CTX_BUFFER_MGR, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30002, s.getId(), entry.getId()));
}
} finally {
if (hasPermit) {
memoryWritePermits.release(memoryBlocks);
}
if (info != null) {
synchronized (info) {
info.adding = false;
if (!success && blockManager != null) {
// invalidate for safety
info.inode = EMPTY_ADDRESS;
}
}
}
if (!success && blockManager != null) {
blockManager.free(false);
}
}
return true;
}
use of java.io.ObjectOutput in project teiid by teiid.
the class TestBufferFrontedFileStoreCache method testMultipleAdds.
@Test
public void testMultipleAdds() throws Exception {
cache = createLayeredCache(1 << 18, 1 << 18, true, true);
Serializer<Integer> s = new SimpleSerializer() {
@Override
public void serialize(Integer obj, ObjectOutput oos) throws IOException {
throw new IOException();
}
};
CacheEntry ce = new CacheEntry(new CacheKey(31l, 0, 0), 1000000, null, null, false);
cache.createCacheGroup(s.getId());
Integer cacheObject = Integer.valueOf(50000);
ce.setObject(cacheObject);
cache.addToCacheGroup(s.getId(), ce.getId());
assertTrue(cache.add(ce, s));
s = new SimpleSerializer();
assertTrue(cache.add(ce, s));
assertNotNull(get(cache, ce.getId(), s));
}
Aggregations