Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Help on ByteArrayInputStream

Thread view: 
K.Z.Zamli@ncl.ac.uk - 13 Jun 2007 06:57 GMT
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.
Gordon Beaton - 13 Jun 2007 07:45 GMT
> 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

--
Roedy Green - 15 Jun 2007 10:56 GMT
>The question is how can I save the bais (ByteArrayInputStream) as a
>binary file ?

The whole point of a ByteArrayInputStream is to get you a byte[].

Now your problem is how to write that byte[] to a file.
The file I/O Amanuensis will show you dozens of ways to do that.

see http://mindprod.com/applets/fileio.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.