Merge pull request #894 from andrewgaul/files-tostring

Prefer Files.toString over FileInputStream
This commit is contained in:
Adrian Cole 2012-10-11 02:05:46 -07:00
commit 9e8b4756c2
6 changed files with 20 additions and 19 deletions

View File

@ -31,7 +31,6 @@ import static org.testng.Assert.assertNull;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -68,12 +67,12 @@ import org.jclouds.io.Payloads;
import org.jclouds.io.WriteTo; import org.jclouds.io.WriteTo;
import org.jclouds.io.payloads.StreamingPayload; import org.jclouds.io.payloads.StreamingPayload;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.util.Strings2;
import org.testng.ITestContext; import org.testng.ITestContext;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
@ -482,10 +481,10 @@ public class BaseBlobIntegrationTest extends BaseBlobStoreIntegrationTest {
@DataProvider(name = "putTests") @DataProvider(name = "putTests")
public Object[][] createData1() throws IOException { public Object[][] createData1() throws IOException {
File file = new File("pom.xml");
String realObject = Files.toString(file, Charsets.UTF_8);
String realObject = Strings2.toStringAndClose(new FileInputStream("pom.xml")); return new Object[][] { { "file", "text/xml", file, realObject },
return new Object[][] { { "file", "text/xml", new File("pom.xml"), realObject },
{ "string", "text/xml", realObject, realObject }, { "string", "text/xml", realObject, realObject },
{ "bytes", "application/octet-stream", realObject.getBytes(), realObject } }; { "bytes", "application/octet-stream", realObject.getBytes(), realObject } };
} }

View File

@ -22,7 +22,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -33,7 +32,6 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import org.jclouds.logging.log4j.config.Log4JLoggingModule; import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.util.Strings2;
import org.testng.annotations.AfterTest; import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Optional; import org.testng.annotations.Optional;
@ -44,6 +42,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.google.common.io.Files;
/** /**
* *
@ -153,8 +152,8 @@ public class GetPathLiveTest {
new File(tmpDirectory).mkdirs(); new File(tmpDirectory).mkdirs();
GetPath.main(uriKey, tmpDirectory); GetPath.main(uriKey, tmpDirectory);
for (Entry<String, String> entry : fiveStrings.entrySet()) { for (Entry<String, String> entry : fiveStrings.entrySet()) {
assertEquals(Strings2.toStringAndClose(new FileInputStream(new File(tmpDirectory, entry assertEquals(Files.toString(new File(tmpDirectory,
.getKey()))), entry.getValue()); entry.getKey())), entry.getValue());
} }
} }

View File

@ -26,7 +26,6 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
@ -49,9 +48,11 @@ import org.jclouds.util.Strings2;
import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.base.Suppliers; import com.google.common.base.Suppliers;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
import com.google.common.io.Files;
import com.google.common.net.HostAndPort; import com.google.common.net.HostAndPort;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
@ -148,7 +149,7 @@ public class JschSshClientLiveTest {
SshClient connection; SshClient connection;
if (Strings.emptyToNull(sshKeyFile) != null) { if (Strings.emptyToNull(sshKeyFile) != null) {
connection = factory.create(HostAndPort.fromParts(sshHost, port), LoginCredentials.builder().user(sshUser) connection = factory.create(HostAndPort.fromParts(sshHost, port), LoginCredentials.builder().user(sshUser)
.privateKey(Strings2.toStringAndClose(new FileInputStream(sshKeyFile))).build()); .privateKey(Files.toString(new File(sshKeyFile), Charsets.UTF_8)).build());
} else { } else {
connection = factory.create(HostAndPort.fromParts(sshHost, port), connection = factory.create(HostAndPort.fromParts(sshHost, port),
LoginCredentials.builder().user(sshUser).password(sshPass).build()); LoginCredentials.builder().user(sshUser).password(sshPass).build());

View File

@ -24,7 +24,6 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
@ -42,9 +41,11 @@ import org.jclouds.util.Strings2;
import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Charsets;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.base.Suppliers; import com.google.common.base.Suppliers;
import com.google.common.io.Closeables; import com.google.common.io.Closeables;
import com.google.common.io.Files;
import com.google.common.net.HostAndPort; import com.google.common.net.HostAndPort;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
@ -136,7 +137,7 @@ public class SshjSshClientLiveTest {
SshClient connection; SshClient connection;
if (Strings.emptyToNull(sshKeyFile) != null) { if (Strings.emptyToNull(sshKeyFile) != null) {
connection = factory.create(HostAndPort.fromParts(sshHost, port), LoginCredentials.builder().user(sshUser) connection = factory.create(HostAndPort.fromParts(sshHost, port), LoginCredentials.builder().user(sshUser)
.privateKey(Strings2.toStringAndClose(new FileInputStream(sshKeyFile))).build()); .privateKey(Files.toString(new File(sshKeyFile), Charsets.UTF_8)).build());
} else { } else {
connection = factory.create(HostAndPort.fromParts(sshHost, port), connection = factory.create(HostAndPort.fromParts(sshHost, port),
LoginCredentials.builder().user(sshUser).password(sshPass).build()); LoginCredentials.builder().user(sshUser).password(sshPass).build());

View File

@ -25,7 +25,6 @@ import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Properties; import java.util.Properties;
@ -42,12 +41,13 @@ import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.nodepool.config.NodePoolProperties; import org.jclouds.nodepool.config.NodePoolProperties;
import org.jclouds.nodepool.internal.NodeMetadataStore; import org.jclouds.nodepool.internal.NodeMetadataStore;
import org.jclouds.util.Strings2;
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.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import com.google.inject.Injector; import com.google.inject.Injector;
/** /**
@ -94,8 +94,8 @@ public class NodeMetadataStoreTest {
public void testStore() throws FileNotFoundException, IOException { public void testStore() throws FileNotFoundException, IOException {
store.store(nodeMeta1, templateOptions, "testgroup"); store.store(nodeMeta1, templateOptions, "testgroup");
store.store(nodeMeta2, templateOptions, "testgroup"); store.store(nodeMeta2, templateOptions, "testgroup");
String readJSon = Strings2.toStringAndClose(new FileInputStream(baseDir + File.separator + "nodes" String readJSon = Files.toString(new File(baseDir + File.separator + "nodes"
+ File.separator + nodeMeta1.getId())); + File.separator + nodeMeta1.getId()), Charsets.UTF_8);
assertEquals(readJSon, "{\"group\":\"testgroup\",\"tags\":[\"tag1\",\"tag2\"]," assertEquals(readJSon, "{\"group\":\"testgroup\",\"tags\":[\"tag1\",\"tag2\"],"
+ "\"userMetadata\":{\"testmetakey\":\"testmetavalue\",\"testmetakey2\":\"testmetavalue2\"}," + "\"userMetadata\":{\"testmetakey\":\"testmetavalue\",\"testmetakey2\":\"testmetavalue2\"},"
+ "\"user\":\"testuser\",\"password\":\"testpass\",\"privateKey\":\"pk\",\"authenticateSudo\":true}"); + "\"user\":\"testuser\",\"password\":\"testpass\",\"privateKey\":\"pk\",\"authenticateSudo\":true}");

View File

@ -22,7 +22,6 @@ package org.jclouds.virtualbox.functions;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -32,7 +31,9 @@ import javax.inject.Named;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.jclouds.virtualbox.config.VirtualBoxConstants; import org.jclouds.virtualbox.config.VirtualBoxConstants;
import com.google.common.base.Charsets;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.io.Files;
/** /**
* A supplier for vbox yaml config that reads a yaml whose path is stored under * A supplier for vbox yaml config that reads a yaml whose path is stored under
@ -59,7 +60,7 @@ public class YamlImagesFromFileConfig implements Supplier<String> {
if (!yamlFile.exists()) { if (!yamlFile.exists()) {
yamlDesc = IOUtils.toString(new InputStreamReader(getClass().getResourceAsStream("/default-images.yaml"))); yamlDesc = IOUtils.toString(new InputStreamReader(getClass().getResourceAsStream("/default-images.yaml")));
} else { } else {
yamlDesc = IOUtils.toString(new FileInputStream(yamlFile)); yamlDesc = Files.toString(yamlFile, Charsets.UTF_8);
} }
checkNotNull(yamlDesc, "yaml descriptor"); checkNotNull(yamlDesc, "yaml descriptor");
return yamlDesc; return yamlDesc;