3 Replies Latest reply: Jun 24, 2007 9:38 PM by EJP RSS

    InputStreamReader

    807605
      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 = new byte[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
        • 1. Re: InputStreamReader
          807605
          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. :-)
          • 2. Re: InputStreamReader
            807605
            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
            • 3. Re: InputStreamReader
              EJP
              read() returns -1
              readLine() returns null
              readXXX() throws EOFException