InputStreamReader

Hi guys,

What I need to know is a way of knowing when the stream has been al read.

Reader objReader =new BufferedReader(new InputStreamReader(this.objInputStream));

while(!objReader.ready());

byteContent =newbyte[this.objInputStream.available()];

this.objInputStream.read(byteContent);

The ready method returns true as soon as some data arrive thus, I don't really know how to do this, can you guys point me some directions.

Many Thanks,

MeTitus

[669 byte] By [Me_Titusa] at [2007-11-27 8:38:38]
# 1

Try this.

String line = null;

BufferedReader br = new BufferedReader(your stuff here);

while((line = br.readLIne()) != null) {

//do your work

}

br.readLine() will return a null when the end of the stream is reached, Or if you're using the various read calls, they will return the number of bytes that have been read, and will return -1 when the end of the stream is reached.

You have to be careful with this if you're doing network io for example, because the call to read can return 0 bytes, but still not be closed. :-)

billrobertson42a at 2007-7-12 20:36:23 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks for your reply billrobertson42,

but there are two things:

- I don't know whether the server will or not, send the end of lne,

- I don't know the length of the stream.

So if the server doesn't write the carriage return to the outputStream the loop will continue indefinitely.

Thanks,

MeTitus

Me_Titusa at 2007-7-12 20:36:23 > top of Java-index,Java Essentials,Java Programming...
# 3
read() returns -1readLine() returns nullreadXXX() throws EOFException
ejpa at 2007-7-12 20:36:23 > top of Java-index,Java Essentials,Java Programming...