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

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

Introduction

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

Prototype

public long getCount() 

Source Link

Document

Returns the number of bytes written.

Usage

From source file:io.prestosql.orc.metadata.OrcMetadataWriter.java

private static int writeProtobufObject(OutputStream output, MessageLite object) throws IOException {
    CountingOutputStream countingOutput = new CountingOutputStream(output);
    object.writeTo(countingOutput);// w w w  .j a  va2s  .c o m
    return toIntExact(countingOutput.getCount());
}

From source file:com.google.cloud.dataflow.sdk.runners.worker.DataflowApiUtils.java

/**
 * Determines the serialized size (in bytes) of the {@link GenericJson} object that will be
 * serialized and sent to the Google Cloud Dataflow service API.
 *
 * <p>Uses only constant memory./* w  w  w .java2s. c  om*/
 */
public static long computeSerializedSizeBytes(GenericJson object) throws IOException {
    JsonFactory factory = object.getFactory();
    if (factory == null) {
        factory = Transport.getJsonFactory();
    }

    CountingOutputStream stream = new CountingOutputStream(ByteStreams.nullOutputStream());
    JsonGenerator generator = null;
    try {
        generator = factory.createJsonGenerator(stream, StandardCharsets.UTF_8);
        generator.serialize(object);
        generator.close(); // also closes the stream.
    } finally {
        if (generator != null) {
            generator.close();
        }
    }
    return stream.getCount();
}

From source file:org.apache.beam.sdk.util.PackageUtil.java

/**
 * Compute and cache the attributes of a classpath element that we will need to stage it.
 *
 * @param classpathElement the file or directory to be staged.
 * @param stagingPath The base location for staged classpath elements.
 * @param overridePackageName If non-null, use the given value as the package name
 *                            instead of generating one automatically.
 * @return a {@link PackageAttributes} that containing metadata about the object to be staged.
 *///ww  w .ja  va  2 s  . c om
static PackageAttributes createPackageAttributes(File classpathElement, String stagingPath,
        String overridePackageName) {
    try {
        boolean directory = classpathElement.isDirectory();

        // Compute size and hash in one pass over file or directory.
        Hasher hasher = Hashing.md5().newHasher();
        OutputStream hashStream = Funnels.asOutputStream(hasher);
        CountingOutputStream countingOutputStream = new CountingOutputStream(hashStream);

        if (!directory) {
            // Files are staged as-is.
            Files.asByteSource(classpathElement).copyTo(countingOutputStream);
        } else {
            // Directories are recursively zipped.
            ZipFiles.zipDirectory(classpathElement, countingOutputStream);
        }

        long size = countingOutputStream.getCount();
        String hash = Base64Variants.MODIFIED_FOR_URL.encode(hasher.hash().asBytes());

        // Create the DataflowPackage with staging name and location.
        String uniqueName = getUniqueContentName(classpathElement, hash);
        String resourcePath = IOChannelUtils.resolve(stagingPath, uniqueName);
        DataflowPackage target = new DataflowPackage();
        target.setName(overridePackageName != null ? overridePackageName : uniqueName);
        target.setLocation(resourcePath);

        return new PackageAttributes(size, hash, directory, target);
    } catch (IOException e) {
        throw new RuntimeException("Package setup failure for " + classpathElement, e);
    }
}

From source file:bear.plugins.java.JenkinsCache.java

public static File download(String jdkVersion, File jenkinsCache, File tempDestDir, String jenkinsUri,
        String user, String pass) {
    try {/* w  w  w  .  j  av a 2  s  .  com*/
        Optional<JDKFile> optional = load(jenkinsCache, jenkinsUri, jdkVersion);

        if (!optional.isPresent()) {
            throw new RuntimeException("could not find: " + jdkVersion);
        }

        String uri = optional.get().filepath;

        SSLContext sslContext = SSLContext.getInstance("TLSv1");

        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                System.out.println("getAcceptedIssuers =============");
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
                System.out.println("checkClientTrusted =============");
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
                System.out.println("checkServerTrusted =============");
            }
        } }, new SecureRandom());

        SSLSocketFactory sf = new SSLSocketFactory(sslContext);

        Scheme httpsScheme = new Scheme("https", 443, sf);
        SchemeRegistry schemeRegistry = new SchemeRegistry();

        Scheme httpScheme = new Scheme("http", 80, PlainSocketFactory.getSocketFactory());

        schemeRegistry.register(httpsScheme);
        schemeRegistry.register(httpScheme);

        DefaultHttpClient httpClient = new DefaultHttpClient(
                new PoolingClientConnectionManager(schemeRegistry));

        CookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie("gpw_e24", ".");
        cookie.setDomain("oracle.com");
        cookie.setPath("/");
        cookie.setSecure(true);

        cookieStore.addCookie(cookie);

        httpClient.setCookieStore(cookieStore);

        HttpPost httppost = new HttpPost("https://login.oracle.com");

        httppost.setHeader("Authorization",
                "Basic " + new String(Base64.encodeBase64((user + ":" + pass).getBytes()), "UTF-8"));

        HttpResponse response = httpClient.execute(httppost);

        int code = response.getStatusLine().getStatusCode();

        if (code != 302) {
            System.out.println(IOUtils.toString(response.getEntity().getContent()));
            throw new RuntimeException("unable to auth: " + code);
        }

        // closes the single connection
        //                EntityUtils.consumeQuietly(response.getEntity());

        httppost = new HttpPost(uri);

        httppost.setHeader("Authorization",
                "Basic " + new String(Base64.encodeBase64((user + ":" + pass).getBytes()), "UTF-8"));

        response = httpClient.execute(httppost);

        code = response.getStatusLine().getStatusCode();

        if (code != 302) {
            System.out.println(IOUtils.toString(response.getEntity().getContent()));
            throw new RuntimeException("to download: " + uri);
        }

        File file = new File(tempDestDir, optional.get().name);
        HttpEntity entity = response.getEntity();

        final long length = entity.getContentLength();

        final CountingOutputStream os = new CountingOutputStream(new FileOutputStream(file));

        System.out.printf("Downloading %s to %s...%n", uri, file);

        Thread progressThread = new Thread(new Runnable() {
            double lastProgress;

            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    long copied = os.getCount();

                    double progress = copied * 100D / length;

                    if (progress != lastProgress) {
                        System.out.printf("\rProgress: %s%%", LangUtils.toConciseString(progress, 1));
                    }

                    lastProgress = progress;

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        }, "progressThread");

        progressThread.start();

        ByteStreams.copy(entity.getContent(), os);

        progressThread.interrupt();

        System.out.println("Download complete.");

        return file;
    } catch (Exception e) {
        throw Exceptions.runtime(e);
    }
}

From source file:bear.plugins.java.JenkinsCache.java

public static File download2(String jdkVersion, File jenkinsCache, File tempDestDir, String jenkinsUri,
        String user, String pass) {
    try {//from   ww  w  . java  2  s . c o  m
        Optional<JDKFile> optional = load(jenkinsCache, jenkinsUri, jdkVersion);

        if (!optional.isPresent()) {
            throw new RuntimeException("could not find: " + jdkVersion);
        }

        String uri = optional.get().filepath;

        //                agent.get()

        //                agent.get()

        SSLContext sslContext = SSLContext.getInstance("TLSv1");

        sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                System.out.println("getAcceptedIssuers =============");
                return null;
            }

            public void checkClientTrusted(X509Certificate[] certs, String authType) {
                System.out.println("checkClientTrusted =============");
            }

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
                System.out.println("checkServerTrusted =============");
            }
        } }, new SecureRandom());

        SSLSocketFactory sf = new SSLSocketFactory(sslContext);

        Scheme httpsScheme = new Scheme("https", 443, sf);
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(httpsScheme);

        DefaultHttpClient httpClient = new DefaultHttpClient(
                new PoolingClientConnectionManager(schemeRegistry));

        MechanizeAgent agent = new MechanizeAgent();
        Cookie cookie2 = agent.cookies().addNewCookie("gpw_e24", ".", "oracle.com");
        cookie2.getHttpCookie().setPath("/");
        cookie2.getHttpCookie().setSecure(false);

        CookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie("gpw_e24", ".");
        cookie.setDomain("oracle.com");
        cookie.setPath("/");
        cookie.setSecure(true);

        cookieStore.addCookie(cookie);

        httpClient.setCookieStore(cookieStore);

        HttpPost httppost = new HttpPost("https://login.oracle.com");

        httppost.setHeader("Authorization",
                "Basic " + new String(Base64.encodeBase64((user + ":" + pass).getBytes()), "UTF-8"));

        HttpResponse response = httpClient.execute(httppost);

        int code = response.getStatusLine().getStatusCode();

        if (code != 302) {
            System.out.println(IOUtils.toString(response.getEntity().getContent()));
            throw new RuntimeException("unable to auth: " + code);
        }

        //                EntityUtils.consumeQuietly(response.getEntity());

        httppost = new HttpPost(uri);

        response = httpClient.execute(httppost);

        code = response.getStatusLine().getStatusCode();

        if (code != 302) {
            System.out.println(IOUtils.toString(response.getEntity().getContent()));
            throw new RuntimeException("to download: " + uri);
        }

        File file = new File(tempDestDir, optional.get().name);
        HttpEntity entity = response.getEntity();

        final long length = entity.getContentLength();

        final CountingOutputStream os = new CountingOutputStream(new FileOutputStream(file));

        System.out.printf("Downloading %s to %s...%n", uri, file);

        Thread progressThread = new Thread(new Runnable() {
            double lastProgress;

            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    long copied = os.getCount();

                    double progress = copied * 100D / length;

                    if (progress != lastProgress) {
                        System.out.printf("\rProgress: %s%%", LangUtils.toConciseString(progress, 1));
                    }

                    lastProgress = progress;

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        break;
                    }
                }
            }
        }, "progressThread");

        progressThread.start();

        ByteStreams.copy(entity.getContent(), os);

        progressThread.interrupt();

        System.out.println("Download complete.");

        return file;
    } catch (Exception e) {
        throw Exceptions.runtime(e);
    }
}

From source file:com.google.cloud.dataflow.sdk.coders.StandardCoder.java

/**
 * Returns the size in bytes of the encoded value using this coder.
 *//*www . j  a  v  a  2 s .  c om*/
protected long getEncodedElementByteSize(T value, Context context) throws Exception {
    try {
        CountingOutputStream os = new CountingOutputStream(ByteStreams.nullOutputStream());
        encode(value, os, context);
        return os.getCount();
    } catch (Exception exn) {
        throw new IllegalArgumentException(
                "Unable to encode element '" + value + "' with coder '" + this + "'.", exn);
    }
}

From source file:com.google.cloud.dataflow.sdk.coders.StringUtf8Coder.java

/**
 * {@inheritDoc}/*from   www .  ja va2  s .c  o  m*/
 *
 * @return the byte size of the UTF-8 encoding of the a string or, in a nested context,
 * the byte size of the encoding plus the encoded length prefix.
 */
@Override
protected long getEncodedElementByteSize(String value, Context context) throws Exception {
    if (value == null) {
        throw new CoderException("cannot encode a null String");
    }
    if (context.isWholeStream) {
        return Utf8.encodedLength(value);
    } else {
        CountingOutputStream countingStream = new CountingOutputStream(ByteStreams.nullOutputStream());
        DataOutputStream stream = new DataOutputStream(countingStream);
        writeString(value, stream);
        return countingStream.getCount();
    }
}

From source file:ch.ledcom.tomcat.valves.SessionSizeValve.java

private long measureSerializedSize(final Object attribute) throws IOException {
    final Closer closer = Closer.create();
    try {//from   www.j a va  2s .co m
        final CountingOutputStream countingStream = closer
                .register(new CountingOutputStream(ByteStreams.nullOutputStream()));
        final ObjectOutputStream out = closer.register(new ObjectOutputStream(countingStream));
        out.writeObject(attribute);
        return countingStream.getCount();
    } finally {
        closer.close();
    }
}

From source file:com.netflix.servo.example.BaseHandler.java

public void handle(HttpExchange exchange) throws IOException {
    CountingInputStream input = new CountingInputStream(exchange.getRequestBody());
    CountingOutputStream output = new CountingOutputStream(exchange.getResponseBody());
    exchange.setStreams(input, output);//from   ww  w .  ja va 2  s.  c  o m
    Stopwatch stopwatch = latency.start();
    try {
        handleImpl(exchange);
    } finally {
        stopwatch.stop();
        bytesReceived.increment(input.getCount());
        bytesSent.increment(output.getCount());
    }
}

From source file:org.jclouds.http.internal.JavaUrlHttpCommandExecutorService.java

/**
 * @param payload//from w  w  w.  j  a v  a  2  s .c  o  m
 *           payload to write
 * @param lengthDesc
 *           what to use in error log when an IOException occurs
 * @param connection
 *           connection to write to
 */
void writePayloadToConnection(Payload payload, Object lengthDesc, HttpURLConnection connection)
        throws IOException {
    connection.setDoOutput(true);
    CountingOutputStream out = new CountingOutputStream(connection.getOutputStream());
    try {
        payload.writeTo(out);
    } catch (IOException e) {
        logger.error(e, "error after writing %d/%s bytes to %s", out.getCount(), lengthDesc,
                connection.getURL());
        throw e;
    }
}