Feed an InputStream with byte arrays

Z

Ziwirila

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
 
Z

Ziwirila

R

Robert Klemme

"RedGrittyBrick wrote
Ziwirila said:
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
 
K

Kevin McMurtrie

Steven Simpson said:
"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.
 
Z

Ziwirila

Ziwirila said:
"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.
 
A

Arne Vajhøj

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top