ByteArrayOutputStream用于短路而不是字节

时间:2014-03-19 20:05:10

标签: java short bytearrayoutputstream

我使用了ByteArrayOutputStream非常有用,但根据我的需要,限制太大了(即我处理的数字范围是+ -32,768)

以下是我将使用它的代码:

ByteArrayOutputStream leftStream = new ByteArrayOutputStream();
ByteArrayOutputStream rightStream = new ByteArrayOutputStream();

while (din.read(temp, 0, 4) != -1) {
    if (decodedFormat.getChannels() == 2) {
       leftStream.write(temp[1] * 256 + temp[0]);
       rightStream.write(temp[3] * 256 + temp[2]);
    }
}

byte[] left = leftStream.toByteArray();
byte[] right = rightStream.toByteArray();

然而,ByteArrayInputStream不允许超过127或低于128的值。是否存在允许短路的等效ShortArrayOutputStream?或者我必须以某种方式自己制作一个?

4 个答案:

答案 0 :(得分:2)

是,使用DataOutputStream:

ByteArrayOuputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);

dos.writeShort(val);

这甚至适用于低版本1.3的嵌入式Java设备

阅读使用:

ByteArrayInputStreamDataInputStream dis,以及dis.readShort()

...
byte[] bytes = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
DataInputStream dis = new DataInputStream(bis);
short val = dis.readShort();

答案 1 :(得分:1)

你想做什么?

将短数据写入字节数组?

然后使用DataOutputStream包装您的字节数组输出流,DataOutputStream包含writeShort(),writeInt()等方法。警告。我认为DataOutputStream的endian是BIG端,所以如果你想使用little endian,你必须自己编写或使用其他选项:

ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
DataOutputStream dataout = new DataOutputStream(byteOut)

dataout.writeShort(shortValue);

写一个简短的[]

最简单的方法是创建一个ByteBuffer,然后使用asShortBuffer()方法将其视为ShortBuffer。 ShortBuffer有一个put(short)和put(short []);

如果你想用Little endian写出短数据,ByteBuffer有一个方法asOrder(ByteOrder),它可以改变它正在读或写的数据的字节序。

//NOTE length should be 2* num shorts since we allocate in bytes
ByteBuffer buf = ByteBuffer.allocate(length);

ShortBuffer shortBuf = buf.asShortBuffer();
shortBuf.put(shortValue);
shortBuf.put(shortArray);

从缓冲区中获取数据很烦人。有可选的array()方法,但并非所有缓冲区驱动都支持它们,因此您必须执行以下操作:

//once all data written to buffer
shortBuf.flip();
short[] dataOut = new short[shortBuf.remaining()];
shortBuf.get(dataOut);

两者结合使用未知输入大小的ShortBuffer

如果您不知道要编写多少字节,并且没有合理的最大长度,那么您可能需要将两个选项组合起来。首先,使用选项#1通过向其写入short来动态增长字节缓冲区。然后使用ShortBuffer将byte []转换为short []。

 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
DataOutputStream dataout = new DataOutputStream(byteOut)

dataout.writeShort(shortValue);
...

ShortBuffer buf =ByteBuffer.wrap(byteOut.toByteArray())
                           .asShortBuffer();

int length = buf.remaining();
short[] asShorts = new short[length];
buf.get(asShorts);

它并不漂亮,因为你制作了一个数组副本,所以使用了2倍的内存。

答案 2 :(得分:1)

您可以将ByteArrayOutputStream打包到DataOutputStream

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);

dataOutputStream.writeShort(someShortValue);

答案 3 :(得分:-1)

创建ObjectOutputStream。这有一个writeShort(http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html#writeShort(int))方法。

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeShort(123);
...
相关问题