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. Advertisements

  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. Advertisements

  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. Advertisements

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. =?Utf-8?B?dHJlbGxvdzQyMg==?=

    HELP! HELP! HELP! Opening Web Application Project Error

    =?Utf-8?B?dHJlbGxvdzQyMg==?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    965
    =?Utf-8?B?dHJlbGxvdzQyMg==?=
    Feb 20, 2004
  2. Harvey
    Replies:
    0
    Views:
    1,051
    Harvey
    Jul 16, 2004
  3. Harvey
    Replies:
    1
    Views:
    1,136
    Daniel
    Jul 16, 2004
  4. rajesh.rapaka
    Replies:
    2
    Views:
    509
    rajesh.rapaka
    Mar 18, 2005
  5. Sard
    Replies:
    1
    Views:
    447
    Michael Rauscher
    Mar 5, 2007
  6. Replies:
    2
    Views:
    572
    Roedy Green
    Dec 29, 2007
  7. Krist
    Replies:
    1
    Views:
    931
    Amit Jain
    Jan 31, 2010
  8. Krist
    Replies:
    6
    Views:
    2,767
Loading...