Search in sources :

Example 1 with OMemoryInputStream

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

the class OCompositeKeySerializer method fromStream.

public Object fromStream(final byte[] iStream) throws IOException {
    final OCompositeKey compositeKey = new OCompositeKey();
    final OMemoryInputStream inputStream = new OMemoryInputStream(iStream);
    final int keysSize = inputStream.getAsInteger();
    for (int i = 0; i < keysSize; i++) {
        final byte[] keyBytes = inputStream.getAsByteArray();
        final String keyString = new String(keyBytes, "UTF-8");
        final int typeSeparatorPos = keyString.indexOf(',');
        final OType type = OType.valueOf(keyString.substring(0, typeSeparatorPos));
        compositeKey.addKey(ORecordSerializerStringAbstract.simpleValueFromStream(keyString.substring(typeSeparatorPos + 1), type));
    }
    return compositeKey;
}
Also used : OMemoryInputStream(com.orientechnologies.orient.core.serialization.OMemoryInputStream) OType(com.orientechnologies.orient.core.metadata.schema.OType) OCompositeKey(com.orientechnologies.orient.core.index.OCompositeKey)

Example 2 with OMemoryInputStream

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

the class OZIPCompression method uncompress.

@Override
public byte[] uncompress(final byte[] content, final int offset, final int length) {
    try {
        final OMemoryInputStream memoryInputStream = new OMemoryInputStream(content, offset, length);
        // 16KB
        final ZipInputStream gzipInputStream = new ZipInputStream(memoryInputStream);
        try {
            final byte[] buffer = new byte[1024];
            byte[] result = new byte[1024];
            int bytesRead;
            ZipEntry entry = gzipInputStream.getNextEntry();
            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 : ZipInputStream(java.util.zip.ZipInputStream) OMemoryInputStream(com.orientechnologies.orient.core.serialization.OMemoryInputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 3 with OMemoryInputStream

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

the class OChannelBinaryAsynchClient method throwSerializedException.

private void throwSerializedException(final byte[] serializedException) throws IOException {
    final OMemoryInputStream inputStream = new OMemoryInputStream(serializedException);
    final ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
    Object throwable = null;
    try {
        throwable = objectInputStream.readObject();
    } catch (ClassNotFoundException e) {
        OLogManager.instance().error(this, "Error during exception deserialization", e);
        throw new IOException("Error during exception deserialization: " + e.toString());
    }
    objectInputStream.close();
    if (throwable instanceof OException) {
        try {
            final Class<? extends OException> cls = (Class<? extends OException>) throwable.getClass();
            final Constructor<? extends OException> constructor;
            constructor = cls.getConstructor(cls);
            final OException proxyInstance = constructor.newInstance(throwable);
            throw proxyInstance;
        } catch (NoSuchMethodException e) {
            OLogManager.instance().error(this, "Error during exception deserialization", e);
        } catch (InvocationTargetException e) {
            OLogManager.instance().error(this, "Error during exception deserialization", e);
        } catch (InstantiationException e) {
            OLogManager.instance().error(this, "Error during exception deserialization", e);
        } catch (IllegalAccessException e) {
            OLogManager.instance().error(this, "Error during exception deserialization", e);
        }
    }
    if (throwable instanceof Throwable) {
        throw new OResponseProcessingException("Exception during response processing", (Throwable) throwable);
    } else
        // WRAP IT
        OLogManager.instance().error(this, "Error during exception serialization, serialized exception is not Throwable, exception type is " + (throwable != null ? throwable.getClass().getName() : "null"));
}
Also used : OMemoryInputStream(com.orientechnologies.orient.core.serialization.OMemoryInputStream) OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OException(com.orientechnologies.common.exception.OException) OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OResponseProcessingException(com.orientechnologies.orient.enterprise.channel.binary.OResponseProcessingException) ObjectInputStream(java.io.ObjectInputStream)

Example 4 with OMemoryInputStream

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

the class OChannelBinaryClientAbstract method throwSerializedException.

protected void throwSerializedException(final byte[] serializedException) throws IOException {
    final OMemoryInputStream inputStream = new OMemoryInputStream(serializedException);
    final ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
    Object throwable = null;
    try {
        throwable = objectInputStream.readObject();
    } catch (ClassNotFoundException e) {
        OLogManager.instance().error(this, "Error during exception deserialization", e);
        throw new IOException("Error during exception deserialization: " + e.toString());
    }
    objectInputStream.close();
    if (throwable instanceof OException) {
        try {
            final Class<? extends OException> cls = (Class<? extends OException>) throwable.getClass();
            final Constructor<? extends OException> constructor;
            constructor = cls.getConstructor(cls);
            final OException proxyInstance = constructor.newInstance(throwable);
            throw proxyInstance;
        } catch (NoSuchMethodException e) {
            OLogManager.instance().error(this, "Error during exception deserialization", e);
        } catch (InvocationTargetException e) {
            OLogManager.instance().error(this, "Error during exception deserialization", e);
        } catch (InstantiationException e) {
            OLogManager.instance().error(this, "Error during exception deserialization", e);
        } catch (IllegalAccessException e) {
            OLogManager.instance().error(this, "Error during exception deserialization", e);
        }
    }
    if (throwable instanceof Throwable) {
        throw new OResponseProcessingException("Exception during response processing", (Throwable) throwable);
    } else
        // WRAP IT
        OLogManager.instance().error(this, "Error during exception serialization, serialized exception is not Throwable, exception type is " + (throwable != null ? throwable.getClass().getName() : "null"));
}
Also used : OMemoryInputStream(com.orientechnologies.orient.core.serialization.OMemoryInputStream) OException(com.orientechnologies.common.exception.OException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OResponseProcessingException(com.orientechnologies.orient.enterprise.channel.binary.OResponseProcessingException)

Example 5 with OMemoryInputStream

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

the class TrackedMapTest method testMapSerialization.

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

        private static final long serialVersionUID = 1L;

        private void writeObject(ObjectOutputStream oos) throws IOException {
            throw new NotSerializableException();
        }
    }
    final OTrackedMap<String> beforeSerialization = new OTrackedMap<String>(new NotSerializableDocument());
    beforeSerialization.put(0, "firstVal");
    beforeSerialization.put(1, "secondVal");
    final OMemoryStream memoryStream = new OMemoryStream();
    final ObjectOutputStream out = new ObjectOutputStream(memoryStream);
    out.writeObject(beforeSerialization);
    out.close();
    final ObjectInputStream input = new ObjectInputStream(new OMemoryInputStream(memoryStream.copy()));
    @SuppressWarnings("unchecked") final Map<Object, String> afterSerialization = (Map<Object, String>) input.readObject();
    Assert.assertEquals(afterSerialization.size(), beforeSerialization.size(), "Map 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) HashMap(java.util.HashMap) Map(java.util.Map) 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