Пример #1
0
  @Override
  public int read(byte[] buffer, int offset, int length) throws IOException {
    if (mClosed) {
      throw new IOException("Cannot read from a closed stream.");
    }
    if (mAlluxioFileInputStream != null) {
      try {
        int ret = mAlluxioFileInputStream.read(buffer, offset, length);
        if (mStatistics != null && ret != -1) {
          mStatistics.incrementBytesRead(ret);
        }
        mCurrentPosition += ret;
        return ret;
      } catch (IOException e) {
        LOG.error(e.getMessage(), e);
        mAlluxioFileInputStream.close();
        mAlluxioFileInputStream = null;
      }
    }

    getHdfsInputStream();
    int byteRead = readFromHdfsBuffer();
    // byteRead is an unsigned byte, if its -1 then we have hit EOF
    if (byteRead == -1) {
      return -1;
    }
    // Convert byteRead back to a signed byte
    buffer[offset] = (byte) byteRead;
    return 1;
  }
Пример #2
0
 @Override
 public int read() throws IOException {
   if (mClosed) {
     throw new IOException("Cannot read from a closed stream.");
   }
   if (mAlluxioFileInputStream != null) {
     try {
       int ret = mAlluxioFileInputStream.read();
       if (mStatistics != null && ret != -1) {
         mStatistics.incrementBytesRead(1);
       }
       mCurrentPosition++;
       return ret;
     } catch (IOException e) {
       LOG.error(e.getMessage(), e);
       mAlluxioFileInputStream.close();
       mAlluxioFileInputStream = null;
     }
   }
   getHdfsInputStream();
   return readFromHdfsBuffer();
 }
Пример #3
0
  @Override
  public synchronized int read(long position, byte[] buffer, int offset, int length)
      throws IOException {
    if (mClosed) {
      throw new IOException(ExceptionMessage.READ_CLOSED_STREAM.getMessage());
    }
    int ret = -1;
    long oldPos = getPos();
    if ((position < 0) || (position >= mFileInfo.getLength())) {
      return ret;
    }

    if (mAlluxioFileInputStream != null) {
      try {
        mAlluxioFileInputStream.seek(position);
        ret = mAlluxioFileInputStream.read(buffer, offset, length);
        if (mStatistics != null && ret != -1) {
          mStatistics.incrementBytesRead(ret);
        }
        return ret;
      } finally {
        mAlluxioFileInputStream.seek(oldPos);
      }
    }

    try {
      getHdfsInputStream(position);
      ret = mHdfsInputStream.read(buffer, offset, length);
      if (mStatistics != null && ret != -1) {
        mStatistics.incrementBytesRead(ret);
      }
      return ret;
    } finally {
      if (mHdfsInputStream != null) {
        mHdfsInputStream.seek(oldPos);
      }
    }
  }