Java 中的内存流 [英] Memory Stream in Java

查看:72
本文介绍了Java 中的内存流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 Java 中的内存流实现.实施应该在 .NET 内存流 实现之后大致建模.

基本上我想要一个类 MemoryStream 必须工厂方法:

 class MemoryStream {内存输入 createInput();内存输出 createOutput();}类 MemoryInput 扩展 InputStream {多头头寸();无效寻求(长位置);}类 MemoryOutput 扩展了 OutputStream {多头头寸();无效寻求(长位置);}

所以一旦我从 MemoryStream 类中获得了一个实例,我应该能够同时创建输入和输出流,这也应该允许任意方向定位.内存流需要不是圆形的,它应该适用于小尺寸并自动增长.内存流只需要被限制在一个过程中.

有可用的开箱即用代码吗?

解决方案

ByteArrayInputStreamByteArrayOutputStream 就是你要找的.

这些是接口InputStreamOutputStream 的实现,它们从内存中的字节数组中读取和写入.对于 ByteArrayOutputStream,当您将数据写入流时,数组将自动增长.

I am looking for a memory stream implementation in Java. The implementation should be roughly modeled after the .NET memory stream implementation.

Basically I would like to have a class MemoryStream which has to factory methods:

 class MemoryStream {
     MemoryInput createInput();
     MemoryOutput createOutput();
 }

 class MemoryInput extends InputStream {
    long position();
    void seek(long pos);
 }

 class MemoryOutput extends OutputStream {
    long position();
    void seek(long pos);
 }

So once I have an instance from the class MemoryStream I should be able to concurrently simultaneously create input and output streams, which should also allow positioning in any direction. The memory stream need not be circular, it should work for small sizes well and automatically grow. The memory stream need only be confined into one process.

Any out of the box code available?

解决方案

ByteArrayInputStream and ByteArrayOutputStream is what you are looking for.

These are implementations of the interfaces InputStream and OutputStream that read from and write to a byte array in memory. For ByteArrayOutputStream, the array will grow automatically as you write data to the stream.

这篇关于Java 中的内存流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆