Help on ByteArrayInputStream

Discussion in 'Java' started by K.Z.Zamli@ncl.ac.uk, Jun 13, 2007.

  1. Guest

    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.
     
    , Jun 13, 2007
    #1
    1. Advertising

  2. On Tue, 12 Jun 2007 22:57:31 -0700, wrote:
    > 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

    --
     
    Gordon Beaton, Jun 13, 2007
    #2
    1. Advertising

  3. Roedy Green Guest

    On Tue, 12 Jun 2007 22:57:31 -0700, wrote, quoted
    or indirectly quoted someone who said :

    >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
     
    Roedy Green, Jun 15, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rajesh.rapaka
    Replies:
    2
    Views:
    431
    rajesh.rapaka
    Mar 18, 2005
  2. Sard
    Replies:
    1
    Views:
    388
    Michael Rauscher
    Mar 5, 2007
  3. Replies:
    2
    Views:
    497
    Roedy Green
    Dec 29, 2007
  4. Krist
    Replies:
    1
    Views:
    852
    Amit Jain
    Jan 31, 2010
  5. Krist
    Replies:
    6
    Views:
    1,967
Loading...

Share This Page