Search in sources :

Example 1 with OMemoryStream

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

the class ORecordBytes method fromInputStream.

/**
   * Reads the input stream in memory. This is less efficient than {@link #fromInputStream(InputStream, int)} because allocation is
   * made multiple times. If you already know the input size use {@link #fromInputStream(InputStream, int)}.
   *
   * @param in
   *          Input Stream, use buffered input stream wrapper to speed up reading
   * @return Buffer read from the stream. It's also the internal buffer size in bytes
   * @throws IOException
   */
public int fromInputStream(final InputStream in) throws IOException {
    final OMemoryStream out = new OMemoryStream();
    try {
        final byte[] buffer = new byte[OMemoryStream.DEF_SIZE];
        int readBytesCount;
        while (true) {
            readBytesCount = in.read(buffer, 0, buffer.length);
            if (readBytesCount == -1) {
                break;
            }
            out.write(buffer, 0, readBytesCount);
        }
        out.flush();
        _source = out.toByteArray();
    } finally {
        out.close();
    }
    _size = _source.length;
    return _size;
}
Also used : OMemoryStream(com.orientechnologies.orient.core.serialization.OMemoryStream)

Example 2 with OMemoryStream

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

the class OCommandRequestTextAbstract method fromStream.

public OSerializableStream fromStream(final byte[] iStream) throws OSerializationException {
    final OMemoryStream buffer = new OMemoryStream(iStream);
    fromStream(buffer);
    return this;
}
Also used : OMemoryStream(com.orientechnologies.orient.core.serialization.OMemoryStream)

Example 3 with OMemoryStream

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

the class OCommandScript method fromStream.

public OSerializableStream fromStream(byte[] iStream) throws OSerializationException {
    final OMemoryStream buffer = new OMemoryStream(iStream);
    language = buffer.getAsString();
    // FIX TO HANDLE USAGE OF EXECUTION MODE STARTING FROM v2.1.3
    final int currPosition = buffer.getPosition();
    final String value = buffer.getAsString();
    try {
        executionMode = OCommandDistributedReplicateRequest.DISTRIBUTED_EXECUTION_MODE.valueOf(value);
    } catch (IllegalArgumentException e) {
        // OLD VERSION: RESET TO THE OLD POSITION
        buffer.setPosition(currPosition);
    }
    fromStream(buffer);
    return this;
}
Also used : OMemoryStream(com.orientechnologies.orient.core.serialization.OMemoryStream)

Example 4 with OMemoryStream

use of com.orientechnologies.orient.core.serialization.OMemoryStream 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)

Example 5 with OMemoryStream

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

the class ONetworkProtocolBinary method serializeExceptionObject.

private void serializeExceptionObject(Throwable original) throws IOException {
    try {
        final ODistributedServerManager srvMgr = server.getDistributedManager();
        if (srvMgr != null)
            original = srvMgr.convertException(original);
        final OMemoryStream memoryStream = new OMemoryStream();
        final ObjectOutputStream objectOutputStream = new ObjectOutputStream(memoryStream);
        objectOutputStream.writeObject(original);
        objectOutputStream.flush();
        final byte[] result = memoryStream.toByteArray();
        objectOutputStream.close();
        channel.writeBytes(result);
    } catch (Exception e) {
        OLogManager.instance().warn(this, "Cannot serialize an exception object", e);
        // Write empty stream for binary compatibility
        channel.writeBytes(OCommonConst.EMPTY_BYTE_ARRAY);
    }
}
Also used : OMemoryStream(com.orientechnologies.orient.core.serialization.OMemoryStream) ObjectOutputStream(java.io.ObjectOutputStream) OLockException(com.orientechnologies.common.concur.lock.OLockException) OException(com.orientechnologies.common.exception.OException) SocketException(java.net.SocketException) OInterruptedException(com.orientechnologies.common.concur.lock.OInterruptedException) OIOException(com.orientechnologies.common.io.OIOException) OOfflineClusterException(com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException) IOException(java.io.IOException)

Aggregations

OMemoryStream (com.orientechnologies.orient.core.serialization.OMemoryStream)14 IOException (java.io.IOException)4 ObjectOutputStream (java.io.ObjectOutputStream)4 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)3 OMemoryInputStream (com.orientechnologies.orient.core.serialization.OMemoryInputStream)3 NotSerializableException (java.io.NotSerializableException)3 ObjectInputStream (java.io.ObjectInputStream)3 Test (org.testng.annotations.Test)3 OInterruptedException (com.orientechnologies.common.concur.lock.OInterruptedException)1 OLockException (com.orientechnologies.common.concur.lock.OLockException)1 OException (com.orientechnologies.common.exception.OException)1 OIOException (com.orientechnologies.common.io.OIOException)1 OOfflineClusterException (com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 SocketException (java.net.SocketException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1