Feed an InputStream with byte arrays

Discussion in 'Java' started by Ziwirila, Dec 21, 2009.

  1. Ziwirila

    Ziwirila Guest

    Hi,

    I have a function that receives many byte[].
    Is there a way to feed incrementally an InputStream with these byte arrays ?

    I would like to avoid to write to file or use a socket.

    Thanks
    Ziwirila, Dec 21, 2009
    #1
    1. Advertising

  2. RedGrittyBrick, Dec 21, 2009
    #2
    1. Advertising

  3. Ziwirila

    Ziwirila Guest

    "RedGrittyBrick wrote
    >
    > Ziwirila wrote:
    >> Hi,
    >>
    >> I have a function that receives many byte[].
    >> Is there a way to feed incrementally an InputStream with these byte
    >> arrays ?
    >>
    >> I would like to avoid to write to file or use a socket.
    >>

    >
    > Is this what you were thinking of
    > http://java.sun.com/javase/6/docs/api/java/io/ByteArrayInputStream.html
    >
    >
    > --
    > RGB


    I have seen it. It works perfectly with the first byte array.
    Then how to append the second byte array to the input stream ?
    Ziwirila, Dec 21, 2009
    #3
  4. Ziwirila

    Roedy Green Guest

    On Mon, 21 Dec 2009 12:02:30 +0100, "Ziwirila" <> wrote,
    quoted or indirectly quoted someone who said :

    >
    >I have seen it. It works perfectly with the first byte array.


    you could read a DataInputStream with arrays in it, or if you had an
    ever greater variety of objects passed, a ObjectInputStream.

    For sample code see http://mindprod.com/applet/fileio.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    If you think it’s expensive to hire a professional to do the job, wait until you hire an amateur.
    ~ Red Adair (born: 1915-06-18 died: 2004-08-07 at age: 89)
    Roedy Green, Dec 21, 2009
    #4
  5. On 12/21/2009 12:02 PM, Ziwirila wrote:
    > "RedGrittyBrick wrote
    >> Ziwirila wrote:
    >>> Hi,
    >>>
    >>> I have a function that receives many byte[].
    >>> Is there a way to feed incrementally an InputStream with these byte
    >>> arrays ?
    >>>
    >>> I would like to avoid to write to file or use a socket.
    >>>

    >> Is this what you were thinking of
    >> http://java.sun.com/javase/6/docs/api/java/io/ByteArrayInputStream.html


    > I have seen it. It works perfectly with the first byte array.
    > Then how to append the second byte array to the input stream ?


    It is not really clear what you are after. If you want to just store
    the content of all your byte arrays somewhere: you need some form of
    *Output*Stream. Either, you need an ByteArrayOutputStream to write your
    byte arrays to if you just want to store the data somewhere. Or you
    want a pair of PipedInputStream and PipedOutputStream if you want to
    fill your input side from one thread and read via a different thread.

    Kind regards

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Dec 21, 2009
    #5
  6. In article <2s.com>,
    Steven Simpson <> wrote:

    > On 21/12/09 11:02, Ziwirila wrote:
    > > "RedGrittyBrick wrote
    > >> Ziwirila wrote:
    > >>
    > >>> I have a function that receives many byte[].
    > >>> Is there a way to feed incrementally an InputStream with these byte
    > >>> arrays ?
    > >>>
    > >> Is this what you were thinking of
    > >> http://java.sun.com/javase/6/docs/api/java/io/ByteArrayInputStream.html
    > >>

    > > I have seen it. It works perfectly with the first byte array.
    > > Then how to append the second byte array to the input stream ?
    > >

    >
    > http://java.sun.com/javase/6/docs/api/java/io/SequenceInputStream.html
    >
    > You could provide it with an Enumeration of ByteArrayInputStreams. If
    > you don't have them all ready when you create the Enumeration, you'll
    > need to write an Enumeration that blocks on
    > nextElement()/hasMoreElements() until either another InputStream is
    > supplied or the Enumeration is 'closed'.
    >
    > Or you could just use PipedInput/OutputStream, but note that the pipe is
    > of fixed size and will block write operations when full.


    Sun's piped I/O streams have such terrible buffering that they should
    generally be avoided. Last time I tried them I got only a few KB/sec on
    a 4 core box.
    --
    I won't see Google Groups replies because I must filter them as spam
    Kevin McMurtrie, Dec 22, 2009
    #6
  7. Ziwirila

    Ziwirila Guest

    "Robert Klemme" wrote
    > Ziwirila wrote:
    >> "RedGrittyBrick wrote
    >>> Ziwirila wrote:
    >>>> Hi,
    >>>>
    >>>> I have a function that receives many byte[].
    >>>> Is there a way to feed incrementally an InputStream with these byte
    >>>> arrays ?
    >>>>
    >>>> I would like to avoid to write to file or use a socket.
    >>>>
    >>> Is this what you were thinking of
    >>> http://java.sun.com/javase/6/docs/api/java/io/ByteArrayInputStream.html

    >
    >> I have seen it. It works perfectly with the first byte array.
    >> Then how to append the second byte array to the input stream ?

    >
    > It is not really clear what you are after. If you want to just store the
    > content of all your byte arrays somewhere: you need some form of
    > *Output*Stream. Either, you need an ByteArrayOutputStream to write your
    > byte arrays to if you just want to store the data somewhere. Or you want
    > a pair of PipedInputStream and PipedOutputStream if you want to fill your
    > input side from one thread and read via a different thread.
    >

    Thanks all for your responses.
    The PipedInputStream and PipedOutputStream is what I was looking for.
    Ziwirila, Dec 22, 2009
    #7
  8. On 22-12-2009 00:31, Kevin McMurtrie wrote:
    > In article<2s.com>,
    > Steven Simpson<> wrote:
    >> Or you could just use PipedInput/OutputStream, but note that the pipe is
    >> of fixed size and will block write operations when full.

    >
    > Sun's piped I/O streams have such terrible buffering that they should
    > generally be avoided. Last time I tried them I got only a few KB/sec on
    > a 4 core box.


    How did you manage to do that?

    I get 100KB-1MB per second for a single core. And that
    is using single byte read and write.

    Arne
    Arne Vajhøj, Jan 2, 2010
    #8
    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. Arun
    Replies:
    2
    Views:
    14,478
    William Brogden
    Dec 27, 2004
  2. R
    Replies:
    5
    Views:
    2,113
    Kevin McMurtrie
    Mar 13, 2005
  3. Replies:
    1
    Views:
    537
    Roedy Green
    Oct 9, 2005
  4. Philipp
    Replies:
    21
    Views:
    1,123
    Philipp
    Jan 20, 2009
  5. Replies:
    2
    Views:
    175
    xDog Walker
    Feb 8, 2013
Loading...

Share This Page