Search in sources :

Example 6 with OMemoryInputStream

use of com.orientechnologies.orient.core.serialization.OMemoryInputStream in project orientdb by orientechnologies.

the class OGZIPCompression method uncompress.

@Override
public byte[] uncompress(byte[] content, final int offset, final int length) {
    try {
        final OMemoryInputStream memoryInputStream = new OMemoryInputStream(content, offset, length);
        // 16KB
        final GZIPInputStream gzipInputStream = new GZIPInputStream(memoryInputStream, 16384);
        try {
            final byte[] buffer = new byte[1024];
            byte[] result = new byte[1024];
            int bytesRead;
            int len = 0;
            while ((bytesRead = gzipInputStream.read(buffer, 0, buffer.length)) > -1) {
                if (len + bytesRead > result.length) {
                    int newSize = 2 * result.length;
                    if (newSize < len + bytesRead)
                        newSize = Integer.MAX_VALUE;
                    final byte[] oldResult = result;
                    result = new byte[newSize];
                    System.arraycopy(oldResult, 0, result, 0, oldResult.length);
                }
                System.arraycopy(buffer, 0, result, len, bytesRead);
                len += bytesRead;
            }
            return Arrays.copyOf(result, len);
        } finally {
            gzipInputStream.close();
        }
    } catch (IOException ioe) {
        throw new IllegalStateException("Exception during data uncompression", ioe);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) OMemoryInputStream(com.orientechnologies.orient.core.serialization.OMemoryInputStream) IOException(java.io.IOException)

Example 7 with OMemoryInputStream

use of com.orientechnologies.orient.core.serialization.OMemoryInputStream in project orientdb by orientechnologies.

the class TrackedListTest method testSerialization.

/**
   * Test that {@link OTrackedList} is serialised correctly.
   */
@Test
public void testSerialization() throws Exception {
    class NotSerializableDocument extends ODocument {

        private static final long serialVersionUID = 1L;

        private void writeObject(ObjectOutputStream oos) throws IOException {
            throw new NotSerializableException();
        }
    }
    final OTrackedList<String> beforeSerialization = new OTrackedList<String>(new NotSerializableDocument());
    beforeSerialization.add("firstVal");
    beforeSerialization.add("secondVal");
    final OMemoryStream memoryStream = new OMemoryStream();
    ObjectOutputStream out = new ObjectOutputStream(memoryStream);
    out.writeObject(beforeSerialization);
    out.close();
    final ObjectInputStream input = new ObjectInputStream(new OMemoryInputStream(memoryStream.copy()));
    @SuppressWarnings("unchecked") final List<String> afterSerialization = (List<String>) input.readObject();
    Assert.assertEquals(afterSerialization.size(), beforeSerialization.size(), "List size");
    for (int i = 0; i < afterSerialization.size(); i++) {
        Assert.assertEquals(afterSerialization.get(i), beforeSerialization.get(i));
    }
}
Also used : OMemoryInputStream(com.orientechnologies.orient.core.serialization.OMemoryInputStream) OMemoryStream(com.orientechnologies.orient.core.serialization.OMemoryStream) ObjectOutputStream(java.io.ObjectOutputStream) NotSerializableException(java.io.NotSerializableException) ArrayList(java.util.ArrayList) List(java.util.List) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ObjectInputStream(java.io.ObjectInputStream) Test(org.testng.annotations.Test)

Example 8 with OMemoryInputStream

use of com.orientechnologies.orient.core.serialization.OMemoryInputStream in project orientdb by orientechnologies.

the class TrackedSetTest method testSetSerialization.

/**
   * Test that {@link OTrackedSet} is serialised correctly.
   */
@Test
public void testSetSerialization() throws Exception {
    class NotSerializableDocument extends ODocument {

        private static final long serialVersionUID = 1L;

        private void writeObject(ObjectOutputStream oos) throws IOException {
            throw new NotSerializableException();
        }
    }
    final OTrackedSet<String> beforeSerialization = new OTrackedSet<String>(new NotSerializableDocument());
    beforeSerialization.add("firstVal");
    beforeSerialization.add("secondVal");
    final OMemoryStream memoryStream = new OMemoryStream();
    ObjectOutputStream out = new ObjectOutputStream(memoryStream);
    out.writeObject(beforeSerialization);
    out.close();
    final ObjectInputStream input = new ObjectInputStream(new OMemoryInputStream(memoryStream.copy()));
    @SuppressWarnings("unchecked") final Set<String> afterSerialization = (Set<String>) input.readObject();
    Assert.assertEquals(afterSerialization.size(), beforeSerialization.size(), "Set size");
    Assert.assertTrue(beforeSerialization.containsAll(afterSerialization));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) OMemoryInputStream(com.orientechnologies.orient.core.serialization.OMemoryInputStream) OMemoryStream(com.orientechnologies.orient.core.serialization.OMemoryStream) ObjectOutputStream(java.io.ObjectOutputStream) NotSerializableException(java.io.NotSerializableException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ObjectInputStream(java.io.ObjectInputStream) Test(org.testng.annotations.Test)

Aggregations

OMemoryInputStream (com.orientechnologies.orient.core.serialization.OMemoryInputStream)8 ObjectInputStream (java.io.ObjectInputStream)4 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)3 OMemoryStream (com.orientechnologies.orient.core.serialization.OMemoryStream)3 IOException (java.io.IOException)3 NotSerializableException (java.io.NotSerializableException)3 ObjectOutputStream (java.io.ObjectOutputStream)3 Test (org.testng.annotations.Test)3 OException (com.orientechnologies.common.exception.OException)2 OResponseProcessingException (com.orientechnologies.orient.enterprise.channel.binary.OResponseProcessingException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 OIOException (com.orientechnologies.common.io.OIOException)1 OCompositeKey (com.orientechnologies.orient.core.index.OCompositeKey)1 OType (com.orientechnologies.orient.core.metadata.schema.OType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1