Buffered ObjectInputStream 是否存在?

发布于 2024-09-12 12:26:52 字数 133 浏览 6 评论 0原文

我正在从一个大小为 350KB 的文件中反序列化一个对象,并且需要相当长的时间。我的计算机科学助教告诉我,有一种方法可以使用 Buffered reader 和 ObjectInputStream 来大大提高性能。然而我在谷歌上找不到任何关于此的信息。

I am deserializing an object from a file that is 350KB in size, and its taking rather a long time. My computer science TA told me that there is a way to use a Buffered reader along with the ObjectInputStream to greatly increase performance. I however can not find anything about this on Google.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城旧梦 2024-09-19 12:26:53

您使用装饰来缓冲输入流。这样

   InputStream in = ...; // your underlying stream (e.g. FileInputStream)
   ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));

就可以保证每次调用ObjectInputStream时都不会调用基流in,比如OS的文件读取系统调用。相反,每个调用都会转到缓冲输入流,该输入流获取并缓存数据块(默认情况下为 8K),并从中读取。这更快,因为从流中读取现在是 java 中的本地方法调用,并且很少会遇到系统调用的方法调用开销。缓存一致性和 JIT 优化也在提高性能方面发挥着作用。

You use decoration to buffer the input stream. Like this

   InputStream in = ...; // your underlying stream (e.g. FileInputStream)
   ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));

This will ensure that each call to ObjectInputStream does not call the base stream in, such as the OS's file read system call. Instead, each call goes to the buffered input stream, which fetches and caches blocks of data (8K by default), and reads from that. This is faster, since reading from the stream is now a local method call in java, and the method call overhead of a system call is encountered less often. Cache coherence and JIT optimizations also come into play in improving performance.

与风相奔跑 2024-09-19 12:26:53

不,但你可以使用
ObjectInputStream(InputStream in) 构造函数

通过将 BufferedInputStream 作为参数传递给上述构造函数来创建缓冲对象输入流。

以下是从文件读取序列化对象的示例:

InputStream file = null;
try {
   file = new FileInputStream("Out.test");
   InputStream buffer = new BufferedInputStream(file);
   ObjectInputStream in = new ObjectInputStream(buffer);
   vector = (Vector)in.readObject();
} catch (Exception e) {
   e.printStackTrace();
} finally{
   if(file != null ) {
       file.close();
   }
}

签出以下链接:

http://java.sun.com/docs/books/performance/1st_edition/html/JPIOPerformance.fm.html

No but You can use
ObjectInputStream(InputStream in) constructor

To create buffered object intput stream by passing BufferedInputStream as argument to above constructor.

Here is example for reading serialized objects from file:

InputStream file = null;
try {
   file = new FileInputStream("Out.test");
   InputStream buffer = new BufferedInputStream(file);
   ObjectInputStream in = new ObjectInputStream(buffer);
   vector = (Vector)in.readObject();
} catch (Exception e) {
   e.printStackTrace();
} finally{
   if(file != null ) {
       file.close();
   }
}

Checkout following link :

http://java.sun.com/docs/books/performance/1st_edition/html/JPIOPerformance.fm.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文