List of usage examples for java.io ByteArrayOutputStream writeTo
public synchronized void writeTo(OutputStream out) throws IOException
From source file:net.vexelon.bgrates.Utils.java
/** * Move a file stored in the cache to the internal storage of the specified context * @param context/*from w w w . ja va 2s . c om*/ * @param cacheFile * @param internalStorageName */ public static boolean moveCacheFile(Context context, File cacheFile, String internalStorageName) { boolean ret = false; FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(cacheFile); ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int read = -1; while ((read = fis.read(buffer)) != -1) { baos.write(buffer, 0, read); } baos.close(); fis.close(); fos = context.openFileOutput(internalStorageName, Context.MODE_PRIVATE); baos.writeTo(fos); fos.close(); // delete cache cacheFile.delete(); ret = true; } catch (Exception e) { //Log.e(TAG, "Error saving previous rates!"); } finally { try { if (fis != null) fis.close(); } catch (IOException e) { } try { if (fos != null) fos.close(); } catch (IOException e) { } } return ret; }
From source file:truelauncher.client.auth.Type2.java
protected static void writeAuthPacket(DataOutputStream dos, PlayerAuthData padata) throws IOException { ///* www . j a v a 2s . c om*/ //fake 1.7.2 handshake packet format. //host = authpacket(AuthConnector + nick + token + password) // //create frame buffer ByteArrayOutputStream frame = new ByteArrayOutputStream(); DataOutputStream frameOut = new DataOutputStream(frame); String authpacket = "AuthConnector|" + padata.getNick() + "|" + padata.getToken() + "|" + padata.getPassword(); //write handshake packet to frame //write packet id writeVarInt(frameOut, 0x00); //write protocolVersion writeVarInt(frameOut, padata.getProtocolVersion()); //write authpacket instead of hostname writeString(frameOut, authpacket); //write port frameOut.writeShort(padata.getPort()); //write state writeVarInt(frameOut, 2); //now write frame to real socket //write length writeVarInt(dos, frame.size()); //write packet frame.writeTo(dos); frame.reset(); //close frames frameOut.close(); frame.close(); }
From source file:by.creepid.docsreporter.converter.PdfConverterAdapterTest.java
@Test public void testSomeMethod() throws IOException { FileOutputStream newOut = null; try {// w ww. j a v a 2 s . c o m InputStream in = new FileInputStream(new File(inPath)); ByteArrayOutputStream out = (ByteArrayOutputStream) converter.convert(DocFormat.getFormat(outPath), in); newOut = new FileOutputStream(new File(outPath)); out.writeTo(newOut); } catch (Exception ex) { fail("Cannot convert template"); ex.printStackTrace(); } finally { if (newOut != null) { newOut.close(); } } assertTrue(outFile.exists() && outFile.length() != 0l); }
From source file:by.creepid.docsreporter.converter.HtmlxConverterAdapterTest.java
/** * Test of getTargetFormat method, of class HtmlxDocConverter. *///w w w . ja v a 2s. c o m @Test public void testGetTargetFormat() throws IOException { FileOutputStream newOut = null; try { InputStream in = new FileInputStream(new File(inPath)); ByteArrayOutputStream out = (ByteArrayOutputStream) converter.convert(DocFormat.getFormat(outPath), in); newOut = new FileOutputStream(outFile); out.writeTo(newOut); } catch (Exception ex) { fail("Cannot convert template"); ex.printStackTrace(); } finally { if (newOut != null) { newOut.close(); } } assertTrue(outFile.exists() && outFile.length() != 0l); }
From source file:com.baidu.jprotobuf.rpc.client.SimpleHttpRequestExecutor.java
/** * Set the given serialized remote invocation as request body. * <p>The default implementation simply write the serialized invocation to the * HttpURLConnection's OutputStream. This can be overridden, for example, to write * a specific encoding and potentially set appropriate HTTP request headers. * @param con the HttpURLConnection to write the request body to * @param baos the ByteArrayOutputStream that contains the serialized * RemoteInvocation object/*from w w w .j a v a 2s . c o m*/ * @throws IOException if thrown by I/O methods * @see java.net.HttpURLConnection#getOutputStream() * @see java.net.HttpURLConnection#setRequestProperty */ protected void writeRequestBody(HttpURLConnection con, ByteArrayOutputStream baos) throws IOException { baos.writeTo(con.getOutputStream()); }
From source file:org.apache.wink.itest.transferencoding.WinkTransferEncodingTest.java
/** * Tests sending in small bits of chunked content. * // w w w.j ava2 s . c om * @throws HttpException * @throws IOException */ public void testSendSmallGzipContentEncoded() throws HttpException, IOException { ByteArrayOutputStream originalContent = new ByteArrayOutputStream(); originalContent.write("Hello world".getBytes("UTF-8")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); originalContent.writeTo(baos); byte[] content = baos.toByteArray(); ClientResponse response = client.resource(BASE_URI + "/chunkedbook").accept(MediaType.TEXT_PLAIN_TYPE) .contentType("text/plain; charset=utf-8").post(content); assertEquals(200, response.getStatusCode()); String responseBody = response.getEntity(String.class); assertEquals("Hello world", responseBody); }
From source file:com.springsecurity.plugin.util.ImageResizer.java
public File reziseTo(File source, File dest, int width, int height, String ext) { try {//from w w w .j a v a 2s . c om ImageResizer img = new ImageResizer(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(img.scale(source, width, height), ext, baos); dest.delete(); baos.writeTo(new FileOutputStream(dest)); baos.flush(); } catch (IOException e) { System.err.println("err" + e.getMessage()); } return dest; }
From source file:de.dfki.resc28.serendipity.client.RepresentationEnricher.java
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { OutputStream originalStream = context.getOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); context.setOutputStream(baos);// w w w .j av a 2 s . co m try { context.proceed(); } finally { // get the original responseModel final Model responseModel = ModelFactory.createDefaultModel(); final String contentType = context.getMediaType().toString(); RDFDataMgr.read(responseModel, new StringReader(baos.toString()), "", RDFLanguages.contentTypeToLang(contentType)); baos.close(); // ask serendipity for affordances to add Model affordances = ModelFactory.createDefaultModel(); StringWriter writer = new StringWriter(); responseModel.write(writer, contentType); StringEntity entity = new StringEntity(writer.toString(), ContentType.create(contentType)); CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(this.serendipityURI); httpPost.setHeader("Accept", contentType); httpPost.setHeader("Content-type", contentType); httpPost.setEntity(entity); CloseableHttpResponse response = client.execute(httpPost); // RDFDataMgr.read(affordances, response.getEntity().getContent(), "", RDFLanguages.contentTypeToLang(response.getEntity().getContentType().getValue())); RDFDataMgr.read(affordances, response.getEntity().getContent(), "", RDFLanguages.contentTypeToLang(contentType)); client.close(); // add the instantiated action to the responseModel responseModel.add(affordances); // overwrite the response ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); RDFDataMgr.write(baos2, responseModel, RDFLanguages.contentTypeToLang(contentType)); baos2.writeTo(originalStream); baos2.close(); context.setOutputStream(originalStream); } }
From source file:org.apache.wink.itest.transferencoding.TransferEncodingTest.java
/** * Tests sending in small bits of chunked content. * /*from w ww.jav a2 s.c o m*/ * @throws HttpException * @throws IOException */ public void testSendSmallGzipContentEncoded() throws HttpException, IOException { PostMethod postMethod = new PostMethod(BASE_URI + "/chunkedbook"); postMethod.setContentChunked(true); postMethod.addRequestHeader("Accept", "text/plain"); ByteArrayOutputStream originalContent = new ByteArrayOutputStream(); originalContent.write("Hello world".getBytes("UTF-8")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); originalContent.writeTo(baos); byte[] content = baos.toByteArray(); postMethod.setRequestEntity(new ByteArrayRequestEntity(content, "text/plain; charset=utf-8")); try { int result = client.executeMethod(postMethod); assertEquals(200, result); String response = postMethod.getResponseBodyAsString(); assertEquals("Hello world", response); } finally { postMethod.releaseConnection(); } }
From source file:org.shimlib.web.servlet.view.filedownload.FileDownloadView.java
@Override protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStream baos) throws IOException { response.setContentLength(baos.size()); // Flush byte array to servlet output stream. ServletOutputStream out = response.getOutputStream(); baos.writeTo(out); out.flush();/* ww w .j a v a2s.c o m*/ }