Search in sources :

Example 86 with ObjectOutput

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
        }
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 87 with ObjectOutput

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();
    }
}
Also used : ObjectOutput(java.io.ObjectOutput) ISQLServerDataSource(com.microsoft.sqlserver.jdbc.ISQLServerDataSource) SQLServerDataSource(com.microsoft.sqlserver.jdbc.SQLServerDataSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) PrintWriter(java.io.PrintWriter) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 88 with ObjectOutput

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();
}
Also used : ObjectOutput(java.io.ObjectOutput) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 89 with ObjectOutput

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;
}
Also used : ObjectOutput(java.io.ObjectOutput) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 90 with ObjectOutput

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));
}
Also used : ObjectOutput(java.io.ObjectOutput) IOException(java.io.IOException) CacheEntry(org.teiid.common.buffer.CacheEntry) CacheKey(org.teiid.common.buffer.CacheKey) Test(org.junit.Test)

Aggregations

ObjectOutput (java.io.ObjectOutput)123 ObjectOutputStream (java.io.ObjectOutputStream)88 ByteArrayOutputStream (java.io.ByteArrayOutputStream)76 IOException (java.io.IOException)63 ObjectInput (java.io.ObjectInput)38 ObjectInputStream (java.io.ObjectInputStream)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26 Test (org.junit.Test)23 Pattern (org.drools.core.rule.Pattern)16 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)15 Consequence (org.drools.core.spi.Consequence)15 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)15 FileOutputStream (java.io.FileOutputStream)13 OutputStream (java.io.OutputStream)12 WorkingMemory (org.drools.core.WorkingMemory)12 ClassObjectType (org.drools.core.base.ClassObjectType)10 Declaration (org.drools.core.rule.Declaration)10 Test (org.junit.jupiter.api.Test)9 File (java.io.File)8 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8