如何同时使用 ByteArrayOutputStream 和 DataOutputStream (Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2984538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:16:24  来源:igfitidea点击:

how to use ByteArrayOutputStream and DataOutputStream simultaneously (Java)

javawrapperbytearrayoutputstreamdataoutputstream

提问by Manuel Aráoz

I'm having quite a problem here, and I think it is because I don't understand very much how I should use the API provided by Java.

我在这里遇到了很大的问题,我认为这是因为我不太明白应该如何使用Java提供的API。

I need to write an intand a byte[]into a byte[]

我需要把一个int和一个byte[]写成一个byte[]

I thought of using a DataOutputStreamto solve the data writing with writeInt(int i)and write(byte[] b), and to be able to put that into a byte array, I should use ByteArrayOutputStreammethod toByteArray().

我想用 aDataOutputStream来解决用writeInt(int i)and写的数据write(byte[] b),为了能够把它放到一个字节数组中,我应该使用ByteArrayOutputStreammethodtoByteArray().

I understand that this classes use the Wrapper pattern, so I had two options:

我知道这些类使用 Wrapper 模式,所以我有两个选择:

DataOutputStream w = new DataOutputStream(new ByteArrayOutputStream());

or

或者

ByteArrayOutputStream w = new ByteArrayOutputStream(new DataOutputStream());

but in both cases, I "loose" a method. in the first case, I can't access the toByteArray()method, and in the second, I can't access the writeInt()method.

但在这两种情况下,我都“放松”了一种方法。在第一种情况下,我无法访问该toByteArray()方法,而在第二种情况下,我无法访问该writeInt()方法。

How should I use this classes together?

我应该如何一起使用这些类?

采纳答案by Mihai Toader

Like this:

像这样:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream w = new DataOutputStream(baos);

w.writeInt(100);
w.write(byteArray);

w.flush();

byte[] result = baos.toByteArray();

Actually your second version will not work at all. DataOutputStreamrequires an actual target stream in which to write the data. You can't do new DataOutputStream(). There isn't actually any constructor like that.

实际上,您的第二个版本根本不起作用。DataOutputStream需要一个实际的目标流来写入数据。你不能做new DataOutputStream()。实际上没有任何这样的构造函数。

回答by President James K. Polk

Use the former case - wrap DataOutputStreamaround the ByteArrayOutputStream. Just make sure you save the reference to the ByteArrayOutputStream. When you are finish close() or at least flush() the DataOutputStreamand then use the toByteArray method of the ByteArrayOutputStream.

使用前一种情况-裹DataOutputStreamByteArrayOutputStream。只要确保您保存对ByteArrayOutputStream. 当您完成 close() 或至少flush() 时DataOutputStream,然后使用ByteArrayOutputStream.

回答by Goibniu

The Integer class has a method to get the byte value of an int. Integer.byteValue()

Integer 类有一个方法来获取一个 int 的字节值。 Integer.byteValue()

回答by Hyman

You could use a stream approach if you connect your outputstream to an inputstream through a PipedInputStream/PipetOutputStream. Then you will consume the data from the inputstream.

如果您通过PipedInputStream/将输出流连接到输入流,则可以使用流方法PipetOutputStream。然后您将使用来自输入流的数据。

Anyway if what you need to do is simple and doesn't not require a stream approach I would use a java.nio.ByteBufferon which you have

无论如何,如果您需要做的很简单并且不需要流方法,我将使用java.nio.ByteBuffer您拥有的

  • put(byte[] src)for your byte[]
  • putInt(int value)
  • and byte[] array()to get the content
  • put(byte[] src)为您 byte[]
  • putInt(int value)
  • byte[] array()获取内容

回答by Yack

Could you make a variable to hold on to the ByteArrayOutputStream and pass it into the DataOutputStream.

您能否创建一个变量来保持 ByteArrayOutputStream 并将其传递到 DataOutputStream 中。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(1);
byte[] result = dos.toByteArray();

回答by mantamusica

You don′t need more like this

你不需要更多这样的

Example exampleExample = method(example); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); marshaller.marshal(exampleExample , baos);
Message message = MessageBuilder.withBody(baos.toByteArray()).build();