code: public static int copyBytes(InputStream in,
                                  OutputStream out,
                                  long byteCount)
           throws IOException
      {     
          byte buffer[] = new byte[IO.bufferSize];
          int len=IO.bufferSize;
          int totalCount=0;
          
          if (byteCount>=0)
          {
              totalCount=(int)byteCount;
              while (byteCount>0)
              {
                  try {
                      if (byteCount<IO.bufferSize)
                          len=in.read(buffer,0,(int)byteCount);
                      else
                          len=in.read(buffer,0,IO.bufferSize);
                      if (len==-1 && totalCount==byteCount)
                          totalCount=(int)byteCount - 1;
                  } catch (InterruptedIOException e) {
                      if (totalCount==byteCount)
                          throw e;
                      LogSupport.ignore(log, e);
                      len=0;
                  }                   
                  
                  if (len<=0)
                      break;
                  
                  byteCount -= len;
                  out.write(buffer,0,len);
              }
              totalCount -= byteCount;
          }
          else
          {
              while (len>0)
              {
                  try {
                      len=in.read(buffer,0,IO.bufferSize);
                      if (len==-1 && totalCount==0)
                          totalCount=-1;
                  } catch (InterruptedIOException e) {
                      if (totalCount==0)
                          throw e;
                      LogSupport.ignore(log, e);
                      len=0;
                  }
                  if (len>0) {
                      out.write(buffer,0,len);
                      totalCount += len;
                  }
              }
          }
          return totalCount;
      }
docs: Copy Stream in to Stream for byteCount bytes or until EOF or exception.
  @return Copied bytes count or -1 if no bytes were read *and* EOF was reached