Search in sources :

Example 1 with ByteArrayFeeder

use of com.fasterxml.jackson.core.async.ByteArrayFeeder in project servicetalk by apple.

the class JacksonSerializationProvider method newDeserializer.

private static <T> StreamingDeserializer<T> newDeserializer(ObjectReader reader) {
    final JsonFactory factory = reader.getFactory();
    final JsonParser parser;
    try {
        // TODO(scott): ByteBufferFeeder is currently not supported by jackson, and the current API throws
        // UnsupportedOperationException if not supported. When jackson does support two NonBlockingInputFeeder
        // types we need an approach which doesn't involve catching UnsupportedOperationException to try to get
        // ByteBufferFeeder and then ByteArrayFeeder.
        parser = factory.createNonBlockingByteArrayParser();
    } catch (IOException e) {
        throw new IllegalArgumentException("parser initialization error for factory: " + factory, e);
    }
    NonBlockingInputFeeder rawFeeder = parser.getNonBlockingInputFeeder();
    if (rawFeeder instanceof ByteBufferFeeder) {
        return new ByteBufferJacksonDeserializer<>(reader, parser, (ByteBufferFeeder) rawFeeder);
    }
    if (rawFeeder instanceof ByteArrayFeeder) {
        return new ByteArrayJacksonDeserializer<>(reader, parser, (ByteArrayFeeder) rawFeeder);
    }
    throw new IllegalArgumentException("unsupported feeder type: " + rawFeeder);
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) NonBlockingInputFeeder(com.fasterxml.jackson.core.async.NonBlockingInputFeeder) ByteBufferFeeder(com.fasterxml.jackson.core.async.ByteBufferFeeder) IOException(java.io.IOException) ByteArrayFeeder(com.fasterxml.jackson.core.async.ByteArrayFeeder) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 2 with ByteArrayFeeder

use of com.fasterxml.jackson.core.async.ByteArrayFeeder in project micronaut-core by micronaut-projects.

the class JacksonProcessor method byteFeeder.

private ByteArrayFeeder byteFeeder(byte[] message) throws IOException {
    ByteArrayFeeder byteFeeder = currentNonBlockingJsonParser.getNonBlockingInputFeeder();
    final boolean needMoreInput = byteFeeder.needMoreInput();
    if (!needMoreInput) {
        currentNonBlockingJsonParser = (NonBlockingJsonParser) jsonFactory.createNonBlockingByteArrayParser();
        byteFeeder = currentNonBlockingJsonParser.getNonBlockingInputFeeder();
    }
    byteFeeder.feedInput(message, 0, message.length);
    return byteFeeder;
}
Also used : ByteArrayFeeder(com.fasterxml.jackson.core.async.ByteArrayFeeder)

Example 3 with ByteArrayFeeder

use of com.fasterxml.jackson.core.async.ByteArrayFeeder in project micronaut-core by micronaut-projects.

the class JacksonCoreProcessor method onUpstreamMessage.

@Override
protected void onUpstreamMessage(byte[] message) {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Received upstream bytes of length: " + message.length);
    }
    try {
        if (message.length == 0) {
            if (needMoreInput()) {
                requestMoreInput();
            }
            return;
        }
        final ByteArrayFeeder byteFeeder = byteFeeder(message);
        JsonToken event;
        while ((event = currentNonBlockingJsonParser.nextToken()) != JsonToken.NOT_AVAILABLE) {
            if (!started) {
                started = true;
                if (streamArray && event == JsonToken.START_ARRAY) {
                    rootIsArray = true;
                    jsonStream = false;
                    continue;
                }
            }
            if (currentGenerator == null) {
                if (event == JsonToken.END_ARRAY && rootIsArray) {
                    byteFeeder.endOfInput();
                    break;
                }
                currentGenerator = treeCodec.createTreeGenerator();
            }
            JsonStreamTransfer.transferCurrentToken(currentNonBlockingJsonParser, currentGenerator, deserializationConfig);
            if (currentGenerator.isComplete()) {
                publishNode(currentGenerator.getCompletedValue());
                currentGenerator = null;
            }
        }
        if (jsonStream) {
            if (currentGenerator == null) {
                byteFeeder.endOfInput();
            }
            requestMoreInput();
        } else {
            if (needMoreInput()) {
                requestMoreInput();
            }
        }
    } catch (IOException e) {
        onError(e);
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) ByteArrayFeeder(com.fasterxml.jackson.core.async.ByteArrayFeeder)

Example 4 with ByteArrayFeeder

use of com.fasterxml.jackson.core.async.ByteArrayFeeder in project jackson-dataformats-binary by FasterXML.

the class AsyncReaderWrapperForByteArray method nextToken.

@Override
public JsonToken nextToken() {
    JsonToken token;
    while ((token = _streamReader.nextToken()) == JsonToken.NOT_AVAILABLE) {
        ByteArrayFeeder feeder = (ByteArrayFeeder) _streamReader.nonBlockingInputFeeder();
        if (!feeder.needMoreInput()) {
            throw new StreamReadException(null, "Got NOT_AVAILABLE, could not feed more input");
        }
        int amount = Math.min(_bytesPerFeed, _end - _offset);
        if (amount < 1) {
            // end-of-input?
            feeder.endOfInput();
        } else {
            // padding?
            if (_padding == 0) {
                feeder.feedInput(_doc, _offset, _offset + amount);
            } else {
                byte[] tmp = new byte[amount + _padding + _padding];
                System.arraycopy(_doc, _offset, tmp, _padding, amount);
                feeder.feedInput(tmp, _padding, _padding + amount);
            }
            _offset += amount;
        }
    }
    return token;
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) StreamReadException(com.fasterxml.jackson.core.exc.StreamReadException) ByteArrayFeeder(com.fasterxml.jackson.core.async.ByteArrayFeeder)

Example 5 with ByteArrayFeeder

use of com.fasterxml.jackson.core.async.ByteArrayFeeder in project jackson-core by FasterXML.

the class AsyncLocationTest method testLocationOffsets.

// for [core#531]
public void testLocationOffsets() throws Exception {
    JsonParser parser = DEFAULT_F.createNonBlockingByteArrayParser(ObjectReadContext.empty());
    ByteArrayFeeder feeder = (ByteArrayFeeder) parser.nonBlockingInputFeeder();
    byte[] input = utf8Bytes("[[[");
    feeder.feedInput(input, 2, 3);
    assertEquals(JsonToken.START_ARRAY, parser.nextToken());
    assertEquals(1, parser.currentLocation().getByteOffset());
    assertEquals(1, parser.currentTokenLocation().getByteOffset());
    assertEquals(1, parser.currentLocation().getLineNr());
    assertEquals(1, parser.currentTokenLocation().getLineNr());
    assertEquals(2, parser.currentLocation().getColumnNr());
    assertEquals(1, parser.currentTokenLocation().getColumnNr());
    feeder.feedInput(input, 0, 1);
    assertEquals(JsonToken.START_ARRAY, parser.nextToken());
    assertEquals(2, parser.currentLocation().getByteOffset());
    assertEquals(2, parser.currentTokenLocation().getByteOffset());
    assertEquals(1, parser.currentLocation().getLineNr());
    assertEquals(1, parser.currentTokenLocation().getLineNr());
    assertEquals(3, parser.currentLocation().getColumnNr());
    assertEquals(2, parser.currentTokenLocation().getColumnNr());
    parser.close();
}
Also used : ByteArrayFeeder(com.fasterxml.jackson.core.async.ByteArrayFeeder)

Aggregations

ByteArrayFeeder (com.fasterxml.jackson.core.async.ByteArrayFeeder)9 JsonToken (com.fasterxml.jackson.core.JsonToken)4 IOException (java.io.IOException)3 JsonParser (com.fasterxml.jackson.core.JsonParser)2 StreamReadException (com.fasterxml.jackson.core.exc.StreamReadException)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 ByteBufferFeeder (com.fasterxml.jackson.core.async.ByteBufferFeeder)1 NonBlockingInputFeeder (com.fasterxml.jackson.core.async.NonBlockingInputFeeder)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1