简体   繁体   English

当从底层 InputStream 解析时,Jackson 如何处理流结束读取(例如 -1、EOF)

[英]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. ,其中jsonInputObjectReader对象。

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之一通知EOFJSON对象的“中间”时, 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 JacksonJsonEOFException异常,它何时出现:

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.

相关问题 Java Path 如何转换为例如 InputStream - How is Java Path converted to e.g. InputStream 如何从 Java 的右侧减少有限的 stream(例如 ArrayList)? - How to reduce a limited stream (e.g. an ArrayList) from right in Java? 如何在Android中使用InputStream对象创建受Android支持的视频文件(例如mp4) - How to Create android supported video file (e.g. mp4) using InputStream object in Android 服务器浏览器如何在游戏中工作? - How does a server browser e.g. in games work? 如何处理大量查询参数的 if/else 语句(例如过滤器) - How to deal with a huge number of query params' if/else statement (e.g. filter) java nio SocketChannel.read不返回-1表示流结束 - java nio SocketChannel.read does not return -1 to indicate end-of-stream JAXB解析带有变量的XML文件(例如$(var1)) - JAXB Parsing an XML file with variables (e.g. $(var1) ) 如何在没有嵌套的情况下使用Jackson序列化单值@Data对象(例如{“id”:123},而不是{“id”:{“value”:123}})? - How to serialize single-valued @Data object using Jackson without nesting (e.g. {“id”:123}, not {“id”:{“value”:123}})? 访问元素标签以获取重复的子规则; 例如,使用ANTLR解析“ a IN(1、2、3)” - Accessing element labels for repeated subrules; e.g. parsing “a IN (1, 2, 3)” with ANTLR Java流阅读器一直阻塞到流结束 - Java stream reader blocks until end-of-stream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM