List of usage examples for com.google.common.io ByteStreams copy
public static long copy(ReadableByteChannel from, WritableByteChannel to) throws IOException
From source file:controllers.UploadController.java
/** * //from w ww . j av a 2s . co m * This upload method expects a file and simply displays the file in the * multipart upload again to the user (in the correct mime encoding). * * @param context * @return * @throws Exception */ public Result uploadFinish(Context context) throws Exception { // we are using a renderable inner class to stream the input again to // the user Renderable renderable = new Renderable() { @Override public void render(Context context, Result result) { try { // make sure the context really is a multipart context... if (context.isMultipart()) { // This is the iterator we can use to iterate over the // contents // of the request. FileItemIterator fileItemIterator = context.getFileItemIterator(); while (fileItemIterator.hasNext()) { FileItemStream item = fileItemIterator.next(); String name = item.getFieldName(); InputStream stream = item.openStream(); String contentType = item.getContentType(); if (contentType != null) { result.contentType(contentType); } else { contentType = mimeTypes.getMimeType(name); } ResponseStreams responseStreams = context.finalizeHeaders(result); if (item.isFormField()) { System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected."); } else { System.out.println( "File field " + name + " with file name " + item.getName() + " detected."); // Process the input stream ByteStreams.copy(stream, responseStreams.getOutputStream()); } } } } catch (IOException | FileUploadException exception) { throw new InternalServerErrorException(exception); } } }; return new Result(200).render(renderable); }
From source file:org.onosproject.yang.impl.YangLiveCompilerManager.java
private void extractYangFile(File dir, InputStream stream) throws IOException { ByteStreams.copy(stream, new FileOutputStream(new File(dir, "model.yang"))); }
From source file:org.ow2.proactive_grid_cloud_portal.dataspace.util.VFSZipper.java
public static void zip(FileObject file, OutputStream out) throws IOException { Closer closer = Closer.create();//ww w . j av a 2 s. com try { closer.register(out); InputStream in = file.getContent().getInputStream(); closer.register(in); ByteStreams.copy(in, out); } catch (IOException ioe) { throw closer.rethrow(ioe); } finally { closer.close(); } }
From source file:co.cask.tigon.flow.DeployClient.java
private static void expandJar(File jarPath, File unpackDir) throws Exception { JarFile jar = new JarFile(jarPath); Enumeration enumEntries = jar.entries(); while (enumEntries.hasMoreElements()) { JarEntry file = (JarEntry) enumEntries.nextElement(); File f = new File(unpackDir + File.separator + file.getName()); if (file.isDirectory()) { f.mkdirs();/* w ww . j a v a2s . c o m*/ continue; } else { f.getParentFile().mkdirs(); } InputStream is = jar.getInputStream(file); try { ByteStreams.copy(is, Files.newOutputStreamSupplier(f)); } finally { Closeables.closeQuietly(is); } } }
From source file:alluxio.proxy.StreamsRestServiceHandler.java
/** * @summary writes to a stream/* w w w.ja v a 2 s. c o m*/ * @param id the stream id * @param is the input stream * @return the response object */ @POST @Path(ID_PARAM + WRITE) @ReturnType("java.lang.Long") @Consumes(MediaType.APPLICATION_OCTET_STREAM) public Response write(@PathParam("id") final Integer id, final InputStream is) { return RestUtils.call(new RestUtils.RestCallable<Long>() { @Override public Long call() throws Exception { FileOutStream os = mStreamCache.getOutStream(id); if (os != null) { return ByteStreams.copy(is, os); } throw new IllegalArgumentException("stream does not exist"); } }); }
From source file:org.gaul.s3proxy.NullBlobStore.java
@Override public String putBlob(String containerName, Blob blob, PutOptions options) { long length;//from w w w . ja va2 s . co m try (InputStream is = blob.getPayload().openStream()) { length = ByteStreams.copy(is, ByteStreams.nullOutputStream()); } catch (IOException ioe) { throw new RuntimeException(ioe); } byte[] array = Longs.toByteArray(length); ByteSourcePayload payload = new ByteSourcePayload(ByteSource.wrap(array)); payload.setContentMetadata(blob.getPayload().getContentMetadata()); payload.getContentMetadata().setContentLength((long) array.length); payload.getContentMetadata().setContentMD5((HashCode) null); blob.setPayload(payload); return super.putBlob(containerName, blob, options); }
From source file:com.github.dirkraft.jash.ShExecutableJarBundler.java
void concatJashAndJar() throws IOException { Preconditions.checkState(!_targetBinary.exists(), "Expected nothing to be here: %s", _targetBinary.getAbsolutePath()); Files.copy(_renderedScriptFile.toPath(), _targetBinary.toPath()); try (FileInputStream jarInputStream = new FileInputStream(jarFile); FileOutputStream appendJarStream = new FileOutputStream(_targetBinary, true)) { ByteStreams.copy(jarInputStream, appendJarStream); appendJarStream.flush();/*from w w w .j a va 2 s .c o m*/ } JashIO.outf("Concatenated jash script and jar contents into %s%n", _targetBinary); }
From source file:google.registry.tools.EscrowDepositEncryptor.java
/** Creates a {@code .ryde} and {@code .sig} file, provided an XML deposit file. */ void encrypt(String tld, Path xmlFile, Path outdir) throws IOException, PGPException, XmlException { try (InputStream xmlFileInput = Files.newInputStream(xmlFile); BufferedInputStream xmlInput = new BufferedInputStream(xmlFileInput, PEEK_BUFFER_SIZE)) { DateTime watermark = RdeUtil.peekWatermark(xmlInput); String name = RdeNamingUtils.makeRydeFilename(tld, watermark, FULL, 1, 0); Path rydePath = outdir.resolve(name + ".ryde"); Path sigPath = outdir.resolve(name + ".sig"); Path pubPath = outdir.resolve(tld + ".pub"); PGPKeyPair signingKey = rdeSigningKey; try (OutputStream rydeOutput = Files.newOutputStream(rydePath); RydePgpSigningOutputStream signLayer = pgpSigningFactory.create(rydeOutput, signingKey)) { try (RydePgpEncryptionOutputStream encryptLayer = pgpEncryptionFactory.create(signLayer, rdeReceiverKey);/* w ww. j av a 2 s. c o m*/ RydePgpCompressionOutputStream compressLayer = pgpCompressionFactory.create(encryptLayer); RydePgpFileOutputStream fileLayer = pgpFileFactory.create(compressLayer, watermark, name + ".tar"); RydeTarOutputStream tarLayer = tarFactory.create(fileLayer, Files.size(xmlFile), watermark, name + ".xml")) { ByteStreams.copy(xmlInput, tarLayer); } Files.write(sigPath, signLayer.getSignature()); try (OutputStream pubOutput = Files.newOutputStream(pubPath); ArmoredOutputStream ascOutput = new ArmoredOutputStream(pubOutput)) { signingKey.getPublicKey().encode(ascOutput); } } } }
From source file:de.dentrassi.pm.storage.service.jpa.blob.DatabaseBlobStoreProcessor.java
protected void writeBlobAsBlob(final EntityManager em, final InputStream stream, final Function<Long, String> idFunction) throws SQLException, IOException { final Connection c = em.unwrap(Connection.class); long size;//from www . ja va 2 s.c om final Blob blob = c.createBlob(); try { try (OutputStream s = blob.setBinaryStream(1)) { size = ByteStreams.copy(stream, s); } // we can only set it now, since we only have the size final String id = idFunction.apply(size); try (PreparedStatement ps = c.prepareStatement("update ARTIFACTS set data=? where id=?")) { ps.setBlob(1, blob); ps.setString(2, id); ps.executeUpdate(); } } finally { blob.free(); } }
From source file:org.kmworks.liferay.rpc.client.RPCExtensionsHttp.java
/** Download a FileEntry's file content to a file on the local file system. * // www . j a va 2 s . c om * @param token The authenticated user token * @param fileEntryId * @param version * @param localFile * @return * @throws PortalException * @throws SystemException */ public static long downloadFile(AuthToken token, long fileEntryId, String version, File localFile) throws PortalException, SystemException { long byteCount; try { try (InputStream from = new ByteArrayInputStream(getFileAsBytes(token, fileEntryId, version)); OutputStream to = new FileOutputStream(localFile)) { byteCount = ByteStreams.copy(from, to); } } catch (IOException ioe) { _log.error(ioe, ioe); throw new SystemException(ioe); } return byteCount; }