Search in sources :

Example 6 with ByteArrayBuilder

use of com.fasterxml.jackson.core.util.ByteArrayBuilder in project jackson-core by FasterXML.

the class Base64Variant method decode.

/**
     * Convenience method for decoding contents of a Base64-encoded String,
     * using this variant's settings.
     * 
     * @param input
     * 
     * @since 2.2.3
     *
     * @throws IllegalArgumentException if input is not valid base64 encoded data
     */
@SuppressWarnings("resource")
public byte[] decode(String input) throws IllegalArgumentException {
    ByteArrayBuilder b = new ByteArrayBuilder();
    decode(input, b);
    return b.toByteArray();
}
Also used : ByteArrayBuilder(com.fasterxml.jackson.core.util.ByteArrayBuilder)

Example 7 with ByteArrayBuilder

use of com.fasterxml.jackson.core.util.ByteArrayBuilder in project jackson-core by FasterXML.

the class ParserBase method getBinaryValue.

@SuppressWarnings("resource")
// since 2.7
@Override
public byte[] getBinaryValue(Base64Variant variant) throws IOException {
    if (_binaryValue == null) {
        if (_currToken != JsonToken.VALUE_STRING) {
            _reportError("Current token (" + _currToken + ") not VALUE_STRING, can not access as binary");
        }
        ByteArrayBuilder builder = _getByteArrayBuilder();
        _decodeBase64(getText(), builder, variant);
        _binaryValue = builder.toByteArray();
    }
    return _binaryValue;
}
Also used : ByteArrayBuilder(com.fasterxml.jackson.core.util.ByteArrayBuilder)

Example 8 with ByteArrayBuilder

use of com.fasterxml.jackson.core.util.ByteArrayBuilder in project jackson-core by FasterXML.

the class JsonStringEncoder method encodeAsUTF8.

/**
     * Will encode given String as UTF-8 (without any quoting), return
     * resulting byte array.
     */
@SuppressWarnings("resource")
public byte[] encodeAsUTF8(String text) {
    ByteArrayBuilder byteBuilder = _bytes;
    if (byteBuilder == null) {
        // no allocator; can add if we must, shouldn't need to
        _bytes = byteBuilder = new ByteArrayBuilder(null);
    }
    int inputPtr = 0;
    int inputEnd = text.length();
    int outputPtr = 0;
    byte[] outputBuffer = byteBuilder.resetAndGetFirstSegment();
    int outputEnd = outputBuffer.length;
    main_loop: while (inputPtr < inputEnd) {
        int c = text.charAt(inputPtr++);
        // first tight loop for ascii
        while (c <= 0x7F) {
            if (outputPtr >= outputEnd) {
                outputBuffer = byteBuilder.finishCurrentSegment();
                outputEnd = outputBuffer.length;
                outputPtr = 0;
            }
            outputBuffer[outputPtr++] = (byte) c;
            if (inputPtr >= inputEnd) {
                break main_loop;
            }
            c = text.charAt(inputPtr++);
        }
        // then multi-byte...
        if (outputPtr >= outputEnd) {
            outputBuffer = byteBuilder.finishCurrentSegment();
            outputEnd = outputBuffer.length;
            outputPtr = 0;
        }
        if (c < 0x800) {
            // 2-byte
            outputBuffer[outputPtr++] = (byte) (0xc0 | (c >> 6));
        } else {
            // Surrogates?
            if (c < SURR1_FIRST || c > SURR2_LAST) {
                // nope
                outputBuffer[outputPtr++] = (byte) (0xe0 | (c >> 12));
                if (outputPtr >= outputEnd) {
                    outputBuffer = byteBuilder.finishCurrentSegment();
                    outputEnd = outputBuffer.length;
                    outputPtr = 0;
                }
                outputBuffer[outputPtr++] = (byte) (0x80 | ((c >> 6) & 0x3f));
            } else {
                // yes, surrogate pair
                if (c > SURR1_LAST) {
                    // must be from first range
                    _illegal(c);
                }
                // and if so, followed by another from next range
                if (inputPtr >= inputEnd) {
                    _illegal(c);
                }
                c = _convert(c, text.charAt(inputPtr++));
                if (c > 0x10FFFF) {
                    // illegal, as per RFC 4627
                    _illegal(c);
                }
                outputBuffer[outputPtr++] = (byte) (0xf0 | (c >> 18));
                if (outputPtr >= outputEnd) {
                    outputBuffer = byteBuilder.finishCurrentSegment();
                    outputEnd = outputBuffer.length;
                    outputPtr = 0;
                }
                outputBuffer[outputPtr++] = (byte) (0x80 | ((c >> 12) & 0x3f));
                if (outputPtr >= outputEnd) {
                    outputBuffer = byteBuilder.finishCurrentSegment();
                    outputEnd = outputBuffer.length;
                    outputPtr = 0;
                }
                outputBuffer[outputPtr++] = (byte) (0x80 | ((c >> 6) & 0x3f));
            }
        }
        if (outputPtr >= outputEnd) {
            outputBuffer = byteBuilder.finishCurrentSegment();
            outputEnd = outputBuffer.length;
            outputPtr = 0;
        }
        outputBuffer[outputPtr++] = (byte) (0x80 | (c & 0x3f));
    }
    return _bytes.completeAndCoalesce(outputPtr);
}
Also used : ByteArrayBuilder(com.fasterxml.jackson.core.util.ByteArrayBuilder)

Aggregations

ByteArrayBuilder (com.fasterxml.jackson.core.util.ByteArrayBuilder)8 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 JsonParser (com.fasterxml.jackson.core.JsonParser)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Test (org.junit.Test)3 RoaringBitmap (org.roaringbitmap.RoaringBitmap)3