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

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

Introduction

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

Prototype

public long getCount() 

Source Link

Document

Returns the number of bytes read.

Usage

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

@Nullable
@Override//from   w  w  w .jav  a2s  . 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.locationtech.geogig.rest.repository.SendObjectResource.java

@Override
public void post(Representation entity) {
    InputStream input = null;/* w  w w.j av  a  2  s  . c o  m*/

    Request request = getRequest();
    try {
        LOGGER.info("Receiving objects from {}", request.getClientInfo().getAddress());
        Representation representation = request.getEntity();
        input = representation.getStream();
        final GeoGIG ggit = getGeogig(request).get();
        final BinaryPackedObjects unpacker = new BinaryPackedObjects(ggit.getRepository().objectDatabase());

        CountingInputStream countingStream = new CountingInputStream(input);

        Stopwatch sw = Stopwatch.createStarted();
        IngestResults ingestResults = unpacker.ingest(countingStream);
        sw.stop();

        LOGGER.info(String.format(
                "SendObjectResource: Processed %,d objects.\nInserted: %,d.\nExisting: %,d.\nTime to process: %s.\nStream size: %,d bytes.\n",
                ingestResults.total(), ingestResults.getInserted(), ingestResults.getExisting(), sw,
                countingStream.getCount()));

    } catch (IOException e) {
        LOGGER.warn("Error processing incoming objects from {}", request.getClientInfo().getAddress(), e);
        throw new RestletException(e.getMessage(), Status.SERVER_ERROR_INTERNAL, e);
    } finally {
        if (input != null)
            Closeables.closeQuietly(input);
    }
}

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;
}

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

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

From source file:org.locationtech.geogig.spring.service.LegacySendObjectService.java

public SendObject sendObject(RepositoryProvider provider, String repoName, InputStream request) {
    final SendObject sendObject = new SendObject();
    final Repository repository = getRepository(provider, repoName);
    final BinaryPackedObjects unpacker = new BinaryPackedObjects(repository.objectDatabase());

    CountingInputStream countingStream = new CountingInputStream(request);

    Stopwatch sw = Stopwatch.createStarted();
    BinaryPackedObjects.IngestResults ingestResults = unpacker.ingest(countingStream);
    sw.stop();/*from ww w  . j a va 2s .  com*/
    sendObject.setExisting(ingestResults.getExisting()).setInserted(ingestResults.getInserted());
    LOGGER.info(String.format(
            "SendObjectResource: Processed %,d objects.\nInserted: %,d.\nExisting: %,d.\nTime to process: %s.\nStream size: %,d bytes.\n",
            ingestResults.total(), ingestResults.getInserted(), ingestResults.getExisting(), sw,
            countingStream.getCount()));
    return sendObject;
}

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

@Override
@Nullable//from   w ww  .j  a  va  2 s . co  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.jabylon.log.viewer.pages.util.LogTail.java

public void nextChunk(int maxLines, Deque<String> buffer) {
    BufferedReader reader = null;
    try {/*from   w  ww  . j a  v a  2 s  .  com*/
        CountingInputStream in = new CountingInputStream(new FileInputStream(logFile));
        //buffer of 1 is slow, but at least predictable, so we can reset
        reader = new BufferedReader(new InputStreamReader(in), 1);
        reader.skip(currentChunk);
        String s = null;
        int lines = 0;
        while ((s = reader.readLine()) != null) {
            buffer.add(s);
            lines++;
            //unless it's the first chunk we stop once we reached max lines
            if (currentChunk > 0 && lines == maxLines)
                break;
        }
        currentChunk = in.getCount();
    } catch (FileNotFoundException e) {
        LOG.warn("Logfile does not seem to exist (yet)", e);
    } catch (IOException e) {
        LOG.warn("Failed to read logfile", e);
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            LOG.error("Failed to close the logfile", e);
        }
    }
}

From source file:org.dcm4chee.storage.cloud.CloudStorageSystemProvider.java

private void upload(StorageContext ctx, InputStream in, String name, long len) throws IOException {
    String container = system.getStorageSystemContainer();
    BlobStore blobStore = context.getBlobStore();
    if (blobStore.blobExists(container, name))
        throw new ObjectAlreadyExistsException(system.getStorageSystemPath(), container + '/' + name);
    CountingInputStream cin = new CountingInputStream(in);
    Payload payload = new InputStreamPayload(cin);
    if (len != -1) {
        payload.getContentMetadata().setContentLength(len);
    }//from w w  w.jav  a  2 s.c  o m
    Blob blob = blobStore.blobBuilder(name).payload(payload).build();
    String etag = (multipartUploader != null) ? multipartUploader.upload(container, blob)
            : blobStore.putBlob(container, blob);
    ctx.setFileSize(cin.getCount());
    log.info("Uploaded[uri={}, container={}, name={}, etag={}]", system.getStorageSystemPath(), container, name,
            etag);
}

From source file:org.openscoring.service.ModelRegistry.java

@SuppressWarnings(value = { "resource" })
public Model load(InputStream is) throws Exception {
    CountingInputStream countingIs = new CountingInputStream(is);

    HashingInputStream hashingIs = new HashingInputStream(Hashing.md5(), countingIs);

    ModelEvaluator<?> evaluator = unmarshal(hashingIs, this.validate);

    PMML pmml = evaluator.getPMML();//w  ww  .  j  a  v a2s  .  c o m

    for (Class<? extends Visitor> visitorClazz : this.visitorClazzes) {
        Visitor visitor = visitorClazz.newInstance();

        visitor.applyTo(pmml);
    }

    evaluator.verify();

    Model model = new Model(evaluator);
    model.putProperty(Model.PROPERTY_FILE_SIZE, countingIs.getCount());
    model.putProperty(Model.PROPERTY_FILE_MD5SUM, (hashingIs.hash()).toString());

    return model;
}

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   w ww.java 2  s  .  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;
}