Exemplo n.º 1
0
  public int read(byte b[], int off, int len) throws SmbException {
    if (len <= 0) {
      return 0;
    }
    long start = fp;

    // ensure file is open
    if (file.isOpen() == false) {
      file.open(openFlags, 0, SmbFile.ATTR_NORMAL, options);
    }

    int r, n;
    SmbComReadAndXResponse response = new SmbComReadAndXResponse(b, off);
    do {
      r = len > readSize ? readSize : len;
      file.send(new SmbComReadAndX(file.fid, fp, r, null), response);
      if ((n = response.dataLength) <= 0) {
        return (int) ((fp - start) > 0L ? fp - start : -1);
      }
      fp += n;
      len -= n;
      response.off += n;
    } while (len > 0 && n == r);

    return (int) (fp - start);
  }
  /**
   * Reads up to len bytes of data from this input stream into an array of bytes.
   *
   * @throws IOException if a network error occurs
   */
  public int read(byte[] b, int off, int len) throws IOException {
    if (len <= 0) {
      return 0;
    }
    long start = fp;

    if (tmp == null) {
      throw new IOException("Bad file descriptor");
    }
    // ensure file is open
    file.open(openFlags, SmbFile.ATTR_NORMAL, 0);

    /*
     * Read AndX Request / Response
     */

    if (file.log.level > 2)
      file.log.println("read: fid=" + file.fid + ",off=" + off + ",len=" + len);

    SmbComReadAndXResponse response = new SmbComReadAndXResponse(b, off);

    if (file.type == SmbFile.TYPE_NAMED_PIPE) {
      response.responseTimeout = 0;
    }

    int r, n;
    do {
      r = len > readSize ? readSize : len;

      if (file.log.level > 2) file.log.println("read: len=" + len + ",r=" + r + ",fp=" + fp);

      try {
        file.send(new SmbComReadAndX(file.fid, fp, r, null), response);
      } catch (SmbException se) {
        if (file.type == SmbFile.TYPE_NAMED_PIPE
            && se.getNtStatus() == NtStatus.NT_STATUS_PIPE_BROKEN) {
          return -1;
        }
        throw se;
      }
      if ((n = response.dataLength) <= 0) {
        return (int) ((fp - start) > 0L ? fp - start : -1);
      }
      fp += n;
      len -= n;
      response.off += n;
    } while (len > 0 && n == r);

    return (int) (fp - start);
  }