Package com.google.common.io

Examples of com.google.common.io.ByteSource


            throw new IOException("Could not create: " + TARGET_RESOURCE_DIR);
        }
        Random random = new Random();
        for (File file : IMAGE_RESOURCES) {
            int length = random.nextInt(2 * 1024 * 1024);
            ByteSource byteSource = randomByteSource().slice(0, length);
            byteSource.copyTo(Files.asByteSink(file));
        }
    }
View Full Code Here


   public void testCredentialsToByteSourceConversion() throws Exception {
      Function<Credentials, ByteSource> toBytesFunc = getCredentialsToByteStoreFunction(createInjector());
      Function<ByteSource, Credentials> fromBytesFunc = getByteStoreToCredentialsFunction(createInjector());
     
      LoginCredentials creds = LoginCredentials.builder().user("myuser").password("mypass").authenticateSudo(true).build();
      ByteSource bytes = toBytesFunc.apply(creds);
      LoginCredentials deserializedCreds = (LoginCredentials) fromBytesFunc.apply(bytes);
     
      String json = bytes.asCharSource(Charsets.UTF_8).read();
      assertEquals(json, "{\"user\":\"myuser\",\"password\":\"mypass\",\"authenticateSudo\":true}");
     
      assertEquals(deserializedCreds.identity, creds.identity);
      assertEquals(deserializedCreds.credential, creds.credential);
      assertEquals(deserializedCreds.getUser(), creds.getUser());
View Full Code Here

         BlobStore blobStore = view.getBlobStore();
         String objectName = "object.txt";
         long countBefore = blobStore.countBlobs(containerName);

         // we want 2 parts
         ByteSource inputSource = createByteSource(PART_SIZE + 1);
         addMultipartBlobToContainer(containerName, objectName, inputSource);

         // did we create enough parts?
         long countAfter = blobStore.countBlobs(containerName);
         assertNotEquals(countAfter, countBefore, "No blob was created");
         assertEquals(countAfter, countBefore + 3,
                 "3 parts (2 objects + 1 manifest) were expected.");

         // download and check if correct
         Blob read = blobStore.getBlob(containerName, objectName);
         Payload readPayload = read.getPayload();
         assertTrue(Arrays.equals(inputSource.read(), ByteStreams2.toByteArrayAndClose(readPayload.openStream())));
      } finally {
         returnContainer(containerName);
      }
   }
View Full Code Here

      try {
         BlobStore blobStore = view.getBlobStore();

         blobStore.createContainerInLocation(null, container);

         ByteSource input = createByteSourceBiggerThan(PART_SIZE);

         Blob write = blobStore.blobBuilder("const.txt")
             .payload(input.openStream())
             .contentLength(input.size())
             .build();
         blobStore.putBlob(container, write, PutOptions.Builder.multipart());

         Blob read = blobStore.getBlob(container, "const.txt");
         InputStream is = read.getPayload().openStream();
         assertEquals(ByteStreams2.hashAndClose(is, Hashing.md5()), input.hash(Hashing.md5()));
      } finally {
         returnContainer(container);
      }
   }
View Full Code Here

   protected int getIncorrectContentMD5StatusCode() {
      return 422;
   }

   protected void addMultipartBlobToContainer(String containerName, String key) throws IOException {
      ByteSource byteSource = createByteSourceBiggerThan(PART_SIZE);
      addMultipartBlobToContainer(containerName, key, byteSource);
   }
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

      assertPayloadEquals(request, "whoops", "application/unknown", false);
   }

   public void testPutPayloadEnclosingGenerateMD5() throws SecurityException, NoSuchMethodException, IOException {
      Invokable<?, ?> method = method(TestTransformers.class, "put", PayloadEnclosing.class);
      ByteSource byteSource = ByteSource.wrap("whoops".getBytes(UTF_8));
      PayloadEnclosing payloadEnclosing = new PayloadEnclosingImpl(Payloads.newByteSourcePayload(byteSource));
      payloadEnclosing.getPayload().getContentMetadata().setContentLength(byteSource.size());
      payloadEnclosing.getPayload().getContentMetadata().setContentMD5(byteSource.hash(Hashing.md5()).asBytes());

      GeneratedHttpRequest request = processor.apply(Invocation.create(method,
            ImmutableList.<Object> of(payloadEnclosing)));
      assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1");
      assertNonPayloadHeadersEqual(request, "");
View Full Code Here

   }

   public void testPutInputStreamPayloadEnclosingGenerateMD5() throws SecurityException, NoSuchMethodException,
         IOException {
      Invokable<?, ?> method = method(TestTransformers.class, "put", PayloadEnclosing.class);
      ByteSource byteSource = ByteSource.wrap("whoops".getBytes(UTF_8));
      PayloadEnclosing payloadEnclosing = new PayloadEnclosingImpl(
            newInputStreamPayload(byteSource.openStream()));
      payloadEnclosing.getPayload().getContentMetadata().setContentLength(byteSource.size());
      payloadEnclosing.getPayload().getContentMetadata().setContentMD5(byteSource.hash(Hashing.md5()).asBytes());

      GeneratedHttpRequest request = processor.apply(Invocation.create(method,
            ImmutableList.<Object> of(payloadEnclosing)));
      assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1");
      assertNonPayloadHeadersEqual(request, "");
View Full Code Here

   @Test
   public void testIterableSliceWithRepeatingByteSource() throws IOException {
      String content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n"; /* 53 chars */
      byte[] contentBytes = content.getBytes(Charsets.UTF_8);
      ByteSource byteSource = ByteSources.repeatingArrayByteSource(contentBytes).slice(0, 1024);
      PayloadSlicer slicer = new BasePayloadSlicer();
      Payload payload = new ByteSourcePayload(byteSource);

      assertEquals(Iterables.size(slicer.slice(payload, 100)), 11);
      assertEquals(Iterables.size(slicer.slice(payload, 53)), 20);
View Full Code Here

      assertPayloadEquals(request, "whoops", "application/unknown", null, null, "en", false);
   }

   public void testPutPayloadWithGeneratedMD5AndNoContentType() throws SecurityException, NoSuchMethodException,
         IOException {
      ByteSource byteSource = ByteSource.wrap("whoops".getBytes(UTF_8));
      Payload payload = Payloads.newByteSourcePayload(byteSource);
      payload.getContentMetadata().setContentLength(byteSource.size());
      payload.getContentMetadata().setContentMD5(byteSource.hash(Hashing.md5()).asBytes());

      Invokable<?, ?> method = method(TestTransformers.class, "put", Payload.class);
      GeneratedHttpRequest request = processor.apply(Invocation.create(method,
            ImmutableList.<Object> of(payload)));
      assertRequestLineEquals(request, "PUT http://localhost:9999 HTTP/1.1");
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.