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);
}
}
use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.
the class TreeSetStringSerialiser method serialise.
@Override
public byte[] serialise(final TreeSet treeSet) throws SerialisationException {
final StringBuilder builder = new StringBuilder(OPEN);
final Iterator values = treeSet.iterator();
if (values.hasNext()) {
builder.append(values.next());
}
while (values.hasNext()) {
builder.append(COMMA).append(values.next());
}
builder.append(CLOSE);
try {
return builder.toString().getBytes(CommonConstants.UTF_8);
} catch (UnsupportedEncodingException e) {
throw new SerialisationException(e.getMessage(), e);
}
}
use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.
the class CompactRawSerialisationUtils method write.
/**
* Writes a long to the provided {@link OutputStream}.
* NB: This code is very similar to the code in the {@link CompactRawSerialisationUtils#writeLong(long)}
* method. This violates the DRY principle, but the alternative is to implement the code in the
* {@link CompactRawSerialisationUtils#writeLong(long)} method by creating a ByteArrayOutputStream from
* the byte array and then using this method. This approach avoids that expense.
*
* @param l The long to write.
* @param output The {@link OutputStream} to write data to.
* @throws SerialisationException if there is an {@link IOException} writing the long.
*/
public static void write(final long l, final OutputStream output) throws SerialisationException {
try {
long value = l;
if (value >= -112 && value <= 127) {
output.write((byte) value);
return;
}
int len = -112;
if (value < 0) {
// take one's complement'
value ^= -1L;
len = -120;
}
long tmp = value;
while (tmp != 0) {
tmp = tmp >> 8;
len--;
}
output.write((byte) len);
len = (len < -120) ? -(len + 120) : -(len + 112);
for (int idx = len; idx != 0; idx--) {
final int shiftbits = (idx - 1) * 8;
final long mask = 0xFFL << shiftbits;
output.write((byte) ((value & mask) >> shiftbits));
}
} catch (final IOException e) {
throw new SerialisationException("Exception reading bytes", e);
}
}
use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.
the class CompactRawSerialisationUtils method read.
/**
* Reads a long from the provided {@link InputStream}. This requires the long to have been written
* by {@link CompactRawSerialisationUtils#write(long, OutputStream)}.
* NB: This code is very similar to the code in the {@link CompactRawSerialisationUtils#readLong(byte[])}
* method. This violates the DRY principle, but the alternative is to implement the code in the
* {@link CompactRawSerialisationUtils#readLong(byte[])} method by creating a ByteArrayInputStream from
* the byte array and then using this method. This approach avoids that expense.
*
* @param input The {@link InputStream} to read data from.
* @return The value of the serialised long.
* @throws SerialisationException if there is an {@link IOException} converting the data to a long.
*/
public static long read(final InputStream input) throws SerialisationException {
try {
final byte firstByte = (byte) input.read();
final int len = decodeVIntSize(firstByte);
if (len == 1) {
return (long) firstByte;
}
long i = 0;
for (int idx = 0; idx < len - 1; idx++) {
final byte b = (byte) input.read();
i = i << 8;
i = i | (b & 0xFF);
}
return (isNegativeVInt(firstByte) ? (i ^ -1L) : i);
} catch (final IOException e) {
throw new SerialisationException("Exception writing bytes", e);
}
}
Aggregations