com.fasterxml.jackson.core.io.IOContext Java Examples

The following examples show how to use com.fasterxml.jackson.core.io.IOContext. 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: BsonParserTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
/**
 * Read and Write in Jackson API but using BSON reader/writer adapters
 */
private void jacksonThenJackson(String json) throws IOException {
  ObjectNode expected = maybeWrap(mapper.readTree(json));

  BasicOutputBuffer buffer = new BasicOutputBuffer();
  BsonWriter writer = new BsonBinaryWriter(buffer);

  BsonGenerator generator = new BsonGenerator(0, writer);
  // write with Jackson
  mapper.writeValue(generator, expected);

  BsonBinaryReader reader = new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
  IOContext ioContext = new IOContext(new BufferRecycler(), null, false);
  BsonParser parser = new BsonParser(ioContext, 0, reader);

  // read with Jackson
  JsonNode actual = mapper.readValue(parser, JsonNode.class);
  check(actual).is(expected);
}
 
Example #2
Source File: SmileIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the {@code messages} from the stream using the given {@code schema}.
 * <p>
 * The {@link LinkedBuffer}'s internal byte array will be used when reading the message.
 */
public static <T> List<T> parseListFrom(InputStream in, Schema<T> schema,
        boolean numeric, LinkedBuffer buffer) throws IOException
{
    final IOContext context = new IOContext(DEFAULT_SMILE_FACTORY._getBufferRecycler(),
            in, false);
    final SmileParser parser = newSmileParser(in, buffer.buffer, 0, 0, false, context);

    // final SmileParser parser = DEFAULT_SMILE_FACTORY.createJsonParser(in);
    try
    {
        return JsonIOUtil.parseListFrom(parser, schema, numeric);
    }
    finally
    {
        parser.close();
    }
}
 
Example #3
Source File: SmileIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the {@code messages} from the stream using the given {@code schema}.
 */
public static <T> List<T> parseListFrom(InputStream in, Schema<T> schema,
        boolean numeric) throws IOException
{
    final IOContext context = new IOContext(DEFAULT_SMILE_FACTORY._getBufferRecycler(),
            in, false);
    final SmileParser parser = newSmileParser(in, context.allocReadIOBuffer(), 0, 0,
            true, context);

    // final SmileParser parser = DEFAULT_SMILE_FACTORY.createJsonParser(in);
    try
    {
        return JsonIOUtil.parseListFrom(parser, schema, numeric);
    }
    finally
    {
        parser.close();
    }
}
 
Example #4
Source File: JsonIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Merges the {@code message} with the byte array using the given {@code schema}.
 */
public static <T> void mergeFrom(byte[] data, int offset, int length, T message,
        Schema<T> schema, boolean numeric) throws IOException
{
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(),
            data, false);
    final JsonParser parser = newJsonParser(null, data, offset, offset + length, false,
            context);
    /*
     * final JsonParser parser = DEFAULT_JSON_FACTORY.createJsonParser(data, offset, length);
     */
    try
    {
        mergeFrom(parser, message, schema, numeric);
    }
    finally
    {
        parser.close();
    }
}
 
Example #5
Source File: UTF8JsonGenerator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public UTF8JsonGenerator(IOContext ctxt, int features, ObjectCodec codec,
        OutputStream out,
        byte[] outputBuffer, int outputOffset, boolean bufferRecyclable)
{
    
    super(ctxt, features, codec);
    _outputStream = out;
    _bufferRecyclable = bufferRecyclable;
    _outputTail = outputOffset;
    _outputBuffer = outputBuffer;
    _outputEnd = _outputBuffer.length;
    // up to 6 bytes per char (see above), rounded up to 1/8
    _outputMaxContiguous = (_outputEnd >> 3);
    _charBuffer = ctxt.allocConcatBuffer();
    _charBufferLength = _charBuffer.length;
}
 
Example #6
Source File: JsonIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Serializes the {@code messages} into the stream using the given schema.
 */
public static <T> void writeListTo(OutputStream out, List<T> messages,
        Schema<T> schema, boolean numeric) throws IOException
{
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(),
            out, false);

    final JsonGenerator generator = newJsonGenerator(out,
            context.allocWriteEncodingBuffer(), 0, true, context);
    /*
     * final JsonGenerator generator = DEFAULT_JSON_FACTORY.createJsonGenerator(out, JsonEncoding.UTF8);
     */
    try
    {
        writeListTo(generator, messages, schema, numeric);
    }
    finally
    {
        generator.close();
    }
}
 
Example #7
Source File: UTF8StreamJsonParser.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public UTF8StreamJsonParser(IOContext ctxt, int features, InputStream in,
        ObjectCodec codec, ByteQuadsCanonicalizer sym,
        byte[] inputBuffer, int start, int end,
        boolean bufferRecyclable)
{
    super(ctxt, features);
    _inputStream = in;
    _objectCodec = codec;
    _symbols = sym;
    _inputBuffer = inputBuffer;
    _inputPtr = start;
    _inputEnd = end;
    _currInputRowStart = start;
    // If we have offset, need to omit that from byte offset, so:
    _currInputProcessed = -start;
    _bufferRecyclable = bufferRecyclable;
}
 
Example #8
Source File: SmileIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Serializes the {@code messages} into the stream using the given schema.
 * <p>
 * The {@link LinkedBuffer}'s internal byte array will be used as the primary buffer when writing the message.
 */
public static <T> void writeListTo(OutputStream out, List<T> messages,
        Schema<T> schema, boolean numeric, LinkedBuffer buffer) throws IOException
{
    final IOContext context = new IOContext(DEFAULT_SMILE_FACTORY._getBufferRecycler(),
            out, false);

    final SmileGenerator generator = newSmileGenerator(out, buffer.buffer, 0, false,
            context);

    // final SmileGenerator generator = DEFAULT_SMILE_FACTORY.createJsonGenerator(out);
    try
    {
        JsonIOUtil.writeListTo(generator, messages, schema, numeric);
    }
    finally
    {
        generator.close();
    }
}
 
Example #9
Source File: SmileIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Serializes the {@code messages} into the stream using the given schema.
 */
public static <T> void writeListTo(OutputStream out, List<T> messages,
        Schema<T> schema, boolean numeric) throws IOException
{
    final IOContext context = new IOContext(DEFAULT_SMILE_FACTORY._getBufferRecycler(),
            out, false);

    final SmileGenerator generator = newSmileGenerator(out,
            context.allocWriteEncodingBuffer(), 0, true, context);

    // final SmileGenerator generator = DEFAULT_SMILE_FACTORY.createJsonGenerator(out);
    try
    {
        JsonIOUtil.writeListTo(generator, messages, schema, numeric);
    }
    finally
    {
        generator.close();
    }
}
 
Example #10
Source File: JsonIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Merges the {@code message} from the {@link InputStream} using the given {@code schema}.
 * <p>
 * The {@link LinkedBuffer}'s internal byte array will be used when reading the message.
 */
public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema,
        boolean numeric, LinkedBuffer buffer) throws IOException
{
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(),
            in, false);
    final JsonParser parser = newJsonParser(in, buffer.buffer, 0, 0, false, context);
    try
    {
        mergeFrom(parser, message, schema, numeric);
    }
    finally
    {
        parser.close();
    }
}
 
Example #11
Source File: SmileIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Merges the {@code message} from the {@link InputStream} using the given {@code schema}.
 * <p>
 * The {@link LinkedBuffer}'s internal byte array will be used when reading the message.
 */
public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema,
        boolean numeric, LinkedBuffer buffer) throws IOException
{
    final IOContext context = new IOContext(DEFAULT_SMILE_FACTORY._getBufferRecycler(),
            in, false);
    final SmileParser parser = newSmileParser(in, buffer.buffer, 0, 0, false, context);

    // final SmileParser parser = DEFAULT_SMILE_FACTORY.createJsonParser(in);

    try
    {
        JsonIOUtil.mergeFrom(parser, message, schema, numeric);
    }
    finally
    {
        parser.close();
    }
}
 
Example #12
Source File: JsonIOUtils.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Merges the {@code message} with the byte array using the given {@code schema}.
 */
public static <T> void mergeFrom(byte[] data, int offset, int length, T message,
                                 Schema<T> schema, boolean numeric) throws IOException {
  final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(),
    data, false);
  final JsonParser parser = newJsonParser(null, data, offset, offset + length, false,
    context);
  /*
   * final JsonParser parser = DEFAULT_JSON_FACTORY.createJsonParser(data, offset, length);
   */
  try {
    mergeFrom(parser, message, schema, numeric);
  } finally {
    parser.close();
  }
}
 
Example #13
Source File: JsonRpcReaderUtil.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Check whether the encoding is valid.
 * @param in input of bytes
 * @throws IOException this is an IO exception
 * @throws UnsupportedException this is an unsupported exception
 */
private static void checkEncoding(ByteBuf in) throws IOException {
    int inputStart = 0;
    int inputLength = 4;
    fliterCharaters(in);
    byte[] buff = new byte[4];
    in.getBytes(in.readerIndex(), buff);
    ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper(new IOContext(new BufferRecycler(),
                                                                                       null,
                                                                                       false),
                                                                         buff, inputStart,
                                                                         inputLength);
    JsonEncoding jsonEncoding = strapper.detectEncoding();
    if (!JsonEncoding.UTF8.equals(jsonEncoding)) {
        throw new UnsupportedException("Only UTF-8 encoding is supported.");
    }
}
 
Example #14
Source File: MincodeFactoryTest.java    From divolte-collector with Apache License 2.0 6 votes vote down vote up
@Override
public Reader decorate(final IOContext ctxt, final Reader r) throws IOException {
    return new Reader() {
        @Override
        public int read(final char[] cbuf,
                        final int off,
                        final int len) throws IOException {
            final int count = r.read(cbuf, off, len);
            invert(cbuf, off, len);
            return count;
        }

        @Override
        public void close() throws IOException {
            r.close();
        }
    };
}
 
Example #15
Source File: JsonIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Serializes the {@code message} into an {@link OutputStream} using the given {@code schema}.
 */
public static <T> void writeTo(OutputStream out, T message, Schema<T> schema,
        boolean numeric) throws IOException
{
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(),
            out, false);

    final JsonGenerator generator = newJsonGenerator(out,
            context.allocWriteEncodingBuffer(), 0, true, context);

    /*
     * final JsonGenerator generator = DEFAULT_JSON_FACTORY.createJsonGenerator(out, JsonEncoding.UTF8);
     */
    try
    {
        writeTo(generator, message, schema, numeric);
    }
    finally
    {
        generator.close();
    }
}
 
Example #16
Source File: JsonIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Serializes the {@code message} into an {@link OutputStream} using the given {@code schema}.
 * <p>
 * The {@link LinkedBuffer}'s internal byte array will be used as the primary buffer when writing the message.
 */
public static <T> void writeTo(OutputStream out, T message, Schema<T> schema,
        boolean numeric, LinkedBuffer buffer) throws IOException
{
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(),
            out, false);

    final JsonGenerator generator = newJsonGenerator(out, buffer.buffer, 0, false,
            context);
    try
    {
        writeTo(generator, message, schema, numeric);
    }
    finally
    {
        generator.close();
    }
}
 
Example #17
Source File: JsonIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the {@code messages} from the stream using the given {@code schema}.
 */
public static <T> List<T> parseListFrom(InputStream in, Schema<T> schema,
        boolean numeric) throws IOException
{
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(),
            in, false);
    final JsonParser parser = newJsonParser(in, context.allocReadIOBuffer(), 0, 0,
            true, context);
    // final JsonParser parser = DEFAULT_JSON_FACTORY.createJsonParser(in);
    try
    {
        return parseListFrom(parser, schema, numeric);
    }
    finally
    {
        parser.close();
    }
}
 
Example #18
Source File: UTF8StreamJsonParser.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
public UTF8StreamJsonParser(IOContext ctxt, int features, InputStream in,
        ObjectCodec codec, ByteQuadsCanonicalizer sym,
        byte[] inputBuffer, int start, int end,
        boolean bufferRecyclable)
{
    super(ctxt, features);
    _inputStream = in;
    _objectCodec = codec;
    _symbols = sym;
    _inputBuffer = inputBuffer;
    _inputPtr = start;
    _inputEnd = end;
    _currInputRowStart = start;
    // If we have offset, need to omit that from byte offset, so:
    _currInputProcessed = -start;
    _bufferRecyclable = bufferRecyclable;
}
 
Example #19
Source File: JsonIOUtil.java    From protostuff with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the {@code messages} from the stream using the given {@code schema}.
 * <p>
 * The {@link LinkedBuffer}'s internal byte array will be used when reading the message.
 */
public static <T> List<T> parseListFrom(InputStream in, Schema<T> schema,
        boolean numeric, LinkedBuffer buffer) throws IOException
{
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(),
            in, false);
    final JsonParser parser = newJsonParser(in, buffer.buffer, 0, 0, false, context);
    try
    {
        return parseListFrom(parser, schema, numeric);
    }
    finally
    {
        parser.close();
    }
}
 
Example #20
Source File: ReaderBasedJsonParser.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Method called when caller wants to provide input buffer directly,
 * and it may or may not be recyclable use standard recycle context.
 * 
 * @since 2.4
 */
public ReaderBasedJsonParser(IOContext ctxt, int features, Reader r,
        ObjectCodec codec, CharsToNameCanonicalizer st,
        char[] inputBuffer, int start, int end,
        boolean bufferRecyclable)
{
    super(ctxt, features);
    _reader = r;
    _inputBuffer = inputBuffer;
    _inputPtr = start;
    _inputEnd = end;
    _objectCodec = codec;
    _symbols = st;
    _hashSeed = st.hashSeed();
    _bufferRecyclable = bufferRecyclable;
}
 
Example #21
Source File: JsonIOUtil.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a json pipe from a byte array.
 */
public static Pipe newPipe(byte[] data, int offset, int length, boolean numeric)
        throws IOException
{
    final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(),
            data, false);
    final JsonParser parser = newJsonParser(null, data, offset, offset + length, false,
            context);

    return newPipe(parser, numeric);
}
 
Example #22
Source File: SmileIOUtil.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link SmileParser} from the inputstream with the supplied buf {@code inBuffer} to use.
 */
static SmileParser newSmileParser(InputStream in, byte[] buf,
        int offset, int limit, boolean bufferRecyclable, IOContext context)
        throws IOException
{
    return new SmileParser(context,
            DEFAULT_SMILE_FACTORY.getParserFeaturesImpl(),
            DEFAULT_SMILE_FACTORY.getSmileParserFeaturesImpl(),
            DEFAULT_SMILE_FACTORY.getCodec(),
            DEFAULT_SMILE_FACTORY.getRootByteSymbols().makeChild(1),
            in,
            buf, offset, limit, bufferRecyclable);
}
 
Example #23
Source File: HoconFactory.java    From jackson-dataformat-hocon with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("resource")
@Override
protected HoconTreeTraversingParser _createParser(InputStream in, IOContext ctxt)
    throws IOException, JsonParseException
{
    Reader r = _createReader(in, null, ctxt);
    return _createParser(r, ctxt);
}
 
Example #24
Source File: JsonIOUtil.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link UTF8StreamJsonParser} from the inputstream with the supplied buf {@code inBuffer} to use.
 */
static UTF8StreamJsonParser newJsonParser(InputStream in, byte[] buf,
        int offset, int limit, boolean bufferRecyclable, IOContext context)
        throws IOException
{
    return new UTF8StreamJsonParser(context,
            DEFAULT_JSON_FACTORY.getParserFeaturesImpl(), in,
            DEFAULT_JSON_FACTORY.getCodec(),
            DEFAULT_JSON_FACTORY.getRootByteSymbols().makeChild(1),
            buf, offset, limit, bufferRecyclable);
}
 
Example #25
Source File: MincodeFactory.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
@Override
public MincodeParser createParser(byte[] data, int offset, int len) throws IOException {
    final IOContext ctxt = _createContext(data, true);
    if (null != _inputDecorator) {
        final InputStream in = _inputDecorator.decorate(ctxt, data, offset, len);
        if (null != in) {
            return _createParser(in, ctxt);
        }
    }
    return _createParser(data, offset, len, ctxt);
}
 
Example #26
Source File: MincodeParser.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
MincodeParser(final IOContext ctxt,
              final int parserFeatures,
              @Nullable final ObjectCodec objectCodec,
              final Reader reader) {
    super(ctxt, parserFeatures);
    this.reader = Objects.requireNonNull(reader);
    this.inputBuffer = ctxt.allocTokenBuffer();
    this._inputPtr = 0;
    this._inputEnd = 0;
    this.bufferRecyclable = true;
    this.objectCodec = objectCodec;
}
 
Example #27
Source File: ProtostuffMessageBodyWriter.java    From datawave with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void writeTo(Object message, Class<?> clazz, Type type, Annotation[] annotations, MediaType media, MultivaluedMap<String,Object> httpHeaders,
                OutputStream out) throws IOException, WebApplicationException {
    
    // TODO: Figure out a method to add the proto file location in the response headers.
    // This map must be mofified before any data is written to out,
    // since at that time the response headers will be flushed.
    
    Schema<Object> schema = null;
    if (message instanceof Message) {
        Message<Object> msg = (Message<Object>) message;
        schema = msg.cachedSchema();
    } else {
        schema = (Schema<Object>) RuntimeSchema.getSchema(clazz);
    }
    
    try {
        if (MediaType.APPLICATION_XML_TYPE.equals(media) || MediaType.TEXT_XML_TYPE.equals(media)) {
            XmlIOUtil.writeTo(out, message, schema);
        } else if ("text/yaml".equals(media.toString()) || "text/x-yaml".equals(media.toString()) || "application/x-yaml".equals(media.toString())) {
            YamlIOUtil.writeTo(out, message, schema, buffer);
        } else if ("application/x-protobuf".equals(media.toString())) {
            ProtobufIOUtil.writeTo(out, message, schema, buffer);
        } else if ("application/x-protostuff".equals(media.toString())) {
            ProtostuffIOUtil.writeTo(out, message, schema, buffer);
        } else if (MediaType.APPLICATION_JSON_TYPE.equals(media)) {
            IOContext ctx = new IOContext(JsonIOUtil.DEFAULT_JSON_FACTORY._getBufferRecycler(), out, false);
            UTF8JsonGenerator generator = new UTF8JsonGenerator(ctx, JsonIOUtil.DEFAULT_JSON_FACTORY.getGeneratorFeatures(),
                            JsonIOUtil.DEFAULT_JSON_FACTORY.getCodec(), out);
            try {
                JsonIOUtil.writeTo(generator, message, schema, false);
            } finally {
                generator.close();
            }
        }
    } finally {
        buffer.clear();
    }
}
 
Example #28
Source File: MessagePackFactory.java    From jackson-dataformat-msgpack with Apache License 2.0 5 votes vote down vote up
@Override
protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt) throws IOException, JsonParseException {
    if (offset != 0 || len != data.length) {
        data = Arrays.copyOfRange(data, offset, offset + len);
    }
    MessagePackParser parser = new MessagePackParser(ctxt, messagePackParserFeature, data);
    return parser;
}
 
Example #29
Source File: JsonIOUtil.java    From protostuff with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a {@link UTF8JsonGenerator} for the outputstream with the supplied buf {@code outBuffer} to use.
 */
static UTF8JsonGenerator newJsonGenerator(OutputStream out, byte[] buf, int offset,
        boolean bufferRecyclable, IOContext context)
{
    context.setEncoding(JsonEncoding.UTF8);

    return new UTF8JsonGenerator(context,
            DEFAULT_JSON_FACTORY.getGeneratorFeaturesImpl(),
            DEFAULT_JSON_FACTORY.getCodec(),
            out,
            buf,
            offset,
            bufferRecyclable);
}
 
Example #30
Source File: MincodeFactoryTest.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream decorate(final IOContext ctxt, final InputStream in) throws IOException {
    return new InputStream() {
        @Override
        public int read() throws IOException {
            final int b = in.read();
            return b != -1 ? invert((byte)b) : -1;
        }
        @Override
        public void close() throws IOException {
            in.close();
            super.close();
        }
    };
}