Help on ByteArrayInputStream

K

K.Z.Zamli

Dear all

I am having a problem to save from ByteArrayInputStream to file..
Refer to the following excerpt of code:

...............
DatagramPacket receivePacket = new
DatagramPacket (buffer, buffer.length);
socket.receive(receivePacket);

System.out.println("Packet received");
System.out.println("\nFrom host" + receivePacket.getAddress() +
"\nHost port" + receivePacket.getPort() +
"\nLength" + receivePacket.getLength()); // +
// "Word" + s);

ByteArrayInputStream bais = new

ByteArrayInputStream(receivePacket.getData(),
0,receivePacket.getLength());


The question is how can I save the bais (ByteArrayInputStream) as a
binary file ?

Any help is really appreciated. Thanks in advanced.
 
G

Gordon Beaton

ByteArrayInputStream bais = new ByteArrayInputStream(receivePacket.getData(),
0,receivePacket.getLength());

The question is how can I save the bais (ByteArrayInputStream) as a
binary file ?

If you just want to save the datagram contents, there's no need for
the ByteArrayInputStream here at all.

Use DatagramPacket.getData() to get the data from the packet as a byte
array. Use a FileOutputStream to write the contents of the byte array
to a file.

/gordon

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top