Example usage for com.google.common.io CountingInputStream close

List of usage examples for com.google.common.io CountingInputStream close

Introduction

In this page you can find the example usage for com.google.common.io CountingInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:org.gradle.internal.resource.local.LocalFileStandInExternalResource.java

@Override
public ExternalResourceReadResult<Void> writeTo(OutputStream output) {
    if (!localFile.exists()) {
        throw ResourceExceptions.getMissing(getURI());
    }/*from   w  w w.jav a 2 s.c o m*/
    try {
        CountingInputStream input = new CountingInputStream(new FileInputStream(localFile));
        try {
            IOUtils.copyLarge(input, output);
        } finally {
            input.close();
        }
        return ExternalResourceReadResult.of(input.getCount());
    } catch (IOException e) {
        throw ResourceExceptions.getFailed(getURI(), e);
    }
}

From source file:org.gradle.internal.resource.local.LocalFileStandInExternalResource.java

public ExternalResourceReadResult<Void> withContent(Action<? super InputStream> readAction) {
    if (!localFile.exists()) {
        throw ResourceExceptions.getMissing(getURI());
    }/*from  ww w . j  a  v  a2 s. c om*/
    try {
        CountingInputStream input = new CountingInputStream(
                new BufferedInputStream(new FileInputStream(localFile)));
        try {
            readAction.execute(input);
        } finally {
            input.close();
        }
        return ExternalResourceReadResult.of(input.getCount());
    } catch (IOException e) {
        throw ResourceExceptions.getFailed(getURI(), e);
    }
}

From source file:org.gradle.internal.resource.transfer.AccessorBackedExternalResource.java

@Nullable
@Override/*from w w  w  . j a  v  a  2 s . c  o  m*/
public <T> ExternalResourceReadResult<T> withContentIfPresent(
        Transformer<? extends T, ? super InputStream> transformer) throws ResourceException {
    try {
        ExternalResourceReadResponse response = accessor.openResource(name.getUri(), revalidate);
        if (response == null) {
            return null;
        }
        try {
            CountingInputStream input = new CountingInputStream(new BufferedInputStream(response.openStream()));
            try {
                T value = transformer.transform(input);
                return ExternalResourceReadResult.of(input.getCount(), value);
            } finally {
                input.close();
            }
        } finally {
            response.close();
        }
    } catch (IOException e) {
        throw ResourceExceptions.getFailed(name.getUri(), e);
    }
}

From source file:org.apache.jackrabbit.oak.plugins.tika.TextExtractor.java

private String parseStringValue(ByteSource byteSource, Metadata metadata, String path) {
    WriteOutContentHandler handler = new WriteOutContentHandler(maxExtractedLength);
    long start = System.currentTimeMillis();
    long size = 0;
    try {/* w w  w.ja v  a 2 s  .  c o m*/
        CountingInputStream stream = new CountingInputStream(new LazyInputStream(byteSource));
        try {
            tika.getParser().parse(stream, handler, metadata, new ParseContext());
        } finally {
            size = stream.getCount();
            stream.close();
        }
    } catch (LinkageError e) {
        // Capture and ignore errors caused by extraction libraries
        // not being present. This is equivalent to disabling
        // selected media types in configuration, so we can simply
        // ignore these errors.
    } catch (Throwable t) {
        // Capture and report any other full text extraction problems.
        // The special STOP exception is used for normal termination.
        if (!handler.isWriteLimitReached(t)) {
            parserErrorCount.incrementAndGet();
            parserError.debug("Failed to extract text from a binary property: " + path
                    + " This is a fairly common case, and nothing to"
                    + " worry about. The stack trace is included to"
                    + " help improve the text extraction feature.", t);
            return ERROR_TEXT;
        }
    }
    String result = handler.toString();
    timeTaken.addAndGet(System.currentTimeMillis() - start);
    if (size > 0) {
        extractedTextSize.addAndGet(result.length());
        extractionCount.incrementAndGet();
        totalSizeRead.addAndGet(size);
        return result;
    }

    return null;
}

From source file:org.gradle.internal.resource.local.LocalFileStandInExternalResource.java

@Nullable
@Override/*w w w  . jav a2  s  .  c  o  m*/
public <T> ExternalResourceReadResult<T> withContentIfPresent(
        Transformer<? extends T, ? super InputStream> readAction) throws ResourceException {
    if (!localFile.exists()) {
        return null;
    }
    try {
        CountingInputStream input = new CountingInputStream(
                new BufferedInputStream(new FileInputStream(localFile)));
        try {
            T resourceReadResult = readAction.transform(input);
            return ExternalResourceReadResult.of(input.getCount(), resourceReadResult);
        } finally {
            input.close();
        }
    } catch (IOException e) {
        throw ResourceExceptions.getFailed(getURI(), e);
    }
}

From source file:org.gradle.internal.resource.transfer.AccessorBackedExternalResource.java

@Nullable
@Override/* www  .j  av  a  2  s.com*/
public <T> ExternalResourceReadResult<T> withContentIfPresent(ContentAction<? extends T> readAction)
        throws ResourceException {
    try {
        ExternalResourceReadResponse response = accessor.openResource(name.getUri(), revalidate);
        if (response == null) {
            return null;
        }
        try {
            CountingInputStream stream = new CountingInputStream(
                    new BufferedInputStream(response.openStream()));
            try {
                T value = readAction.execute(stream, response.getMetaData());
                return ExternalResourceReadResult.of(stream.getCount(), value);
            } finally {
                stream.close();
            }
        } finally {
            response.close();
        }
    } catch (IOException e) {
        throw ResourceExceptions.getFailed(name.getUri(), e);
    }
}

From source file:org.gradle.internal.resource.local.LocalFileStandInExternalResource.java

@Nullable
@Override/*from w  ww  . ja v  a  2  s  .c  o  m*/
public <T> ExternalResourceReadResult<T> withContentIfPresent(ContentAction<? extends T> readAction)
        throws ResourceException {
    if (!localFile.exists()) {
        return null;
    }
    try {
        CountingInputStream input = new CountingInputStream(
                new BufferedInputStream(new FileInputStream(localFile)));
        try {
            T resourceReadResult = readAction.execute(input, getMetaData());
            return ExternalResourceReadResult.of(input.getCount(), resourceReadResult);
        } finally {
            input.close();
        }
    } catch (IOException e) {
        throw ResourceExceptions.getFailed(getURI(), e);
    }
}

From source file:org.gradle.internal.resource.local.LocalFileStandInExternalResource.java

@Override
@Nullable/*from ww  w.  java  2s.c  o m*/
public ExternalResourceReadResult<Void> writeToIfPresent(File destination) {
    if (!localFile.exists()) {
        return null;
    }
    try {
        CountingInputStream input = new CountingInputStream(new FileInputStream(localFile));
        try {
            FileOutputStream output = new FileOutputStream(destination);
            try {
                IOUtils.copyLarge(input, output);
            } finally {
                output.close();
            }
        } finally {
            input.close();
        }
        return ExternalResourceReadResult.of(input.getCount());
    } catch (IOException e) {
        throw ResourceExceptions.getFailed(getURI(), e);
    }
}

From source file:org.gradle.internal.resource.transfer.AccessorBackedExternalResource.java

@Nullable
@Override//from   w w  w  . jav  a 2  s. c o  m
public ExternalResourceReadResult<Void> writeToIfPresent(File destination) throws ResourceException {
    try {
        ExternalResourceReadResponse response = accessor.openResource(name.getUri(), revalidate);
        if (response == null) {
            return null;
        }
        try {
            CountingInputStream input = new CountingInputStream(response.openStream());
            try {
                FileOutputStream output = new FileOutputStream(destination);
                try {
                    IOUtils.copyLarge(input, output);
                    return ExternalResourceReadResult.of(input.getCount());
                } finally {
                    output.close();
                }
            } finally {
                input.close();
            }
        } finally {
            response.close();
        }
    } catch (IOException e) {
        throw ResourceExceptions.getFailed(getURI(), e);
    }
}

From source file:org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexEditor.java

private String parseStringValue0(Blob v, Metadata metadata, String path) {
    WriteOutContentHandler handler = new WriteOutContentHandler(context.getDefinition().getMaxExtractLength());
    long start = System.currentTimeMillis();
    long bytesRead = 0;
    try {//  www  . ja va2  s  . com
        CountingInputStream stream = new CountingInputStream(new LazyInputStream(new BlobByteSource(v)));
        try {
            context.getParser().parse(stream, handler, metadata, new ParseContext());
        } finally {
            bytesRead = stream.getCount();
            stream.close();
        }
    } catch (LinkageError e) {
        // Capture and ignore errors caused by extraction libraries
        // not being present. This is equivalent to disabling
        // selected media types in configuration, so we can simply
        // ignore these errors.
    } catch (Throwable t) {
        // Capture and report any other full text extraction problems.
        // The special STOP exception is used for normal termination.
        if (!handler.isWriteLimitReached(t)) {
            log.debug("[{}] Failed to extract text from a binary property: {}."
                    + " This is a fairly common case, and nothing to"
                    + " worry about. The stack trace is included to"
                    + " help improve the text extraction feature.", getIndexName(), path, t);
            context.getExtractedTextCache().put(v, ExtractedText.ERROR);
            return TEXT_EXTRACTION_ERROR;
        }
    }
    String result = handler.toString();
    if (bytesRead > 0) {
        context.recordTextExtractionStats(System.currentTimeMillis() - start, bytesRead, result.length());
    }
    context.getExtractedTextCache().put(v, new ExtractedText(ExtractionResult.SUCCESS, result));
    return result;
}