Example usage for org.apache.commons.io.input ClosedInputStream CLOSED_INPUT_STREAM

List of usage examples for org.apache.commons.io.input ClosedInputStream CLOSED_INPUT_STREAM

Introduction

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

Prototype

ClosedInputStream CLOSED_INPUT_STREAM

To view the source code for org.apache.commons.io.input ClosedInputStream CLOSED_INPUT_STREAM.

Click Source Link

Document

A singleton.

Usage

From source file:de.mklinger.commons.exec.CmdSettingsTest.java

@Override
protected Object createValue(final Type type) {
    if (type == OutputStream.class) {
        return ClosedOutputStream.CLOSED_OUTPUT_STREAM;
    }/*w  ww  . j  a  va2s.com*/
    if (type == InputStream.class) {
        return ClosedInputStream.CLOSED_INPUT_STREAM;
    }
    if (type == File.class) {
        return new File(String.valueOf(getNextTestValue()));
    }
    if (type == Pingable.class) {
        return new Pingable() {
            @Override
            public void ping() {
            }
        };
    }
    if (type == ExecutorProvider.class) {
        return new DefaultExecutorProvider();
    }
    return super.createValue(type);
}

From source file:com.joyent.manta.http.ApacheHttpGetResponseEntityContentContinuator.java

/**
 * Get an {@link InputStream} which picks up starting {@code bytesRead} bytes from the beginning of the logical
 * object being downloaded. Implementations should compare headers across all requests and responses to ensure that
 * the object being downloaded has not changed between the initial and subsequent requests.
 *
 * @param ex the exception which occurred while downloading (either the first response or a continuation)
 * @param bytesRead byte offset at which the new stream should start
 * @return another stream which continues to deliver the bytes from the initial request
 * @throws HttpDownloadContinuationException if the provided {@link IOException} is not recoverable or the number of
 * retries has been reached, or there is an error
 * @throws HttpDownloadContinuationUnexpectedResponseException if the continuation response was incompatible or
 * indicated that the remote object has somehow changed
 *///  www .  j av a 2s .c  o m
@Override
public InputStream buildContinuation(final IOException ex, final long bytesRead) throws IOException {
    requireNonNull(ex);

    if (!isRecoverable(ex)) {
        throw ex;
    }

    this.continuation++;

    if (this.maxContinuations != INFINITE_CONTINUATIONS && this.maxContinuations <= this.continuation) {
        throw new HttpDownloadContinuationException(
                String.format("Maximum number of continuations reached [%s], aborting auto-retry: %s",
                        this.maxContinuations, ex.getMessage()),
                ex);
    }

    LOG.debug(
            "Attempting to build a continuation for " + "[{}] request " + "to path [{}] "
                    + "to recover at byte offset {} " + "from exception {}",
            this.request.getMethod(), this.request.getRequestLine().getUri(), bytesRead, ex.getMessage());

    // if an IOException occurs while reading EOF the user may ask us for a continuation
    // starting after the last valid byte.
    if (bytesRead == this.marker.getTotalRangeSize()) {
        if (this.metricRegistry != null) {
            this.metricRegistry.counter(METRIC_NAME_RECOVERED_EXCEPTION_PREFIX + ex.getClass().getSimpleName())
                    .inc();
            this.metricRegistry.counter(METRIC_NAME_RECOVERED_EOF).inc();
        }

        return ClosedInputStream.CLOSED_INPUT_STREAM;
    }

    try {
        this.marker.updateRangeStart(bytesRead);
    } catch (final IllegalArgumentException iae) {
        // we should wrap and rethrow this so that the caller doesn't get stuck in a loop
        throw new HttpDownloadContinuationException("Failed to update download continuation offset", iae);
    }

    this.request.setHeader(RANGE, this.marker.getCurrentRange().render());

    // not yet trying to handle exceptions during request execution
    final HttpResponse response;
    try {
        final HttpContext httpContext = new BasicHttpContext();
        httpContext.setAttribute(CONTEXT_ATTRIBUTE_MANTA_RETRY_DISABLE, true);
        response = this.client.execute(this.request, httpContext);
    } catch (final IOException ioe) {
        throw new HttpDownloadContinuationException(
                "Exception occurred while attempting to build continuation: " + ioe.getMessage(), ioe);
    }

    final int statusCode = response.getStatusLine().getStatusCode();

    if (statusCode != SC_PARTIAL_CONTENT) {
        throw new HttpDownloadContinuationUnexpectedResponseException(String
                .format("Invalid response code: expecting [%d], got [%d]", SC_PARTIAL_CONTENT, statusCode));
    }

    try {
        validateResponseWithMarker(extractDownloadResponseFingerprint(response, false));
    } catch (final HttpException he) {
        throw new HttpDownloadContinuationUnexpectedResponseException(
                "Continuation request failed validation: " + he.getMessage(), he);
    }

    final InputStream content;
    try {
        final HttpEntity entity = response.getEntity();

        if (entity == null) {
            throw new HttpDownloadContinuationUnexpectedResponseException(
                    "Entity missing from continuation response");
        }

        content = entity.getContent();

        if (content == null) {
            throw new HttpDownloadContinuationUnexpectedResponseException(
                    "Entity content missing from continuation response");
        }
    } catch (final UnsupportedOperationException | IOException uoe) {
        throw new HttpDownloadContinuationUnexpectedResponseException(uoe);
    }

    if (this.metricRegistry != null) {
        this.metricRegistry.counter(METRIC_NAME_RECOVERED_EXCEPTION_PREFIX + ex.getClass().getSimpleName())
                .inc();
    }

    LOG.debug("Successfully constructed continuation at byte offset {} to recover from {}", bytesRead, ex);

    return content;
}

From source file:org.apache.jackrabbit.oak.commons.io.LazyInputStream.java

@Override
public void close() throws IOException {
    // make sure the file is not opened afterwards
    opened = true;/*from  www .  j av a 2s  . co m*/

    // only close the file if it was in fact opened
    if (in != null) {
        super.close();
    } else {
        in = ClosedInputStream.CLOSED_INPUT_STREAM;
    }
}

From source file:org.codehaus.httpcache4j.payload.ClosedInputStreamPayload.java

public InputStream getInputStream() {
    return ClosedInputStream.CLOSED_INPUT_STREAM;
}

From source file:org.codehaus.httpcache4j.payload.InputStreamPayload.java

public InputStream getInputStream() {
    if (available) {
        return stream;
    }
    return ClosedInputStream.CLOSED_INPUT_STREAM;
}