ByteArrayInputStream

Hi i have a simple question about ByteArrayInputStream class.

if i create a new ByteArrayInputStream object like this

byte data[] = new byte[2000];

ByteArrayInputStream bin = new ByteArrayInputStream(data);

Can i still change the value of mmy byte array data after creating the ByteArrayInputStream ? Of if the value in my by array change do i need to create a new ByteArrayInputStream object each time?

thanks a lot

sebastien

[469 byte] By [sebvena] at [2007-11-26 18:26:35]
# 1

A good question!

The API does not state this explicitly, this portion might be a hint that changes in the byte array might affect the stream.

protected byte[] buf

// An array of bytes that was provided by the creator of the stream.

For a given version, you can look into the source of ByteArrayInputStream and see whether it is the case. But if the API does not mandate (imply) a behaviour, it can change with a next version, and its use can be considered as unclean programming.

BIJ001a at 2007-7-9 6:00:39 > top of Java-index,Java Essentials,Java Programming...
# 2

Have a look at that Stream's ctor:public ByteArrayInputStream(byte buf[]) {

this.buf = buf;

this.pos = 0;

this.count = buf.length;

}

It doesn't copy the array so you're still the owner and you can do with

its contents whatever you like. Note though that those bytes that have

not yet been read will read the changed values (if any) in a next read()

call. The bytes that have been read will never be read again, so

changing those bytes will go by unnoticed unless you mark the old

position and reset to it again.

kind regards,

Jos

JosAHa at 2007-7-9 6:00:39 > top of Java-index,Java Essentials,Java Programming...
# 3
Also if a buffered stream is built around the ByteArrayInputStream, the old contents might have been buffered and changes to the underlying byte array may go unnoticed.
BIJ001a at 2007-7-9 6:00:39 > top of Java-index,Java Essentials,Java Programming...
# 4

> Also if a buffered stream is built around the ByteArrayInputStream, the

> old contents might have been buffered and changes to the underlying

> byte array may go unnoticed.

Yup; it can be tricky but at all times you are allowed to modify the content

of the byte array as far as the ByteArrayInputStream is concerned.

If those changes come through w.r.t. rappingbuffered readers (as in

your scenario) is quite another question ;-)

kind regards,

Jos

JosAHa at 2007-7-9 6:00:39 > top of Java-index,Java Essentials,Java Programming...
# 5
JoshAH... if u there.. plz contact me....in this forum
mzbarz1986a at 2007-7-9 6:00:39 > top of Java-index,Java Essentials,Java Programming...
# 6

hi thanks for the multiple replis. i mad emy own tests and the ByteArrayInputStream does not change if we update the array which has been passed in its constructor. So each time a new ByteArrayInputStream needs to be created when the array changes.

What i try to do is my own inputStream and i need some advice to that side. i was trying to do something like this:

created ONE inputstream to pass have it get data from the network reading.

i.e. the input stream would get notified when new data has arrived and would awake who ever called read methods (which are blocked by using wait() if no data is available)"

I tries to extend a class with inpuStream but i am a bit stuck. If somehow understand what i try to do i would be happy to have some guidelines.

regards

seb

sebvena at 2007-7-9 6:00:39 > top of Java-index,Java Essentials,Java Programming...
# 7

here is what i coded:

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

public class VideoInputStream extends InputStream{

private ByteArrayInputStream bin;

private Object dataAvailable;

public VideoInputStream(){

dataAvailable = new Object();

}

public int read() throws IOException {

synchronized (dataAvailable) {

try {

dataAvailable.wait();

} catch (InterruptedException ignore){}

}

return bin.read();

}

public void setData(byte[] data){

bin = new ByteArrayInputStream(data);

synchronized (dataAvailable) {

dataAvailable.notify();

}

}

}

any comments are welcome

sebvena at 2007-7-9 6:00:39 > top of Java-index,Java Essentials,Java Programming...
# 8

If the buffer's changing afterwards would not affect a ByteArrayInputStream built around it, the following program would print 0. It prints 133693440 however.

import java.io.*;

public class a

{

public static void main( String a[] ) throws Exception

{

int i;

bytebuffer[] = new byte[ 0x100000 ];

InputStream is = new ByteArrayInputStream( buffer );

for ( i = 0; i < buffer.length; i++ )

{

buffer[ i ] = ( byte ) ( i % 0x100 );

}

int r, ct = 0;

while ( ( r = is.read() ) != -1 )

{

ct += r;

}

System.out.println( ct );

}

}

BIJ001a at 2007-7-9 6:00:39 > top of Java-index,Java Essentials,Java Programming...
# 9
You create a new ByteArrayInputStream every time setData is called:bin = new ByteArrayInputStream(data);
BIJ001a at 2007-7-9 6:00:39 > top of Java-index,Java Essentials,Java Programming...
# 10
> JoshAH... if u there.. plz contact me....in this forum? whiskey tango foxtrot roger; I repeat whiskey tango foxtrot.kind regards andO&O&<something I forgot, Sabre and Ceci know this stuff ;-)>Jos
JosAHa at 2007-7-9 6:00:39 > top of Java-index,Java Essentials,Java Programming...
# 11
yes i want to be able to read the updated value of my array using the same ByteArrayInputStream object since it is not possible i create a new ByteArrayInputStream object each time.thanksseb
sebvena at 2007-7-9 6:00:39 > top of Java-index,Java Essentials,Java Programming...
# 12

> What i try to do is my own inputStream and i need

> some advice to that side. i was trying to do

> something like this:

> created ONE inputstream to pass have it get data from

> the network reading.

> i.e. the input stream would get notified when new

> data has arrived and would awake who ever called read

> methods (which are blocked by using wait() if no data

> is available)"

Why all the elaboration? Why can't they just block in the normal socket read() method?

ejpa at 2007-7-9 6:00:39 > top of Java-index,Java Essentials,Java Programming...