Example usage for org.apache.commons.io.input ProxyInputStream ProxyInputStream

List of usage examples for org.apache.commons.io.input ProxyInputStream ProxyInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io.input ProxyInputStream ProxyInputStream.

Prototype

public ProxyInputStream(InputStream proxy) 

Source Link

Document

Constructs a new ProxyInputStream.

Usage

From source file:ch.cyberduck.core.azure.AzureReadFeature.java

@Override
public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback)
        throws BackgroundException {
    try {/*from www. j  a va2 s.co  m*/
        final CloudBlob blob = session.getClient()
                .getContainerReference(containerService.getContainer(file).getName())
                .getBlobReferenceFromServer(containerService.getKey(file));
        final BlobRequestOptions options = new BlobRequestOptions();
        options.setConcurrentRequestCount(1);
        final BlobInputStream in = blob.openInputStream(AccessCondition.generateEmptyCondition(), options,
                context);
        if (status.isAppend()) {
            try {
                return StreamCopier.skip(in, status.getOffset());
            } catch (IndexOutOfBoundsException e) {
                // If offset is invalid
                throw new DefaultExceptionMappingService().map(e);
            }
        }
        return new ProxyInputStream(in) {
            @Override
            protected void handleIOException(final IOException e) throws IOException {
                if (StringUtils.equals(SR.STREAM_CLOSED, e.getMessage())) {
                    log.warn(String.format("Ignore failure %s", e));
                    return;
                }
                final Throwable cause = ExceptionUtils.getRootCause(e);
                if (cause instanceof StorageException) {
                    throw new IOException(e.getMessage(),
                            new AzureExceptionMappingService().map((StorageException) cause));
                }
                throw e;
            }
        };
    } catch (StorageException e) {
        throw new AzureExceptionMappingService().map("Download {0} failed", e, file);
    } catch (URISyntaxException e) {
        throw new NotfoundException(e.getMessage(), e);
    }
}

From source file:grails.plugin.cloudfoundry.GrailsHttpRequestFactory.java

protected InputStream wrap(final InputStream inputStream) {
    return new ProxyInputStream(inputStream) {
        @Override/*from w w  w.  j a  v  a  2s . c  om*/
        public int read(byte[] bts, int st, int end) throws IOException {
            int count = super.read(bts, st, end);
            if (count > 0) {
                bytes.write(bts, st, st + count);
            }
            return count;
        }
    };
}

From source file:com.streamsets.datacollector.io.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:/*from   ww w  .  jav  a2 s.co m*/
 *
 * 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;
    }
}

From source file:ch.cyberduck.core.local.FinderLocal.java

@Override
public InputStream getInputStream() throws AccessDeniedException {
    final NSURL resolved;
    try {//from w  w  w.  j a v  a2s . co  m
        resolved = this.lock(false);
    } catch (AccessDeniedException e) {
        return super.getInputStream();
    }
    try {
        return new ProxyInputStream(new LocalRepeatableFileInputStream(new File(resolved.path()))) {
            @Override
            public void close() throws IOException {
                try {
                    super.close();
                } finally {
                    release(resolved);
                }
            }
        };
    } catch (FileNotFoundException e) {
        throw new LocalAccessDeniedException(e.getMessage(), e);
    }
}

From source file:ch.cyberduck.core.ftp.FTPPath.java

@Override
public InputStream read(final TransferStatus status) throws IOException {
    final FTPSession session = getSession();
    this.data(new DataConnectionAction() {
        @Override/*from w ww. j  a va  2  s.  c  o m*/
        public boolean run() throws IOException {
            if (!session.getClient().setFileType(FTP.BINARY_FILE_TYPE)) {
                throw new FTPException(session.getClient().getReplyString());
            }
            return true;
        }
    });
    if (status.isResume()) {
        // Where a server process supports RESTart in STREAM mode
        if (!session.getClient().isFeatureSupported("REST STREAM")) {
            status.setResume(false);
        } else {
            session.getClient().setRestartOffset(status.getCurrent());
        }
    }
    return new ProxyInputStream(session.getClient().retrieveFileStream(getAbsolute())) {
        private boolean complete;

        @Override
        protected void afterRead(final int n) throws IOException {
            if (-1 == n) {
                complete = true;
            }
        }

        @Override
        public void close() throws IOException {
            try {
                super.close();
            } finally {
                if (complete) {
                    // Read 226 status
                    if (!session.getClient().completePendingCommand()) {
                        throw new FTPException(session.getClient().getReplyString());
                    }
                } else {
                    // Interrupted transfer
                    if (!session.getClient().abort()) {
                        log.error("Error closing data socket:" + session.getClient().getReplyString());
                    }
                }
            }
        }
    };
}

From source file:org.nuxeo.ecm.web.resources.wro.processor.FlavorResourceProcessor.java

@Override
protected void process(final Resource resource, final Reader reader, final Writer writer, String flavorName)
        throws IOException {
    final InputStream is = new ProxyInputStream(new ReaderInputStream(reader, getEncoding())) {
    };//from   www .j  a  v a  2s  . c  om
    final OutputStream os = new ProxyOutputStream(new WriterOutputStream(writer, getEncoding()));
    try {
        Map<String, String> presets = null;
        if (flavorName != null) {
            ThemeStylingService s = Framework.getService(ThemeStylingService.class);
            presets = s.getPresetVariables(flavorName);
        }
        if (presets == null || presets.isEmpty()) {
            IOUtils.copy(is, os);
        } else {
            String content = IOUtils.toString(reader);
            for (Map.Entry<String, String> preset : presets.entrySet()) {
                content = Pattern.compile("\"" + preset.getKey() + "\"", Pattern.LITERAL).matcher(content)
                        .replaceAll(Matcher.quoteReplacement(preset.getValue()));
            }
            writer.write(content);
            writer.flush();
        }
        is.close();
    } catch (final Exception e) {
        log.error("Error while serving resource " + resource.getUri(), e);
        throw WroRuntimeException.wrap(e);
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}

From source file:org.nuxeo.theme.styling.wro.FlavorResourceProcessor.java

protected void process(final Resource resource, final Reader reader, final Writer writer, String flavorName)
        throws IOException {
    final InputStream is = new ProxyInputStream(new ReaderInputStream(reader, getEncoding())) {
    };// www  .j a  v  a  2 s . c o  m
    final OutputStream os = new ProxyOutputStream(new WriterOutputStream(writer, getEncoding()));
    try {
        Map<String, String> presets = null;
        if (flavorName != null) {
            ThemeStylingService s = Framework.getService(ThemeStylingService.class);
            presets = s.getPresetVariables(flavorName);
        }
        if (presets == null || presets.isEmpty()) {
            IOUtils.copy(is, os);
        } else {
            String content = IOUtils.toString(reader);
            for (Map.Entry<String, String> preset : presets.entrySet()) {
                content = Pattern.compile(String.format("\"%s\"", preset.getKey()), Pattern.LITERAL)
                        .matcher(content).replaceAll(Matcher.quoteReplacement(preset.getValue()));
            }
            writer.write(content);
            writer.flush();
        }
        is.close();
        os.close();
    } catch (final Exception e) {
        throw WroRuntimeException.wrap(e);
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}