字节数组通过输入流

huangapple go评论73阅读模式
英文:

Byte Array through Input Stream

问题

我正在处理客户端和服务器之间的文件/图像发送和接收。我使用来自Socket的InputStream。

这段代码:

byte[] sizeAr = new byte[4];
int num = inputStream.read();
sizeAr = ByteBuffer.allocate(4).putInt(num).array();
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();

与这段代码是否相同:

byte[] sizeAr = new byte[4];
inputStream.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
英文:

I am working on client-server sending and receiving of files/images. I use inputstream from a Socket.

Is this code

byte[] sizeAr = new byte[4];
int num = inputStream.read();
sizeAr = ByteBuffer.allocate(4).putInt(num).array();
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();

same as this code

byte[] sizeAr = new byte[4];
inputStream.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();

答案1

得分: 1

这段代码[...] 和这段代码是否相同?

不相同。


对于第一个代码段,我感到有些费解:

  1. 分配了一个 4 字节的数组(没有任何原因?)
  2. 从流中读取 一个字节
  3. 创建了一个新的 4 字节缓冲区,
    • 将上一行中读取的字节作为整数放入缓冲区,
    • 获取缓冲区的支持数组,替换了第一行中的数组
  4. 用新的字节缓冲区包装了上一个缓冲区中的数组,
    • 将其转换为整数缓冲区
    • 从此缓冲区获取整数,并将此整数赋值给 size

基本上,这是一种非常复杂且低效的方式,相当于执行了

int size = inputStream.read();

我认为这可能不是你想要的。 字节数组通过输入流

第二个代码段更有意义:

  1. 分配了一个 4 字节的数组
  2. 从输入流中最多读取 4 个字节到数组中(注意应该检查 read(byte[]) 的返回值,以获取实际读取的字节数,可能会比数组大小
  3. 将数组包装在缓冲区中,
    • 将其转换为整数缓冲区
    • 获取值作为整数,并将其赋值给 size

这个版本将会把一个完整的 32 位整数值读入 size 中,这可能是你想要的。然而,第 2 步并不安全,因为你可能会读取少于 4 个字节,正如前面提到的。

也许,一个更好的方式是使用类似于:

DataInput dataInput = new DataInputStream(inputStream);
int size = dataInput.readInt(); // 将会精确地读取 4 个字节,或抛出 EOFException 异常
英文:

> Is this code [...] same as this code?

No.


The first one does not make much sense to me:

  1. allocates a 4 byte array (for no reason?)
  2. reads a single byte from the stream
  3. creates a new 4 byte buffer,
    • puts the byte read in the previous line into the buffer as an int,
    • gets the backing array of the buffer, replacing the array in the first line
  4. wraps the array from the previous buffer in a new byte buffer,
    • converts it to an int buffer
    • gets the int from this buffer, and assigns this int to size

Basically, this is a very convoluted an inefficient way of doing

int size = inputStream.read();

I don't think this is what you want. 字节数组通过输入流

The second one makes more sense:

  1. allocates a 4 byte array
  2. reads up to 4 bytes from the input stream into the array (note that you should check the return value of read(byte[]), to get the number of bytes read, it may be less than the size of your array)
  3. wraps the array in a buffer,
    • converts it to an int buffer
    • gets the value as an int and assigns it to size

This version will read a full 32 bit int value into size, which is probably what you want. However, step 2 is not safe, as you may read less than 4 bytes as mentioned.

Probably, a better way would be to use something like:

DataInput dataInput = new DataInputStream(inputStream);
int size = dataInput.readInt(); // Will read exactly 4 bytes or throw EOFException 

huangapple
  • 本文由 发表于 2020年9月27日 17:25:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64086897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定