List of usage examples for org.apache.commons.io IOUtils write
public static void write(StringBuffer data, OutputStream output) throws IOException
StringBuffer
to bytes on an OutputStream
using the default character encoding of the platform. From source file:com.joshlong.esb.springintegration.modules.nativefs.TestRecievingUsingNativeFsEventing.java
void write(String fileName, String msg) { try {/*from ww w . ja v a 2s . com*/ File nFile = new File(fsfile, fileName); OutputStream outputStream = new FileOutputStream(nFile); IOUtils.write(msg, outputStream); IOUtils.closeQuietly(outputStream); } catch (Throwable t) { // don't care } }
From source file:ductive.console.jline.NonInteractiveTerminal.java
@Override public void errorln(String value) throws IOException { IOUtils.write(new Ansi().bold().fg(Color.RED).a(value).reset().a(NL).toString(), out); }
From source file:com.logsniffer.model.file.FileLogTest.java
@Test public void testReadArray() throws IOException { File openFile = File.createTempFile("test", "txt"); openFile.deleteOnExit();//w ww .j av a 2s . c o m FileOutputStream out = new FileOutputStream(openFile); FileLog flog = new FileLog(openFile); IOUtils.write("line1\n", out); out.flush(); ByteLogInputStream lis = new DirectFileLogAccess(flog).getInputStream(null); byte[] buffer = new byte[1024]; // Log instantiated before data is written assertEquals(-1, lis.read(buffer)); flog = new FileLog(openFile); lis = new DirectFileLogAccess(flog).getInputStream(null); assertEquals(6, lis.read(buffer)); assertEquals(-1, lis.read(buffer)); // Write more, but lis doesn't see the new data due to size limitation IOUtils.write("l2\n", out); out.flush(); assertEquals(-1, lis.read(buffer)); LogPointer pointer = lis.getPointer(); // Reopen input stream flog = new FileLog(openFile); lis = new DirectFileLogAccess(flog).getInputStream(pointer); assertEquals(3, lis.read(buffer, 0, 3)); assertEquals(-1, lis.read(buffer, 0, 1)); assertEquals('l', buffer[0]); assertEquals('2', buffer[1]); assertEquals('\n', buffer[2]); }
From source file:com.smartitengineering.util.opensearch.io.impl.dom.OpenSearchDescriptorWriter.java
public void write() throws IOException { try {// ww w. j ava2 s .c o m final Document document; document = buildDoc(); IOUtils.write(document.toXML(), sink); } finally { if (closeOnFinish) { try { sink.close(); } catch (Exception ex) { ex.printStackTrace(); } } } }
From source file:de.knurt.fam.core.model.config.CronjobActionController.java
private ModelAndView pseudoResponse(HttpServletResponse rs) { PrintWriter pw = null;/* w w w .java2 s. c om*/ try { rs.setContentType("text/plain;charset=UTF-8"); pw = rs.getWriter(); IOUtils.write("done", pw); } catch (IOException ex) { FamLog.exception(ex, 200911182012l); } finally { IOUtils.closeQuietly(pw); } return null; }
From source file:com.smartsheet.api.HttpTestServer.java
/** * Creates an {@link AbstractHandler handler} returning an arbitrary String as a response. * * @return never <code>null</code>. *//*ww w .ja v a2 s . c om*/ public Handler getMockHandler() { Handler handler = new AbstractHandler() { //@Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { setRequestBody(IOUtils.toString(baseRequest.getInputStream())); response.setStatus(getStatus()); response.setContentType(getContentType()); byte[] body = getResponseBody(); response.setContentLength(body.length); IOUtils.write(body, response.getOutputStream()); baseRequest.setHandled(true); } }; return handler; }
From source file:ch.cyberduck.core.shared.DefaultUploadFeatureTest.java
@Test public void testTransferAppend() throws Exception { final Host host = new Host(new SFTPProtocol(), "test.cyberduck.ch", new Credentials(System.getProperties().getProperty("sftp.user"), System.getProperties().getProperty("sftp.password"))); final SFTPSession session = new SFTPSession(host); session.open(new DisabledHostKeyCallback()); session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback()); final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()); final byte[] content = new byte[32770]; new Random().nextBytes(content); final OutputStream out = local.getOutputStream(false); IOUtils.write(content, out); out.close();/*from ww w . j a v a 2 s . co m*/ final Path test = new Path(new SFTPHomeDirectoryService(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file)); { final TransferStatus status = new TransferStatus().length(content.length / 2); new DefaultUploadFeature<Void>(new SFTPWriteFeature(session)).upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), status, new DisabledConnectionCallback()); } { final TransferStatus status = new TransferStatus().length(content.length / 2).skip(content.length / 2) .append(true); new DefaultUploadFeature<Void>(new SFTPWriteFeature(session)).upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), status, new DisabledConnectionCallback()); } final byte[] buffer = new byte[content.length]; final Read read = session.getFeature(Read.class); final InputStream in = read.read(test, new TransferStatus().length(content.length), new DisabledConnectionCallback()); IOUtils.readFully(in, buffer); in.close(); assertArrayEquals(content, buffer); final Delete delete = session.getFeature(Delete.class); delete.delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); local.delete(); session.close(); }
From source file:com.moz.fiji.mapreduce.kvstore.lib.TestAvroRecordKeyValueStore.java
private File createTempBinaryFile(final byte[] bytes) throws IOException { final File file = File.createTempFile("temp-", ".txt", getLocalTempDir()); final OutputStream fos = new FileOutputStream(file); try {/*www .j a v a 2s .co m*/ IOUtils.write(bytes, fos); } finally { fos.close(); } return file; }
From source file:net.daporkchop.porkselfbot.util.HTTPUtils.java
/** * Performs a POST request to the specified URL and returns the result. * <p />/*from w ww . j a va 2s .c om*/ * The POST data will be encoded in UTF-8 as the specified contentType. The response will be parsed as UTF-8. * If the server returns an error but still provides a body, the body will be returned as normal. * If the server returns an error without any body, a relevant {@link java.io.IOException} will be thrown. * * @param url URL to submit the POST request to * @param post POST data in the correct format to be submitted * @param contentType Content type of the POST data * @return Raw text response from the server * @throws IOException The request was not successful */ public static String performPostRequest(final URL url, final String post, final String contentType) throws IOException { Validate.notNull(url); Validate.notNull(post); Validate.notNull(contentType); final HttpURLConnection connection = createUrlConnection(url); final byte[] postAsBytes = post.getBytes(Charsets.UTF_8); connection.setRequestProperty("Content-Type", contentType + "; charset=utf-8"); connection.setRequestProperty("Content-Length", "" + postAsBytes.length); connection.setDoOutput(true); OutputStream outputStream = null; try { outputStream = connection.getOutputStream(); IOUtils.write(postAsBytes, outputStream); } finally { IOUtils.closeQuietly(outputStream); } InputStream inputStream = null; try { inputStream = connection.getInputStream(); final String result = IOUtils.toString(inputStream, Charsets.UTF_8); return result; } catch (final IOException e) { IOUtils.closeQuietly(inputStream); inputStream = connection.getErrorStream(); if (inputStream != null) { final String result = IOUtils.toString(inputStream, Charsets.UTF_8); return result; } else { throw e; } } finally { IOUtils.closeQuietly(inputStream); } }
From source file:io.restassured.examples.springmvc.controller.MultiPartFileUploadITest.java
@Test public void file_uploading_works() throws IOException { File file = folder.newFile("something"); IOUtils.write("Something21", new FileOutputStream(file)); RestAssuredMockMvc.given().multiPart(file).when().post("/fileUpload").then().body("size", greaterThan(10)) .body("name", equalTo("file")); }