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