Example usage for com.google.common.io ByteStreams nullOutputStream

List of usage examples for com.google.common.io ByteStreams nullOutputStream

Introduction

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

Prototype

public static OutputStream nullOutputStream() 

Source Link

Document

Returns an OutputStream that simply discards written bytes.

Usage

From source file:com.google.devtools.build.lib.util.Fingerprint.java

/** Creates and initializes a new instance. */
public Fingerprint() {
    md5 = cloneOrCreateMd5();/*from w  w w .j a  v  a  2  s  .  co m*/
    // This is a lot of indirection, but CodedOutputStream does a reasonable job of converting
    // strings to bytes without creating a whole bunch of garbage, which pays off.
    codedOut = CodedOutputStream.newInstance(new DigestOutputStream(ByteStreams.nullOutputStream(), md5),
            /*bufferSize=*/ 1024);
}

From source file:com.google.gerrit.server.project.Index.java

@Override
public Response.Accepted apply(ProjectResource resource, ProjectInput input) {
    Project.NameKey project = resource.getNameKey();
    Task mpt = new MultiProgressMonitor(ByteStreams.nullOutputStream(), "Reindexing project").beginSubTask("",
            MultiProgressMonitor.UNKNOWN);
    PrintWriter pw = new PrintWriter(CharStreams.nullWriter());
    executor.submit(allChangesIndexer.reindexProject(indexer, project, mpt, mpt, pw));
    return Response.accepted("Project " + project + " submitted for reindexing");
}

From source file:com.google.devtools.build.lib.sandbox.DarwinSandboxRunner.java

static boolean isSupported() {
    // Check osx version, only >=10.11 is supported.
    // And we should check if sandbox still work when it gets 11.x
    String osxVersion = OS.getVersion();
    String[] parts = osxVersion.split("\\.");
    if (parts.length != 3) {
        // Currently the format is 10.11.x
        return false;
    }// www  .ja va  2  s. com
    try {
        int v0 = Integer.parseInt(parts[0]);
        int v1 = Integer.parseInt(parts[1]);
        if (v0 != 10 || v1 < 11) {
            return false;
        }
    } catch (NumberFormatException e) {
        return false;
    }

    List<String> args = new ArrayList<>();
    args.add(SANDBOX_EXEC);
    args.add("-p");
    args.add("(version 1) (allow default)");
    args.add("/usr/bin/true");

    ImmutableMap<String, String> env = ImmutableMap.of();
    File cwd = new File("/usr/bin");

    Command cmd = new Command(args.toArray(new String[0]), env, cwd);
    try {
        cmd.execute(/* stdin */ new byte[] {}, Command.NO_OBSERVER, ByteStreams.nullOutputStream(),
                ByteStreams.nullOutputStream(), /* killSubprocessOnInterrupt */ true);
    } catch (CommandException e) {
        return false;
    }

    return true;
}

From source file:com.google.devtools.build.lib.sandbox.NamespaceSandboxRunner.java

static boolean isSupported(BlazeRuntime runtime) {
    Path execRoot = runtime.getExecRoot();
    BinTools binTools = runtime.getBinTools();

    List<String> args = new ArrayList<>();
    args.add(execRoot.getRelative(binTools.getExecPath(NAMESPACE_SANDBOX)).getPathString());
    args.add("-C");

    ImmutableMap<String, String> env = ImmutableMap.of();
    File cwd = execRoot.getPathFile();

    Command cmd = new Command(args.toArray(new String[0]), env, cwd);
    try {// w  w  w.j  av  a  2  s  .c o m
        cmd.execute(/* stdin */ new byte[] {}, Command.NO_OBSERVER, ByteStreams.nullOutputStream(),
                ByteStreams.nullOutputStream(), /* killSubprocessOnInterrupt */ true);
    } catch (CommandException e) {
        return false;
    }

    return true;
}

From source file:com.google.devtools.build.lib.sandbox.LinuxSandboxRunner.java

static boolean isSupported(CommandEnvironment commandEnv) {
    Path execRoot = commandEnv.getExecRoot();

    PathFragment embeddedTool = commandEnv.getBlazeWorkspace().getBinTools().getExecPath(LINUX_SANDBOX);
    if (embeddedTool == null) {
        // The embedded tool does not exist, meaning that we don't support sandboxing (e.g., while
        // bootstrapping).
        return false;
    }//from  ww  w.j av a 2s .  com

    List<String> args = new ArrayList<>();
    args.add(execRoot.getRelative(embeddedTool).getPathString());
    args.add("-C");

    ImmutableMap<String, String> env = ImmutableMap.of();
    File cwd = execRoot.getPathFile();

    Command cmd = new Command(args.toArray(new String[0]), env, cwd);
    try {
        cmd.execute(/* stdin */ new byte[] {}, Command.NO_OBSERVER, ByteStreams.nullOutputStream(),
                ByteStreams.nullOutputStream(), /* killSubprocessOnInterrupt */ true);
    } catch (CommandException e) {
        return false;
    }

    return true;
}

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

private long measureSerializedSize(final Object attribute) throws IOException {
    final Closer closer = Closer.create();
    try {/* www.jav a 2s. c om*/
        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:org.eclipse.hawkbit.ui.artifacts.upload.FileTransferHandlerStreamVariable.java

@Override
public final OutputStream getOutputStream() {
    if (isUploadInterrupted()) {
        return ByteStreams.nullOutputStream();
    }//  www.j a v  a  2s . c om

    // we return the outputstream so we cannot close it here
    @SuppressWarnings("squid:S2095")
    final PipedOutputStream outputStream = new PipedOutputStream();
    PipedInputStream inputStream = null;
    try {
        inputStream = new PipedInputStream(outputStream);
        publishUploadProgressEvent(fileUploadId, 0, fileSize);
        startTransferToRepositoryThread(inputStream, fileUploadId, mimeType);
    } catch (final IOException e) {
        LOG.error("Creating piped Stream failed {}.", e);
        tryToCloseIOStream(outputStream);
        tryToCloseIOStream(inputStream);
        interruptUploadDueToUploadFailed();
        publishUploadFailedAndFinishedEvent(fileUploadId, e);
        return ByteStreams.nullOutputStream();
    }
    return outputStream;
}

From source file:org.jclouds.glacier.util.TreeHash.java

/**
 * Builds the Hash and the TreeHash values of the payload.
 *
 * @return The calculated TreeHash./*from   w ww  . ja  v  a  2  s.co m*/
 * @see <a href="http://docs.aws.amazon.com/amazonglacier/latest/dev/checksum-calculations.html" />
 */
public static TreeHash buildTreeHashFromPayload(Payload payload) throws IOException {
    InputStream is = null;
    try {
        is = checkNotNull(payload, "payload").openStream();
        Builder<HashCode> list = ImmutableList.builder();
        HashingInputStream linearHis = new HashingInputStream(Hashing.sha256(), is);
        while (true) {
            HashingInputStream chunkedHis = new HashingInputStream(Hashing.sha256(),
                    ByteStreams.limit(linearHis, CHUNK_SIZE));
            long count = ByteStreams.copy(chunkedHis, ByteStreams.nullOutputStream());
            if (count == 0) {
                break;
            }
            list.add(chunkedHis.hash());
        }
        //The result list contains exactly one element now.
        return new TreeHash(hashList(list.build()), linearHis.hash());
    } finally {
        closeQuietly(is);
    }
}

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

/**
 * Check if an object is serializable, emit a warning log if it is not.
 *
 * @param attribute//from  w  ww .  j ava 2  s  . co  m
 *            the attribute to check
 * @throws IOException
 */
private void checkSerializable(final Object attribute) throws IOException {
    if (!Serializable.class.isAssignableFrom(attribute.getClass())) {
        log.warn(format("Session attribute [%s] of class [%s] is not " + "serializable.", attribute,
                attribute.getClass()));
    }
    final Closer closer = Closer.create();
    try {
        final ObjectOutputStream out = closer.register(new ObjectOutputStream(ByteStreams.nullOutputStream()));
        out.writeObject(attribute);
    } catch (Throwable t) {
        closer.rethrow(t);
    } finally {
        closer.close();
    }
}

From source file:org.gaul.areweconsistentyet.AreWeConsistentYet.java

public int readAfterCreate() throws IOException {
    int count = 0;
    for (int i = 0; i < iterations; ++i) {
        String blobName = makeBlobName();
        blobStore.putBlob(containerName, makeBlob(blobName, payload1));
        Blob getBlob = blobStoreRead.getBlob(containerName, blobName);
        if (getBlob == null) {
            ++count;/*w  ww  .  j ava  2 s .c o m*/
        } else {
            try (Payload payload = getBlob.getPayload(); InputStream is = payload.openStream()) {
                ByteStreams.copy(is, ByteStreams.nullOutputStream());
            }
        }
        blobStore.removeBlob(containerName, blobName);
    }
    return count;
}