1 Reply Latest reply: May 12, 2012 2:44 PM by jtahlborn RSS

    InputStream read(byte b[], int off, int len) blocks client application

    934053
      The server code.
      public void writeByteArray(byte var1, byte var2, byte[] value) {
          byte[] bytes = new byte[value.length + 2];
          bytes[0] = var1;
          bytes[1] = var2;
      
          // Add byte array value to byte array bytes
          for(int i = 2; i < (value.length + 2); i++) { 
              bytes[i] = value[i - 2];
          }
      
          // Write byte array bytes to client
          System.out.println("Bytes length: " + bytes.length); // This value needs to set byte array size on client - If this value was for example, 1024 the client would work
          try {
              outputStream.write(bytes, 0, bytes.length);
          } catch (IOException ex) {
              Logger.getLogger(PanelDriverByte.class.getName()).log(Level.SEVERE, "Problem writing to output stream"l, ex);
          }
          try {
              outputStream.flush();
          } catch (IOException ex) {
              Logger.getLogger(PanelDriverByte.class.getName()).log(Level.SEVERE, "Problem flushing output stream"l, ex);
          }
      }
      The client code.
      public byte[] readBytesArray() { // Read all bytes
          int bytesRead = 0;
          int bytesToRead = 1024; // If set to bytes.length the application works;
          byte[] input = new byte[bytesToRead];
          while (bytesRead < bytesToRead) {
              int result = 0;
                  try {
                      result = inputStream.read(input, bytesRead, bytesToRead - bytesRead);
                  } catch (IOException ex) {
                      Logger.getLogger(ClientBytes.class.getName()).log(Level.SEVERE, "Problem reading input stream", ex);
                  }
              System.out.println("Result: " + result);
              if (result == -1) break;
              bytesRead += result;              
          }
          printData(input);
          return input;
      }
      For the application to work I need to set the client bytesToRead variable to the same value as bytes.length on the server.

      The byte[] value variable on the server varies in length each time it is called sometimes the byte array may hold a single value and sometimes it my be an image converted to a byte array. If the client bytesToRead is set too low and value was an image the image does not display and if the value is set too high the client waits for more data that is never going to come. I have tried adding another byte containing -1 to tell the client that all the data has been sent using the following code but did not work. The result variable held the value total number of bytes that was sent rather than -1
      public void writeByteArray(byte var1, byte var2, byte[] value) {
          byte[] bytes = new byte[value.length + 3];
          bytes[0] = var1;
          bytes[1] = var2;
      
          // Add byte array value to byte array bytes
          for(int i = 2; i < (value.length + 2); i++) { 
              bytes[i] = value[i - 2];
          }
           bytes[value.length + 2] = -1;
      
          // Write byte array bytes to client
          System.out.println("Bytes length: " + bytes.length); // This value needs to set byte array size on client - If this value was for example, 1024 the client would work
          try {
              outputStream.write(bytes, 0, bytes.length);
          } catch (IOException ex) {
              Logger.getLogger(PanelDriverByte.class.getName()).log(Level.SEVERE, "Problem writing to output stream"l, ex);
          }
          try {
              outputStream.flush();
          } catch (IOException ex) {
              Logger.getLogger(PanelDriverByte.class.getName()).log(Level.SEVERE, "Problem flushing output stream"l, ex);
          }
      }
      I some how need to set the clients bytesToRead higher than the data being sent from the server then inform the client that all data has been sent.

      If possible I need to make the application work using the inputstream and outputstream read and write methods rather than other sub class stream methods, for example. bufferedInputStream. unless it is impossible for the above code to ever work.