Search in sources :

Example 21 with SerialisationException

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);
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) Iterator(java.util.Iterator) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 22 with SerialisationException

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);
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) IOException(java.io.IOException)

Example 23 with SerialisationException

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);
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) IOException(java.io.IOException)

Example 24 with SerialisationException

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;
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) TreeSet(java.util.TreeSet) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 25 with SerialisationException

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());
}
Also used : HyperLogLogPlus(com.clearspring.analytics.stream.cardinality.HyperLogLogPlus) SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) Test(org.junit.Test)

Aggregations

SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)39 IOException (java.io.IOException)12 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 AccumuloElementConversionException (uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 RangeFactoryException (uk.gov.gchq.gaffer.accumulostore.key.exception.RangeFactoryException)4 SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Properties (uk.gov.gchq.gaffer.data.element.Properties)3 Serialisation (uk.gov.gchq.gaffer.serialisation.Serialisation)3 StoreException (uk.gov.gchq.gaffer.store.StoreException)3 TypeDefinition (uk.gov.gchq.gaffer.store.schema.TypeDefinition)3 HyperLogLogPlus (com.clearspring.analytics.stream.cardinality.HyperLogLogPlus)2 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 Entry (java.util.Map.Entry)2 Key (org.apache.accumulo.core.data.Key)2 Test (org.junit.Test)2 RoaringBitmap (org.roaringbitmap.RoaringBitmap)2 Entity (uk.gov.gchq.gaffer.data.element.Entity)2