List of usage examples for com.google.common.io ByteStreams copy
public static long copy(ReadableByteChannel from, WritableByteChannel to) throws IOException
From source file:com.metamx.common.StreamUtils.java
/** * Copy from `is` to `os` and close the streams regardless of the result. * * @param is The `InputStream` to copy results from. It is closed * @param os The `OutputStream` to copy results to. It is closed * * @return The count of bytes written to `os` * * @throws IOException/* w w w. ja v a2 s. c o m*/ */ public static long copyAndClose(InputStream is, OutputStream os) throws IOException { try { return ByteStreams.copy(is, os); } finally { CloseQuietly.close(is); CloseQuietly.close(os); } }
From source file:io.druid.segment.data.EntireLayoutLongSupplierSerializer.java
@Override public void closeAndConsolidate(ByteSink consolidatedOut) throws IOException { close();//from ww w . ja va 2 s . c om try (OutputStream out = consolidatedOut.openStream(); InputStream meta = ioPeon.makeInputStream(metaFile); InputStream value = ioPeon.makeInputStream(valueFile)) { ByteStreams.copy(meta, out); ByteStreams.copy(value, out); } }
From source file:tachyon.util.io.FileUtils.java
/** * Blocking operation that copies the processes stdout/stderr to this JVM's stdout/stderr. * * @param process process whose stdout/stderr to copy * @throws IOException when operation fails *//*w ww . ja v a2 s .c o m*/ private static void redirectIO(final Process process) throws IOException { Preconditions.checkNotNull(process); /* * Because chmod doesn't have a lot of error or output messages, it is safe to process the * output after the process is done. As of java 7, you can have the process redirect to * System.out and System.err without forking a process. * * TODO(cc): When java 6 support is dropped switch to ProcessBuilder.html#inheritIO(). */ Closer closer = Closer.create(); try { ByteStreams.copy(closer.register(process.getInputStream()), System.out); ByteStreams.copy(closer.register(process.getErrorStream()), System.err); } catch (Exception e) { throw closer.rethrow(e); } finally { closer.close(); } }
From source file:org.locationtech.geogig.rest.geotools.SimpleFeatureStoreRepresentation.java
@Override public void write(OutputStream outputStream) { try {/*from w w w. j av a2 s .c o m*/ // Block until get() returns. We don't actually do anything with the result, we just // need to wait for the operation to complete. Once complete, we can stream the binary // file back. // NOTE: This will not work if the command takes longer than the TCP timeout to // complete. The client has issued a GET for this data, so the clock is ticking. If the // command blocks too long, we won't start sending data back to the client and the // connection will close due to data timeout. Eventually, this should be changed to // return a data retrieval URL that the client can poll and retrieve when complete. // Until then, this solution is somewhat fragile. command.get(); // command is finished, send the binary file contents out through the output stream try (FileInputStream stream = getStream()) { ByteStreams.copy(stream, outputStream); } // flush the stream outputStream.flush(); } catch (IOException | InterruptedException | ExecutionException ex) { throw new RuntimeException("Could not successfully stream File", ex); } finally { // remove the file, we don't want it hanging around binary.delete(); } }
From source file:net.oneandone.shared.artifactory.DownloadResponseHandler.java
@Override public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException { final HttpEntity entity = returnEntityWhenStatusValid(response); final Header sha1Header = checkHeaderNotNull(response, X_CHECKSUM_SHA1); final Header fileNameHeader = checkHeaderNotNull(response, X_ARTIFACTORY_FILENAME); final Sha1 sha1FromHeader = Sha1.valueOf(sha1Header.getValue()); checkState(sha1FromHeader.equals(expectedSha1), "sha1 from header %s (%s) does not equal expected sha1 (%s)", X_CHECKSUM_SHA1, sha1FromHeader, expectedSha1);//from w ww . j ava 2 s.c o m final File outputFile = new File(targetDirectory, fileNameHeader.getValue()); final Sha1FilterOutputStream sha1FilterOutputStream = new Sha1FilterOutputStream( new BufferedOutputStream(new FileOutputStream(outputFile))); try { ByteStreams.copy(entity.getContent(), sha1FilterOutputStream); } finally { sha1FilterOutputStream.close(); } final Sha1 sha1OfOutputFile = sha1FilterOutputStream.getSha1(); checkState(sha1OfOutputFile.equals(expectedSha1), "sha1 from file %s (%s) does not equal expected sha1 (%s)", outputFile, sha1OfOutputFile, expectedSha1); return null; }
From source file:com.facebook.buck.util.unarchive.Unzip.java
private void writeZipContents(ZipFile zip, ZipArchiveEntry entry, ProjectFilesystem filesystem, Path target) throws IOException { // Write file try (InputStream is = zip.getInputStream(entry)) { if (entry.isUnixSymlink()) { filesystem.createSymLink(target, filesystem.getPath(new String(ByteStreams.toByteArray(is), Charsets.UTF_8)), /* force */ true); } else {/*from w ww . j a v a 2 s . co m*/ try (OutputStream out = filesystem.newFileOutputStream(target)) { ByteStreams.copy(is, out); } } } Path filePath = filesystem.resolve(target); File file = filePath.toFile(); // restore mtime for the file file.setLastModified(entry.getTime()); // TODO(simons): Implement what the comment below says we should do. // // Sets the file permissions of the output file given the information in {@code entry}'s // extra data field. According to the docs at // http://www.opensource.apple.com/source/zip/zip-6/unzip/unzip/proginfo/extra.fld there // are two extensions that might support file permissions: Acorn and ASi UNIX. We shall // assume that inputs are not from an Acorn SparkFS. The relevant section from the docs: // // <pre> // The following is the layout of the ASi extra block for Unix. The // local-header and central-header versions are identical. // (Last Revision 19960916) // // Value Size Description // ----- ---- ----------- // (Unix3) 0x756e Short tag for this extra block type ("nu") // TSize Short total data size for this block // CRC Long CRC-32 of the remaining data // Mode Short file permissions // SizDev Long symlink'd size OR major/minor dev num // UID Short user ID // GID Short group ID // (var.) variable symbolic link filename // // Mode is the standard Unix st_mode field from struct stat, containing // user/group/other permissions, setuid/setgid and symlink info, etc. // </pre> // // From the stat man page, we see that the following mask values are defined for the file // permissions component of the st_mode field: // // <pre> // S_ISUID 0004000 set-user-ID bit // S_ISGID 0002000 set-group-ID bit (see below) // S_ISVTX 0001000 sticky bit (see below) // // S_IRWXU 00700 mask for file owner permissions // // S_IRUSR 00400 owner has read permission // S_IWUSR 00200 owner has write permission // S_IXUSR 00100 owner has execute permission // // S_IRWXG 00070 mask for group permissions // S_IRGRP 00040 group has read permission // S_IWGRP 00020 group has write permission // S_IXGRP 00010 group has execute permission // // S_IRWXO 00007 mask for permissions for others // (not in group) // S_IROTH 00004 others have read permission // S_IWOTH 00002 others have write permission // S_IXOTH 00001 others have execute permission // </pre> // // For the sake of our own sanity, we're going to assume that no-one is using symlinks, // but we'll check and throw if they are. // // Before we do anything, we should check the header ID. Pfft! // // Having jumped through all these hoops, it turns out that InfoZIP's "unzip" store the // values in the external file attributes of a zip entry (found in the zip's central // directory) assuming that the OS creating the zip was one of an enormous list that // includes UNIX but not Windows, it first searches for the extra fields, and if not found // falls through to a code path that supports MS-DOS and which stores the UNIX file // attributes in the upper 16 bits of the external attributes field. // // We'll support neither approach fully, but we encode whether this file was executable // via storing 0100 in the fields that are typically used by zip implementations to store // POSIX permissions. If we find it was executable, use the platform independent java // interface to make this unpacked file executable. Set<PosixFilePermission> permissions = MorePosixFilePermissions .fromMode(entry.getExternalAttributes() >> 16); if (permissions.contains(PosixFilePermission.OWNER_EXECUTE) && file.getCanonicalFile().exists()) { MostFiles.makeExecutable(filePath); } }
From source file:org.geogig.web.functional.MultiPartBuilder.java
public void addFile(final File file, final String fieldName) throws IOException { Writer writer = new OutputStreamWriter(out, characterSet); String fileName = file.getName(); writer.append("--")// .append(boundary)// .append(CRLF);/* w w w. j a va2 s. c o m*/ writer.append("Content-Disposition: form-data; name=\"")// .append(fieldName)// .append("\"; filename=\"")// .append(fileName)// .append("\"")// .append(CRLF); writer.append("Content-Type: application/octect-stream")// .append(CRLF); writer.append("Content-Transfer-Encoding: binary")// .append(CRLF); writer.append(CRLF); writer.flush(); try (FileInputStream in = new FileInputStream(file)) { ByteStreams.copy(in, out); } out.flush(); writer.append(CRLF); writer.flush(); }
From source file:org.geoserver.wps.ppio.WFSPPIO.java
@Override public Object decode(InputStream input) throws Exception { Parser p = getParser(configuration); byte[] streamBytes = null; if (LOGGER.isLoggable(Level.FINEST)) { //allow WFS result to be logged for debugging purposes //WFS result can be large, so use only for debugging ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteStreams.copy(input, outputStream); streamBytes = outputStream.toByteArray(); input = new ByteArrayInputStream(streamBytes); }//from w w w. j a va 2 s.com Object result = p.parse(input); if (result instanceof FeatureCollectionType) { FeatureCollectionType fct = (FeatureCollectionType) result; return decode(fct); } else { if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.log(Level.FINEST, "Decoding the following WFS response did not result in an object of type FeatureCollectionType: \n" + new String(streamBytes)); } throw new IllegalArgumentException("Decoded WFS result is not a feature collection, got a: " + result); } }
From source file:org.obiba.opal.core.runtime.jdbc.DefaultJdbcDriverRegistry.java
@Override public void addDriver(String filename, InputStream jarFile) throws IOException { try (FileOutputStream fos = new FileOutputStream(new File(System.getenv("OPAL_HOME") + "/ext", filename))) { ByteStreams.copy(jarFile, fos); } finally {/*from w w w . jav a 2s . c o m*/ try { jarFile.close(); } catch (IOException ignored) { } } }
From source file:org.apache.brooklyn.util.http.HttpToolResponse.java
public HttpToolResponse(HttpResponse response, long startTime) { this.response = response; this.startTime = startTime; try {/*from ww w . ja v a 2 s . c om*/ ByteArrayOutputStream out = new ByteArrayOutputStream(); HttpEntity entity = response.getEntity(); if (entity != null) { entity.getContentLength(); durationMillisOfFirstResponse = Duration.sinceUtc(startTime).toMilliseconds(); ByteStreams.copy(entity.getContent(), out); content = out.toByteArray(); entity.getContentLength(); } else { durationMillisOfFirstResponse = Duration.sinceUtc(startTime).toMilliseconds(); content = new byte[0]; } durationMillisOfFullContent = Duration.sinceUtc(startTime).toMilliseconds(); if (log.isTraceEnabled()) log.trace("HttpPollValue latency " + Time.makeTimeStringRounded(durationMillisOfFirstResponse) + " / " + Time.makeTimeStringRounded(durationMillisOfFullContent) + ", content size " + content.length); } catch (IOException e) { throw Throwables.propagate(e); } }