下面列出了com.fasterxml.jackson.core.util.BufferRecycler#com.fasterxml.jackson.core.io.IOContext 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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();
}
}
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;
}
/**
* 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();
}
}
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;
}
/**
* 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();
}
}
/**
* 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();
}
}
/**
* 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();
}
}
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;
}
/**
* 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;
}
/**
* 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);
}
/**
* 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();
}
}
/**
* 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();
}
}
@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();
}
};
}
/**
* 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.");
}
}
/**
* 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();
}
}
/**
* 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();
}
}
/**
* 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();
}
}
/**
* 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();
}
}
/**
* 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();
}
}
/**
* 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();
}
}
@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();
}
}
@SuppressWarnings("resource")
@Override
public HoconTreeTraversingParser createParser(byte[] data)
throws IOException, JsonParseException
{
IOContext ctxt = _createContext(data, true);
// [JACKSON-512]: allow wrapping with InputDecorator
if (_inputDecorator != null) {
InputStream in = _inputDecorator.decorate(ctxt, data, 0, data.length);
if (in != null) {
return _createParser(in, ctxt);
}
}
return _createParser(data, 0, data.length, ctxt);
}
public JsonGeneratorImpl(IOContext ctxt, int features, ObjectCodec codec)
{
super(features, codec);
_ioContext = ctxt;
if (Feature.ESCAPE_NON_ASCII.enabledIn(features)) {
// inlined `setHighestNonEscapedChar()`
_maximumNonEscapedChar = 127;
}
_cfgUnqNames = !Feature.QUOTE_FIELD_NAMES.enabledIn(features);
}
/**
* Method called when input comes as a {@link java.io.Reader}, and buffer allocation
* can be done using default mechanism.
*/
public ReaderBasedJsonParser(IOContext ctxt, int features, Reader r,
ObjectCodec codec, CharsToNameCanonicalizer st)
{
super(ctxt, features);
_reader = r;
_inputBuffer = ctxt.allocTokenBuffer();
_inputPtr = 0;
_inputEnd = 0;
_objectCodec = codec;
_symbols = st;
_hashSeed = st.hashSeed();
_bufferRecyclable = true;
}
public NonBlockingJsonParserBase(IOContext ctxt, int parserFeatures,
ByteQuadsCanonicalizer sym)
{
super(ctxt, parserFeatures);
_symbols = sym;
_currToken = null;
_majorState = MAJOR_INITIAL;
_majorStateAfterValue = MAJOR_ROOT;
}
/**
* Creates a {@link UTF8StreamJsonParser} from the inputstream with the supplied buf {@code inBuffer} to use.
*/
public static UTF8StreamJsonParser newJsonParser(InputStream in, byte[] buf,
int offset, int limit) throws IOException {
return newJsonParser(in, buf, offset, limit, false,
new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(), in,
false));
}
/**
* 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.getParserFeatures(), in,
DEFAULT_JSON_FACTORY.getCodec(),
DEFAULT_JSON_FACTORY.getRootByteSymbols().makeChild(1),
buf, offset, limit, bufferRecyclable);
}
/**
* 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.getGeneratorFeatures(),
DEFAULT_JSON_FACTORY.getCodec(),
out,
buf,
offset,
bufferRecyclable);
}
/**
* Merges the {@code message} from the {@link InputStream} using the given {@code schema}.
*/
public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema,
boolean numeric) throws IOException {
final IOContext context = new IOContext(DEFAULT_JSON_FACTORY._getBufferRecycler(),
in, false);
// final JsonParser parser = DEFAULT_JSON_FACTORY.createJsonParser(in);
try (JsonParser parser = newJsonParser(in, context.allocReadIOBuffer(), 0, 0,
true, context)) {
mergeFrom(parser, message, schema, numeric);
}
}
/**
* 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);
try (JsonParser parser = newJsonParser(in, buffer.buffer, 0, 0, false, context)) {
mergeFrom(parser, message, schema, numeric);
}
}