Search in sources :

Example 1 with SerialisationException

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);
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OperationException(uk.gov.gchq.gaffer.operation.OperationException) URL(java.net.URL) StoreException(uk.gov.gchq.gaffer.store.StoreException)

Example 2 with SerialisationException

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

Example 3 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 4 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 5 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)

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