Refine search
/** * Returns the data from a {@link InputStream} factory as a byte array. * * @param supplier the factory * @throws IOException if an I/O error occurs */ public static byte[] toByteArray( InputSupplier<? extends InputStream> supplier) throws IOException { boolean threw = true; InputStream in = supplier.getInput(); try { byte[] result = toByteArray(in); threw = false; return result; } finally { Closeables.close(in, threw); } }
public InputStream copy(final String header, InputStream instream) { int limit = 256 * 1024; FileBackedOutputStream out = null; try { out = new FileBackedOutputStream(limit); long bytesRead = ByteStreams.copy(instream, out); if (bytesRead >= limit) logger.debug("over limit %d/%d: wrote temp file", bytesRead, limit); wire(header, out.getSupplier().getInput()); return out.getSupplier().getInput(); } catch (IOException e) { throw new RuntimeException("Error tapping line", e); } finally { closeQuietly(out); closeQuietly(instream); } }
/** * Opens an input stream from the supplier, copies all bytes from the * input to the output, and closes the input stream. Does not close * or flush the output stream. * * @param from the input factory * @param to the output stream to write to * @return the number of bytes copied * @throws IOException if an I/O error occurs */ public static long copy(InputSupplier<? extends InputStream> from, OutputStream to) throws IOException { boolean threw = true; InputStream in = from.getInput(); try { long count = copy(in, to); threw = false; return count; } finally { Closeables.close(in, threw); } }
public static void unzip(File zipFile, File destFolder) throws IOException { ZipInputStream zis = null; try { InputSupplier<FileInputStream> fis = Files.newInputStreamSupplier(zipFile); zis = new ZipInputStream(fis.getInput()); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { if (!entry.isDirectory()) { final File file = new File(destFolder, entry.getName()); Files.createParentDirs(file); Files.write(ByteStreams.toByteArray(zis), file); } } } finally { Closeables.closeQuietly(zis); } }
@Override public byte[] encrypt(InputSupplier<? extends InputStream> supplier) throws IOException { return ByteStreams.toByteArray(new RSAEncryptingPayload(Payloads.newPayload(supplier.getInput()), privateKey .get())); }
/** Returns the length of a supplied input stream, in bytes. */ public static long length(InputSupplier<? extends InputStream> supplier) throws IOException { long count = 0; boolean threw = true; InputStream in = supplier.getInput(); try { while (true) { // We skip only Integer.MAX_VALUE due to JDK overflow bugs. long amt = in.skip(Integer.MAX_VALUE); if (amt == 0) { if (in.read() == -1) { threw = false; return count; } count++; } else { count += amt; } } } finally { Closeables.close(in, threw); } }
public static void unpackConfig(InputSupplier<? extends InputStream> inputSupplier, File outputDir) throws IOException { ZipInputStream in = new ZipInputStream(inputSupplier.getInput()); try { for (ZipEntry zipEntry = in.getNextEntry(); zipEntry != null; zipEntry = in.getNextEntry()) { File file = new File(outputDir, zipEntry.getName()); if (zipEntry.getName().endsWith("/")) { // this is a directory file.mkdirs(); } else { file.getParentFile().mkdirs(); ByteStreams.copy(in, Files.newOutputStreamSupplier(file)); file.setLastModified(zipEntry.getTime()); } } } finally { in.close(); } }
@Override public InputStream getInput() throws IOException { InputStream in = supplier.getInput(); if (offset > 0) { try { skipFully(in, offset); } catch (IOException e) { Closeables.closeQuietly(in); throw e; } } return new LimitInputStream(in, length); } };
public FlowSpecification fromJson(InputSupplier<? extends Reader> inputSupplier) throws IOException { Reader reader = inputSupplier.getInput(); try { return fromJson(reader); } finally { Closeables.closeQuietly(reader); } }
@Override public InputStream openStream() throws IOException { return supplier.getInput(); } };
/** * Process the bytes of a supplied stream * * @param supplier the input stream factory * @param processor the object to which to pass the bytes of the stream * @return the result of the byte processor * @throws IOException if an I/O error occurs */ public static <T> T readBytes( InputSupplier<? extends InputStream> supplier, ByteProcessor<T> processor) throws IOException { checkNotNull(supplier); checkNotNull(processor); Closer closer = Closer.create(); try { InputStream in = closer.register(supplier.getInput()); return readBytes(in, processor); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
public static InputStream fromString(String input) { try { byte[] bytes = checkNotNull(input, "input").getBytes(Charsets.UTF_8); InputSupplier<ByteArrayInputStream> supplier = ByteStreams.newInputStreamSupplier(bytes); InputStream stream = supplier.getInput(); return stream; } catch (IOException ioe) { if (log.isDebugEnabled()) log.debug("Error creating InputStream from String: " + ioe.getMessage()); throw Exceptions.propagate(ioe); } }
@Override public Object getPayload() { // Note that while _bulk requests are made up of JSON, the body as a whole isn't actually valid JSON. final InputStream input; try { input = ByteStreams.join(Iterables.transform(actions, new PayloadFn())).getInput(); } catch (IOException e) { throw new RuntimeException(e); } return input; }
public InputStream copy(final String header, InputStream instream) { int limit = 256 * 1024; final FileBackedOutputStream out = new FileBackedOutputStream(limit); try { long bytesRead = ByteStreams.copy(instream, out); if (bytesRead >= limit) logger.debug("over limit %d/%d: wrote temp file", bytesRead, limit); wire(header, out.getSupplier().getInput()); // we must call FileBackedOutputStream.reset to remove temporary file return new FilterInputStream(out.getSupplier().getInput()) { @Override public void close() throws IOException { super.close(); out.reset(); } }; } catch (IOException e) { throw new RuntimeException("Error tapping line", e); } finally { closeQuietly(out); closeQuietly(instream); } }
/** * Opens an input stream from the supplier, copies all bytes from the * input to the output, and closes the input stream. Does not close * or flush the output stream. * * @param from the input factory * @param to the output stream to write to * @return the number of bytes copied * @throws IOException if an I/O error occurs */ public static long copy(InputSupplier<? extends InputStream> from, OutputStream to) throws IOException { boolean threw = true; InputStream in = from.getInput(); try { long count = copy(in, to); threw = false; return count; } finally { Closeables.close(in, threw); } }
@Override public byte[] encrypt(InputSupplier<? extends InputStream> supplier) throws IOException { return ByteStreams.toByteArray(new RSAEncryptingPayload(Payloads.newPayload(supplier.getInput()), privateKey .get())); }
/** * Returns the characters from a {@link Readable} & {@link Closeable} object * supplied by a factory as a new {@link StringBuilder} instance. * * @param supplier the factory to read from * @throws IOException if an I/O error occurs */ private static <R extends Readable & Closeable> StringBuilder toStringBuilder( InputSupplier<R> supplier) throws IOException { boolean threw = true; R r = supplier.getInput(); try { StringBuilder result = toStringBuilder(r); threw = false; return result; } finally { Closeables.close(r, threw); } }
private Location copyFile(Id.Artifact artifactId, InputSupplier<? extends InputStream> artifactContentSupplier) throws IOException { Location fileDirectory = namespacedLocationFactory.get(artifactId.getNamespace().toEntityId()) .append(ARTIFACTS_PATH).append(artifactId.getName()); Location destination = fileDirectory.append(artifactId.getVersion().getVersion()).getTempFile(".jar"); Locations.mkdirsIfNotExists(fileDirectory); // write the file contents try (InputStream artifactContents = artifactContentSupplier.getInput(); OutputStream destinationStream = destination.getOutputStream()) { ByteStreams.copy(artifactContents, destinationStream); } return destination; }
/*@Override*/ public InputStream getInput() throws IOException { InputStream in = supplier.getInput(); if (offset > 0) { try { skipFully(in, offset); } catch (IOException e) { Closeables.closeQuietly(in); throw e; } } return new LimitInputStream(in, length); } };
static String getVersion(String groupId, String artifactId) { final Properties props = new Properties(); InputStream is = null; try { is = getPomInputStreamSupplier(groupId, artifactId).getInput(); props.load(is); return props.getProperty("version", UNKNOWN_VERSION); } catch (Exception e) { logger.debug("Could not find version for maven artifact {}:{}", groupId, artifactId); logger.debug("Got the following exception:", e); return UNKNOWN_VERSION; } finally { closeQuietly(is); } }