Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4072863

available() method in inputStream from process object always returns 0

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.1.2, 1.4.2
    • core-libs
    • x86, sparc
    • solaris_2.5.1, windows_xp



      Name: rlT66838 Date: 08/19/97


      I submitted this bug a while back, and just received
      a request to submit it again. The latest version
      I have tested it on is 1.1.2, but I saw nothing to
      indicate that it was fixed on 1.1.3.

      When I use the getInputStream() method on a process, then attempt
      to use the available() method on that inputStream, the value returned
      is always 0. This only appears to occur under Solaris, because
      I have successfully run the same code under Windows 95.
      Code to reproduce the bug is included below:

      Firstly, the Test class execs a TestChild, then gets the input and output
      streams, and writes "Blak\n" to the output. It then attemps to read
      from the input using the available() method to check for incoming data:


      import java.lang.*;
      import java.io.*;


      class Test {

        private static Process bidders;
        private static InputStream bids;
        private static OutputStream yell;

        public static void main (String[] args) throws InterruptedException {
          loadChild();
          yellAll("Blak\n");
          /*while (true) {
            try {String blah = getProcString(bids);
                 if (blah != "") {System.out.println(blah);}
                 } catch (IOException e) {}
          }*/
          bidders.waitFor();
        }

        //Test () {}

        public static void yellAll(String message) {
          byte[] byteMessage = new byte[20];
          System.out.println("Writing message: "+message);
          message.getBytes(0, message.length(), byteMessage, 1);
          ByteArrayInputStream l = new ByteArrayInputStream(byteMessage);

          try { yell.write(byteMessage);
          yell.flush();
          //yell.close();
          } catch (IOException e) {
            System.err.println("Error writing to process.\nError code: " + e);
          }
        } //End yellAll method



        private static void loadChild() {

          // try {
          try {
            bidders = Runtime.getRuntime().exec("java TestChild");
            bids = bidders.getInputStream();
            yell = bidders.getOutputStream();
          } catch (IOException e) {
            System.err.print("Bad news, I couldn't read from the file.\n Error code: " + e + ".\n");
          }
        } // End of method loadChild.


        private static String getProcString(InputStream input) throws IOException {
          int length = input.available(); //Under Solaris, this always returns 0.
          if (length != 0) {
            byte[] data = new byte[length];
            try {
              int i = input.read(data);
            } catch (IOException e) {}
            ByteArrayInputStream transStream = new ByteArrayInputStream(data);
            String retval = "";
            while (retval.length() < length /*&& (!retval.endsWith(" "))*/) {
              retval += (char)transStream.read();
            }
            return (retval.trim());
          }
          else {
            //happy = false;
            return "";
          }
        }


      }//End of Test class

      **************************************************

      The TestChild class should read input, and bounce any one line of
      input back through System.out. I've tested this by instead writing
      to a file, and it is receiving the data perfectly. It is also writing
      back perfectly, because when I removed the (available != 0) condition,
      the read() method in Test blocked for a while and then read the data.


      import java.lang.*;
      import java.io.*;


      class TestChild {
        public static void main (String[] args) {
         try {
          InputStream input = System.in;
          byte[] blah = new byte[1000];
          try {
            int i = input.read(blah);
          } catch (IOException e) {}
          ByteArrayInputStream BAS = new ByteArrayInputStream(blah);
          String readstuff = "";
          while ((BAS.available() > 0) && !(readstuff.endsWith("\n"))) {
            readstuff += (char)BAS.read();
          }
          FileOutputStream out = new FileOutputStream("TestOut2.txt");
          byte[] eeep = new byte[readsuff.length()];
          readstuff.getBytes(0, readstuff.length(), eeep, 0);
          out.write(eeep);
         } catch (IOException e) {}

        }

      }


      ======================================================================

            apalanissunw Anand Palaniswamy (Inactive)
            rlewis Roger Lewis (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: