下面列出了怎么用com.google.common.io.InputSupplier的API类实例代码及写法,或者点击链接到github查看源代码。
private ArrayList<String> extractProjectFiles(ZipFile inputZip, File projectRoot)
throws IOException {
ArrayList<String> projectFileNames = Lists.newArrayList();
Enumeration<? extends ZipEntry> inputZipEnumeration = inputZip.entries();
while (inputZipEnumeration.hasMoreElements()) {
ZipEntry zipEntry = inputZipEnumeration.nextElement();
final InputStream extractedInputStream = inputZip.getInputStream(zipEntry);
File extractedFile = new File(projectRoot, zipEntry.getName());
LOG.info("extracting " + extractedFile.getAbsolutePath() + " from input zip");
Files.createParentDirs(extractedFile); // Do I need this?
Files.copy(
new InputSupplier<InputStream>() {
public InputStream getInput() throws IOException {
return extractedInputStream;
}
},
extractedFile);
projectFileNames.add(extractedFile.getPath());
}
return projectFileNames;
}
@Override
public void put(String tableName, String blobId, InputSupplier<? extends InputStream> in, Map<String, String> attributes) throws IOException {
checkLegalTableName(tableName);
checkLegalBlobId(blobId);
checkNotNull(in, "in");
checkNotNull(attributes, "attributes");
Table table = _tableDao.get(tableName);
StorageSummary summary = putObject(table, blobId, in, attributes);
try {
_metadataProvider.writeMetadata(table, blobId, summary);
} catch (Throwable t) {
LOGGER.error("Failed to upload metadata for table: {}, blobId: {}, attempt to delete blob. Exception: {}", tableName, blobId, t.getMessage());
try {
_storageProvider.deleteObject(table, blobId);
} catch (Exception e1) {
LOGGER.error("Failed to delete blob for table: {}, blobId: {}. Inconsistency between blob and metadata storages. Exception: {}", tableName, blobId, e1.getMessage());
_metaDataNotPresentMeter.mark();
} finally {
Throwables.propagate(t);
}
}
}
@Override
public void put(String apiKey, String table, String blobId, InputSupplier<? extends InputStream> in,
Map<String, String> attributes)
throws IOException {
checkNotNull(table, "table");
checkNotNull(blobId, "blobId");
checkNotNull(in, "in");
checkNotNull(attributes, "attributes");
try {
// Encode the ttl as a URL query parameter
URI uri = _blobStore.clone()
.segment(table, blobId)
.build();
// Encode the rest of the attributes as request headers
EmoResource request = _client.resource(uri);
for (Map.Entry<String, String> entry : attributes.entrySet()) {
request.header(X_BVA_PREFIX + entry.getKey(), entry.getValue());
}
// Upload the object
request.type(MediaType.APPLICATION_OCTET_STREAM_TYPE)
.header(ApiKeyRequest.AUTHENTICATION_HEADER, apiKey)
.put(in.getInput());
} catch (EmoClientException e) {
throw convertException(e);
}
}
@Test
public void read_properties_file_guava() throws IOException {
URL url = Resources.getResource(PROPERTY_FILE_NAME);
InputSupplier<InputStream> inputSupplier =
Resources.newInputStreamSupplier(url);
Properties properties = new Properties();
properties.load(inputSupplier.getInput());
logger.info(properties);
assertEquals("http://www.leveluplunch.com", properties.getProperty("website"));
assertEquals("English", properties.getProperty("language"));
assertEquals("Welcome up to leveluplunch.com", properties.getProperty("message"));
}
private void downloadZip(String url, File targetZip) {
if (targetZip.exists()) {
return;
}
File zipTemp = new File(targetZip.getAbsolutePath() + ".temp");
try {
zipTemp.getParentFile().mkdirs();
InputSupplier<InputStream> input = Resources
.newInputStreamSupplier(URI.create(url).toURL());
OutputSupplier<FileOutputStream> ouput = Files
.newOutputStreamSupplier(zipTemp);
ByteStreams.copy(input, ouput);
} catch (IOException e) {
String message = "Unable to download phantomjs from " + url;
throw new IllegalStateException(message, e);
}
zipTemp.renameTo(targetZip);
}
private static void unzip(File zip, File toDir) throws IOException {
try (final ZipFile zipFile = new ZipFile(zip);) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
continue;
}
File to = new File(toDir, entry.getName());
to.getParentFile().mkdirs();
InputSupplier<InputStream> from = new InputSupplier<InputStream>() {
public InputStream getInput() throws IOException {
return zipFile.getInputStream(entry);
}
};
Files.copy(from, to);
}
}
}
private void downloadZip(String url, File targetZip) {
if (targetZip.exists()) {
return;
}
File zipTemp = new File(targetZip.getAbsolutePath() + ".temp");
try {
zipTemp.getParentFile().mkdirs();
InputSupplier<InputStream> input = Resources
.newInputStreamSupplier(URI.create(url).toURL());
OutputSupplier<FileOutputStream> ouput = Files
.newOutputStreamSupplier(zipTemp);
ByteStreams.copy(input, ouput);
} catch (IOException e) {
String message = "Unable to download phantomjs from " + url;
throw new IllegalStateException(message, e);
}
zipTemp.renameTo(targetZip);
}
private static void unzip(File zip, File toDir) throws IOException {
try (final ZipFile zipFile = new ZipFile(zip);) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
continue;
}
File to = new File(toDir, entry.getName());
to.getParentFile().mkdirs();
InputSupplier<InputStream> from = new InputSupplier<InputStream>() {
public InputStream getInput() throws IOException {
return zipFile.getInputStream(entry);
}
};
Files.copy(from, to);
}
}
}
private void downloadZip(String url, File targetZip) {
if (targetZip.exists()) {
return;
}
File zipTemp = new File(targetZip.getAbsolutePath() + ".temp");
try {
zipTemp.getParentFile().mkdirs();
InputSupplier<InputStream> input = Resources
.newInputStreamSupplier(URI.create(url).toURL());
OutputSupplier<FileOutputStream> ouput = Files
.newOutputStreamSupplier(zipTemp);
ByteStreams.copy(input, ouput);
} catch (IOException e) {
String message = "Unable to download phantomjs from " + url;
throw new IllegalStateException(message, e);
}
zipTemp.renameTo(targetZip);
}
private static void unzip(File zip, File toDir) throws IOException {
try (final ZipFile zipFile = new ZipFile(zip);) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
continue;
}
File to = new File(toDir, entry.getName());
to.getParentFile().mkdirs();
InputSupplier<InputStream> from = new InputSupplier<InputStream>() {
public InputStream getInput() throws IOException {
return zipFile.getInputStream(entry);
}
};
Files.copy(from, to);
}
}
}
public void prepareJavaCommand(JavaExecSpec execSpec) {
execSpec.setMain("jarjar." + GradleWorkerMain.class.getName());
execSpec.classpath(classPathRegistry.getClassPath("WORKER_MAIN").getAsFiles());
Object requestedSecurityManager = execSpec.getSystemProperties().get("java.security.manager");
if (requestedSecurityManager != null) {
execSpec.systemProperty("org.gradle.security.manager", requestedSecurityManager);
}
execSpec.systemProperty("java.security.manager", "jarjar." + BootstrapSecurityManager.class.getName());
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
DataOutputStream outstr = new DataOutputStream(new EncodedStream.EncodedOutput(bytes));
LOGGER.debug("Writing an application classpath to child process' standard input.");
outstr.writeInt(processBuilder.getApplicationClasspath().size());
for (File file : processBuilder.getApplicationClasspath()) {
outstr.writeUTF(file.getAbsolutePath());
}
outstr.close();
final InputStream originalStdin = execSpec.getStandardInput();
InputStream input = ByteStreams.join(ByteStreams.newInputStreamSupplier(bytes.toByteArray()), new InputSupplier<InputStream>() {
public InputStream getInput() throws IOException {
return originalStdin;
}
}).getInput();
execSpec.setStandardInput(input);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public void prepareJavaCommand(JavaExecSpec execSpec) {
execSpec.setMain("jarjar." + GradleWorkerMain.class.getName());
execSpec.classpath(classPathRegistry.getClassPath("WORKER_MAIN").getAsFiles());
Object requestedSecurityManager = execSpec.getSystemProperties().get("java.security.manager");
if (requestedSecurityManager != null) {
execSpec.systemProperty("org.gradle.security.manager", requestedSecurityManager);
}
execSpec.systemProperty("java.security.manager", "jarjar." + BootstrapSecurityManager.class.getName());
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
DataOutputStream outstr = new DataOutputStream(new EncodedStream.EncodedOutput(bytes));
LOGGER.debug("Writing an application classpath to child process' standard input.");
outstr.writeInt(processBuilder.getApplicationClasspath().size());
for (File file : processBuilder.getApplicationClasspath()) {
outstr.writeUTF(file.getAbsolutePath());
}
outstr.close();
final InputStream originalStdin = execSpec.getStandardInput();
InputStream input = ByteStreams.join(ByteStreams.newInputStreamSupplier(bytes.toByteArray()), new InputSupplier<InputStream>() {
public InputStream getInput() throws IOException {
return originalStdin;
}
}).getInput();
execSpec.setStandardInput(input);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public long computeMD5() throws IOException {
InputSupplier<InputStream> supplier = new InputSupplier<InputStream>() {
public InputStream getInput() throws IOException {
return newInputStream();
}
};
return ByteStreams.hash(supplier, Hashing.md5()).asLong();
}
public long computeMD5() throws IOException {
InputSupplier<InputStream> supplier = new InputSupplier<InputStream>() {
public InputStream getInput() throws IOException {
return newInputStream();
}
};
return ByteStreams.hash(supplier, Hashing.md5()).asLong();
}
/**
* Returns an InputSupplier that throws an exception if the caller attempts to consume the input stream
* multiple times.
*/
private InputSupplier<InputStream> onceOnlySupplier(final InputStream in) {
final AtomicBoolean once = new AtomicBoolean();
return new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
if (!once.compareAndSet(false, true)) {
throw new IllegalStateException("Input stream may be consumed only once per BlobStore call.");
}
return in;
}
};
}
private StorageSummary putObject(Table table, String blobId, InputSupplier<? extends InputStream> in, Map<String, String> attributes) throws IOException {
long timestamp = _storageProvider.getCurrentTimestamp(table);
int chunkSize = _storageProvider.getDefaultChunkSize();
checkArgument(chunkSize > 0);
DigestInputStream md5In = new DigestInputStream(in.getInput(), getMessageDigest("MD5"));
DigestInputStream sha1In = new DigestInputStream(md5In, getMessageDigest("SHA-1"));
// A more aggressive solution like the Astyanax ObjectWriter recipe would improve performance by pipelining
// reading the input stream and writing chunks, and issuing the writes in parallel.
byte[] bytes = new byte[chunkSize];
long length = 0;
int chunkCount = 0;
for (; ; ) {
int chunkLength;
try {
chunkLength = ByteStreams.read(sha1In, bytes, 0, bytes.length);
} catch (IOException e) {
LOGGER.error("Failed to read input stream", e);
throw Throwables.propagate(e);
}
if (chunkLength == 0) {
break;
}
ByteBuffer buffer = ByteBuffer.wrap(bytes, 0, chunkLength);
_storageProvider.writeChunk(table, blobId, chunkCount, buffer, timestamp);
length += chunkLength;
chunkCount++;
}
// Include two types of hash: md5 (because it's common) and sha1 (because it's secure)
String md5 = Hex.encodeHexString(md5In.getMessageDigest().digest());
String sha1 = Hex.encodeHexString(sha1In.getMessageDigest().digest());
return new StorageSummary(length, chunkCount, chunkSize, md5, sha1, attributes, timestamp);
}
@Test
public void testPut() throws IOException {
InputSupplier<InputStream> in = new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return new ByteArrayInputStream("blob-content".getBytes());
}
};
Map<String, String> attributes = ImmutableMap.of("key", "value");
blobClient().put("table-name", "blob-id", in, attributes);
//noinspection unchecked
verify(_server).put(eq("table-name"), eq("blob-id"), isA(InputSupplier.class), eq(attributes));
verifyNoMoreInteractions(_server);
}
@Test
@SuppressWarnings("unchecked")
public void testMalformedChunkException() throws Exception {
InputSupplier<InputStream> input = ByteStreams.join(
ByteStreams.newInputStreamSupplier("[5,6".getBytes(Charsets.UTF_8)),
exceptionStreamSupplier(new MalformedChunkCodingException("Bad chunk header")));
assertThrowsEOFException(input.getInput(), Integer.class);
}
@Test
@SuppressWarnings("unchecked")
public void testTruncatedChunkException() throws Exception {
InputSupplier<InputStream> input = ByteStreams.join(
ByteStreams.newInputStreamSupplier("[5,6".getBytes(Charsets.UTF_8)),
exceptionStreamSupplier(new TruncatedChunkException("Truncated chunk ( expected size: 3996; actual size: 1760)")));
assertThrowsEOFException(input.getInput(), Integer.class);
}
private InputSupplier<InputStream> exceptionStreamSupplier(final Throwable t) {
return new InputSupplier<InputStream>() {
@Override
public InputStream getInput() throws IOException {
return exceptionStream(t);
}
};
}
public void prepareJavaCommand(JavaExecSpec execSpec) {
execSpec.setMain("jarjar." + GradleWorkerMain.class.getName());
execSpec.classpath(classPathRegistry.getClassPath("WORKER_MAIN").getAsFiles());
Object requestedSecurityManager = execSpec.getSystemProperties().get("java.security.manager");
if (requestedSecurityManager != null) {
execSpec.systemProperty("org.gradle.security.manager", requestedSecurityManager);
}
execSpec.systemProperty("java.security.manager", "jarjar." + BootstrapSecurityManager.class.getName());
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
DataOutputStream outstr = new DataOutputStream(new EncodedStream.EncodedOutput(bytes));
LOGGER.debug("Writing an application classpath to child process' standard input.");
outstr.writeInt(processBuilder.getApplicationClasspath().size());
for (File file : processBuilder.getApplicationClasspath()) {
outstr.writeUTF(file.getAbsolutePath());
}
outstr.close();
final InputStream originalStdin = execSpec.getStandardInput();
InputStream input = ByteStreams.join(ByteStreams.newInputStreamSupplier(bytes.toByteArray()), new InputSupplier<InputStream>() {
public InputStream getInput() throws IOException {
return originalStdin;
}
}).getInput();
execSpec.setStandardInput(input);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public void prepareJavaCommand(JavaExecSpec execSpec) {
execSpec.setMain("jarjar." + GradleWorkerMain.class.getName());
execSpec.classpath(classPathRegistry.getClassPath("WORKER_MAIN").getAsFiles());
Object requestedSecurityManager = execSpec.getSystemProperties().get("java.security.manager");
if (requestedSecurityManager != null) {
execSpec.systemProperty("org.gradle.security.manager", requestedSecurityManager);
}
execSpec.systemProperty("java.security.manager", "jarjar." + BootstrapSecurityManager.class.getName());
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
DataOutputStream outstr = new DataOutputStream(new EncodedStream.EncodedOutput(bytes));
LOGGER.debug("Writing an application classpath to child process' standard input.");
outstr.writeInt(processBuilder.getApplicationClasspath().size());
for (File file : processBuilder.getApplicationClasspath()) {
outstr.writeUTF(file.getAbsolutePath());
}
outstr.close();
final InputStream originalStdin = execSpec.getStandardInput();
InputStream input = ByteStreams.join(ByteStreams.newInputStreamSupplier(bytes.toByteArray()), new InputSupplier<InputStream>() {
public InputStream getInput() throws IOException {
return originalStdin;
}
}).getInput();
execSpec.setStandardInput(input);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Loads legacy warnings list from the file.
* @return The lines of the file.
*/
protected static Set<String> loadWhitelistedJsWarnings(
InputSupplier<? extends Reader> supplier) {
try {
return loadWhitelistedJsWarnings(supplier.getInput());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Arguments decode(InputSupplier<? extends Reader> readerSupplier) throws IOException {
try (Reader reader = readerSupplier.getInput()) {
return GSON.fromJson(reader, Arguments.class);
}
}
void put(String table, String blobId, InputSupplier<? extends InputStream> in, Map<String, String> attributes)
throws IOException;
void put(@Credential String apiKey, String table, String blobId, InputSupplier<? extends InputStream> in, Map<String, String> attributes)
throws IOException;
@Override
public void put(String table, String blobId, InputSupplier<? extends InputStream> in, Map<String, String> attributes)
throws IOException {
_local.get().put(table, blobId, in, attributes);
}
@Override
public void put(String table, String blobId, InputSupplier<? extends InputStream> in, Map<String, String> attributes)
throws IOException {
_authBlobStore.put(_apiKey, table, blobId, in, attributes);
}