code: public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException {
          int availableBytes = input.available();
          log.debug("copy - bufferSize: {} available: {}", bufferSize, availableBytes);
          byte[] buf = null;
          if (availableBytes > 0) {
              if (availableBytes >= bufferSize) {
                  buf = new byte[bufferSize];
              } else {
                  buf = new byte[availableBytes];
              }
              int bytesRead = input.read(buf);
              while (bytesRead != -1) {
                  output.write(buf, 0, bytesRead);
                  bytesRead = input.read(buf);
                  log.trace("Bytes read: {}", bytesRead);
              }
              output.flush();
          } else {
              log.debug("Available is 0, attempting to read anyway");
              buf = new byte[bufferSize];
              int bytesRead = input.read(buf);
              while (bytesRead != -1) {
                  output.write(buf, 0, bytesRead);
                  bytesRead = input.read(buf);
                  log.trace("Bytes read: {}", bytesRead);
              }
              output.flush();
          }
      }
docs: Copies information from the input stream to the output stream using the specified buffer size
  
  @param input
  input
  @param bufferSize
  buffer size
  @param output
  output
  @throws java.io.IOException
  on error