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.orange.clara.cloud.servicedbdumper.filer.S3Filer.java
@Override public void retrieve(OutputStream outputStream, String filename) throws IOException { BlobStore blobStore = this.blobStoreContext.getBlobStore(); Blob blob = blobStore.getBlob(this.bucketName, filename); InputStream inputStream = blob.getPayload().openStream(); ByteStreams.copy(inputStream, outputStream); outputStream.flush();//from w w w. ja v a2s . c o m inputStream.close(); outputStream.close(); }
From source file:de.nx42.maps4cim.util.Compression.java
/** * Reads the first file entry in a zip file and writes it in uncompressed * form to the desired file.//from ww w.j av a2 s .co m * @param zipFile the zip file to read from * @param dest the file to write the first zip file entry to * @return same as destination * @throws IOException if there is an error accessing the zip file or the * destination file */ public static File readFirstZipEntry(File zipFile, File dest) throws IOException { // open zip and get first entry ZipFile zf = new ZipFile(zipFile); Enumeration<ZipArchiveEntry> entries = zf.getEntries(); ZipArchiveEntry entry = entries.nextElement(); // write to file InputStream in = zf.getInputStream(entry); OutputStream out = new FileOutputStream(dest); ByteStreams.copy(in, out); // close all streams and return the new file in.close(); out.close(); zf.close(); return dest; }
From source file:com.metamx.druid.index.v1.MetricHolder.java
public static void writeComplexMetric(OutputSupplier<? extends OutputStream> outSupplier, String name, String typeName, GenericIndexedWriter column) throws IOException { OutputStream out = null;/* w w w. j a va2 s .c om*/ InputStream in = null; try { out = outSupplier.getOutput(); out.write(version); serializerUtils.writeString(out, name); serializerUtils.writeString(out, typeName); final InputSupplier<InputStream> supplier = column.combineStreams(); in = supplier.getInput(); ByteStreams.copy(in, out); } finally { Closeables.closeQuietly(out); Closeables.closeQuietly(in); } }
From source file:ofandroidclipse.ofReleaseDir.OFAndroidProjectGenerator.java
/** * Copy generator ant file./*from w ww .ja v a 2 s.c om*/ * * @return the file * @throws IOException Signals that an I/O exception has occurred. */ public File copyGeneratorANTFile() throws IOException { URL url = new URL("platform:/plugin/OFAndroidClipse/ofAndroidclipseProjectGenerator.xml"); InputStream is = url.openConnection().getInputStream(); File tmpFile = File.createTempFile("ofAndroidclipse", ".xml"); ByteStreams.copy(is, new FileOutputStream(tmpFile)); return tmpFile; }
From source file:com.nirima.jenkins.webdav.impl.methods.Get.java
@Override protected void writeContent(IDavFile fileItem) throws IOException { InputStream is = null;//from w w w .j av a2s . c o m try { logger.trace("GET {}", fileItem); is = fileItem.getContent(); OutputStream os = this.getResponse().getOutputStream(); long start = System.currentTimeMillis(); long bytes = ByteStreams.copy(is, os); os.flush(); long duration = System.currentTimeMillis() - start; if (duration == 0) duration = 1; // round up. logger.info("Sent {} : {} bytes in {} ms ({}kB/sec", fileItem, bytes, duration, (bytes / duration)); } catch (IOException ex) { logger.error("Error trying to GET item {} ", fileItem); logger.error("Error: ", ex); throw ex; } finally { if (is != null) is.close(); } }
From source file:eu.esdihumboldt.util.io.IOUtils.java
/** * Extract a ZIP archive./* www . ja v a 2 s . c o m*/ * * @param baseDir the base directory to extract to * @param in the input stream of the ZIP archive, which is closed after * extraction * @return the collection of extracted files * @throws IOException if an error occurs */ public static Collection<File> extract(File baseDir, InputStream in) throws IOException { final Path basePath = baseDir.getAbsoluteFile().toPath(); Collection<File> collect = new ArrayList<>(); try (ZipInputStream zis = new ZipInputStream(in)) { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { if (!entry.isDirectory()) { final Path file = basePath.resolve(entry.getName()).normalize(); if (!file.startsWith(basePath)) { // not inside target directory log.warn("Skipped extraction of file {} as it is not in the target directory", file); continue; } File fileObj = file.toFile(); Files.createParentDirs(fileObj); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(fileObj))) { ByteStreams.copy(zis, out); } collect.add(fileObj); } } } return collect; }
From source file:com.linecorp.armeria.internal.grpc.GrpcTestUtil.java
public static byte[] compressedFrame(ByteBuf uncompressed) throws Exception { ByteBuf compressed = Unpooled.buffer(); try (ByteBufInputStream is = new ByteBufInputStream(uncompressed, true); GZIPOutputStream os = new GZIPOutputStream(new ByteBufOutputStream(compressed))) { ByteStreams.copy(is, os); }/*ww w. j av a 2 s.c o m*/ ByteBuf buf = Unpooled.buffer(); buf.writeByte(1); buf.writeInt(compressed.readableBytes()); buf.writeBytes(compressed); compressed.release(); byte[] result = ByteBufUtil.getBytes(buf); buf.release(); return result; }
From source file:com.google.devtools.build.lib.remote.blobstore.OnDiskBlobStore.java
@Override public void put(String key, long length, InputStream in) throws IOException { // Write a temporary file first, and then rename, to avoid data corruption in case of a crash. Path temp = toPath(UUID.randomUUID().toString()); try (OutputStream out = temp.getOutputStream()) { ByteStreams.copy(in, out); }// www . j a v a2 s .co m // TODO(ulfjack): Fsync temp here before we rename it to avoid data loss in the case of machine // crashes (the OS may reorder the writes and the rename). Path f = toPath(key); temp.renameTo(f); }
From source file:net.minecraftforge.gradle.patcher.TaskCompressLZMA.java
@TaskAction public void doTask() throws IOException { final BufferedInputStream in = new BufferedInputStream(new FileInputStream(getInputFile())); final OutputStream out = new LzmaOutputStream.Builder(new FileOutputStream(getOutputFile())) .useEndMarkerMode(true).build(); ByteStreams.copy(in, out); in.close();// w ww .j ava 2 s .co m out.close(); }
From source file:com.metamx.druid.utils.CompressionUtils.java
public static long zip(File directory, OutputStream out) throws IOException { if (!directory.isDirectory()) { throw new IOException(String.format("directory[%s] is not a directory", directory)); }//from w w w. j a v a2 s .c o m long totalSize = 0; ZipOutputStream zipOut = null; try { zipOut = new ZipOutputStream(out); File[] files = directory.listFiles(); for (File file : files) { log.info("Adding file[%s] with size[%,d]. Total size so far[%,d]", file, file.length(), totalSize); if (file.length() >= Integer.MAX_VALUE) { zipOut.finish(); throw new IOException(String.format("file[%s] too large [%,d]", file, file.length())); } zipOut.putNextEntry(new ZipEntry(file.getName())); totalSize += ByteStreams.copy(Files.newInputStreamSupplier(file), zipOut); } zipOut.closeEntry(); } finally { if (zipOut != null) { zipOut.finish(); } } return totalSize; }