Package com.google.common.io

Examples of com.google.common.io.ByteSource


      // write files
      storageStrategy.putBlob(CONTAINER_NAME, blob);

      // verify that the files is equal
      File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey);
      ByteSource expectedInput = Files.asByteSource(sourceFile);
      ByteSource actualInput = Files.asByteSource(blobFullPath);
      assertTrue(expectedInput.contentEquals(actualInput),
            "Files are not equal");
   }
View Full Code Here


      }
   }

   @Test(groups = { "integration", "live" })
   public void testFileGetParallel() throws Exception {
      final ByteSource supplier = createTestInput(32 * 1024);
      final String expectedContentDisposition = "attachment; filename=constit.txt";
      final String container = getContainerName();
      try {
         final String name = "constitution.txt";

         uploadByteSource(container, name, expectedContentDisposition, supplier);
         Map<Integer, ListenableFuture<?>> responses = Maps.newHashMap();
         for (int i = 0; i < 10; i++) {

            responses.put(i, Futures.transform(view.getAsyncBlobStore().getBlob(container, name),
                     new Function<Blob, Void>() {

                        @Override
                        public Void apply(Blob from) {
                           try {
                              validateMetadata(from.getMetadata(), container, name);
                              assertEquals(ByteStreams2.hashAndClose(from.getPayload().openStream(), md5()), supplier.hash(md5()));
                              checkContentDisposition(from, expectedContentDisposition);
                           } catch (IOException e) {
                              Throwables.propagate(e);
                           }
                           return null;
View Full Code Here

      @Override
      public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
         try {
            MockResponse response = new MockResponse();
            String expectedMd5 = request.getHeader("Content-MD5");
            ByteSource body = ByteSource.wrap(request.getBody());
            String realMd5FromRequest = base64().encode(body.hash(md5()).asBytes());
            boolean matched = expectedMd5.equals(realMd5FromRequest);
            if (matched) {
               response.addHeader("x-Content-MD5", realMd5FromRequest);
            } else {
               response.setResponseCode(500);
View Full Code Here

      try {
         f = File.createTempFile("jclouds", "tmp");
         long length = (new Random().nextInt(32) + 1) * 1024 * 1024;
         TestUtils.randomByteSource().slice(0, length).copyTo(Files.asByteSink(f));

         ByteSource byteSource = asByteSource(f);
         payload = newByteSourcePayload(byteSource);
         byte[] digest = byteSource.hash(md5()).asBytes();
         String strDigest = base64().encode(digest);

         payload.getContentMetadata().setContentMD5(digest);
         payload.getContentMetadata().setContentLength(f.length());
         Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
View Full Code Here

      MockWebServer server = mockWebServer(new MockResponse().addHeader("x-Content-Disposition",
            "attachment; filename=photo.jpg"));
      IntegrationTestClient client = client(server.getUrl("/").toString());
      Payload payload = null;
      try {
         ByteSource body = ByteSource.wrap("foo".getBytes());
         payload = newByteSourcePayload(body);
         payload.getContentMetadata().setContentDisposition("attachment; filename=photo.jpg");
         payload.getContentMetadata().setContentLength(body.size());
         Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
         RecordedRequest request = server.takeRequest();
         assertEquals(request.getHeader("Content-Disposition"), "attachment; filename=photo.jpg");
         assertEquals(headers.get("x-Content-Disposition"), ImmutableList.of("attachment; filename=photo.jpg"));
      } finally {
View Full Code Here

   public void testPostContentEncoding() throws Exception {
      MockWebServer server = mockWebServer(new MockResponse().addHeader("x-Content-Encoding", "gzip"));
      IntegrationTestClient client = client(server.getUrl("/").toString());
      Payload payload = null;
      try {
         ByteSource body = ByteSource.wrap("foo".getBytes());
         payload = newByteSourcePayload(body);
         payload.getContentMetadata().setContentEncoding("gzip");
         payload.getContentMetadata().setContentLength(body.size());
         Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
         RecordedRequest request = server.takeRequest();
         assertEquals(request.getHeader("Content-Encoding"), "gzip");
         assertEquals(headers.get("x-Content-Encoding"), ImmutableList.of("gzip"));
      } finally {
View Full Code Here

   public void testPostContentLanguage() throws Exception {
      MockWebServer server = mockWebServer(new MockResponse().addHeader("x-Content-Language", "mi, en"));
      IntegrationTestClient client = client(server.getUrl("/").toString());
      Payload payload = null;
      try {
         ByteSource body = ByteSource.wrap("foo".getBytes());
         payload = newByteSourcePayload(body);
         payload.getContentMetadata().setContentLanguage("mi, en");
         payload.getContentMetadata().setContentLength(body.size());
         Multimap<String, String> headers = client.postPayloadAndReturnHeaders("", payload);
         RecordedRequest request = server.takeRequest();
         assertEquals(request.getHeader("Content-Language"), "mi, en");
         assertEquals(headers.get("x-Content-Language"), ImmutableList.of("mi, en"));
      } finally {
View Full Code Here

   * @param charset the charset to interpret the input as.
   * @return String contents of the stream.
   * @throws IOException if there is an problem reading from the stream.
   */
  public static String readAll(final InputStream inputStream, Charset charset) throws IOException {
    return new ByteSource() {
      public InputStream openStream() {
        return inputStream;
      }
    }.asCharSource(charset).read();
  }
View Full Code Here

   * Copies the {@code inputStream} into the {@code outputSteam} and finally
   * closes the both streams.
   */
  public static void copy(final InputStream inputStream, final OutputStream outputStream)
      throws IOException {
    new ByteSource() {
      public InputStream openStream() {
        return inputStream;
      }
    }.copyTo(new ByteSink() {
      public OutputStream openStream() {
View Full Code Here

   * @param inputStream the {@code InputStream} to get the byte array from
   * @return a byte array containing all data from the input stream
   * @throws IOException if the stream cannot be read
   */
  private static byte[] getByteArrayFromStream(final InputStream inputStream) throws IOException {
    return new ByteSource() {
      public InputStream openStream() {
        return inputStream;
      }
    }.read();
  }
View Full Code Here

TOP

Related Classes of com.google.common.io.ByteSource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.