org.apache.commons.io.input.ProxyInputStream源码实例Demo

类org.apache.commons.io.input.ProxyInputStream源码实例Demo

下面列出了org.apache.commons.io.input.ProxyInputStream 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: datacollector   文件: DataStore.java
/**
 * Returns an input stream for the requested file.
 *
 * After completing the read the stream must be closed. It is not necessary to call the {@link #release()}
 * method after reading from the input stream.
 *
 * Example usage:
 *
 * DataStore dataStore = new DataStore(...);
 * try (InputStream is = dataStore.getInputStream()) {
 *   // read from is
 * } catch (IOException e) {
 *   ...
 * }
 *
 * @return
 * @throws IOException
 */
public InputStream getInputStream() throws IOException {
  acquireLock();
  try {
    isClosed = false;
    forWrite = false;
    LOG.trace("Starts read '{}'", file);
    verifyAndRecover();
    InputStream is = new ProxyInputStream(new FileInputStream(file.toFile())) {
      @Override
      public void close() throws IOException {
        if (isClosed) {
          return;
        }
        try {
          super.close();
        } finally {
          release();
          isClosed = true;
          stream = null;
        }
        LOG.trace("Finishes read '{}'", file);
      }
    };
    stream = is;
    return is;
  } catch (Exception ex) {
    release();
    throw ex;
  }
}
 
源代码评论
动弹
沙发等你来抢
 类所在包
 类方法
 同包方法