gpt4 book ai didi

java - 缓冲短 I/O

转载 作者:行者123 更新时间:2023-12-02 07:24:46 24 4
gpt4 key购买 nike

如何在短缓冲区中进行读/写?

我正在尝试为短值实现 BufferedReader 和 Writer。每次都会传递一个短[]并读取一个短[]。

但是java API没有这个接口(interface),只有byte[]。

实现此功能的最佳方式是什么?

最佳答案

那么,对于您的 BufferedInputStream(不是读取器),您可以尝试同时读取 2 个字节:

public synchronized int read(short[] s, int off, int len) throws IOException {
byte[] b = new byte[s.length * 2];
int read = read(b, off * 2, len * 2);
for (int i = 0; i < read; i+=2) {
int b1 = b[i];
int b2 = b[i+1];
s[i/2] = (short) ((b1 << 8) | b2);
}
return read / 2;
}

对于您的 BufferedOutputStream(不是 writer),您可以尝试同时写入 2 个字节的相反操作。

关于java - 缓冲短 I/O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13688377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com