use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.
the class ReservoirLongsUnionSerialiserTest method testSerialiser.
private void testSerialiser(final ReservoirLongsUnion union) {
final ReservoirLongsSketch result = union.getResult();
final boolean resultIsNull = result == null;
long[] sample = new long[] {};
if (!resultIsNull) {
sample = union.getResult().getSamples();
}
final byte[] unionSerialised;
try {
unionSerialised = SERIALISER.serialise(union);
} catch (final SerialisationException exception) {
fail("A SerialisationException occurred");
return;
}
final ReservoirLongsUnion unionDeserialised;
try {
unionDeserialised = SERIALISER.deserialise(unionSerialised);
} catch (final SerialisationException exception) {
fail("A SerialisationException occurred");
return;
}
final ReservoirLongsSketch deserialisedResult = unionDeserialised.getResult();
if (deserialisedResult == null) {
assertTrue(resultIsNull);
} else {
assertArrayEquals(sample, unionDeserialised.getResult().getSamples());
}
}
use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.
the class UnionSerialiserTest method testSerialiser.
private void testSerialiser(final Union union) {
final double estimate = union.getResult().getEstimate();
final byte[] unionSerialised;
try {
unionSerialised = SERIALISER.serialise(union);
} catch (final SerialisationException exception) {
fail("A SerialisationException occurred");
return;
}
final Union unionDeserialised;
try {
unionDeserialised = SERIALISER.deserialise(unionSerialised);
} catch (final SerialisationException exception) {
fail("A SerialisationException occurred");
return;
}
assertEquals(estimate, unionDeserialised.getResult().getEstimate(), DELTA);
}
use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.
the class RoaringBitmapUtils method upConvertSerialisedForm.
public static byte[] upConvertSerialisedForm(final byte[] serialisedBitmap) throws SerialisationException {
DataInputStream input = new DataInputStream(new ByteArrayInputStream(serialisedBitmap));
int cookie;
try {
cookie = Integer.reverseBytes(input.readInt());
} catch (final IOException e) {
throw new SerialisationException("I failed to read the bitmap version cookie", e);
}
if (cookie == VERSION_ZERO_ONE_FIVE_TO_ZERO_THREE_SEVEN_SERIAL_COOKIE) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
DataOutputStream out = new DataOutputStream(baos);
out.write(VERSION_ZERO_FOUR_ZERO_TO_SIX_THIRTY_FIVE_NO_RUNCONTAINER_COOKIE_BYTES);
int sizeInt = input.readInt();
int size = Integer.reverseBytes(sizeInt);
//Doesn't need to be reversed (already read as reversed)
out.writeInt(sizeInt);
int startOffSet = 4 + 4 + 4 * size + 4 * size;
//Need to extract the cardinalities to calculate the offsets
//Bitmap containers are a fixed size (BITMAP_CONTAINER_SIZE) and are used when the cardinality is greater than 4096
//Array containers are variable size (cardinality * 2)
int[] cardinalities = new int[size];
for (int i = 0; i < size; ++i) {
//Read and write the key (Don't need to use this just copy it across)
out.writeShort(input.readShort());
//Get the cardinality and store it for calculating offsets
short cardShort = input.readShort();
cardinalities[i] = 1 + (0xFFFF & Short.reverseBytes(cardShort));
//Doesn't need reversing (already read as reversed)
out.writeShort(cardShort);
}
int currentOffSet = startOffSet;
for (int i = 0; i < size; ++i) {
out.writeInt(Integer.reverseBytes(currentOffSet));
if (cardinalities[i] > MAX_ARRAY_CONTAINER_SIZE) {
//It's a bitmap container
currentOffSet += BITMAP_CONTAINER_SIZE;
} else {
//It's an array container
currentOffSet += (cardinalities[i] * 2);
}
}
int expectedNumContainerBytes = currentOffSet - startOffSet;
//Write out all the container data
int numContainerBytes = 0;
int b;
while ((b = input.read()) != -1) {
out.write(b);
++numContainerBytes;
}
out.flush();
if (numContainerBytes != expectedNumContainerBytes) {
throw new SerialisationException("I failed to convert roaring bitmap from pre 0.4.0 version");
}
return baos.toByteArray();
} catch (SerialisationException e) {
throw (e);
} catch (final IOException e) {
throw new SerialisationException("IOException: I failed to convert roaring bitmap from pre 0.4.0 version", e);
}
} else if (cookie == VERSION_ZERO_FOUR_ZERO_TO_SIX_THRIRTY_FIVE_NO_RUNCONTAINER_COOKIE || (cookie & 0xFFFF) == VERSION_ZERO_FIVE_ZERO_TO_SIX_THIRTY_FIVE_COOKIE) {
return serialisedBitmap;
} else {
throw new SerialisationException("I failed to find a known roaring bitmap cookie (cookie = " + cookie + ")");
}
}
use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.
the class ProxyStore method executeOpChainViaUrl.
protected <OUTPUT> OUTPUT executeOpChainViaUrl(final OperationChain<OUTPUT> operationChain, final Context context) throws OperationException {
final String opChainJson;
try {
opChainJson = new String(jsonSerialiser.serialise(operationChain), CommonConstants.UTF_8);
} catch (final UnsupportedEncodingException | SerialisationException e) {
throw new OperationException("Unable to serialise operation chain into JSON.", e);
}
final URL url = getProperties().getGafferUrl("graph/doOperation");
try {
return doPost(url, opChainJson, operationChain.getOutputTypeReference(), context);
} catch (final StoreException e) {
throw new OperationException(e.getMessage(), e);
}
}
use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.
the class JavaSerialiser method serialise.
public byte[] serialise(final Object object) throws SerialisationException {
ObjectOutputStream out = null;
ByteArrayOutputStream byteOut = null;
try {
byteOut = new ByteArrayOutputStream();
out = new ObjectOutputStream(byteOut);
out.writeObject(object);
return byteOut.toByteArray();
} catch (final IOException e) {
throw new SerialisationException("Unable to serialise given object of class: " + object.getClass().getName() + ", does it implement the serializable interface?", e);
} finally {
close(out);
close(byteOut);
}
}
Aggregations