java Java序列化,ObjectInputStream.readObject(),检查是否会阻塞

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1501378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:47:25  来源:igfitidea点击:

Java serialization, ObjectInputStream.readObject(), check if will block

javaserializationnonblocking

提问by Mike

I'm using an ObjectInputStreamto call readObjectfor reading in serialized Objects. I would like to avoid having this method block, so I'm looking to use something like Inputstream.available().

我正在使用 anObjectInputStream来调用readObjectserialized 中的读取Objects。我想避免使用此方法块,因此我希望使用类似Inputstream.available().

InputStream.available()will tell you there are bytes available and that read()will not block. Is there an equivalent method for seriailzation that will tell you if there are Objects available and readObjectwill not block?

InputStream.available()会告诉你有可用的字节并且read()不会阻塞。是否有等效的序列化方法可以告诉您是否有Object可用且readObject不会阻塞?

回答by Tom Hawtin - tackline

No. Although you could use the ObjectInputStreamin another thread and check to see whether that has an object available. Generally polling isn't a great idea, particularly with the poor guarantees of InputStream.available.

不。虽然您可以ObjectInputStream在另一个线程中使用 并检查它是否有可用的对象。一般来说,轮询不是一个好主意,特别是在InputStream.available.

回答by David Crawshaw

The Java serialization API was not designed to support an available()function. If you implement your own object reader/writer functions, you can read any amount of data off the stream you like, and there is no reporting method.

Java 序列化 API 并非旨在支持available()功能。如果您实现自己的对象读取器/写入器功能,则可以从您喜欢的流中读取任意数量的数据,并且没有报告方法。

So readObject()does not know how much data it will read, so it does not know how many objects are available.

所以readObject()不知道它会读取多少数据,所以它不知道有多少对象可用。

As the other post suggested, your best bet is to move the reading into a separate thread.

正如另一篇文章所建议的那样,最好的办法是将阅读内容移到一个单独的线程中。

回答by Vladimir Dyuzhev

I have an idea that by adding another InputStream into the chain one can make availability information readable by the client:

我有一个想法,通过将另一个 InputStream 添加到链中,可以使客户端可以读取可用性信息:

HACK!

哈克!

InputStream is = ... // where we actually read the data
BufferedInputStream bis = new BufferedInputStream(is);
ObjectInputStream ois = new ObjectInputStream(bis);

if( bis.available() > N ) {
  Object o = ois.readObject();
}

The tricky point is value of N. It should be big enough to cover both serialization header and object data. If those are varying wildly, no luck.

棘手的一点是 N 的值。它应该足够大以覆盖序列化标头和对象数据。如果这些变化很大,那就没有运气了。

回答by Tjen Wellens

The BufferedInputStream works for me, and why not just check if(bis.available() > 0) instead of a N value, this works perfectly for me. I think ObjectInputStream.readObject blocks(= waits until) when no input is to be read. So if there is any input at all in the stream aka if(bis.available() > 0) ObjectInputStream.readObject will not block. Keep in mind that ObjectInputStream.readObject might throw a ClassNotFoundException, and that is't a problem at all to me.

BufferedInputStream 对我有用,为什么不检查 if(bis.available() > 0) 而不是 N 值,这对我来说非常有用。我认为 ObjectInputStream.readObject 阻止(= 等到)当没有输入被读取时。因此,如果流中有任何输入,也就是 if(bis.available() > 0) ObjectInputStream.readObject 将不会阻塞。请记住,ObjectInputStream.readObject 可能会抛出 ClassNotFoundException,这对我来说根本不是问题。