ByteBuffer 流

概述 Java NIO 中提供了一个可以进行高效 IO 操作 ByteBuffer 类,但是 ByteBuffer 在使用上有一点不便,就是分配的空间大小是确定的。这就像数组,而不能像 List 一样自动扩容。Java 还提供了可以自动扩容的输入输出字节数组流(ByteArrayInputStream 和 ByteArrayOutputStream),但是它们底层是用数组实现的,而不能直接操作 ByteBuffer。

为了更方便的操作 ByteBuffer,Hprose 提供了基于 ByteBuffer 的输入输出流,可以在 ByteBuffer 上进行流式操作。这部分包括了三个类:

ByteBufferStream ByteBufferInputStream ByteBufferOutputStream ByteBufferStream 类是这部分的核心,它包含了输入输出流的实现。

ByteBufferInputStream 类是对 ByteBufferStream 类的一个 InputSteam 包装。

ByteBufferOutputStream 类是对 ByteBufferStream 类的一个 OutputSteam 包装。

这三个类包含在 hprose.io 包中。

下面重点介绍 ByteBufferStream 类。

  1. ByteBufferStream 类
  2. ByteBufferStream 构造器
  3. public ByteBufferStream();
  4. public ByteBufferStream(int capacity);
  5. public ByteBufferStream(ByteBuffer buffer);
  6. 无参构造器创建一个容量大小为 1024 的 ByteBufferStream 对象。包含 capacity 参数的构造器创建一个容量大小为 capacity 的 ByteBufferStream 对象。通常,这两种构造器创建的 ByteBufferStream 对象用于写数据。包含 buffer 参数的构造器创建一个以 buffer 为底层操作对象的 ByteBufferStream 对象,通常,该构造器创建的 ByteBufferStream 对象用于读数据。

静态方法

allocate 方法

public final static ByteBuffer allocate(int capacity); 分配一个容量大小为 capacity 的直接内存的 ByteBuffer 对象。该方法功能 ByteBuffer.allocateDirect 类似,但是它会优先使用 ByteBufferStream 内存池中的已分配的空闲对象作为返回结果。

free 方法

public final static void free(ByteBuffer buffer); 将 ByteBuffer 对象放入 ByteBufferStream 内存池,这里的 buffer 应该是在直接内存上分配的 ByteBuffer 对象,并且容量不小于 1024 且为 2 的 n 次方,否则将直接释放。

wrap 方法

public final static ByteBufferStream wrap(byte[] array, int offset, int length); public final static ByteBufferStream wrap(byte[] array); 将字节数组 array 包装为一个 ByteBufferStream 对象并返回。

实例方法

close 方法 关闭并回收该对象所包含的 ByteBuffer 对象。

getInputStream 方法

返回该对象的 InputStream 包装。

getOutputStream 方法

返回该对象的 OutputStream 包装。

read 方法

public final int read(); public final int read(byte b[]); public final int read(byte b[], int off, int len); public final int read(ByteBuffer b); 无参的 read 方法读取一个字节并返回。

其它三种形式将结果读入到 b 中,并返回读入长度。

如果已经读到结尾,则返回 -1。

skip 方法 public final long skip(long n); 针对读取操作,略过长度为 n 的字节。

available 方法

public final int available(); 返回还可以读取的字节数。

markSupported 方法

永远返回 true,表示是否支持 mark 操作。

mark 方法

public final void mark(int readlimit); 标记当前流的读取位置。该位置可以通过 reset 方法恢复。 readlimit 参数仅为兼容 InputStream,本方法在实际执行时,忽略该参数。

reset 方法

恢复 mark 方法标记的位置。

write 方法

public final void write(int b); public final void write(byte b[]); public final void write(byte b[], int off, int len); public final void write(ByteBuffer b); 将指定的字节,数组或 ByteBuffer 写入到 ByteBufferStream 对象中。

flip 方法

如果当前读取位置不为 0,则执行底层 ByteBuffer 上的 flip 操作。否则,什么也不做。

rewind 方法

执行底层 ByteBuffer 上的 rewind 方法。

toArray 方法

将该对象中数据以字节数组形式返回。

readFrom 方法

public final void readFrom(InputStream istream) throws IOException; 从 istream 中读取数据到该 ByteBufferStream 对象。

public final void readFrom(ByteChannel channel, int length) throws IOException; 从 channel 中读取长度为 length 的数据到该 ByteBufferStream 对象。

writeTo 方法

public final void writeTo(OutputStream ostream) throws IOException; 将该 ByteBufferStream 对象中的数据写入到 ostream 中。

public final void writeTo(ByteChannel channel) throws IOException; 将该 ByteBufferStream 对象中的数据写入到 channel 中。