[英]How does Jackson deal with end-of-stream reads (e.g. -1, EOF) when parsing from underlying InputStream
I have an ObjectReader
parsing Json input from an underlying socket InputStream
as follows:我有一个
ObjectReader
解析来自底层套接字InputStream
Json 输入,如下所示:
jsonInput.readValue(handlerSocket.getInputStream());
, where jsonInput
is the ObjectReader
object. ,其中
jsonInput
是ObjectReader
对象。
I had configured jsonInput
to parse to a particular type via the default readFor(Class<?>)
method provided by the parent ObjectMapper
.我已将
jsonInput
配置为通过父ObjectMapper
提供的默认readFor(Class<?>)
方法解析为特定类型。 At the end of execution, part of the shutdown sequence involves shutting down the socket input ie calling handlerSocket.shutdownInput()
and according to the Socket API Javadocs regarding shutdownInput()
:在执行结束时,部分关闭序列涉及关闭套接字输入,即调用
handlerSocket.shutdownInput()
并根据 Socket API Javadocs关于shutdownInput()
:
Places the input stream for this socket at "end of stream".
将此套接字的输入流放置在“流末尾”。 Any data sent to the input stream side of the socket is acknowledged and then silently discarded.
任何发送到套接字输入流端的数据都会被确认,然后被静默丢弃。 If you read from a socket input stream after invoking this method on the socket, the stream's available method will return 0, and its read methods will return -1 (end of stream).
如果在套接字上调用此方法后从套接字输入流中读取,则流的可用方法将返回 0,其读取方法将返回 -1(流结束)。
I would have referred to the Jackson documentation regarding this particular implementation of readValue()
but there is little to no relevant information to go on.我会参考关于
readValue()
这个特定实现的 Jackson 文档,但几乎没有相关信息可以继续。 My question is then how does Jackson treat end-of-stream type scenarios such as this one?我的问题是,Jackson 如何处理这种流结束类型的场景? Will
null
be returned?会返回
null
吗? A parsing exception?解析异常? Would appreciate your insights.
将不胜感激您的见解。
Generally, EOF
is normal situation when you working with streams.通常,当您使用流时,
EOF
是正常情况。 Problem with EOF
will rise only when one of JsonParser
's notice EOF
in "the middle" of JSON
object.只有当
JsonParser
之一通知EOF
在JSON
对象的“中间”时, EOF
问题才会出现。 When parser will read the whole object and will be satisfied there will be no problems.当解析器将读取整个对象并满意时,就不会有问题了。 Since version
2.8
Jackson
has JsonEOFException
exception which precise when it will appear:由于版本
2.8
Jackson
有JsonEOFException
异常,它何时出现:
Specialized JsonParseException that is thrown when end-of-input is reached unexpectedly, either within token being decoded, or during skipping of intervening white-space that is not between root-level tokens (that is, is within JSON Object or JSON Array construct).
特殊的 JsonParseException 在意外到达输入结束时抛出,无论是在被解码的令牌内,还是在跳过不在根级令牌之间的中间空白期间(即,在 JSON 对象或 JSON 数组构造内) .
It is used in internal com.fasterxml.jackson.core.base.ParserMinimalBase
parser class which is extended by many specific parsers such as UTF8StreamJsonParser
.它用于内部
com.fasterxml.jackson.core.base.ParserMinimalBase
解析器类,该类由许多特定解析器(例如UTF8StreamJsonParser
。 One from many methods which is used to handle EOF
:用于处理
EOF
许多方法之一:
protected void _reportInvalidEOF(String msg, JsonToken currToken) throws JsonParseException {
throw new JsonEOFException(this, currToken, "Unexpected end-of-input"+msg);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.