org.apache.commons.io.input.ProxyInputStream Java Examples

The following examples show how to use org.apache.commons.io.input.ProxyInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataStore.java    From datacollector with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
  }
}