List of usage examples for org.apache.commons.io IOUtils copy
public static void copy(Reader input, OutputStream output) throws IOException
Reader
to bytes on an OutputStream
using the default character encoding of the platform, and calling flush. From source file:com.google.mr4c.content.AbstractContentFactory.java
public void readContent(URI uri, Writer writer) throws IOException { Reader reader = readContentAsReader(uri); try {// w w w .j a va 2s. com IOUtils.copy(reader, writer); } finally { reader.close(); } }
From source file:com.datatorrent.lib.io.HttpPostOutputOperatorTest.java
@Test public void testHttpOutputNode() throws Exception { final List<String> receivedMessages = new ArrayList<String>(); Handler handler = new AbstractHandler() { @Override//ww w . jav a 2s. co m @Consumes({ MediaType.APPLICATION_JSON }) public void handle(String string, Request rq, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(request.getInputStream(), bos); receivedMessages.add(new String(bos.toByteArray())); response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println("<h1>Thanks</h1>"); ((Request) request).setHandled(true); receivedMessage = true; } }; Server server = new Server(0); server.setHandler(handler); server.start(); String url = "http://localhost:" + server.getConnectors()[0].getLocalPort() + "/somecontext"; HttpPostOutputOperator<Object> node = new HttpPostOutputOperator<Object>(); node.setUrl(url); node.setup(null); Map<String, String> data = new HashMap<String, String>(); data.put("somekey", "somevalue"); node.input.process(data); // Wait till the message is received or a maximum timeout elapses int timeoutMillis = 10000; while (!receivedMessage && timeoutMillis > 0) { timeoutMillis -= 20; Thread.sleep(20); } Assert.assertEquals("number requests", 1, receivedMessages.size()); JSONObject json = new JSONObject(data); Assert.assertTrue("request body " + receivedMessages.get(0), receivedMessages.get(0).contains(json.toString())); receivedMessages.clear(); String stringData = "stringData"; node.input.process(stringData); Assert.assertEquals("number requests", 1, receivedMessages.size()); Assert.assertEquals("request body " + receivedMessages.get(0), stringData, receivedMessages.get(0)); node.teardown(); server.stop(); }
From source file:de.cosmocode.palava.store.CacheServiceStore.java
@Override public void create(InputStream stream, String identifier) throws IOException { Preconditions.checkNotNull(stream, "Stream"); Preconditions.checkState(cache.read(identifier) == null, "Byte array for %s already present", identifier); final ByteArrayOutputStream output = new ByteArrayOutputStream(); IOUtils.copy(stream, output); final byte[] data = output.toByteArray(); LOG.trace("Storing {} to {}", stream, identifier); cache.store(identifier, data);/* ww w. j ava 2 s . c o m*/ }
From source file:au.org.ala.biocache.util.AlaFileUtils.java
/** * Creates a zip entry for the path specified with a name built from the base passed in and the file/directory * name. If the path is a directory, a recursive call is made such that the full directory is added to the zip. * * @param zOut The zip file's output stream * @param path The filesystem path of the file/directory being added * @param base The base prefix to for the name of the zip file entry * * @throws IOException If anything goes wrong */// w w w . j av a 2s. c o m private static void addFileToZip(ZipArchiveOutputStream zOut, String path, String base) throws IOException { File f = new File(path); String entryName = base + f.getName(); ZipArchiveEntry zipEntry = new ZipArchiveEntry(f, entryName); zOut.putArchiveEntry(zipEntry); if (f.isFile()) { FileInputStream fInputStream = null; try { fInputStream = new FileInputStream(f); IOUtils.copy(fInputStream, zOut); zOut.closeArchiveEntry(); } finally { IOUtils.closeQuietly(fInputStream); } } else { zOut.closeArchiveEntry(); File[] children = f.listFiles(); if (children != null) { for (File child : children) { addFileToZip(zOut, child.getAbsolutePath(), entryName + "/"); } } } }
From source file:cd.education.data.collector.android.utilities.ZipUtils.java
private static File doExtractInTheSameFolder(File zipFile, ZipInputStream zipInputStream, ZipEntry zipEntry) throws IOException { File targetFile;//ww w.java 2 s . com String fileName = zipEntry.getName(); Log.w(t, "Found zipEntry with name: " + fileName); if (fileName.contains("/") || fileName.contains("\\")) { // that means that this is a directory of a file inside a directory, so ignore it Log.w(t, "Ignored: " + fileName); return null; } // extract the new file targetFile = new File(zipFile.getParentFile(), fileName); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(targetFile); IOUtils.copy(zipInputStream, fileOutputStream); } finally { IOUtils.closeQuietly(fileOutputStream); } Log.w(t, "Extracted file \"" + fileName + "\" out of " + zipFile.getName()); return targetFile; }
From source file:com.doitnext.swing.widgets.json.JSONJTreeNodeTest.java
@Test public void pushTest() throws Exception { InputStream resource = null;/*from w ww. j a va2s.c om*/ StringWriter sw = null; try { resource = this.getClass().getClassLoader().getResourceAsStream(fileName); sw = new StringWriter(); IOUtils.copy(resource, sw); String json = sw.toString(); sw.close(); sw = null; resource.close(); resource = null; JsonElement rootElt = new JsonParser().parse(json); JSONJTreeNode root = new JSONJTreeNode(null, -1, rootElt); JsonElement converted = root.asJsonElement(); String expected = rootElt.toString(); String actual = converted.toString(); Assert.assertEquals("Json does not match", expected, actual); if (expectedError != null) throw new IllegalArgumentException( "We didnt' get an " + expectedError.getClass().getName() + " exception as expected.", expectedError); } catch (Exception e) { if (!e.getClass().isInstance(expectedError)) throw e; } finally { if (resource != null) resource.close(); if (sw != null) sw.close(); } }
From source file:com.netscape.certsrv.client.PKIRESTProvider.java
@Override public StreamingOutput readFrom(Class<StreamingOutput> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { final File file = File.createTempFile("PKIRESTProvider-", ".tmp"); file.deleteOnExit();/*from ww w . j av a 2 s . c o m*/ FileOutputStream out = new FileOutputStream(file); IOUtils.copy(entityStream, out); return new StreamingOutput() { @Override public void write(OutputStream out) throws IOException, WebApplicationException { FileInputStream in = new FileInputStream(file); IOUtils.copy(in, out); } public void finalize() { file.delete(); } }; }
From source file:com.ctriposs.r2.filter.compression.Bzip2Compressor.java
@Override public byte[] deflate(InputStream data) throws CompressionException { ByteArrayOutputStream out = new ByteArrayOutputStream(); BZip2CompressorOutputStream compressor = null; try {//from ww w .j a v a2 s . c om out = new ByteArrayOutputStream(); compressor = new BZip2CompressorOutputStream(out); IOUtils.copy(data, compressor); compressor.finish(); } catch (IOException e) { throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e); } finally { if (compressor != null) { IOUtils.closeQuietly(compressor); } } return out.toByteArray(); }