private int read(int bytesToRead) throws
    IOException
  {
      int nBytesRead = 0;
      int nBytesTotalRead = 0;

      //check buffer size and adjust it
      if(inputBuffer.length < bytesToRead)
        inputBuffer = new byte[bytesToRead];

      while (nBytesRead != -1 && bytesToRead > 0)
      {
        nBytesRead = in.read(inputBuffer, nBytesTotalRead, bytesToRead);
        if (nBytesRead != -1)
        {
          bytesToRead -= nBytesRead;
          nBytesTotalRead += nBytesRead;
        }
      }

      return nBytesTotalRead;
  }