List of usage examples for com.google.common.io ByteSource openStream
public abstract InputStream openStream() throws IOException;
From source file:talkeeg.bf.schema.SchemaSource.java
public static Schema fromResource(String resourceUri) throws Exception { final URL url = Resources.getResource(resourceUri); final ByteSource bs = Resources.asByteSource(url); try (InputStream is = bs.openStream()) { return fromInputStream(is); }/*from w w w . j ava2 s .c o m*/ }
From source file:org.jclouds.digitalocean2.ssh.ECDSAKeys.java
/** * Returns {@link java.security.spec.DSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub} * * @param supplier the input stream factory, formatted {@code ssh-dss AAAAB3NzaC1yc2EAAAADAQABAAAB...} * * @return the {@link java.security.spec.DSAPublicKeySpec} which was OpenSSH Base64 Encoded {@code id_rsa.pub} * @throws java.io.IOException if an I/O error occurs */// w w w .j a v a 2 s .c o m public static ECPublicKeySpec publicKeySpecFromOpenSSH(ByteSource supplier) throws IOException { InputStream stream = supplier.openStream(); Iterable<String> parts = Splitter.on(' ').split(toStringAndClose(stream).trim()); String signatureFormat = get(parts, 0); checkArgument(size(parts) >= 2 && signatureFormat.startsWith(ECDSA_SHA2_PREFIX), "bad format, should be: ecdsa-sha2-xxx AAAAB3..."); String curveName = signatureFormat.substring(ECDSA_SHA2_PREFIX.length()); if (!CURVES.containsKey(curveName)) { throw new IOException("Unsupported curve: " + curveName); } ECParameterSpec spec = CURVES.get(curveName); stream = new ByteArrayInputStream(base64().decode(get(parts, 1))); readLengthFirst(stream); // ignore return value String curveMarker = new String(readLengthFirst(stream)); checkArgument(curveName.equals(curveMarker), "looking for marker %s but got %s", curveName, curveMarker); ECPoint ecPoint = decodeECPoint(readLengthFirst(stream), spec.getCurve()); return new ECPublicKeySpec(ecPoint, spec); }
From source file:com.google.devtools.build.xcode.plmerge.PlistMerging.java
public static NSDictionary readPlistFile(final Path sourceFilePath) throws IOException { ByteSource rawBytes = new Utf8BomSkippingByteSource(sourceFilePath); try {//w w w. j a va 2s .c o m try (InputStream in = rawBytes.openStream()) { return (NSDictionary) PropertyListParser.parse(in); } catch (PropertyListFormatException | ParseException e) { // If we failed to parse, the plist may implicitly be a map. To handle this, wrap the plist // with {}. // TODO(bazel-team): Do this in a cleaner way. ByteSource concatenated = ByteSource.concat(ByteSource.wrap(new byte[] { '{' }), rawBytes, ByteSource.wrap(new byte[] { '}' })); try (InputStream in = concatenated.openStream()) { return (NSDictionary) PropertyListParser.parse(in); } } } catch (PropertyListFormatException | ParseException | ParserConfigurationException | SAXException e) { throw new IOException(e); } }
From source file:io.druid.java.util.common.StreamUtils.java
/** * Retry copy attempts from input stream to output stream. Does *not* check to make sure data was intact during the transfer * * @param byteSource Supplier for input streams to copy from. The stream is closed on every retry. * @param byteSink Supplier for output streams. The stream is closed on every retry. * @param shouldRetry Predicate to determine if the throwable is recoverable for a retry * @param maxAttempts Maximum number of retries before failing *///www . j ava 2 s . co m public static long retryCopy(final ByteSource byteSource, final ByteSink byteSink, final Predicate<Throwable> shouldRetry, final int maxAttempts) { try { return RetryUtils.retry(new Callable<Long>() { @Override public Long call() throws Exception { try (InputStream inputStream = byteSource.openStream()) { try (OutputStream outputStream = byteSink.openStream()) { final long retval = ByteStreams.copy(inputStream, outputStream); // Workarround for http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf outputStream.flush(); return retval; } } } }, shouldRetry, maxAttempts); } catch (Exception e) { throw Throwables.propagate(e); } }
From source file:com.opengamma.strata.collect.io.UnicodeBom.java
/** * Converts a {@code ByteSource} to a {@code CharSource}. * <p>//from w w w.j av a2s.c o m * This ensures that any Unicode byte order marker is used correctly. * The default encoding is UTF-8 if no BOM is found. * * @param byteSource the byte source * @return the char source, that uses the BOM to determine the encoding */ public static CharSource toCharSource(ByteSource byteSource) { return new CharSource() { @Override public ByteSource asByteSource(Charset charset) { return byteSource; } @Override public Reader openStream() throws IOException { return toReader(byteSource.openStream()); } @Override public String toString() { return "UnicodeBom.toCharSource(" + byteSource.toString() + ")"; } }; }
From source file:com.metamx.common.StreamUtils.java
/** * Retry copy attempts from input stream to output stream. Does *not* check to make sure data was intact during the transfer * * @param byteSource Supplier for input streams to copy from. The stream is closed on every retry. * @param byteSink Supplier for output streams. The stream is closed on every retry. * @param shouldRetry Predicate to determine if the throwable is recoverable for a retry * @param maxAttempts Maximum number of retries before failing *///from w ww . j a v a 2 s .c o m public static long retryCopy(final ByteSource byteSource, final ByteSink byteSink, final Predicate<Throwable> shouldRetry, final int maxAttempts) { try { return RetryUtils.retry(new Callable<Long>() { @Override public Long call() throws Exception { InputStream inputStream = null; OutputStream outputStream = null; try { inputStream = byteSource.openStream(); outputStream = byteSink.openStream(); return ByteStreams.copy(inputStream, outputStream); } finally { CloseQuietly.close(inputStream); CloseQuietly.close(outputStream); } } }, shouldRetry, maxAttempts); } catch (Exception e) { throw Throwables.propagate(e); } }
From source file:com.metamx.common.CompressionUtils.java
/** * A gunzip function to store locally//from ww w . ja v a 2 s .co m * * @param in The factory to produce input streams * @param outFile The file to store the result into * @param shouldRetry A predicate to indicate if the Throwable is recoverable * * @return The count of bytes written to outFile */ public static FileUtils.FileCopyResult gunzip(final ByteSource in, final File outFile, Predicate<Throwable> shouldRetry) { return FileUtils.retryCopy(new ByteSource() { @Override public InputStream openStream() throws IOException { return gzipInputStream(in.openStream()); } }, outFile, shouldRetry, DEFAULT_RETRY_COUNT); }
From source file:org.apache.druid.java.util.common.CompressionUtils.java
/** * Unzip the byteSource to the output directory. If cacheLocally is true, the byteSource is cached to local disk before unzipping. * This may cause more predictable behavior than trying to unzip a large file directly off a network stream, for example. * * @param byteSource The ByteSource which supplies the zip data * * @param byteSource The ByteSource which supplies the zip data * @param outDir The output directory to put the contents of the zip * @param shouldRetry A predicate expression to determine if a new InputStream should be acquired from ByteSource and the copy attempted again * @param cacheLocally A boolean flag to indicate if the data should be cached locally * * @return A FileCopyResult containing the result of writing the zip entries to disk * * @throws IOException/* w w w .ja va 2 s . c o m*/ */ public static FileUtils.FileCopyResult unzip(final ByteSource byteSource, final File outDir, final Predicate<Throwable> shouldRetry, boolean cacheLocally) throws IOException { if (!cacheLocally) { try { return RetryUtils.retry(() -> unzip(byteSource.openStream(), outDir), shouldRetry, DEFAULT_RETRY_COUNT); } catch (IOException e) { throw e; } catch (Exception e) { throw Throwables.propagate(e); } } else { final File tmpFile = File.createTempFile("compressionUtilZipCache", ZIP_SUFFIX); try { FileUtils.retryCopy(byteSource, tmpFile, shouldRetry, DEFAULT_RETRY_COUNT); return unzip(tmpFile, outDir); } finally { if (!tmpFile.delete()) { log.warn("Could not delete zip cache at [%s]", tmpFile.toString()); } } } }
From source file:org.renjin.primitives.io.serialization.RDataReader.java
public static boolean isRDataFile(ByteSource inputSupplier) throws IOException { InputStream in = inputSupplier.openStream(); try {/* www. j av a2 s . co m*/ byte streamType = readStreamType(in); return streamType != -1; } finally { Closeables.closeQuietly(in); } }
From source file:com.metamx.common.CompressionUtils.java
/** * Unzip the byteSource to the output directory. If cacheLocally is true, the byteSource is cached to local disk before unzipping. * This may cause more predictable behavior than trying to unzip a large file directly off a network stream, for example. * * @param byteSource The ByteSource which supplies the zip data * * @param byteSource The ByteSource which supplies the zip data * @param outDir The output directory to put the contents of the zip * @param shouldRetry A predicate expression to determine if a new InputStream should be acquired from ByteSource and the copy attempted again * @param cacheLocally A boolean flag to indicate if the data should be cached locally * * @return A FileCopyResult containing the result of writing the zip entries to disk * * @throws IOException/*w w w. ja va 2 s . com*/ */ public static FileUtils.FileCopyResult unzip(final ByteSource byteSource, final File outDir, final Predicate<Throwable> shouldRetry, boolean cacheLocally) throws IOException { if (!cacheLocally) { try { return RetryUtils.retry(new Callable<FileUtils.FileCopyResult>() { @Override public FileUtils.FileCopyResult call() throws Exception { return unzip(byteSource.openStream(), outDir); } }, shouldRetry, DEFAULT_RETRY_COUNT); } catch (Exception e) { throw Throwables.propagate(e); } } else { final File tmpFile = File.createTempFile("compressionUtilZipCache", ZIP_SUFFIX); try { FileUtils.FileCopyResult copyResult = FileUtils.retryCopy(byteSource, tmpFile, shouldRetry, DEFAULT_RETRY_COUNT); return unzip(tmpFile, outDir); } finally { if (!tmpFile.delete()) { log.warn("Could not delete zip cache at [%s]", tmpFile.toString()); } } } }