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