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);
}
}
use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.
the class TreeSetStringSerialiser method deserialise.
@Override
public TreeSet<String> deserialise(final byte[] bytes) throws SerialisationException {
final String str;
try {
str = new String(bytes, CommonConstants.UTF_8);
} catch (UnsupportedEncodingException e) {
throw new SerialisationException(e.getMessage(), e);
}
final TreeSet<String> treeSet = new TreeSet<>();
final Iterable<String> items = Splitter.on(COMMA).omitEmptyStrings().split(str.substring(1, str.length() - 1));
for (final String item : items) {
treeSet.add(item);
}
return treeSet;
}
use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.
the class HyperLogLogPlusSerialiserTest method testSerialiseAndDeserialiseWhenEmpty.
@Test
public void testSerialiseAndDeserialiseWhenEmpty() {
HyperLogLogPlus hyperLogLogPlus = new HyperLogLogPlus(5, 5);
long preSerialisationCardinality = hyperLogLogPlus.cardinality();
byte[] hyperLogLogPlusSerialised;
try {
hyperLogLogPlusSerialised = HYPER_LOG_LOG_PLUS_SERIALISER.serialise(hyperLogLogPlus);
} catch (SerialisationException exception) {
fail("A Serialisation Exception Occurred");
return;
}
HyperLogLogPlus hyperLogLogPlusDeserialised;
try {
hyperLogLogPlusDeserialised = HYPER_LOG_LOG_PLUS_SERIALISER.deserialise(hyperLogLogPlusSerialised);
} catch (SerialisationException exception) {
fail("A Serialisation Exception Occurred");
return;
}
assertEquals(preSerialisationCardinality, hyperLogLogPlusDeserialised.cardinality());
}
Aggregations