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

The following examples show how to use com.fasterxml.jackson.core.util.BufferRecycler. 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: BsonParserTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
/**
 * write with BSON read with jackson.
 * inverse of {@link #jacksonThenBson(String)}
 */
private void bsonThenJackson(String json) throws IOException {
  ObjectNode toWrite = maybeWrap(mapper.readTree(json));

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

  // write with BSON
  BsonDocument expected = BsonDocument.parse(toWrite.toString());
  MongoClientSettings.getDefaultCodecRegistry().get(BsonDocument.class)
          .encode(writer, expected, EncoderContext.builder().build());

  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
  BsonDocument actual = BsonDocument.parse(mapper.readValue(parser, JsonNode.class).toString());

  if (!actual.equals(expected)) {
     check(maybeUnwrap(actual)).is(maybeUnwrap(expected));
     Assertions.fail("Should have failed before");
  }
}
 
Example #3
Source File: JSONIO.java    From pom-manipulation-ext with Apache License 2.0 6 votes vote down vote up
public static JsonEncoding detectEncoding(File jsonFile) throws ManipulationException
{
    try ( FileInputStream in = new FileInputStream( jsonFile ) )
    {
        byte[] inputBuffer = new byte[4];
        in.read( inputBuffer );
        IOContext ctxt = new IOContext( new BufferRecycler(), null, false );
        ByteSourceJsonBootstrapper strapper = new ByteSourceJsonBootstrapper( ctxt, inputBuffer, 0,
                4 );
        JsonEncoding encoding = strapper.detectEncoding();
        logger.debug( "Detected JSON encoding {} for file {}", encoding, jsonFile );
        return encoding;
    }
    catch ( IOException e )
    {
        logger.error( "Unable to detect charset for file: {}", jsonFile, e );
        throw new ManipulationException( "Unable to detect charset for file {}", jsonFile, e );
    }
}
 
Example #4
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 #5
Source File: IOContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void releaseWriteEncodingBuffer(byte[] buf) {
    if (buf != null) {
        /* Let's do sanity checks to ensure once-and-only-once release,
         * as well as avoiding trying to release buffers not owned
         */
        _verifyRelease(buf, _writeEncodingBuffer);
        _writeEncodingBuffer = null;
        _bufferRecycler.releaseByteBuffer(BufferRecycler.BYTE_WRITE_ENCODING_BUFFER, buf);
    }
}
 
Example #6
Source File: IOContext.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Method to call when all the processing buffers can be safely
 * recycled.
 */
public void releaseReadIOBuffer(byte[] buf) {
    if (buf != null) {
        /* Let's do sanity checks to ensure once-and-only-once release,
         * as well as avoiding trying to release buffers not owned
         */
        _verifyRelease(buf, _readIOBuffer);
        _readIOBuffer = null;
        _bufferRecycler.releaseByteBuffer(BufferRecycler.BYTE_READ_IO_BUFFER, buf);
    }
}
 
Example #7
Source File: IOContext.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public void releaseWriteEncodingBuffer(byte[] buf) {
    if (buf != null) {
        /* Let's do sanity checks to ensure once-and-only-once release,
         * as well as avoiding trying to release buffers not owned
         */
        _verifyRelease(buf, _writeEncodingBuffer);
        _writeEncodingBuffer = null;
        _bufferRecycler.releaseByteBuffer(BufferRecycler.BYTE_WRITE_ENCODING_BUFFER, buf);
    }
}
 
Example #8
Source File: JsonFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method used by factory to create buffer recycler instances
 * for parsers and generators.
 *<p>
 * Note: only public to give access for <code>ObjectMapper</code>
 */
public BufferRecycler _getBufferRecycler()
{
    /* 23-Apr-2015, tatu: Let's allow disabling of buffer recycling
     *   scheme, for cases where it is considered harmful (possibly
     *   on Android, for example)
     */
    if (Feature.USE_THREAD_LOCAL_FOR_BUFFER_RECYCLING.enabledIn(_factoryFeatures)) {
        return BufferRecyclers.getBufferRecycler();
    }
    return new BufferRecycler();
}
 
Example #9
Source File: IOContext.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public void releaseBase64Buffer(byte[] buf) {
    if (buf != null) { // sanity checks, release once-and-only-once, must be one owned
        _verifyRelease(buf, _base64Buffer);
        _base64Buffer = null;
        _bufferRecycler.releaseByteBuffer(BufferRecycler.BYTE_BASE64_CODEC_BUFFER, buf);
    }
}
 
Example #10
Source File: IOContext.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public void releaseTokenBuffer(char[] buf) {
    if (buf != null) {
        _verifyRelease(buf, _tokenCBuffer);
        _tokenCBuffer = null;
        _bufferRecycler.releaseCharBuffer(BufferRecycler.CHAR_TOKEN_BUFFER, buf);
    }
}
 
Example #11
Source File: IOContext.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public void releaseConcatBuffer(char[] buf) {
    if (buf != null) {
        // 14-Jan-2014, tatu: Let's actually allow upgrade of the original buffer.
        _verifyRelease(buf, _concatCBuffer);
        _concatCBuffer = null;
        _bufferRecycler.releaseCharBuffer(BufferRecycler.CHAR_CONCAT_BUFFER, buf);
    }
}
 
Example #12
Source File: IOContext.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public void releaseNameCopyBuffer(char[] buf) {
    if (buf != null) {
        // 14-Jan-2014, tatu: Let's actually allow upgrade of the original buffer.
        _verifyRelease(buf, _nameCopyBuffer);
        _nameCopyBuffer = null;
        _bufferRecycler.releaseCharBuffer(BufferRecycler.CHAR_NAME_COPY_BUFFER, buf);
    }
}
 
Example #13
Source File: IOContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void releaseNameCopyBuffer(char[] buf) {
    if (buf != null) {
        // 14-Jan-2014, tatu: Let's actually allow upgrade of the original buffer.
        _verifyRelease(buf, _nameCopyBuffer);
        _nameCopyBuffer = null;
        _bufferRecycler.releaseCharBuffer(BufferRecycler.CHAR_NAME_COPY_BUFFER, buf);
    }
}
 
Example #14
Source File: IOContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void releaseConcatBuffer(char[] buf) {
    if (buf != null) {
        // 14-Jan-2014, tatu: Let's actually allow upgrade of the original buffer.
        _verifyRelease(buf, _concatCBuffer);
        _concatCBuffer = null;
        _bufferRecycler.releaseCharBuffer(BufferRecycler.CHAR_CONCAT_BUFFER, buf);
    }
}
 
Example #15
Source File: IOContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void releaseTokenBuffer(char[] buf) {
    if (buf != null) {
        _verifyRelease(buf, _tokenCBuffer);
        _tokenCBuffer = null;
        _bufferRecycler.releaseCharBuffer(BufferRecycler.CHAR_TOKEN_BUFFER, buf);
    }
}
 
Example #16
Source File: IOContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void releaseBase64Buffer(byte[] buf) {
    if (buf != null) { // sanity checks, release once-and-only-once, must be one owned
        _verifyRelease(buf, _base64Buffer);
        _base64Buffer = null;
        _bufferRecycler.releaseByteBuffer(BufferRecycler.BYTE_BASE64_CODEC_BUFFER, buf);
    }
}
 
Example #17
Source File: IOContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method to call when all the processing buffers can be safely
 * recycled.
 */
public void releaseReadIOBuffer(byte[] buf) {
    if (buf != null) {
        /* Let's do sanity checks to ensure once-and-only-once release,
         * as well as avoiding trying to release buffers not owned
         */
        _verifyRelease(buf, _readIOBuffer);
        _readIOBuffer = null;
        _bufferRecycler.releaseByteBuffer(BufferRecycler.BYTE_READ_IO_BUFFER, buf);
    }
}
 
Example #18
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 #19
Source File: ContextualStoredAsJsonSerializer.java    From Rosetta with Apache License 2.0 5 votes vote down vote up
private String serializeToString(T value, ObjectMapper mapper, SerializerProvider provider) throws IOException {
  try (SegmentedStringWriter sw = new SegmentedStringWriter(new BufferRecycler())) {
    if (trySerializeToWriter(value, mapper, provider, sw)) {
      return sw.getAndClear();
    }
  }

  // fallback on old behavior
  JsonNode tree = mapper.valueToTree(value);
  if (tree.isNull()) {
    return tree.asText();
  } else {
    return mapper.writeValueAsString(tree);
  }
}
 
Example #20
Source File: JacksonCodecs.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Override
public T decode(BsonReader reader, DecoderContext decoderContext) {
  final IOContext ioContext = new IOContext(new BufferRecycler(), null, false);
  final BsonParser parser = new BsonParser(ioContext, 0, (AbstractBsonReader) reader);
  try {
    return mapper.readValue(parser, getEncoderClass());
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #21
Source File: BsonParserTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
/**
 * Converts string to json
 */
private void compare(String string) throws IOException {

  JsonNode expected = mapper.readTree(string);

  // BSON likes encoding full document (not simple elements like BsonValue)
  if (!expected.isObject()) {
    ObjectNode temp = mapper.createObjectNode();
    temp.set("ignore", expected);
    expected = temp;
  }

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

  BsonGenerator generator = new BsonGenerator(0, mapper, writer);
  // write
  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
  JsonNode actual = mapper.readValue(parser, JsonNode.class);
  check(actual).is(expected);
}
 
Example #22
Source File: JacksonCodecRegistry.java    From immutables with Apache License 2.0 5 votes vote down vote up
JacksonCodec(Class<T> clazz, ObjectMapper mapper) {
  this.clazz = Objects.requireNonNull(clazz, "clazz");
  Objects.requireNonNull(mapper, "mapper");
  this.reader = mapper.readerFor(clazz);
  this.writer = mapper.writerFor(clazz);
  this.ioContext =  new IOContext(new BufferRecycler(), null, false);
}
 
Example #23
Source File: JacksonCodecsTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <T> T writeThenRead(CodecRegistry registry, ObjectMapper mapper, T value) throws IOException {
  BasicOutputBuffer buffer = new BasicOutputBuffer();
  BsonWriter writer = new BsonBinaryWriter(buffer);
  registry.get((Class<T>) value.getClass()).encode(writer, value, EncoderContext.builder().build());
  BsonBinaryReader reader = new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
  IOContext ioContext = new IOContext(new BufferRecycler(), null, false);
  BsonParser parser = new BsonParser(ioContext, 0, reader);
  return mapper.readValue(parser, (Class<T>) value.getClass());
}
 
Example #24
Source File: Parsers.java    From immutables with Apache License 2.0 5 votes vote down vote up
static BsonParser createParser(BsonDocument bson) {
  BasicOutputBuffer buffer = new BasicOutputBuffer();
  CodecRegistry registry = MongoClientSettings.getDefaultCodecRegistry();
  registry.get(BsonDocument.class)
          .encode(new BsonBinaryWriter(buffer), bson, EncoderContext.builder().build());

  BsonBinaryReader reader = new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
  IOContext ioContext = new IOContext(new BufferRecycler(), null, false);
  return new BsonParser(ioContext, 0, reader);
}
 
Example #25
Source File: IOContext.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @since 2.4
 */
public byte[] allocWriteEncodingBuffer(int minSize) {
    _verifyAlloc(_writeEncodingBuffer);
    return (_writeEncodingBuffer = _bufferRecycler.allocByteBuffer(BufferRecycler.BYTE_WRITE_ENCODING_BUFFER, minSize));
}
 
Example #26
Source File: SegmentedStringWriter.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
public SegmentedStringWriter(BufferRecycler br) {
    super();
    _buffer = new TextBuffer(br);
}
 
Example #27
Source File: IOContext.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
public char[] allocNameCopyBuffer(int minSize) {
    _verifyAlloc(_nameCopyBuffer);
    return (_nameCopyBuffer = _bufferRecycler.allocCharBuffer(BufferRecycler.CHAR_NAME_COPY_BUFFER, minSize));
}
 
Example #28
Source File: IOContext.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public char[] allocConcatBuffer() {
    _verifyAlloc(_concatCBuffer);
    return (_concatCBuffer = _bufferRecycler.allocCharBuffer(BufferRecycler.CHAR_CONCAT_BUFFER));
}
 
Example #29
Source File: IOContext.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public IOContext(BufferRecycler br, Object sourceRef, boolean managedResource)
{
    _bufferRecycler = br;
    _sourceRef = sourceRef;
    _managedResource = managedResource;
}
 
Example #30
Source File: IOContext.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @since 2.4
 */
public byte[] allocReadIOBuffer(int minSize) {
    _verifyAlloc(_readIOBuffer);
    return (_readIOBuffer = _bufferRecycler.allocByteBuffer(BufferRecycler.BYTE_READ_IO_BUFFER, minSize));
}