[英]How to use ByteArrayOutputStream and DataOutputStream simultaneously in Java?
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 int
and a byte[]
into a byte[]
.我需要将
int
和byte[]
写入byte[]
。
I thought of using a DataOutputStream
to 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 ByteArrayOutputStream
method toByteArray().
我想使用
DataOutputStream
来解决writeInt(int i)
和write(byte[] b)
的数据写入,并且能够将其放入字节数组中,我应该使用ByteArrayOutputStream
方法toByteArray().
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?我应该如何一起使用这些类?
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.实际上,您的第二个版本根本不起作用。
DataOutputStream
requires an actual target stream in which to write the data. DataOutputStream
需要一个实际的目标流来写入数据。 You can't do new DataOutputStream()
.你不能做
new DataOutputStream()
。 There isn't actually any constructor like that.实际上没有任何这样的构造函数。
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();
Use the former case - wrap DataOutputStream
around the ByteArrayOutputStream
.使用前一种情况 - 将
DataOutputStream
包裹在ByteArrayOutputStream
周围。 Just make sure you save the reference to the ByteArrayOutputStream
.只需确保保存对
ByteArrayOutputStream
的引用。 When you are finished, close() or at least flush() the DataOutputStream
and then use the toByteArray method of the ByteArrayOutputStream
.完成后,关闭()或至少刷新()
DataOutputStream
,然后使用ByteArrayOutputStream
的 toByteArray 方法。
You could use a stream approach if you connect your outputstream to an inputstream through a PipedInputStream
/ PipetOutputStream
.如果您通过
PipedInputStream
/ PipetOutputStream
将输出流连接到输入流,则可以使用流方法。 Then you will consume the data from the inputstream.然后您将使用来自输入流的数据。
Anyway if what you need to do is simple and doesn't not require a stream approach I would use a java.nio.ByteBuffer
on which you have无论如何,如果您需要做的很简单并且不需要流方法,我将使用您拥有的
java.nio.ByteBuffer
put(byte[] src)
for your byte[]
put(byte[] src)
为你的byte[]
putInt(int value)
byte[] array()
to get the contentbyte[] array()
来获取内容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();
The Integer class has a method to get the byte value of an int. Integer 类有一个方法来获取一个 int 的字节值。
Integer.byteValue()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.