Prefer Guava InputSupplier methods over IOUtils

This commit is contained in:
Andrew Gaul 2012-09-25 11:44:10 -07:00
parent 4990934470
commit 44a8ed155c
1 changed files with 15 additions and 13 deletions

View File

@ -37,7 +37,6 @@ import java.util.Properties;
import java.util.Set; import java.util.Set;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.jclouds.ContextBuilder; import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.BlobRequestSigner; import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.BlobStore; import org.jclouds.blobstore.BlobStore;
@ -56,13 +55,16 @@ import org.jclouds.filesystem.reference.FilesystemConstants;
import org.jclouds.filesystem.utils.TestUtils; import org.jclouds.filesystem.utils.TestUtils;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.io.InputSuppliers; import org.jclouds.io.InputSuppliers;
import org.jclouds.io.Payload;
import org.jclouds.io.payloads.PhantomPayload; import org.jclouds.io.payloads.PhantomPayload;
import org.jclouds.io.payloads.StringPayload; import org.jclouds.io.payloads.StringPayload;
import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.io.CharStreams;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
import com.google.common.io.Files; import com.google.common.io.Files;
import com.google.common.io.InputSupplier; import com.google.common.io.InputSupplier;
@ -730,39 +732,39 @@ public class FilesystemAsyncBlobStoreTest {
public void testRanges() throws IOException { public void testRanges() throws IOException {
blobStore.createContainerInLocation(null, CONTAINER_NAME); blobStore.createContainerInLocation(null, CONTAINER_NAME);
String payload = "abcdefgh"; String input = "abcdefgh";
InputStream is; Payload payload;
Blob blob = blobStore.blobBuilder("test").payload(new StringPayload(payload)).build(); Blob blob = blobStore.blobBuilder("test").payload(new StringPayload(input)).build();
blobStore.putBlob(CONTAINER_NAME, blob); blobStore.putBlob(CONTAINER_NAME, blob);
GetOptions getOptionsRangeStartAt = new GetOptions(); GetOptions getOptionsRangeStartAt = new GetOptions();
getOptionsRangeStartAt.startAt(1); getOptionsRangeStartAt.startAt(1);
Blob blobRangeStartAt = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsRangeStartAt); Blob blobRangeStartAt = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsRangeStartAt);
is = blobRangeStartAt.getPayload().getInput(); payload = blobRangeStartAt.getPayload();
try { try {
assertEquals("bcdefgh", IOUtils.toString(is)); assertEquals(input.substring(1), CharStreams.toString(CharStreams.newReaderSupplier(payload, Charsets.UTF_8)));
} finally { } finally {
Closeables.closeQuietly(is); Closeables.closeQuietly(payload);
} }
GetOptions getOptionsRangeTail = new GetOptions(); GetOptions getOptionsRangeTail = new GetOptions();
getOptionsRangeTail.tail(3); getOptionsRangeTail.tail(3);
Blob blobRangeTail = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsRangeTail); Blob blobRangeTail = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsRangeTail);
is = blobRangeTail.getPayload().getInput(); payload = blobRangeTail.getPayload();
try { try {
assertEquals("fgh", IOUtils.toString(is)); assertEquals(input.substring(5), CharStreams.toString(CharStreams.newReaderSupplier(payload, Charsets.UTF_8)));
} finally { } finally {
Closeables.closeQuietly(is); Closeables.closeQuietly(payload);
} }
GetOptions getOptionsFragment = new GetOptions(); GetOptions getOptionsFragment = new GetOptions();
getOptionsFragment.range(4, 6); getOptionsFragment.range(4, 6);
Blob blobFragment = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsFragment); Blob blobFragment = blobStore.getBlob(CONTAINER_NAME, blob.getMetadata().getName(), getOptionsFragment);
is = blobFragment.getPayload().getInput(); payload = blobFragment.getPayload();
try { try {
assertEquals("efg", IOUtils.toString(is)); assertEquals(input.substring(4, 7), CharStreams.toString(CharStreams.newReaderSupplier(payload, Charsets.UTF_8)));
} finally { } finally {
Closeables.closeQuietly(is); Closeables.closeQuietly(payload);
} }
} }