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.webpagebytes.cms.engine.FileContentBuilder.java
public void writeFileContent(WPBFile wbFile, OutputStream os) throws WPBException { InputStream is = getFileContent(wbFile); try {/* w w w.j ava2 s. c o m*/ IOUtils.copy(is, os); } catch (IOException e) { throw new WPBIOException(e.getMessage(), e); } finally { IOUtils.closeQuietly(is); } }
From source file:com.github.trask.sandbox.sshj.SshSession.java
public String exec(String command) throws IOException { logger.debug("exec(\"{}\")", command); Session session = client.startSession(); try {//from w w w .j a v a 2 s . c o m session.allocateDefaultPTY(); Command cmd = session.exec(command); ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(cmd.getInputStream(), new TeeOutputStream(out, new LoggingOutputStream())); return new String(out.toByteArray(), Charsets.UTF_8.name()); } finally { session.close(); } }
From source file:de.burlov.bouncycastle.io.test.EncryptOutputStreamTest.java
@Test public void testEncryptDecrypt() throws IOException { BlockCipher cipher = new SerpentEngine(); Random rnd = new Random(); byte[] key = new byte[256 / 8]; rnd.nextBytes(key);//from w w w . j a v a 2 s . c om byte[] iv = new byte[cipher.getBlockSize()]; rnd.nextBytes(iv); byte[] data = new byte[1230000]; new Random().nextBytes(data); ByteArrayOutputStream bout = new ByteArrayOutputStream(); CryptOutputStream eout = new CryptOutputStream(bout, cipher, key); eout.write(data); eout.close(); byte[] eData = bout.toByteArray(); // eData[1000] = (byte) (eData[1000] & 0x88); ByteArrayInputStream bin = new ByteArrayInputStream(eData); CryptInputStream din = new CryptInputStream(bin, cipher, key); bout = new ByteArrayOutputStream(); IOUtils.copy(din, bout); eData = bout.toByteArray(); Assert.assertTrue(Arrays.areEqual(data, eData)); }
From source file:baggage.hypertoolkit.views.InputStreamResource.java
@Override public void render(OutputStream outputStream) throws IOException { IOUtils.copy(inputStream, outputStream); outputStream.close(); }
From source file:com.qwazr.extractor.ParserAbstract.java
protected static Path createTempFile(final InputStream inputStream, final String extension) throws IOException { final Path tempFile = Files.createTempFile("oss-extractor", extension); try (final OutputStream out = Files.newOutputStream(tempFile); final BufferedOutputStream bOut = new BufferedOutputStream(out);) { IOUtils.copy(inputStream, bOut); bOut.close();/*from w w w . jav a 2 s .com*/ return tempFile; } }
From source file:com.redhat.red.build.koji.http.httpclient4.ObjectResponseHandler.java
@Override public T handleResponse(final HttpResponse resp) throws IOException { Logger logger = LoggerFactory.getLogger(getClass()); final StatusLine status = resp.getStatusLine(); logger.debug(status.toString());//from w ww . j a va 2s . c o m if (status.getStatusCode() > 199 && status.getStatusCode() < 203) { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(resp.getEntity().getContent(), baos); if (logger.isTraceEnabled()) { File recording = null; FileOutputStream stream = null; try { recording = File.createTempFile("xml-rpc.response.", ".xml"); stream = new FileOutputStream(recording); stream.write(baos.toByteArray()); } catch (final IOException e) { logger.debug("Failed to record xml-rpc response to file.", e); // this is an auxilliary function. ignore errors. } finally { IOUtils.closeQuietly(stream); logger.info("\n\n\nRecorded response to: {}\n\n\n", recording); } } try { logger.trace("Got response: \n\n{}", new Object() { @Override public String toString() { try { return new String(baos.toByteArray(), "UTF-8"); } catch (final UnsupportedEncodingException e) { return new String(baos.toByteArray()); } } }); return new RWXMapper().parse(new ByteArrayInputStream(baos.toByteArray()), responseType); } catch (final XmlRpcException e) { error = e; return null; } } else { error = new XmlRpcException("Invalid response status: '" + status + "'."); return null; } }
From source file:com.splout.db.common.CompressorUtil.java
public static void uncompress(File file, File dest) throws IOException { ZipFile zipFile = new ZipFile(file); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File entryDestination = new File(dest, entry.getName()); entryDestination.getParentFile().mkdirs(); InputStream in = zipFile.getInputStream(entry); OutputStream out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); in.close();/* w ww .j a va2s. c om*/ out.close(); } }
From source file:eu.eexcess.sourceselection.redde.indexer.trec.topic.TrecTopicTokenizer.java
/** * Tokenize trec-topic xml file respecting missing closing xml tags. * @param input//from w w w. jav a 2 s . com * @return * @throws IOException */ public List<Topic> tokenize(InputStream input) throws IOException { Writer writer = new StringWriter(); IOUtils.copy(input, writer); String data = writer.toString(); Matcher startTopicMatcher = getStartTopicTagMatcher(data); Matcher endTopicTagMatcher = getEndTopicTagMatcher(data); int processedChars = 0; while (startTopicMatcher.find(processedChars)) { if (endTopicTagMatcher.find(startTopicMatcher.end())) { String topicData = data.substring(processedChars, endTopicTagMatcher.start()); Matcher startTagMatcher = getStartGenericTagMatcher(topicData); while (startTagMatcher.find()) { Matcher endTagMatcher = getStartGenericTagMatcher(topicData); if (endTagMatcher.find(startTagMatcher.end())) { assignValueToField(startTagMatcher.group(), topicData.substring(startTagMatcher.end(), endTagMatcher.start())); } else { // reached last tag in topic assignValueToField(startTagMatcher.group(), topicData.substring(startTagMatcher.end(), topicData.length())); } } } else { throw new IllegalArgumentException("expected topic end tag"); } processedChars = endTopicTagMatcher.end(); topics.add(currentTopic); currentTopic = new Topic(); } return topics; }
From source file:com.streamsets.datacollector.restapi.ZipEdgeArchiveBuilder.java
@Override public void finish() throws IOException { try (ZipArchiveOutputStream zipArchiveOutput = new ZipArchiveOutputStream(outputStream); ZipArchiveInputStream zipArchiveInput = new ZipArchiveInputStream( new FileInputStream(edgeArchive))) { ZipArchiveEntry entry = zipArchiveInput.getNextZipEntry(); while (entry != null) { zipArchiveOutput.putArchiveEntry(entry); IOUtils.copy(zipArchiveInput, zipArchiveOutput); zipArchiveOutput.closeArchiveEntry(); entry = zipArchiveInput.getNextZipEntry(); }//from w ww .j a va2 s .c om for (PipelineConfigurationJson pipelineConfiguration : pipelineConfigurationList) { addArchiveEntry(zipArchiveOutput, pipelineConfiguration, pipelineConfiguration.getPipelineId(), PIPELINE_JSON_FILE); addArchiveEntry(zipArchiveOutput, pipelineConfiguration.getInfo(), pipelineConfiguration.getPipelineId(), PIPELINE_INFO_FILE); } zipArchiveOutput.finish(); } }
From source file:net.gbmb.collector.WrappingApplicationRunner.java
@RequestMapping(value = "/test/final/{cid}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) public byte[] getFinal(@PathVariable("cid") String cid) throws IOException { // ensure there is no queued content pushListenerWorker.wake();/*from w ww .ja v a 2s . co m*/ // retrieve content try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); IOUtils.copy(finalStorage.get(cid), bos); IOUtils.closeQuietly(bos); return bos.toByteArray(); } catch (IOException e) { e.printStackTrace(); throw e; } catch (RuntimeException e) { e.printStackTrace(); throw e; } }