Search in sources :

Example 1 with GridByteArrayOutputStream

use of org.apache.ignite.internal.util.io.GridByteArrayOutputStream in project ignite by apache.

the class JdkMarshaller method marshal0.

/**
 * {@inheritDoc}
 */
@Override
protected byte[] marshal0(@Nullable Object obj) throws IgniteCheckedException {
    GridByteArrayOutputStream out = null;
    try {
        out = new GridByteArrayOutputStream(DFLT_BUFFER_SIZE);
        marshal(obj, out);
        return out.toByteArray();
    } finally {
        U.close(out, null);
    }
}
Also used : GridByteArrayOutputStream(org.apache.ignite.internal.util.io.GridByteArrayOutputStream)

Example 2 with GridByteArrayOutputStream

use of org.apache.ignite.internal.util.io.GridByteArrayOutputStream in project ignite by apache.

the class GridClientJdkMarshaller method marshal.

/**
 * {@inheritDoc}
 */
@Override
public ByteBuffer marshal(Object obj, int off) throws IOException {
    GridByteArrayOutputStream bOut = new GridByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(bOut);
    out.writeObject(obj);
    out.flush();
    ByteBuffer buf = ByteBuffer.allocate(off + bOut.size());
    buf.position(off);
    buf.put(bOut.internalArray(), 0, bOut.size());
    buf.flip();
    return buf;
}
Also used : ObjectOutput(java.io.ObjectOutput) GridByteArrayOutputStream(org.apache.ignite.internal.util.io.GridByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ByteBuffer(java.nio.ByteBuffer)

Example 3 with GridByteArrayOutputStream

use of org.apache.ignite.internal.util.io.GridByteArrayOutputStream in project ignite by apache.

the class GridCacheReplicatedQueueRemoveSelfTest method testQueueRemovalProcessor.

/**
 * This a unit test of Ignite queue's RemoveProcessor process() method.
 *
 * @throws Exception If failed.
 */
@Test
@SuppressWarnings("unchecked")
public void testQueueRemovalProcessor() throws Exception {
    GridCacheContext cctx = grid(0).context().cache().cache("ignite-sys-cache").context();
    IgniteUuid id = IgniteUuid.randomUuid();
    CacheInvokeEntry entry = new CacheInvokeEntry<>(null, null, null, false, new GridDhtCacheEntry(cctx, null, new KeyCacheObjectImpl(1, BigInteger.valueOf(1).toByteArray(), 1)));
    entry.setValue(new GridCacheQueueHeader(id, 2147483647, false, 0L, 10000L, Collections.singleton(1L)));
    GridCacheQueueAdapter.RemoveProcessor rp = new GridCacheQueueAdapter.RemoveProcessor(id, 1L);
    rp.process(entry);
    GridCacheQueueAdapter.RemoveProcessor externalRP = new GridCacheQueueAdapter.RemoveProcessor();
    GridByteArrayOutputStream output = new GridByteArrayOutputStream();
    rp.writeExternal(new ObjectOutputStream(output));
    externalRP.readExternal(new ObjectInputStream(new GridByteArrayInputStream(output.toByteArray())));
    assertEquals(id, GridTestUtils.getFieldValue(externalRP, "id"));
    // idx should be null, cause entry was already removed, see GridCacheQueueAdapter.RemoveProcessor.code
    // for more details.
    assertNull(GridTestUtils.getFieldValue(externalRP, "idx"));
}
Also used : GridCacheContext(org.apache.ignite.internal.processors.cache.GridCacheContext) GridByteArrayOutputStream(org.apache.ignite.internal.util.io.GridByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) GridDhtCacheEntry(org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry) CacheInvokeEntry(org.apache.ignite.internal.processors.cache.CacheInvokeEntry) GridByteArrayInputStream(org.apache.ignite.internal.util.io.GridByteArrayInputStream) IgniteUuid(org.apache.ignite.lang.IgniteUuid) KeyCacheObjectImpl(org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl) ObjectInputStream(java.io.ObjectInputStream) IgniteCollectionAbstractTest(org.apache.ignite.internal.processors.cache.datastructures.IgniteCollectionAbstractTest) Test(org.junit.Test)

Example 4 with GridByteArrayOutputStream

use of org.apache.ignite.internal.util.io.GridByteArrayOutputStream in project ignite by apache.

the class ZookeeperDiscoveryImpl method zip.

/**
 * @param bytes Bytes to compress.
 * @return Zip-compressed bytes.
 */
private static byte[] zip(byte[] bytes) {
    Deflater deflater = new Deflater();
    deflater.setInput(bytes);
    deflater.finish();
    GridByteArrayOutputStream out = new GridByteArrayOutputStream(bytes.length);
    final byte[] buf = new byte[bytes.length];
    while (!deflater.finished()) {
        int cnt = deflater.deflate(buf);
        out.write(buf, 0, cnt);
    }
    return out.toByteArray();
}
Also used : Deflater(java.util.zip.Deflater) GridByteArrayOutputStream(org.apache.ignite.internal.util.io.GridByteArrayOutputStream)

Example 5 with GridByteArrayOutputStream

use of org.apache.ignite.internal.util.io.GridByteArrayOutputStream in project ignite by apache.

the class ZookeeperDiscoveryImpl method unzip.

/**
 * @param zipBytes Zip-compressed bytes.
 * @return Uncompressed bytes.
 * @throws DataFormatException If compressed data format is invalid.
 */
public static byte[] unzip(byte[] zipBytes) throws DataFormatException {
    Inflater inflater = new Inflater();
    inflater.setInput(zipBytes);
    GridByteArrayOutputStream out = new GridByteArrayOutputStream(zipBytes.length * 2);
    final byte[] buf = new byte[zipBytes.length];
    while (!inflater.finished()) {
        int cnt = inflater.inflate(buf);
        out.write(buf, 0, cnt);
    }
    return out.toByteArray();
}
Also used : GridByteArrayOutputStream(org.apache.ignite.internal.util.io.GridByteArrayOutputStream) Inflater(java.util.zip.Inflater)

Aggregations

GridByteArrayOutputStream (org.apache.ignite.internal.util.io.GridByteArrayOutputStream)5 ObjectOutputStream (java.io.ObjectOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutput (java.io.ObjectOutput)1 ByteBuffer (java.nio.ByteBuffer)1 Deflater (java.util.zip.Deflater)1 Inflater (java.util.zip.Inflater)1 CacheInvokeEntry (org.apache.ignite.internal.processors.cache.CacheInvokeEntry)1 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)1 KeyCacheObjectImpl (org.apache.ignite.internal.processors.cache.KeyCacheObjectImpl)1 IgniteCollectionAbstractTest (org.apache.ignite.internal.processors.cache.datastructures.IgniteCollectionAbstractTest)1 GridDhtCacheEntry (org.apache.ignite.internal.processors.cache.distributed.dht.GridDhtCacheEntry)1 GridByteArrayInputStream (org.apache.ignite.internal.util.io.GridByteArrayInputStream)1 IgniteUuid (org.apache.ignite.lang.IgniteUuid)1 Test (org.junit.Test)1