Java Code Examples for com.fasterxml.jackson.core.util.ByteArrayBuilder#toByteArray()

The following examples show how to use com.fasterxml.jackson.core.util.ByteArrayBuilder#toByteArray() . 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: 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 3
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 4
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 5
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 6
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();
}
 
Example 7
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();
}