List of usage examples for com.google.common.io ByteSource size
public long size() throws IOException
From source file:org.aegis.app.sample.s3.blobsample1.java
public static void main(String[] args) throws IOException { if (args.length < PARAMETERS) throw new IllegalArgumentException(INVALID_SYNTAX); // Args//from ww w . j a v a 2 s .c o m String provider = args[0]; // note that you can check if a provider is present ahead of time checkArgument(contains(allKeys, provider), "provider %s not in supported list: %s", provider, allKeys); String identity = args[1]; String credential = args[2]; String containerName = args[3]; // Init BlobStoreContext context = ContextBuilder.newBuilder(provider).credentials(identity, credential) .buildView(BlobStoreContext.class); try { // Create Container BlobStore blobStore = context.getBlobStore(); blobStore.createContainerInLocation(null, containerName); String blobName = "test"; ByteSource payload = ByteSource.wrap("testdata".getBytes(Charsets.UTF_8)); // List Container Metadata for (StorageMetadata resourceMd : blobStore.list()) { if (containerName.equals(resourceMd.getName())) { System.out.println(resourceMd); } } // Add Blob Blob blob = blobStore.blobBuilder(blobName).payload(payload).contentLength(payload.size()).build(); blobStore.putBlob(containerName, blob); // Use Provider API if (context.getBackendType().getRawType().equals(RestContext.class)) { RestContext<?, ?> rest = context.unwrap(); Object object = null; if (rest.getApi() instanceof S3Client) { RestContext<S3Client, S3AsyncClient> providerContext = context.unwrap(); object = providerContext.getApi().headObject(containerName, blobName); } if (object != null) { System.out.println(object); } } } catch (Exception e) { System.err.println("error: " + e.getMessage()); } finally { // Close connecton context.close(); System.exit(0); } }
From source file:org.jclouds.examples.blobstore.basics.MainApp.java
public static void main(String[] args) throws IOException { if (args.length < PARAMETERS) throw new IllegalArgumentException(INVALID_SYNTAX); // Args//from ww w.j ava2 s . c o m String provider = args[0]; // note that you can check if a provider is present ahead of time checkArgument(contains(allKeys, provider), "provider %s not in supported list: %s", provider, allKeys); String identity = args[1]; String credential = args[2]; String containerName = args[3]; // Init BlobStoreContext context = ContextBuilder.newBuilder(provider).credentials(identity, credential) .buildView(BlobStoreContext.class); try { ApiMetadata apiMetadata = context.unwrap().getProviderMetadata().getApiMetadata(); // Create Container BlobStore blobStore = context.getBlobStore(); Location location = null; if (apiMetadata instanceof SwiftApiMetadata) { location = Iterables.getFirst(blobStore.listAssignableLocations(), null); } blobStore.createContainerInLocation(location, containerName); String blobName = "test"; ByteSource payload = ByteSource.wrap("testdata".getBytes(Charsets.UTF_8)); // List Container Metadata for (StorageMetadata resourceMd : blobStore.list()) { if (containerName.equals(resourceMd.getName())) { System.out.println(resourceMd); } } // Add Blob Blob blob = blobStore.blobBuilder(blobName).payload(payload).contentLength(payload.size()).build(); blobStore.putBlob(containerName, blob); // Use Provider API Object object = null; if (apiMetadata instanceof S3ApiMetadata) { S3Client api = context.unwrapApi(S3Client.class); object = api.headObject(containerName, blobName); } else if (apiMetadata instanceof SwiftApiMetadata) { SwiftApi api = context.unwrapApi(SwiftApi.class); object = api.getObjectApi(location.getId(), containerName).getWithoutBody(blobName); } else if (apiMetadata instanceof AzureBlobApiMetadata) { AzureBlobClient api = context.unwrapApi(AzureBlobClient.class); object = api.getBlobProperties(containerName, blobName); } else if (apiMetadata instanceof AtmosApiMetadata) { AtmosClient api = context.unwrapApi(AtmosClient.class); object = api.headFile(containerName + "/" + blobName); } else if (apiMetadata instanceof GoogleCloudStorageApiMetadata) { GoogleCloudStorageApi api = context.unwrapApi(GoogleCloudStorageApi.class); object = api.getObjectApi().getObject(containerName, blobName); } if (object != null) { System.out.println(object); } } finally { // Close connecton context.close(); } }
From source file:org.jclouds.examples.glacier.MainApp.java
private static void multipartUploadExample(BlobStore blobstore) throws IOException { // Create a container String containerName = "jclouds_multipartUploadExample_" + UUID.randomUUID().toString(); blobstore.createContainerInLocation(null, containerName); // Create a vault // Create a blob ByteSource payload = buildData(16 * MiB); Blob blob = blobstore.blobBuilder("ignored") // The blob name is ignored in Glacier .payload(payload).contentLength(payload.size()).build(); // Create the PutOptions PutOptions options = PutOptions.Builder.multipart(true); // Put the blob in the container blobstore.putBlob(containerName, blob, options); System.out.println("The blob has been uploaded"); }
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 {/* w w w .j av a 2 s.co m*/ String data = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8)); System.out.println("The retrieved payload is: " + data); } finally { is.close(); } }
From source file:org.jclouds.examples.glacier.MainApp.java
private static void interruptionExample(final BlobStore blobstore) throws IOException { // Create a container final String containerName = "jclouds_interruptionExample_" + 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 final String blobId = blobstore.putBlob(containerName, blob); // New thread Thread thread = new Thread() { public void run() { try { blobstore.getBlob(containerName, blobId); } catch (RuntimeException e) { System.out.println("The request was aborted"); }/* w w w.ja va 2 s .c om*/ } }; // Start and interrupt the thread thread.start(); thread.interrupt(); try { thread.join(); } catch (InterruptedException e) { Throwables.propagate(e); } }
From source file:org.springframework.xd.integration.util.StreamUtils.java
/** * Copy a file from test machine to remote machine. * * @param privateKey The ssh private key for the remote container * @param host The remote machine's ip.// w w w . j a va 2 s . c o m * @param uri to the location where the file will be copied to remote machine. * @param file The file to migrate to remote machine. */ public static void copyFileToRemote(String privateKey, String host, URI uri, File file, long waitTime) { Assert.hasText(privateKey, "privateKey must not be empty nor null."); Assert.hasText(host, "The remote machine's URL must be specified."); Assert.notNull(uri, "uri should not be null"); Assert.notNull(file, "file must not be null"); boolean isFileCopied = false; long timeout = System.currentTimeMillis() + waitTime; while (!isFileCopied && System.currentTimeMillis() < timeout) { final SshjSshClient client = getSSHClient(host, privateKey); Assert.isTrue(file.exists(), "File to be copied to remote machine does not exist"); ByteSource byteSource = com.google.common.io.Files.asByteSource(file); ByteSourcePayload payload = new ByteSourcePayload(byteSource); long byteSourceSize = -1; try { byteSourceSize = byteSource.size(); payload.getContentMetadata().setContentLength(byteSourceSize); } catch (IOException ioe) { throw new IllegalStateException("Unable to retrieve size for file to be copied to remote machine.", ioe); } client.put(uri.getPath(), payload); if (client.exec("ls -al " + uri.getPath()).getExitStatus() == 0) { ExecResponse statResponse = client.exec("stat --format=%s " + uri.getPath()); long copySize = Long.valueOf(statResponse.getOutput().trim()); if (copySize == byteSourceSize) { isFileCopied = true; } } } }
From source file:de.l3s.concatgz.io.FileBackedBytesWritable.java
@Override public void write(DataOutput dataOutput) throws IOException { if (stream == null) return;//from ww w . j av a2 s . c o m ByteSource bytes = stream.asByteSource(); long size = bytes.size(); dataOutput.writeLong(size); byte[] buffer = new byte[BUFFER_SIZE]; InputStream stream = bytes.openBufferedStream(); int read = stream.read(buffer); while (read != -1) { if (read > 0) dataOutput.write(buffer, 0, read); read = stream.read(buffer); } stream.close(); }
From source file:de.l3s.concatgz.util.CacheInputStream.java
public InputStream getCacheStream() throws IOException { ByteSource cachedBytes = cache.asByteSource(); InputStream cachedStream = cachedBytes.slice(0, cachedBytes.size() - bufferLeft).openBufferedStream(); return new SequenceInputStream(cachedStream, this); }
From source file:at.ac.univie.isc.asio.atos.FakeAtosService.java
private void send(final HttpExchange exchange, final ByteSource content) throws IOException { exchange.getResponseHeaders().set(HttpHeaders.CONTENT_TYPE, "application/xml"); exchange.sendResponseHeaders(HttpStatus.SC_OK, content.size()); content.copyTo(exchange.getResponseBody()); }
From source file:org.apache.airavata.gfac.jclouds.utils.JCloudsFileTransfer.java
public void uploadFileToEc2(String remoteFile, String localFile) throws FileTransferException { try {//from w ww. j a v a 2 s .c om ssh.connect(); File file = new File(localFile); ByteSource byteSource = Files.asByteSource(file); Payload payload = new ByteSourcePayload(byteSource); payload.getContentMetadata().setContentLength(byteSource.size()); ssh.put(remoteFile, payload); } catch (Exception e) { log.error("Error occured while uploading file to ec2"); throw new FileTransferException("Error occured while uploading file " + remoteFile + " to ec2 host"); } finally { if (ssh != null) { ssh.disconnect(); } } }