how to use ByteArrayOutputStream and DataOutputStream simultaneously (Java)

Posted by Manuel on Stack Overflow See other posts from Stack Overflow or by Manuel
Published on 2010-06-06T14:32:26Z Indexed on 2010/06/06 14:42 UTC
Read the original article Hit count: 136

Hi! 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's 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?

© Stack Overflow or respective owner

Related posts about java

Related posts about wrapper