code: @Trivial
      private static byte[] getBytes(InputStream stream, int knownSize) throws IOException {
          try {
              if (knownSize == -1) {
                  ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                  try {
                      byte[] bytes = new byte[1024];
                      int read;
                      while (0 <= (read = stream.read(bytes)))
                          byteOut.write(bytes, 0, read);
  
                      return byteOut.toByteArray();
                  } finally {
                      Util.tryToClose(byteOut);
                  }
              } else {
                  byte[] bytes = new byte[knownSize];
                  int read;
                  int offset = 0;
                  while (knownSize > 0 && (read = stream.read(bytes, offset, knownSize)) > 0) {
                      offset += read;
                      knownSize -= read;
                  }
  
                  return bytes;
              }
          } finally {
              Util.tryToClose(stream);
          }
      }
docs: Util method to totally read an input stream into a byte array.
  Used for class definition.
  
  @param stream the stream to read.
  @return byte array of data from stream.
  @throws IOException if an error occurs.