Example usage for com.fasterxml.jackson.core.util ByteArrayBuilder toByteArray

List of usage examples for com.fasterxml.jackson.core.util ByteArrayBuilder toByteArray

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core.util ByteArrayBuilder toByteArray.

Prototype

public byte[] toByteArray() 

Source Link

Document

Method called when results are finalized and we can get the full aggregated result buffer to return to the caller

Usage

From source file:org.nuxeo.client.api.marshaller.NuxeoRequestConverterFactory.java

@Override
public RequestBody convert(T value) throws IOException {
    ByteArrayBuilder bb = new ByteArrayBuilder();
    byte[] bytes;
    if (nuxeoMarshaller != null) {
        JsonGenerator jg = objectMapper.getFactory().createGenerator(bb, JsonEncoding.UTF8);
        nuxeoMarshaller.write(jg, value);
        bytes = bb.toByteArray();
    } else {/*from   ww  w.j  a  v  a2 s.  com*/
        bytes = adapter.writeValueAsBytes(value);
    }
    return RequestBody.create(ConstantsV1.APPLICATION_JSON_CHARSET_UTF_8, bytes);
}

From source file:net.logstash.logback.LogstashAbstractFormatter.java

public byte[] writeValueAsBytes(EventType event, Context context) throws IOException {
    ByteArrayBuilder outputStream = new ByteArrayBuilder(getBufferRecycler());

    try {//from w w w.j a v a2 s.  c o  m
        writeValueToOutputStream(event, context, outputStream);
        return outputStream.toByteArray();
    } finally {
        outputStream.release();
    }
}

From source file:net.logstash.logback.composite.CompositeJsonFormatter.java

public byte[] writeEventAsBytes(Event event) throws IOException {
    ByteArrayBuilder outputStream = new ByteArrayBuilder(getBufferRecycler());

    try {/*from ww w  . j a v a  2 s  .  c o m*/
        writeEventToOutputStream(event, outputStream);
        outputStream.flush();
        return outputStream.toByteArray();
    } finally {
        outputStream.release();
    }
}

From source file:gaffer.jsonserialisation.JSONSerialiser.java

/**
 * Serialises an object.//from  w w w  .  ja v a 2 s.  c o  m
 *
 * @param object      the object to be serialised
 * @param prettyPrint true if the object should be serialised with pretty printing
 * @return the provided object serialised (with pretty printing) into bytes
 * @throws SerialisationException if the object fails to serialise
 */
public byte[] serialise(final Object object, final boolean prettyPrint) throws SerialisationException {
    final ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder();
    try {
        serialise(object, JSON_FACTORY.createGenerator(byteArrayBuilder, JsonEncoding.UTF8), prettyPrint);
    } catch (IOException e) {
        throw new SerialisationException(e.getMessage(), e);
    }

    return byteArrayBuilder.toByteArray();
}

From source file:com.bazaarvoice.jackson.rison.RisonParser.java

@Override
public byte[] getBinaryValue(Base64Variant b64variant) throws IOException, JsonParseException {
    if (_currToken != JsonToken.VALUE_STRING
            && (_currToken != JsonToken.VALUE_EMBEDDED_OBJECT || _binaryValue == null)) {
        _reportError("Current token (" + _currToken
                + ") not VALUE_STRING or VALUE_EMBEDDED_OBJECT, can not access as binary");
    }//from  ww  w  . j  av a 2 s .c om
    /* To ensure that we won't see inconsistent data, better clear up
     * state...
     */
    if (_tokenIncomplete) {
        try {
            _binaryValue = _decodeBase64(b64variant);
        } catch (IllegalArgumentException iae) {
            throw _constructError(
                    "Failed to decode VALUE_STRING as base64 (" + b64variant + "): " + iae.getMessage());
        }
        /* let's clear incomplete only now; allows for accessing other
         * textual content in error cases
         */
        _tokenIncomplete = false;
    } else { // may actually require conversion...
        if (_binaryValue == null) {
            ByteArrayBuilder builder = _getByteArrayBuilder();
            _decodeBase64(getText(), builder, b64variant);
            _binaryValue = builder.toByteArray();
        }
    }
    return _binaryValue;
}

From source file:com.bazaarvoice.jackson.rison.RisonParser.java

/**
 * Efficient handling for incremental parsing of base64-encoded
 * textual content./*from   w w w .java  2  s.  com*/
 */
protected byte[] _decodeBase64(Base64Variant b64variant) throws IOException, JsonParseException {
    ByteArrayBuilder builder = _getByteArrayBuilder();

    //main_loop:
    while (true) {
        // first, we'll skip preceding white space, if any
        char ch;
        do {
            if (_inputPtr >= _inputEnd) {
                loadMoreGuaranteed();
            }
            ch = _inputBuffer[_inputPtr++];
        } while (ch <= INT_SPACE);
        int bits = b64variant.decodeBase64Char(ch);
        if (bits < 0) {
            if (ch == INT_APOSTROPHE) { // reached the end, fair and square?
                return builder.toByteArray();
            }
            bits = _decodeBase64Escape2(b64variant, ch, 0);
            if (bits < 0) { // white space to skip
                continue;
            }
        }
        int decodedData = bits;

        // then second base64 char; can't get padding yet, nor ws

        if (_inputPtr >= _inputEnd) {
            loadMoreGuaranteed();
        }
        ch = _inputBuffer[_inputPtr++];
        bits = b64variant.decodeBase64Char(ch);
        if (bits < 0) {
            bits = _decodeBase64Escape2(b64variant, ch, 1);
        }
        decodedData = (decodedData << 6) | bits;

        // third base64 char; can be padding, but not ws
        if (_inputPtr >= _inputEnd) {
            loadMoreGuaranteed();
        }
        ch = _inputBuffer[_inputPtr++];
        bits = b64variant.decodeBase64Char(ch);

        // First branch: can get padding (-> 1 byte)
        if (bits < 0) {
            if (bits != Base64Variant.BASE64_VALUE_PADDING) {
                // as per [JACKSON-631], could also just be 'missing'  padding
                if (ch == INT_APOSTROPHE && !b64variant.usesPadding()) {
                    decodedData >>= 4;
                    builder.append(decodedData);
                    return builder.toByteArray();
                }
                bits = _decodeBase64Escape2(b64variant, ch, 2);
            }
            if (bits == Base64Variant.BASE64_VALUE_PADDING) {
                // Ok, must get more padding chars, then
                if (_inputPtr >= _inputEnd) {
                    loadMoreGuaranteed();
                }
                ch = _inputBuffer[_inputPtr++];
                if (!b64variant.usesPaddingChar(ch)) {
                    throw reportInvalidBase64Char(b64variant, ch, 3,
                            "expected padding character '" + b64variant.getPaddingChar() + "'");
                }
                // Got 12 bits, only need 8, need to shift
                decodedData >>= 4;
                builder.append(decodedData);
                continue;
            }
            // otherwise we got escaped other char, to be processed below
        }
        // Nope, 2 or 3 bytes
        decodedData = (decodedData << 6) | bits;
        // fourth and last base64 char; can be padding, but not ws
        if (_inputPtr >= _inputEnd) {
            loadMoreGuaranteed();
        }
        ch = _inputBuffer[_inputPtr++];
        bits = b64variant.decodeBase64Char(ch);
        if (bits < 0) {
            if (bits != Base64Variant.BASE64_VALUE_PADDING) {
                // as per [JACKSON-631], could also just be 'missing'  padding
                if (ch == INT_APOSTROPHE && !b64variant.usesPadding()) {
                    decodedData >>= 2;
                    builder.appendTwoBytes(decodedData);
                    return builder.toByteArray();
                }
                bits = _decodeBase64Escape2(b64variant, ch, 3);
            }
            if (bits == Base64Variant.BASE64_VALUE_PADDING) {
                // With padding we only get 2 bytes; but we have
                // to shift it a bit so it is identical to triplet
                // case with partial output.
                // 3 chars gives 3x6 == 18 bits, of which 2 are
                // dummies, need to discard:
                decodedData >>= 2;
                builder.appendTwoBytes(decodedData);
                continue;
            }
            // otherwise we got escaped other char, to be processed below
        }
        // otherwise, our triplet is now complete
        decodedData = (decodedData << 6) | bits;
        builder.appendThreeBytes(decodedData);
    }
}

From source file:uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser.java

/**
 * Serialises an object.//from   w w  w .j av a2s  . c o m
 *
 * @param object          the object to be serialised
 * @param prettyPrint     true if the object should be serialised with pretty printing
 * @param fieldsToExclude optional property names to exclude from the json
 * @return the provided object serialised (with pretty printing) into bytes
 * @throws SerialisationException if the object fails to serialise
 */
public byte[] serialise(final Object object, final boolean prettyPrint, final String... fieldsToExclude)
        throws SerialisationException {
    final ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder();
    try {
        serialise(object, JSON_FACTORY.createGenerator(byteArrayBuilder, JsonEncoding.UTF8), prettyPrint,
                fieldsToExclude);
    } catch (final IOException e) {
        throw new SerialisationException(e.getMessage(), e);
    }

    return byteArrayBuilder.toByteArray();
}