How to use ByteArrayOutputStream and DataOutputStream simultaneously in Java?
Asked Answered
P

6

13

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.

I need to write an int and a byte[] into a 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().

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

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.

How should I use this classes together?

Pasquil answered 6/6, 2010 at 14:32 Comment(0)
M
42

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. You can't do new DataOutputStream(). There isn't actually any constructor like that.

Microdont answered 6/6, 2010 at 14:35 Comment(4)
I've always wondered, is there really I reason to flush a wrapped ByteArrayOutputStream?Raasch
there isn't but you never know the behaviour of a wrapper. It could actually buffering something so I flush the wrapper by habit :). If it's a pass-thru wrapper it shouldn't hurt.Microdont
can you declare the byteArray variable? and what actually w.write(byteArray); do?Upolu
ah its working now, there is some problem on my hex editor (i wrote it to file just to prove it and it works)Upolu
G
3

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

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeInt(1);
byte[] result = dos.toByteArray();
Gallnut answered 6/6, 2010 at 14:41 Comment(0)
F
2

Use the former case - wrap DataOutputStream around the ByteArrayOutputStream. Just make sure you save the reference to the ByteArrayOutputStream. When you are finished, close() or at least flush() the DataOutputStream and then use the toByteArray method of the ByteArrayOutputStream.

Fuqua answered 6/6, 2010 at 14:36 Comment(0)
U
1

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.

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

  • put(byte[] src) for your byte[]
  • putInt(int value)
  • and byte[] array() to get the content
Upstanding answered 6/6, 2010 at 14:40 Comment(0)
L
1

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();
Launceston answered 3/4, 2017 at 16:10 Comment(1)
Welcome to stackoverflow! Please consider formatting code with markdown so it's easier to read. More info: stackoverflow.com/editing-helpMaloney
F
0

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

Fuddle answered 6/6, 2010 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.