com.fasterxml.jackson.core.util.ByteArrayBuilder Java Examples

The following examples show how to use com.fasterxml.jackson.core.util.ByteArrayBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TextNode.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
     * Method for accessing textual contents assuming they were
     * base64 encoded; if so, they are decoded and resulting binary
     * data is returned.
     */
    @SuppressWarnings("resource")
    public byte[] getBinaryValue(Base64Variant b64variant) throws IOException
    {
        final String str = _value.trim();
        ByteArrayBuilder builder = new ByteArrayBuilder(4 + ((str.length() * 3) << 2));
        try {
            b64variant.decode(str, builder);
        } catch (IllegalArgumentException e) {
            throw InvalidFormatException.from(null,
                    String.format(
"Cannot access contents of TextNode as binary due to broken Base64 encoding: %s",
e.getMessage()),
                    str, byte[].class);
        }
        return builder.toByteArray();
    }
 
Example #2
Source File: JsonStringEncoder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private int _appendByte(int ch, int esc, ByteArrayBuilder bb, int ptr)
{
    bb.setCurrentSegmentLength(ptr);
    bb.append('\\');
    if (esc < 0) { // standard escape
        bb.append('u');
        if (ch > 0xFF) {
            int hi = (ch >> 8);
            bb.append(HB[hi >> 4]);
            bb.append(HB[hi & 0xF]);
            ch &= 0xFF;
        } else {
            bb.append('0');
            bb.append('0');
        }
        bb.append(HB[ch >> 4]);
        bb.append(HB[ch & 0xF]);
    } else { // 2-char simple escape
        bb.append((byte) esc);
    }
    return bb.getCurrentSegmentLength();
}
 
Example #3
Source File: JsonStringEncoder.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
private int _appendByte(int ch, int esc, ByteArrayBuilder bb, int ptr)
{
    bb.setCurrentSegmentLength(ptr);
    bb.append('\\');
    if (esc < 0) { // standard escape
        bb.append('u');
        if (ch > 0xFF) {
            int hi = (ch >> 8);
            bb.append(HB[hi >> 4]);
            bb.append(HB[hi & 0xF]);
            ch &= 0xFF;
        } else {
            bb.append('0');
            bb.append('0');
        }
        bb.append(HB[ch >> 4]);
        bb.append(HB[ch & 0xF]);
    } else { // 2-char simple escape
        bb.append((byte) esc);
    }
    return bb.getCurrentSegmentLength();
}
 
Example #4
Source File: JettyReactiveHttpClient.java    From feign-reactive with Apache License 2.0 5 votes vote down vote up
protected ContentChunk toJsonChunk(Object data, boolean stream){
	try {
		ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder();
		bodyWriter.writeValue(byteArrayBuilder, data);
		if(stream) {
			byteArrayBuilder.write(NEWLINE_SEPARATOR);
		}
		ByteBuffer buffer = ByteBuffer.wrap(byteArrayBuilder.toByteArray());
		return new ContentChunk(buffer);
	} catch (java.io.IOException e) {
		throw new UncheckedIOException(e);
	}
}
 
Example #5
Source File: TokenBuffer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("resource")
public byte[] getBinaryValue(Base64Variant b64variant) throws IOException, JsonParseException
{
    // First: maybe we some special types?
    if (_currToken == JsonToken.VALUE_EMBEDDED_OBJECT) {
        // Embedded byte array would work nicely...
        Object ob = _currentObject();
        if (ob instanceof byte[]) {
            return (byte[]) ob;
        }
        // fall through to error case
    }
    if (_currToken != JsonToken.VALUE_STRING) {
        throw _constructError("Current token ("+_currToken+") not VALUE_STRING (or VALUE_EMBEDDED_OBJECT with byte[]), cannot access as binary");
    }
    final String str = getText();
    if (str == null) {
        return null;
    }
    ByteArrayBuilder builder = _byteBuilder;
    if (builder == null) {
        _byteBuilder = builder = new ByteArrayBuilder(100);
    } else {
        _byteBuilder.reset();
    }
    _decodeBase64(str, builder, b64variant);
    return builder.toByteArray();
}
 
Example #6
Source File: ParserBase.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("resource")
@Override // since 2.7
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;
}
 
Example #7
Source File: ParserMinimalBase.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Helper method that can be used for base64 decoding in cases where
 * encoded content has already been read as a String.
 */
protected void _decodeBase64(String str, ByteArrayBuilder builder, Base64Variant b64variant) throws IOException
{
    try {
        b64variant.decode(str, builder);
    } catch (IllegalArgumentException e) {
        _reportError(e.getMessage());
    }
}
 
Example #8
Source File: JSONComposer.java    From jackson-jr with Apache License 2.0 5 votes vote down vote up
protected JSONComposer(int features, JsonGenerator gen, ByteArrayBuilder w)
{
    super(gen);
    _features = features;
    _stringWriter = null;
    _byteWriter = w;
    _closeGenerator = true;
}
 
Example #9
Source File: NonBlockingJsonParserBase.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public byte[] getBinaryValue(Base64Variant b64variant) throws IOException
{
    if (_currToken != JsonToken.VALUE_STRING) {
        _reportError("Current token (%s) not VALUE_STRING or VALUE_EMBEDDED_OBJECT, can not access as binary",
                _currToken);
    }
    if (_binaryValue == null) {
        @SuppressWarnings("resource")
        ByteArrayBuilder builder = _getByteArrayBuilder();
        _decodeBase64(getText(), builder, b64variant);
        _binaryValue = builder.toByteArray();
    }
    return _binaryValue;
}
 
Example #10
Source File: MessageMarshaller.java    From curiostack with MIT License 5 votes vote down vote up
/**
 * Converts a {@link Message} into JSON as UTF-8 encoded bytes.
 *
 * @throws InvalidProtocolBufferException if there are unknown Any types in the message.
 */
public <T extends Message> byte[] writeValueAsBytes(T message) throws IOException {
  checkNotNull(message, "message");
  ByteArrayBuilder builder = new ByteArrayBuilder(jsonFactory._getBufferRecycler());
  try (JsonGenerator gen = jsonFactory.createGenerator(builder)) {
    writeValue(message, gen);
  }
  return builder.toByteArray();
}
 
Example #11
Source File: ParserMinimalBase.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Helper method that can be used for base64 decoding in cases where
 * encoded content has already been read as a String.
 */
protected void _decodeBase64(String str, ByteArrayBuilder builder, Base64Variant b64variant) throws IOException
{
    // just call helper method introduced in 2.2.3
    try {
        b64variant.decode(str, builder);
    } catch (IllegalArgumentException e) {
        _reportError(e.getMessage());
    }
}
 
Example #12
Source File: ContextualStoredAsJsonSerializer.java    From Rosetta with Apache License 2.0 5 votes vote down vote up
private byte[] serializeToBytes(T value, ObjectMapper mapper, SerializerProvider provider) throws IOException {
  try (ByteArrayBuilder array = new ByteArrayBuilder(new BufferRecycler())) {
    if (trySerialzieToArray(value, mapper, provider, array)) {
      byte[] result = array.toByteArray();
      array.release();
      return result;
    }
  }

  // fallback on old behavior
  return mapper.writeValueAsBytes(value);
}
 
Example #13
Source File: JSON.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
public JSONComposer<byte[]> composeBytes() throws IOException, JSONObjectException {
    ByteArrayBuilder out = new ByteArrayBuilder(_streamFactory._getBufferRecycler());
    JsonGenerator gen = _config(_streamFactory.createGenerator(this, out));
    return JSONComposer.bytesComposer(_features, gen, out);
}
 
Example #14
Source File: JSONComposer.java    From jackson-jr with Apache License 2.0 4 votes vote down vote up
public static JSONComposer<byte[]> bytesComposer(int features,
        JsonGenerator gen, ByteArrayBuilder w) {
    return new JSONComposer<byte[]>(features, gen, w);
}
 
Example #15
Source File: ContextualStoredAsJsonSerializer.java    From Rosetta with Apache License 2.0 4 votes vote down vote up
private boolean trySerialzieToArray(T value, ObjectMapper mapper, SerializerProvider provider, ByteArrayBuilder builder) throws IOException {
  try (JsonGenerator gen = mapper.getFactory().createGenerator(builder, JsonEncoding.UTF8)) {
    return trySerializeToGenerator(value, mapper, provider, gen);
  }
}
 
Example #16
Source File: JsonStringEncoder.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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 { // 3 or 4 bytes
            // 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);
}
 
Example #17
Source File: Base64Variant.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Convenience method for decoding contents of a Base64-encoded String,
 * using this variant's settings
 * and appending decoded binary data using provided {@link ByteArrayBuilder}.
 *<p>
 * NOTE: builder will NOT be reset before decoding (nor cleared afterwards);
 * assumption is that caller will ensure it is given in proper state, and
 * used as appropriate afterwards.
 * 
 * @since 2.2.3
 *
 * @throws IllegalArgumentException if input is not valid base64 encoded data
 */
public void decode(String str, ByteArrayBuilder builder) throws IllegalArgumentException
{
    int ptr = 0;
    int len = str.length();
    
    main_loop:
    while (ptr < len) {
        // first, we'll skip preceding white space, if any
        char ch;
        do {
            ch = str.charAt(ptr++);
            if (ptr >= len) {
                break main_loop;
            }
        } while (ch <= INT_SPACE);
        int bits = decodeBase64Char(ch);
        if (bits < 0) {
            _reportInvalidBase64(ch, 0, null);
        }
        int decodedData = bits;
        // then second base64 char; can't get padding yet, nor ws
        if (ptr >= len) {
            _reportBase64EOF();
        }
        ch = str.charAt(ptr++);
        bits = decodeBase64Char(ch);
        if (bits < 0) {
            _reportInvalidBase64(ch, 1, null);
        }
        decodedData = (decodedData << 6) | bits;
        // third base64 char; can be padding, but not ws
        if (ptr >= len) {
            // but as per [JACKSON-631] can be end-of-input, iff not using padding
            if (!usesPadding()) {
                decodedData >>= 4;
                builder.append(decodedData);
                break;
            }
            _reportBase64EOF();
        }
        ch = str.charAt(ptr++);
        bits = decodeBase64Char(ch);
        
        // First branch: can get padding (-> 1 byte)
        if (bits < 0) {
            if (bits != Base64Variant.BASE64_VALUE_PADDING) {
                _reportInvalidBase64(ch, 2, null);
            }
            // Ok, must get padding
            if (ptr >= len) {
                _reportBase64EOF();
            }
            ch = str.charAt(ptr++);
            if (!usesPaddingChar(ch)) {
                _reportInvalidBase64(ch, 3, "expected padding character '"+getPaddingChar()+"'");
            }
            // Got 12 bits, only need 8, need to shift
            decodedData >>= 4;
            builder.append(decodedData);
            continue;
        }
        // Nope, 2 or 3 bytes
        decodedData = (decodedData << 6) | bits;
        // fourth and last base64 char; can be padding, but not ws
        if (ptr >= len) {
            // but as per [JACKSON-631] can be end-of-input, iff not using padding
            if (!usesPadding()) {
                decodedData >>= 2;
                builder.appendTwoBytes(decodedData);
                break;
            }
            _reportBase64EOF();
        }
        ch = str.charAt(ptr++);
        bits = decodeBase64Char(ch);
        if (bits < 0) {
            if (bits != Base64Variant.BASE64_VALUE_PADDING) {
                _reportInvalidBase64(ch, 3, null);
            }
            decodedData >>= 2;
            builder.appendTwoBytes(decodedData);
        } else {
            // otherwise, our triple is now complete
            decodedData = (decodedData << 6) | bits;
            builder.appendThreeBytes(decodedData);
        }
    }
}
 
Example #18
Source File: JsonStringEncoder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 { // 3 or 4 bytes
            // 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);
}
 
Example #19
Source File: Base64Variant.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Convenience method for decoding contents of a Base64-encoded String,
 * using this variant's settings
 * and appending decoded binary data using provided {@link ByteArrayBuilder}.
 *<p>
 * NOTE: builder will NOT be reset before decoding (nor cleared afterwards);
 * assumption is that caller will ensure it is given in proper state, and
 * used as appropriate afterwards.
 * 
 * @since 2.3
 *
 * @throws IllegalArgumentException if input is not valid base64 encoded data
 */
public void decode(String str, ByteArrayBuilder builder) throws IllegalArgumentException
{
    int ptr = 0;
    int len = str.length();

main_loop:
    while (true) {
        // first, we'll skip preceding white space, if any
        char ch;
        do {
            if (ptr >= len) {
                break main_loop;
            }
            ch = str.charAt(ptr++);
        } while (ch <= INT_SPACE);
        int bits = decodeBase64Char(ch);
        if (bits < 0) {
            _reportInvalidBase64(ch, 0, null);
        }
        int decodedData = bits;
        // then second base64 char; can't get padding yet, nor ws
        if (ptr >= len) {
            _reportBase64EOF();
        }
        ch = str.charAt(ptr++);
        bits = decodeBase64Char(ch);
        if (bits < 0) {
            _reportInvalidBase64(ch, 1, null);
        }
        decodedData = (decodedData << 6) | bits;
        // third base64 char; can be padding, but not ws
        if (ptr >= len) {
            // but as per [JACKSON-631] can be end-of-input, iff not using padding
            if (!usesPadding()) {
                decodedData >>= 4;
                builder.append(decodedData);
                break;
            }
            _reportBase64EOF();
        }
        ch = str.charAt(ptr++);
        bits = decodeBase64Char(ch);
        
        // First branch: can get padding (-> 1 byte)
        if (bits < 0) {
            if (bits != Base64Variant.BASE64_VALUE_PADDING) {
                _reportInvalidBase64(ch, 2, null);
            }
            // Ok, must get padding
            if (ptr >= len) {
                _reportBase64EOF();
            }
            ch = str.charAt(ptr++);
            if (!usesPaddingChar(ch)) {
                _reportInvalidBase64(ch, 3, "expected padding character '"+getPaddingChar()+"'");
            }
            // Got 12 bits, only need 8, need to shift
            decodedData >>= 4;
            builder.append(decodedData);
            continue;
        }
        // Nope, 2 or 3 bytes
        decodedData = (decodedData << 6) | bits;
        // fourth and last base64 char; can be padding, but not ws
        if (ptr >= len) {
            // but as per [JACKSON-631] can be end-of-input, iff not using padding
            if (!usesPadding()) {
                decodedData >>= 2;
                builder.appendTwoBytes(decodedData);
                break;
            }
            _reportBase64EOF();
        }
        ch = str.charAt(ptr++);
        bits = decodeBase64Char(ch);
        if (bits < 0) {
            if (bits != Base64Variant.BASE64_VALUE_PADDING) {
                _reportInvalidBase64(ch, 3, null);
            }
            decodedData >>= 2;
            builder.appendTwoBytes(decodedData);
        } else {
            // otherwise, our triple is now complete
            decodedData = (decodedData << 6) | bits;
            builder.appendThreeBytes(decodedData);
        }
    }
}
 
Example #20
Source File: LoadFlowParameters.java    From powsybl-core with Mozilla Public License 2.0 4 votes vote down vote up
private byte[] writeInMemory() {
    try (ByteArrayBuilder byteArrayBuilder = new ByteArrayBuilder()) {
        JsonLoadFlowParameters.write(this, byteArrayBuilder);
        return byteArrayBuilder.toByteArray();
    }
}
 
Example #21
Source File: Base64Variant.java    From openbd-core with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 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();
}
 
Example #22
Source File: Base64Variant.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * 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();
}