Prefer ByteSource over deprecated InputSupplier

This commit is contained in:
Andrew Gaul 2013-12-01 22:15:36 -08:00
parent 076e31b076
commit 266d7f847b
9 changed files with 58 additions and 106 deletions

View File

@ -26,7 +26,6 @@ import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
@ -60,9 +59,9 @@ import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.google.common.collect.Sets;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import com.google.inject.CreationException;
/**
@ -585,9 +584,7 @@ public class FilesystemAsyncBlobStoreTest {
assertNotNull(resultBlob, "Blob exists");
// checks file content
InputSupplier<FileInputStream> expectedFile =
Files.newInputStreamSupplier(new File(
TARGET_CONTAINER_NAME, blobKey));
ByteSource expectedFile = Files.asByteSource(new File(TARGET_CONTAINER_NAME, blobKey));
assertTrue(ByteStreams.equal(expectedFile, resultBlob.getPayload()),
"Blob payload differs from file content");
// metadata are verified in the test for blobMetadata, so no need to

View File

@ -47,9 +47,9 @@ import org.testng.annotations.Test;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
/**
* Test class for {@link FilesystemStorageStrategyImpl } class
@ -354,10 +354,8 @@ public class FilesystemStorageStrategyImplTest {
// verify that the files is equal
File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey);
InputSupplier<FileInputStream> expectedInput =
Files.newInputStreamSupplier(sourceFile);
InputSupplier<FileInputStream> actualInput =
Files.newInputStreamSupplier(blobFullPath);
ByteSource expectedInput = Files.asByteSource(sourceFile);
ByteSource actualInput = Files.asByteSource(blobFullPath);
assertTrue(ByteStreams.equal(expectedInput, actualInput),
"Files are not equal");
}
@ -375,10 +373,8 @@ public class FilesystemStorageStrategyImplTest {
// verify that the files is equal
File blobFullPath = new File(TARGET_CONTAINER_NAME, blobKey);
InputSupplier<FileInputStream> expectedInput =
Files.newInputStreamSupplier(sourceFile);
InputSupplier<FileInputStream> actualInput =
Files.newInputStreamSupplier(blobFullPath);
ByteSource expectedInput = Files.asByteSource(sourceFile);
ByteSource actualInput = Files.asByteSource(blobFullPath);
assertTrue(ByteStreams.equal(expectedInput, actualInput),
"Files are not equal");
}

View File

@ -40,9 +40,8 @@ import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.google.common.hash.Hashing;
import com.google.common.io.ByteStreams;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
/**
*
@ -65,7 +64,7 @@ public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
return props;
}
private InputSupplier<InputStream> oneHundredOneConstitutions;
private ByteSource oneHundredOneConstitutions;
public SwiftBlobIntegrationLiveTest() {
provider = System.getProperty("test.swift.provider", "swift");
@ -178,10 +177,10 @@ public class SwiftBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
private File createFileBiggerThan(long partSize) throws IOException {
long copiesNeeded = (partSize / getOneHundredOneConstitutionsLength()) + 1;
InputSupplier<InputStream> temp = ByteStreams.join(oneHundredOneConstitutions);
ByteSource temp = ByteSource.concat(oneHundredOneConstitutions);
for (int i = 0; i < copiesNeeded; i++) {
temp = ByteStreams.join(temp, oneHundredOneConstitutions);
temp = ByteSource.concat(temp, oneHundredOneConstitutions);
}
File fileToUpload = new File("target/lots-of-const.txt");

View File

@ -29,7 +29,6 @@ import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@ -80,6 +79,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.hash.HashCode;
import com.google.common.io.ByteSource;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
@ -91,7 +91,7 @@ import com.google.common.util.concurrent.Uninterruptibles;
* @author Adrian Cole
*/
public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
private InputSupplier<InputStream> oneHundredOneConstitutions;
private ByteSource oneHundredOneConstitutions;
private byte[] oneHundredOneConstitutionsMD5;
private static long oneHundredOneConstitutionsLength;
@ -100,23 +100,23 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
public void setUpResourcesOnThisThread(ITestContext testContext) throws Exception {
super.setUpResourcesOnThisThread(testContext);
oneHundredOneConstitutions = getTestDataSupplier();
oneHundredOneConstitutionsMD5 = md5Supplier(oneHundredOneConstitutions);
oneHundredOneConstitutionsMD5 = oneHundredOneConstitutions.hash(md5()).asBytes();
}
protected static byte[] md5Supplier(InputSupplier<? extends InputStream> supplier) throws IOException {
private static byte[] md5Supplier(InputSupplier<? extends InputStream> supplier) throws IOException {
return ByteStreams.hash(supplier, md5()).asBytes();
}
@SuppressWarnings("unchecked")
public static InputSupplier<InputStream> getTestDataSupplier() throws IOException {
public static ByteSource getTestDataSupplier() throws IOException {
byte[] oneConstitution = ByteStreams.toByteArray(new GZIPInputStream(BaseJettyTest.class
.getResourceAsStream("/const.txt.gz")));
InputSupplier<ByteArrayInputStream> constitutionSupplier = ByteStreams.newInputStreamSupplier(oneConstitution);
ByteSource constitutionSupplier = ByteSource.wrap(oneConstitution);
InputSupplier<InputStream> temp = ByteStreams.join(constitutionSupplier);
ByteSource temp = ByteSource.concat(constitutionSupplier);
for (int i = 0; i < 100; i++) {
temp = ByteStreams.join(temp, constitutionSupplier);
temp = ByteSource.concat(temp, constitutionSupplier);
}
oneHundredOneConstitutionsLength = oneConstitution.length * 101l;
return temp;
@ -181,7 +181,7 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
@Test(groups = { "integration", "live" })
public void testFileGetParallel() throws Exception {
final InputSupplier<? extends InputStream> supplier = createTestInput(32 * 1024);
final ByteSource supplier = createTestInput(32 * 1024);
final String expectedContentDisposition = "attachment; filename=constit.txt";
final String container = getContainerName();
try {
@ -198,7 +198,7 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
public Void apply(Blob from) {
try {
validateMetadata(from.getMetadata(), container, name);
assertEquals(md5Supplier(from.getPayload()), md5Supplier(supplier));
assertEquals(md5Supplier(from.getPayload()), supplier.hash(md5()).asBytes());
checkContentDisposition(from, expectedContentDisposition);
} catch (IOException e) {
Throwables.propagate(e);
@ -221,12 +221,12 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
}
private void uploadInputSupplier(String container, String name, String contentDisposition,
InputSupplier<? extends InputStream> supplier) throws IOException {
ByteSource supplier) throws IOException {
BlobStore blobStore = view.getBlobStore();
blobStore.putBlob(container, blobStore.blobBuilder(name)
.payload(new InputStreamSupplierPayload(supplier))
.contentType("text/plain")
.contentMD5(md5Supplier(supplier))
.contentMD5(supplier.hash(md5()).asBytes())
.contentLength(ByteStreams.length(supplier))
.contentDisposition(contentDisposition)
.build());
@ -667,12 +667,12 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
assertEquals(metadata.getContentMetadata().getContentMD5(), md5().hashString(TEST_STRING, UTF_8).asBytes());
}
/** @return InputSupplier containing a random length 0..length of random bytes. */
/** @return ByteSource containing a random length 0..length of random bytes. */
@SuppressWarnings("unchecked")
private static InputSupplier<? extends InputStream> createTestInput(int length) {
private static ByteSource createTestInput(int length) {
Random random = new Random();
byte[] buffer = new byte[random.nextInt(length)];
random.nextBytes(buffer);
return ByteStreams.newInputStreamSupplier(buffer);
return ByteSource.wrap(buffer);
}
}

View File

@ -33,8 +33,9 @@ import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.Map;
import com.google.common.io.Resources;
import org.jclouds.crypto.Pems;
import org.jclouds.io.Payloads;
import org.jclouds.util.Strings2;
import org.testng.annotations.Test;
@ -118,7 +119,7 @@ public class SshKeysTest {
@Test
public void testEncodeAsOpenSSH() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
String encoded = SshKeys.encodeAsOpenSSH((RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(
SshKeys.publicKeySpecFromOpenSSH(Payloads.newPayload(getClass().getResourceAsStream("/test.pub")))));
SshKeys.publicKeySpecFromOpenSSH(Resources.asByteSource(Resources.getResource(getClass(), "/test.pub")))));
assertEquals(encoded, Strings2.toStringAndClose(getClass().getResourceAsStream("/test.pub")).trim());
}

View File

@ -18,8 +18,6 @@ package org.jclouds.http;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.join;
import static com.google.common.io.ByteStreams.newInputStreamSupplier;
import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.io.Closeables.close;
import static com.google.common.io.Files.asByteSource;
@ -31,7 +29,6 @@ import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@ -55,7 +52,6 @@ import com.google.common.collect.Multimap;
import com.google.common.io.ByteSource;
import com.google.common.io.CharSink;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import com.squareup.okhttp.mockwebserver.Dispatcher;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
@ -77,12 +73,12 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
private String constitutionsMd5;
private long constitutionsLength;
private InputSupplier<InputStream> oneHundredOneConstitutions;
private ByteSource oneHundredOneConstitutions;
@BeforeClass(groups = "integration")
public void setup() throws IOException {
oneHundredOneConstitutions = getTestDataSupplier();
constitutionsMd5 = base64().encode(asByteSource(oneHundredOneConstitutions.getInput()).hash(md5()).asBytes());
constitutionsMd5 = base64().encode(oneHundredOneConstitutions.hash(md5()).asBytes());
}
protected IntegrationTestClient client(String url) {
@ -647,13 +643,13 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
}
@SuppressWarnings("unchecked")
private InputSupplier<InputStream> getTestDataSupplier() throws IOException {
private ByteSource getTestDataSupplier() throws IOException {
byte[] oneConstitution = toByteArray(new GZIPInputStream(
BaseHttpCommandExecutorServiceIntegrationTest.class.getResourceAsStream("/const.txt.gz")));
InputSupplier<ByteArrayInputStream> constitutionSupplier = newInputStreamSupplier(oneConstitution);
InputSupplier<InputStream> temp = join(constitutionSupplier);
ByteSource constitutionSupplier = ByteSource.wrap(oneConstitution);
ByteSource temp = ByteSource.concat(constitutionSupplier);
for (int i = 0; i < 100; i++) {
temp = join(temp, constitutionSupplier);
temp = ByteSource.concat(temp, constitutionSupplier);
}
constitutionsLength = oneConstitution.length * 101;
return temp;

View File

@ -20,8 +20,6 @@ import static com.google.common.base.Throwables.getStackTraceAsString;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.BaseEncoding.base64;
import static com.google.common.io.ByteStreams.copy;
import static com.google.common.io.ByteStreams.join;
import static com.google.common.io.ByteStreams.newInputStreamSupplier;
import static com.google.common.io.ByteStreams.toByteArray;
import static com.google.common.net.HttpHeaders.CONTENT_DISPOSITION;
import static com.google.common.net.HttpHeaders.CONTENT_ENCODING;
@ -35,7 +33,6 @@ import static org.jclouds.io.ByteSources.asByteSource;
import static org.jclouds.util.Closeables2.closeQuietly;
import static org.jclouds.util.Strings2.toStringAndClose;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
@ -70,8 +67,7 @@ import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.io.ByteStreams;
import com.google.common.io.InputSupplier;
import com.google.common.io.ByteSource;
import com.google.inject.Injector;
import com.google.inject.Module;
@ -94,8 +90,8 @@ public abstract class BaseJettyTest {
public void setUpJetty(@Optional("8123") final int testPort) throws Exception {
this.testPort = testPort;
final InputSupplier<InputStream> oneHundredOneConstitutions = getTestDataSupplier();
md5 = base64().encode(ByteStreams.hash(oneHundredOneConstitutions, md5()).asBytes());
final ByteSource oneHundredOneConstitutions = getTestDataSupplier();
md5 = base64().encode(oneHundredOneConstitutions.hash(md5()).asBytes());
Handler server1Handler = new AbstractHandler() {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
@ -251,14 +247,14 @@ public abstract class BaseJettyTest {
}
@SuppressWarnings("unchecked")
public static InputSupplier<InputStream> getTestDataSupplier() throws IOException {
public static ByteSource getTestDataSupplier() throws IOException {
byte[] oneConstitution = toByteArray(new GZIPInputStream(BaseJettyTest.class.getResourceAsStream("/const.txt.gz")));
InputSupplier<ByteArrayInputStream> constitutionSupplier = newInputStreamSupplier(oneConstitution);
ByteSource constitutionSupplier = ByteSource.wrap(oneConstitution);
InputSupplier<InputStream> temp = join(constitutionSupplier);
ByteSource temp = ByteSource.concat(constitutionSupplier);
for (int i = 0; i < 100; i++) {
temp = join(temp, constitutionSupplier);
temp = ByteSource.concat(temp, constitutionSupplier);
}
return temp;
}

View File

@ -17,8 +17,6 @@
package org.jclouds.aws.s3;
import static com.google.common.hash.Hashing.md5;
import static com.google.common.io.ByteStreams.join;
import static com.google.common.io.ByteStreams.newInputStreamSupplier;
import static com.google.common.io.ByteStreams.toByteArray;
import static org.jclouds.aws.s3.blobstore.options.AWSS3PutOptions.Builder.storageClass;
import static org.jclouds.io.Payloads.newByteArrayPayload;
@ -28,10 +26,8 @@ import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import java.util.UUID;
import java.util.zip.GZIPInputStream;
@ -61,9 +57,8 @@ import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.ByteStreams;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
/**
* Tests behavior of {@code S3Client}
@ -75,7 +70,7 @@ public class AWSS3ClientLiveTest extends S3ClientLiveTest {
public AWSS3ClientLiveTest() {
provider = "aws-s3";
}
private InputSupplier<InputStream> oneHundredOneConstitutions;
private ByteSource oneHundredOneConstitutions;
private byte[] oneHundredOneConstitutionsMD5;
private static long oneHundredOneConstitutionsLength;
@ -89,18 +84,18 @@ public class AWSS3ClientLiveTest extends S3ClientLiveTest {
public void setUpResourcesOnThisThread(ITestContext testContext) throws Exception {
super.setUpResourcesOnThisThread(testContext);
oneHundredOneConstitutions = getTestDataSupplier();
oneHundredOneConstitutionsMD5 = ByteStreams.hash(oneHundredOneConstitutions, md5()).asBytes();
oneHundredOneConstitutionsMD5 = oneHundredOneConstitutions.hash(md5()).asBytes();
}
@SuppressWarnings("unchecked")
public static InputSupplier<InputStream> getTestDataSupplier() throws IOException {
public static ByteSource getTestDataSupplier() throws IOException {
byte[] oneConstitution = toByteArray(new GZIPInputStream(BaseJettyTest.class.getResourceAsStream("/const.txt.gz")));
InputSupplier<ByteArrayInputStream> constitutionSupplier = newInputStreamSupplier(oneConstitution);
ByteSource constitutionSupplier = ByteSource.wrap(oneConstitution);
InputSupplier<InputStream> temp = join(constitutionSupplier);
ByteSource temp = ByteSource.concat(constitutionSupplier);
// we have to go beyond 5MB per part
for (oneHundredOneConstitutionsLength = oneConstitution.length; oneHundredOneConstitutionsLength < 5 * 1024 * 1024; oneHundredOneConstitutionsLength += oneConstitution.length) {
temp = join(temp, constitutionSupplier);
temp = ByteSource.concat(temp, constitutionSupplier);
}
return temp;
}
@ -112,7 +107,7 @@ public class AWSS3ClientLiveTest extends S3ClientLiveTest {
String key = "constitution.txt";
String uploadId = getApi().initiateMultipartUpload(containerName,
ObjectMetadataBuilder.create().key(key).contentMD5(oneHundredOneConstitutionsMD5).build());
byte[] buffer = toByteArray(oneHundredOneConstitutions);
byte[] buffer = oneHundredOneConstitutions.read();
assertEquals(oneHundredOneConstitutionsLength, (long) buffer.length);
Payload part1 = newByteArrayPayload(buffer);

View File

@ -18,13 +18,11 @@ package org.jclouds.azureblob.blobstore.integration;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import com.google.common.io.ByteStreams;
import com.google.common.collect.Iterables;
import com.google.common.io.ByteSource;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
import org.jclouds.azureblob.blobstore.strategy.MultipartUploadStrategy;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.domain.Blob;
@ -42,7 +40,7 @@ import static com.google.common.hash.Hashing.md5;
*/
@Test(groups = "live")
public class AzureBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
private InputSupplier<InputStream> oneHundredOneConstitutions;
private ByteSource oneHundredOneConstitutions;
private byte[] oneHundredOneConstitutionsMD5;
public AzureBlobIntegrationLiveTest() {
@ -81,7 +79,7 @@ public class AzureBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
*/
public void testMultipartChunkedFileStream() throws IOException, InterruptedException {
oneHundredOneConstitutions = getTestDataSupplier();
oneHundredOneConstitutionsMD5 = ByteStreams.hash(oneHundredOneConstitutions, md5()).asBytes();
oneHundredOneConstitutionsMD5 = oneHundredOneConstitutions.hash(md5()).asBytes();
File file = new File("target/const.txt");
Files.copy(oneHundredOneConstitutions, file);
String containerName = getContainerName();
@ -100,12 +98,7 @@ public class AzureBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
public void testMultipartChunkedFileStreamPowerOfTwoSize() throws IOException, InterruptedException {
final long limit = MultipartUploadStrategy.MAX_BLOCK_SIZE;
InputSupplier<InputStream> input = new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return ByteStreams.limit(ZERO_INPUT_STREAM, limit);
}
};
ByteSource input = repeatingArrayByteSource(new byte[1024]).slice(0, limit);
File file = new File("target/const.txt");
Files.copy(input, file);
String containerName = getContainerName();
@ -122,28 +115,7 @@ public class AzureBlobIntegrationLiveTest extends BaseBlobIntegrationTest {
}
}
/** An infinite-length zero byte InputStream. */
// Guava feature request:
// https://code.google.com/p/guava-libraries/issues/detail?id=1370
private static final InputStream ZERO_INPUT_STREAM = new InputStream() {
@Override
public int read() {
return 0;
}
@Override
public int read(final byte[] b) {
return read(b, 0, b.length);
}
@Override
public int read(final byte[] b, final int off, final int len) {
if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
}
int length = Math.min(len, b.length - off);
Arrays.fill(b, off, length, (byte) 0);
return length;
}
};
private static ByteSource repeatingArrayByteSource(final byte[] input) {
return ByteSource.concat(Iterables.cycle(ByteSource.wrap(input)));
}
}