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

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

Introduction

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

Prototype

public CountingInputStream(InputStream in) 

Source Link

Document

Wraps another input stream, counting the number of bytes read.

Usage

From source file:uk.ac.ebi.mdk.service.loader.location.RemoteLocation.java

/**
 * Open a stream to the remote resource. This first opens the URLConnection
 * and then the stream/*from  w  ww  . j a  v  a2 s .co  m*/
 *
 * @inheritDoc
 */
public InputStream open() throws IOException {
    if (stream == null) {
        connection = getConnection();
        stream = counter = new CountingInputStream(connection.getInputStream());
    }
    return stream;
}

From source file:hudson.plugins.timestamper.io.TimestampsReader.java

/**
 * Read the next time-stamp from the given input stream.
 * /*from  w  w  w  .  ja  v  a 2  s . c o  m*/
 * @param inputStream
 * @return the next time-stamp
 */
private Timestamp readNext(InputStream inputStream) throws IOException {
    CountingInputStream countingInputStream = new CountingInputStream(inputStream);
    long elapsedMillisDiff = Varint.read(countingInputStream);

    elapsedMillis += elapsedMillisDiff;
    millisSinceEpoch = timeShiftsReader.getTime(entry).or(millisSinceEpoch + elapsedMillisDiff);
    filePointer += countingInputStream.getCount();
    entry++;
    return new Timestamp(elapsedMillis, millisSinceEpoch);
}

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

@Nullable
@Override//from   ww  w. jav  a 2 s . c  om
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/* w  w  w .j av a2 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:c5db.log.SequentialLogWithHeader.java

private static CountingInputStream getCountingInputStream(PersistenceReader reader) {
    return new CountingInputStream(Channels.newInputStream(reader));
}

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

@Override
public ExternalResourceReadResult<Void> withContent(Action<? super InputStream> readAction)
        throws ResourceException {
    try {//from ww  w . j  a  v  a 2s .  c  om
        ExternalResourceReadResponse response = accessor.openResource(name.getUri(), revalidate);
        if (response == null) {
            throw ResourceExceptions.getMissing(getURI());
        }
        try {
            CountingInputStream inputStream = new CountingInputStream(response.openStream());
            readAction.execute(inputStream);
            return ExternalResourceReadResult.of(inputStream.getCount());
        } finally {
            response.close();
        }
    } catch (IOException e) {
        throw ResourceExceptions.getFailed(name.getUri(), e);
    }
}

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

@Nullable
@Override/*w  ww  .ja v  a 2 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:com.palantir.atlasdb.stream.AbstractPersistentStreamStore.java

protected final StreamMetadata storeBlocksAndGetFinalMetadata(@Nullable Transaction t, long id,
        InputStream stream) {//from   www .  ja v a  2  s  . co  m
    // Set up for finding hash and length
    MessageDigest digest = Sha256Hash.getMessageDigest();
    stream = new DigestInputStream(stream, digest);
    CountingInputStream countingStream = new CountingInputStream(stream);

    // Try to store the bytes to the stream and get length
    try {
        storeBlocksFromStream(t, id, countingStream);
    } catch (IOException e) {
        long length = countingStream.getCount();
        StreamMetadata metadata = StreamMetadata.newBuilder().setStatus(Status.FAILED).setLength(length)
                .setHash(com.google.protobuf.ByteString.EMPTY).build();
        storeMetadataAndIndex(id, metadata);
        log.error("Could not store stream " + id + ". Failed after " + length + " bytes.", e);
        throw Throwables.rewrapAndThrowUncheckedException("Failed to store stream.", e);
    }

    // Get hash and length
    ByteString hashByteString = ByteString.copyFrom(digest.digest());
    long length = countingStream.getCount();

    // Return the final metadata.
    StreamMetadata metadata = StreamMetadata.newBuilder().setStatus(Status.STORED).setLength(length)
            .setHash(hashByteString).build();
    return metadata;
}

From source file:net.sf.mzmine.modules.projectmethods.projectload.ProjectOpeningTask.java

/**
 * @see java.lang.Runnable#run()//from ww  w .  j  ava 2s  .co m
 */
public void run() {

    try {
        // Check if existing raw data files are present
        ProjectManager projectManager = MZmineCore.getProjectManager();
        if (projectManager.getCurrentProject().getDataFiles().length > 0) {
            int dialogResult = JOptionPane.showConfirmDialog(null,
                    "Loading the project will replace the existing raw data files and peak lists. Do you want to proceed?",
                    "Warning", JOptionPane.YES_NO_OPTION);

            if (dialogResult != JOptionPane.YES_OPTION) {
                cancel();
                return;
            }
        }

        logger.info("Started opening project " + openFile);
        setStatus(TaskStatus.PROCESSING);

        // Create a new project
        newProject = new MZmineProjectImpl();
        newProject.setProjectFile(openFile);

        // Close all windows related to previous project
        GUIUtils.closeAllWindows();

        // Replace the current project with the new one
        projectManager.setCurrentProject(newProject);

        // Open the ZIP file
        ZipFile zipFile = new ZipFile(openFile);

        // Get total uncompressed size
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            totalBytes += entry.getSize();
        }

        final Pattern rawFilePattern = Pattern.compile("Raw data file #([\\d]+) (.*)\\.xml$");
        final Pattern scansFilePattern = Pattern.compile("Raw data file #([\\d]+) (.*)\\.scans$");
        final Pattern peakListPattern = Pattern.compile("Peak list #([\\d]+) (.*)\\.xml$");

        boolean versionInformationLoaded = false;

        // Iterate over the entries and read them
        entries = zipFile.entries();
        while (entries.hasMoreElements()) {

            if (isCanceled()) {
                zipFile.close();
                return;
            }

            ZipEntry entry = entries.nextElement();
            String entryName = entry.getName();
            cis = new CountingInputStream(zipFile.getInputStream(entry));

            // Load version
            if (entryName.equals(ProjectSavingTask.VERSION_FILENAME)) {
                loadVersion(cis);
                versionInformationLoaded = true;
            }

            // Load configuration
            if (entryName.equals(ProjectSavingTask.CONFIG_FILENAME))
                loadConfiguration(cis);

            // Load user parameters
            if (entryName.equals(ProjectSavingTask.PARAMETERS_FILENAME)) {
                loadUserParameters(cis);
            }

            // Load a raw data file
            final Matcher rawFileMatcher = rawFilePattern.matcher(entryName);
            if (rawFileMatcher.matches()) {
                final String fileID = rawFileMatcher.group(1);
                final String fileName = rawFileMatcher.group(2);
                loadRawDataFile(cis, fileID, fileName);
            }

            // Load the scan data of a raw data file
            final Matcher scansFileMatcher = scansFilePattern.matcher(entryName);
            if (scansFileMatcher.matches()) {
                final String fileID = scansFileMatcher.group(1);
                final String fileName = scansFileMatcher.group(2);
                loadScansFile(cis, fileID, fileName);
            }

            // Load a peak list
            final Matcher peakListMatcher = peakListPattern.matcher(entryName);
            if (peakListMatcher.matches()) {
                final String peakListName = peakListMatcher.group(2);
                loadPeakList(cis, peakListName);
            }

            // Close the ZIP entry
            cis.close();

            // Add the uncompressed entry size finishedBytes
            synchronized (this) {
                finishedBytes += entry.getSize();
                cis = null;
            }

        }

        // Finish and close the project ZIP file
        zipFile.close();

        if (!versionInformationLoaded) {
            throw new IOException(
                    "This file is not valid MZmine 2 project. It does not contain version information.");
        }

        // Final check for cancel
        if (isCanceled())
            return;

        logger.info("Finished opening project " + openFile);

        setStatus(TaskStatus.FINISHED);

    } catch (Throwable e) {

        // If project opening was canceled, parser was stopped by a
        // SAXException which can be safely ignored
        if (isCanceled())
            return;

        setStatus(TaskStatus.ERROR);
        e.printStackTrace();
        setErrorMessage("Failed opening project: " + ExceptionUtils.exceptionToString(e));
    }

}

From source file:org.apache.tephra.persist.HDFSTransactionStateStorage.java

private TransactionSnapshot readSnapshotInputStream(InputStream in) throws IOException {
    CountingInputStream countingIn = new CountingInputStream(in);
    TransactionSnapshot snapshot = codecProvider.decode(countingIn);
    LOG.info("Read encoded transaction snapshot of {} bytes", countingIn.getCount());
    return snapshot;
}