com.google.common.io.InputSupplier Java Examples

The following examples show how to use com.google.common.io.InputSupplier. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PhantomJsDownloader.java    From blog with MIT License 6 votes vote down vote up
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);
		}
	}
}
 
Example #2
Source File: ProjectBuilder.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: PhantomJsDownloader.java    From blog with MIT License 6 votes vote down vote up
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);
}
 
Example #4
Source File: PhantomJsDownloader.java    From blog with MIT License 6 votes vote down vote up
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);
		}
	}
}
 
Example #5
Source File: PhantomJsDownloader.java    From blog with MIT License 6 votes vote down vote up
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);
}
 
Example #6
Source File: PhantomJsDownloader.java    From blog with MIT License 6 votes vote down vote up
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);
		}
	}
}
 
Example #7
Source File: PhantomJsDownloader.java    From blog with MIT License 6 votes vote down vote up
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);
}
 
Example #8
Source File: ReadPropertiesFile.java    From levelup-java-examples with Apache License 2.0 6 votes vote down vote up
@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"));
}
 
Example #9
Source File: DefaultBlobStore.java    From emodb with Apache License 2.0 6 votes vote down vote up
@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);
        }
    }
}
 
Example #10
Source File: BlobStoreClient.java    From emodb with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example #11
Source File: JsonStreamingArrayParserTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #12
Source File: WhitelistWarningsGuard.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
  }
}
 
Example #13
Source File: ApplicationClassesInSystemClassLoaderWorkerFactory.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
    }
}
 
Example #14
Source File: ApplicationClassesInSystemClassLoaderWorkerFactory.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
    }
}
 
Example #15
Source File: JsonStreamingArrayParserTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
private InputSupplier<InputStream> exceptionStreamSupplier(final Throwable t) {
    return new InputSupplier<InputStream>() {
        @Override
        public InputStream getInput() throws IOException {
            return exceptionStream(t);
        }
    };
}
 
Example #16
Source File: ApplicationClassesInSystemClassLoaderWorkerFactory.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
    }
}
 
Example #17
Source File: JsonStreamingArrayParserTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #18
Source File: BlobStoreJerseyTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #19
Source File: DefaultBlobStore.java    From emodb with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #20
Source File: BlobStoreResource1.java    From emodb with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
        }
    };
}
 
Example #21
Source File: UriTaskProvider.java    From workspacemechanic with Eclipse Public License 1.0 5 votes vote down vote up
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();
}
 
Example #22
Source File: InMemoryTaskProvider.java    From workspacemechanic with Eclipse Public License 1.0 5 votes vote down vote up
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();
}
 
Example #23
Source File: ApplicationClassesInSystemClassLoaderWorkerFactory.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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);
    }
}
 
Example #24
Source File: BlobStoreAuthenticatorProxy.java    From emodb with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example #25
Source File: BlobStoreProviderProxy.java    From emodb with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example #26
Source File: AuthBlobStore.java    From emodb with Apache License 2.0 4 votes vote down vote up
void put(@Credential String apiKey, String table, String blobId, InputSupplier<? extends InputStream> in, Map<String, String> attributes)
throws IOException;
 
Example #27
Source File: BlobStore.java    From emodb with Apache License 2.0 4 votes vote down vote up
void put(String table, String blobId, InputSupplier<? extends InputStream> in, Map<String, String> attributes)
throws IOException;
 
Example #28
Source File: ArgumentsCodec.java    From twill with Apache License 2.0 4 votes vote down vote up
public static Arguments decode(InputSupplier<? extends Reader> readerSupplier) throws IOException {
  try (Reader reader = readerSupplier.getInput()) {
    return GSON.fromJson(reader, Arguments.class);
  }
}