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:alluxio.underfs.ObjectUnderFileInputStream.java

/**
 * Open a new stream./*from   w w w . java  2 s .c  o  m*/
 *
 * @param options for opening a stream
 * @throws IOException if a non-Alluxio error occurs
 */
private void openStream(OpenOptions options) throws IOException {
    if (mStream != null) {
        mStream.close();
    }
    mInitPos = options.getOffset();
    mStream = new CountingInputStream(mUfs.openObject(mKey, options));
}

From source file:org.archive.io.GZIPMembersInputStream.java

/**
 * A CountingInputStream is inserted to read compressed-offsets. 
 * // ww  w. j a  v  a 2  s. co m
 * @param in stream to wrap
 * @param lookback tolerance of initial mark
 * @return original stream wrapped in CountingInputStream
 * @throws IOException
 */
protected static InputStream countingStream(InputStream in, int lookback) throws IOException {
    CountingInputStream cin = new CountingInputStream(in);
    cin.mark(lookback);
    return cin;
}

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

private Map<Long, Long> readTimeShifts() throws IOException {
    if (!timeShiftsFile.isFile()) {
        return Collections.emptyMap();
    }/*ww  w  .  j  a  v  a2s .  c o  m*/
    Map<Long, Long> timeShifts = new HashMap<Long, Long>();
    CountingInputStream inputStream = new CountingInputStream(
            new BufferedInputStream(new FileInputStream(timeShiftsFile)));
    boolean threw = true;
    try {
        while (inputStream.getCount() < timeShiftsFile.length()) {
            long entry = Varint.read(inputStream);
            long shift = Varint.read(inputStream);
            timeShifts.put(entry, shift);
        }
        threw = false;
    } finally {
        Closeables.close(inputStream, threw);
    }
    return timeShifts;
}

From source file:com.haulmont.cuba.core.sys.remoting.ClusteredHttpInvokerRequestExecutor.java

@Override
protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config,
        ByteArrayOutputStream baos) throws IOException, ClassNotFoundException {

    RemoteInvocationResult result;/*from ww  w.  ja  v  a2s .c  o  m*/

    Object context = serverSelector.initContext();
    String url = currentServiceUrl(serverSelector.getUrl(context), config);
    if (url == null)
        throw new IllegalStateException("Server URL list is empty");

    while (true) {
        HttpURLConnection con = openConnection(url);
        try {
            StopWatch sw = new StopWatch();
            prepareConnection(con, baos.size());
            writeRequestBody(config, con, baos);
            sw.start("waiting time");
            validateResponse(config, con);
            CountingInputStream responseInputStream = new CountingInputStream(readResponseBody(config, con));
            sw.stop();

            serverSelector.success(context);

            sw.start("reading time");
            try (ObjectInputStream ois = createObjectInputStream(decorateInputStream(responseInputStream),
                    config.getCodebaseUrl())) {
                result = doReadRemoteInvocationResult(ois);
            }
            sw.stop();
            if (log.isDebugEnabled()) {
                log.debug(String.format("Receiving HTTP invoker response for service at [%s], with size %s, %s",
                        config.getServiceUrl(), responseInputStream.getCount(), printStopWatch(sw)));
            }
            break;
        } catch (IOException e) {
            log.info(String.format("Invocation of %s failed: %s", url, e));

            serverSelector.fail(context);
            url = currentServiceUrl(serverSelector.getUrl(context), config);
            if (url != null) {
                log.info("Trying to invoke the next available URL: " + url);
                continue;
            }
            log.info("No more URL available");
            throw e;
        }
    }
    return result;
}

From source file:io.druid.data.input.impl.prefetch.RetryingInputStream.java

private void waitOrThrow(Throwable t, int nTry) throws IOException {
    final boolean isConnectionReset = isConnectionReset(t);
    if (isConnectionReset || retryCondition.apply(t)) {
        if (isConnectionReset) {
            // Re-open the input stream on connection reset
            startOffset += delegate.getCount();
            try {
                delegate.close();/*  w  ww  .j av a  2  s. c  o  m*/
            } catch (IOException e) {
                // ignore this exception
                log.warn(e, "Error while closing the delegate input stream");
            }
        }
        try {
            // Wait for the next try
            RetryUtils.awaitNextRetry(t, null, nTry + 1, maxRetry, false);

            if (isConnectionReset) {
                log.info("retrying from offset[%d]", startOffset);
                delegate = new CountingInputStream(objectOpenFunction.open(object, startOffset));
            }
        } catch (InterruptedException | IOException e) {
            t.addSuppressed(e);
            throwAsIOException(t);
        }
    } else {
        throwAsIOException(t);
    }
}

From source file:net.sf.mzmine.modules.peaklistmethods.io.xmlimport.XMLImportTask.java

/**
 * @see java.lang.Runnable#run()/* ww w  . j  a v a2s  .c o  m*/
 */
public void run() {

    setStatus(TaskStatus.PROCESSING);
    logger.info("Started parsing file " + fileName);

    try {

        if ((!fileName.exists()) || (!fileName.canRead())) {
            throw new Exception("Parsing Cancelled, file does not exist or is not readable");
        }

        totalBytes = fileName.length();

        FileInputStream fis = new FileInputStream(fileName);
        cis = new CountingInputStream(fis);
        InputStream finalStream = cis;
        byte b[] = new byte[32];
        fis.read(b);
        String firstLine = new String(b);
        if (!firstLine.contains("<?xml")) {
            FileChannel fc = fis.getChannel();
            fc.position(0);
            @SuppressWarnings("resource")
            ZipInputStream zis = new ZipInputStream(cis);
            zis.getNextEntry();
            finalStream = zis;
        } else {
            FileChannel fc = fis.getChannel();
            fc.position(0);
        }

        Hashtable<String, RawDataFile> dataFilesIDMap = new Hashtable<String, RawDataFile>();
        for (RawDataFile file : project.getDataFiles()) {
            dataFilesIDMap.put(file.getName(), file);
        }

        peakListOpenHander = new PeakListOpenHandler_2_0(dataFilesIDMap);

        buildingPeakList = peakListOpenHander.readPeakList(finalStream);
        finalStream.close();

    } catch (Throwable e) {
        /* we may already have set the status to CANCELED */
        if (getStatus() == TaskStatus.PROCESSING)
            setStatus(TaskStatus.ERROR);
        setErrorMessage(e.toString());
        e.printStackTrace();
        return;
    }

    // Add new peaklist to the project or MZviewer.desktop
    project.addPeakList(buildingPeakList);

    logger.info("Finished parsing " + fileName);
    setStatus(TaskStatus.FINISHED);

}

From source file:org.phoenicis.tools.archive.Tar.java

List<File> uncompressTarXzFile(File inputFile, File outputDir, Consumer<ProgressEntity> stateCallback) {
    try (CountingInputStream countingInputStream = new CountingInputStream(new FileInputStream(inputFile));
            InputStream inputStream = new XZCompressorInputStream(countingInputStream)) {
        final long finalSize = FileUtils.sizeOf(inputFile);
        return uncompress(inputStream, countingInputStream, outputDir, finalSize, stateCallback);
    } catch (IOException e) {
        throw new ArchiveException(TAR_ERROR_MESSAGE, e);
    }//  w  w  w . j  a v a 2 s.  c  o  m
}

From source file:org.b1.pack.standard.reader.ChunkCursor.java

private void resumeLzmaDecoder() throws IOException {
    if (lzmaDecoder == null) {
        lzmaDecoder = new LzmaDecoder(blockCursor.getExecutorService());
    }// ww w  . ja  v  a 2  s.  c om
    lzmaDecoder.init(lzmaProperties);
    inputStream = new CountingInputStream(lzmaDecoder.getInputStream(encodedInputStream));
}

From source file:com.broadwave.android.brut.androlib.res.decoder.ARSCDecoder.java

private ARSCDecoder(InputStream arscStream, ResTable resTable, boolean storeFlagsOffsets, boolean keepBroken) {
    if (storeFlagsOffsets) {
        arscStream = mCountIn = new CountingInputStream(arscStream);
        mFlagsOffsets = new ArrayList<FlagsOffset>();
    } else {//from  ww w  . j a  v a2 s  .c om
        mCountIn = null;
        mFlagsOffsets = null;
    }
    mIn = new ExtDataInput((DataInput) new LittleEndianDataInputStream(arscStream));
    mResTable = resTable;
    mKeepBroken = keepBroken;
}

From source file:com.palantir.atlasdb.stream.AbstractExpiringStreamStore.java

protected final StreamMetadata storeBlocksAndGetFinalMetadata(ID id, InputStream stream, long duration,
        TimeUnit durationUnit) {//from  w  ww  . j ava  2  s .  c om
    // 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(id, countingStream, duration, durationUnit);
    } 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, duration, durationUnit);
        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;
}