Example usage for com.google.common.io ByteSource wrap

List of usage examples for com.google.common.io ByteSource wrap

Introduction

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

Prototype

public static ByteSource wrap(byte[] b) 

Source Link

Document

Returns a view of the given byte array as a ByteSource .

Usage

From source file:org.jclouds.digitalocean2.ssh.DSAKeys.java

/**
 * Executes {@link org.jclouds.crypto.Pems#publicKeySpecFromOpenSSH(com.google.common.io.InputSupplier)} on the
 * string which was OpenSSH Base64 Encoded {@code id_rsa.pub}
 * //  w  w w.j  a  v  a2 s  . c om
 * @param idRsaPub formatted {@code ssh-dss AAAAB3NzaC1yc2EAAAADAQABAAAB...}
 * @see org.jclouds.crypto.Pems#publicKeySpecFromOpenSSH(com.google.common.io.InputSupplier)
 */
public static DSAPublicKeySpec publicKeySpecFromOpenSSH(String idDsaPub) {
    try {
        return publicKeySpecFromOpenSSH(ByteSource.wrap(idDsaPub.getBytes(Charsets.UTF_8)));
    } catch (IOException e) {
        throw propagate(e);
    }
}

From source file:com.torodb.torod.db.backends.mysql.converters.json.BinaryValueToJsonConverter.java

@Override
public ScalarBinary toValue(String value) {
    if (!value.startsWith("base64:type15:")) {
        throw new ToroImplementationException(
                "A binary in escape format was expected, but " + value + " was found");
    }//from   w  w  w .  j a v  a 2  s . c o  m
    return new ByteSourceScalarBinary(KVBinarySubtype.MONGO_GENERIC, (byte) 0,
            ByteSource.wrap(DatatypeConverter.parseBase64Binary(value.substring(14))));
}

From source file:com.torodb.torod.db.backends.mysql.converters.array.BinaryToArrayConverter.java

@Override
public ScalarBinary fromJsonValue(JsonString value) {
    if (!value.getString().startsWith("base64:type15:")) {
        throw new ToroImplementationException(
                "A binary in escape format was expected, but " + value + " was found");
    }/*  w  ww .j a  va  2 s.  c  o m*/
    return new ByteSourceScalarBinary(KVBinarySubtype.MONGO_GENERIC, (byte) 0,
            ByteSource.wrap(DatatypeConverter.parseBase64Binary(value.getString().substring(14))));
}

From source file:com.torodb.torod.db.backends.mysql.converters.jooq.BinaryValueConverter.java

@Override
public ScalarBinary from(byte[] databaseObject) {
    return new ByteSourceScalarBinary(KVBinarySubtype.MONGO_GENERIC, (byte) 0, ByteSource.wrap(databaseObject));
}

From source file:de.nx42.maps4cim.header.HeaderParser.java

/**
 * Takes a byte-array representing a full CiM 2 map (or at least the full
 * header of the map), parses the header and returns a CustomHeader-object
 * representing the relevant contents of this header.
 * @param map the map to parse//from   www  . j  av  a 2  s.c o  m
 * @return the CustomHeader-object containing the data of this header
 * @throws ParseException if there is an error parsing the header
 * @throws IOException if there is an error accessing the array-contents
 * (highly unlikely, this is owed to the InputStream-abstraction that is used)
 */
public static CustomHeader parse(byte[] map) throws ParseException, IOException {
    ByteSource bs = ByteSource.wrap(map);
    return parse(bs);
}

From source file:org.spka.cursus.publish.website.ftp.Uploader.java

public void to(FTPClient ftp, Map<String, ByteSource> newFiles) throws IOException {
    if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) {
        throw new IllegalStateException("Unable to set mode to binary");
    }//w w w  .  j a v  a2  s . co  m

    for (Map.Entry<String, ByteSource> file : newFiles.entrySet()) {
        ByteSource other = null;

        if (existingFiles.containsKey(file.getKey())) {
            other = existingFiles.get(file.getKey());

            if (other == null) {
                ByteArrayOutputStream buf = new ByteArrayOutputStream();
                if (!ftp.retrieveFile(file.getKey(), buf)) {
                    throw new IllegalStateException("Unable to retrieve " + file.getKey());
                }
                buf.close();
                other = ByteSource.wrap(buf.toByteArray());
                existingFiles.put(file.getKey(), other);
            }
        }

        if (other == null || !file.getValue().contentEquals(other)) {
            InputStream in = file.getValue().openStream();
            if (!ftp.storeFile(file.getKey(), in)) {
                throw new IllegalStateException("Unable to store " + file.getKey());
            }
            in.close();

            existingFiles.put(file.getKey(), file.getValue());
        }
    }
}

From source file:org.haiku.haikudepotserver.support.AbstractExternalToolService.java

protected byte[] execute(T context, byte[] input) throws IOException {
    Preconditions.checkArgument(null != input && 0 != input.length, "the input is not specified");
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    execute(context, ByteSource.wrap(input), new ByteSink() {
        @Override//from  w  ww.  ja va 2s .c om
        public OutputStream openStream() {
            return baos;
        }
    });

    return baos.toByteArray();
}

From source file:org.jclouds.examples.glacier.MainApp.java

private static void putAndRetrieveBlobExample(BlobStore blobstore) throws IOException {
    // Create a container
    String containerName = "jclouds_putAndRetrieveBlobExample_" + UUID.randomUUID().toString();
    blobstore.createContainerInLocation(null, containerName); // Create a vault

    // Create a blob
    ByteSource payload = ByteSource.wrap("data".getBytes(Charsets.UTF_8));
    Blob blob = blobstore.blobBuilder("ignored") // The blob name is ignored in Glacier
            .payload(payload).contentLength(payload.size()).build();

    // Put the blob in the container
    String blobId = blobstore.putBlob(containerName, blob);

    // Retrieve the blob
    Blob result = blobstore.getBlob(containerName, blobId);

    // Print the result
    InputStream is = result.getPayload().openStream();
    try {/*from   w ww. j a  va2  s. c om*/
        String data = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8));
        System.out.println("The retrieved payload is: " + data);
    } finally {
        is.close();
    }
}

From source file:com.android.builder.internal.packaging.zip.utils.ByteTracker.java

/**
 * Creates a new byte source by snapshotting the provided stream.
 * @param stream the stream with the data
 * @return a byte source containing the cached data from the given stream
 * @throws IOException failed to read the stream
 *///w ww  .  j av  a  2  s .  c om
public CloseableDelegateByteSource fromStream(@NonNull ByteArrayOutputStream stream) throws IOException {
    byte[] data = stream.toByteArray();
    updateUsage(data.length);
    return new CloseableDelegateByteSource(ByteSource.wrap(data), data.length) {
        @Override
        public synchronized void innerClose() throws IOException {
            super.innerClose();
            updateUsage(-sizeNoException());
        }
    };
}

From source file:org.apache.brooklyn.core.mgmt.persist.jclouds.JcloudsStoreObjectAccessor.java

@Override
public void put(String val) {
    if (val == null)
        val = "";

    blobStore.createContainerInLocation(null, containerName);
    // seems not needed, at least not w SoftLayer
    //        blobStore.createDirectory(containerName, directoryName);
    ByteSource payload = ByteSource.wrap(val.getBytes(Charsets.UTF_8));
    Blob blob;/*  w w w.  j a v a 2s  .  c o m*/
    try {
        blob = blobStore.blobBuilder(blobName).payload(payload).contentLength(payload.size()).build();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
    blobStore.putBlob(containerName, blob);
}