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;
}
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;
}
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;
}
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));
}
}
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);
}
}
Aggregations