List of usage examples for java.io Writer flush
public abstract void flush() throws IOException;
From source file:com.dajodi.scandic.Util.java
public static void writeMemberInfo(Context context, MemberInfo memberInfo) throws IOException { PersistedData data = new PersistedData(); data.setMemberInfo(memberInfo);//w w w .ja v a2s. co m File f = context.getFilesDir(); Writer writer = new BufferedWriter(new FileWriter(new File(f, JSON_FILENAME))); try { new JSONSerializer().deepSerialize(data, writer); writer.flush(); } finally { writer.close(); } }
From source file:com.zhch.example.commons.http.v4_5.ProxyTunnelDemo.java
/** * ?? demo//from w w w . jav a2 s.c om * * @throws Exception */ public final static void officalDemo() throws Exception { ProxyClient proxyClient = new ProxyClient(); HttpHost target = new HttpHost("www.yahoo.com", 80); HttpHost proxy = new HttpHost("localhost", 8888); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd"); Socket socket = proxyClient.tunnel(proxy, target, credentials); try { Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET); out.write("GET / HTTP/1.1\r\n"); out.write("Host: " + target.toHostString() + "\r\n"); out.write("Agent: whatever\r\n"); out.write("Connection: close\r\n"); out.write("\r\n"); out.flush(); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET)); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } } finally { socket.close(); } }
From source file:com.zhch.example.commons.http.v4_5.ProxyTunnelDemo.java
/** * demo /*from w w w . j av a 2 s.c om*/ * * @throws Exception */ public final static void zhchModifyDemo() throws Exception { ProxyClient proxyClient = new ProxyClient(); HttpHost target = new HttpHost("pan.baidu.com", 80); // HttpHost proxy = new HttpHost("120.24.0.162", 80); HttpHost proxy = new HttpHost("127.0.0.1", 80); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("abc", "def"); System.out.println("before tunnel."); Socket socket = proxyClient.tunnel(proxy, target, credentials); System.out.println("after tunnel."); try { Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET); out.write("GET / HTTP/1.1\r\n"); out.write("Host: " + target.toHostString() + "\r\n"); out.write("Agent: whatever\r\n"); out.write("Connection: close\r\n"); out.write("\r\n"); out.flush(); BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET)); String line = null; while ((line = in.readLine()) != null) { System.out.println(line); } } finally { socket.close(); } }
From source file:Main.java
public static void saveInstance(Writer output, Object instance) throws JAXBException, IOException { Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_ENCODING, ENCODING); marshaller.marshal(instance, output); output.flush(); }
From source file:cz.incad.kramerius.k5indexer.Commiter.java
/** * Pipes everything from the reader to the writer via a buffer *//*w w w . j a va 2 s.co m*/ private static void pipe(Reader reader, Writer writer) throws IOException { char[] buf = new char[1024]; int read = 0; while ((read = reader.read(buf)) >= 0) { writer.write(buf, 0, read); } writer.flush(); }
From source file:FileUtil.java
/** * Just copies all characters from <I>in</I> to <I>out</I>. The copying * is performed using a buffer of bytes. * * @since 1.5.8//w w w . ja v a 2 s .c o m * @param in The reader to copy from * @param out The reader to copy to * @throws IOException If reading or writing failed. */ public static void copyContents(Reader in, Writer out) throws IOException { char[] buf = new char[BUFFER_SIZE]; int bytesRead = 0; while ((bytesRead = in.read(buf)) > 0) { out.write(buf, 0, bytesRead); } out.flush(); }
From source file:com.ms.commons.test.classloader.util.CLFileUtil.java
public static void copyAndProcessFileContext(URL url, File newFile, String encoding, Processor processor) { try {/*from w w w . j a va 2 s. c om*/ if (processor == null) { processor = DEFAULT_PROCESSOR; } if (!newFile.exists()) { String fileContext = readUrlToString(url, null); if (encoding == null) { if (FileUtil.convertURLToFilePath(url).endsWith(".xml") && fileContext.contains("\"UTF-8\"")) { encoding = "UTF-8"; fileContext = readUrlToString(url, "UTF-8"); } } (new File(getFilePath(newFile.getPath()))).mkdirs(); Writer writer; if (encoding == null) { writer = new OutputStreamWriter(new FileOutputStream(newFile)); } else { writer = new OutputStreamWriter(new FileOutputStream(newFile), encoding); } try { writer.write(processor.process(fileContext)); writer.flush(); } finally { writer.close(); } } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.gm.machine.util.CommonUtils.java
private synchronized static void writeHtml(String htmlFileName, String content, String encoding) throws Exception { Writer fw = null; try {/* w w w . j a v a2s. co m*/ fw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFileName), encoding)); fw.write(content); fw.flush(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // ? if (fw != null) { fw.close(); } } }
From source file:org.ambientdynamix.web.WebUtils.java
/** * xports an certificate to a file.//from www . ja v a 2s .co m * * @param cert * The certificate to export. * @param file * The destination file. * @param binary * True if the cert should be written as a binary file; false to encode using Base64. */ public static void exportCertificate(java.security.cert.Certificate cert, File file, boolean binary) { Log.i(TAG, "Writing cert to: " + file.getAbsolutePath()); try { // Get the encoded form which is suitable for exporting byte[] buf = cert.getEncoded(); FileOutputStream os = new FileOutputStream(file); if (binary) { // Write in binary form os.write(buf); } else { // Write in text form Writer wr = new OutputStreamWriter(os, Charset.forName("UTF-8")); wr.write("-----BEGIN CERTIFICATE-----\n"); Base64.encodeBase64(buf); wr.write("\n-----END CERTIFICATE-----\n"); wr.flush(); } os.close(); } catch (Exception e) { Log.w(TAG, "Error writing cert for " + file); } }
From source file:net.sf.jasperreports.engine.xml.JRXmlTemplateWriter.java
protected static void writeTemplate(JasperReportsContext jasperReportsContext, JRTemplate template, Writer out, String encoding) throws IOException { JRXmlTemplateWriter writer = new JRXmlTemplateWriter(jasperReportsContext, template, out, encoding); writer.write();//from w w w .jav a2 s .c om out.flush(); }