List of usage examples for java.io PrintWriter PrintWriter
public PrintWriter(File file) throws FileNotFoundException
From source file:Main.java
/** * Extract the full stacktrace from {@code t} into a String for logging * @param t an exception/*w w w .j a v a 2 s . co m*/ * @return a string representation of the full stack trace */ public static String sprintFullStack(final Throwable t) { if (t == null) { return ""; } StringWriter writer = new StringWriter(); t.printStackTrace(new PrintWriter(writer)); return writer.toString(); }
From source file:com.textocat.textokit.commons.io.IoUtils.java
public static PrintWriter openPrintWriter(File file, String encoding, boolean append) throws IOException { return new PrintWriter(openBufferedWriter(file, encoding, append)); }
From source file:org.thymeleaf.spring4.view.FragmentRenderer.java
public static String render(final ThymeleafView view, final IFragmentSpec fragmentSpec, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws Exception { final StringWriter htmlStringWriter = new StringWriter(); new ThymeleafView() { @Override/*from w w w . ja v a 2s . co m*/ public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper(response) { @Override public PrintWriter getWriter() throws IOException { return new PrintWriter(htmlStringWriter); } }; view.renderFragment(fragmentSpec, model, request, wrapper); } }.render(modelMap, request, response); return htmlStringWriter.toString(); }
From source file:MainClass.java
public void run() { try {/*from w ww .ja v a2s.c o m*/ BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream())); PrintWriter pw = new PrintWriter(sock.getOutputStream()); String data = br.readLine(); pw.println("What is she?"); pw.close(); sock.close(); } catch (IOException ioe) { // Client disconnected } }
From source file:com.eyeq.pivot4j.util.CssWriter.java
/** * @param writer/* w w w. j av a 2 s. c o m*/ */ public CssWriter(Writer writer) { if (writer == null) { throw new NullArgumentException("writer"); } this.writer = new PrintWriter(writer); }
From source file:SimpleProxyServer.java
/** * runs a single-threaded proxy server on * the specified local port. It never returns. *///from w ww .ja v a 2 s .com public static void runServer(String host, int remoteport, int localport) throws IOException { // Create a ServerSocket to listen for connections with ServerSocket ss = new ServerSocket(localport); final byte[] request = new byte[1024]; byte[] reply = new byte[4096]; while (true) { Socket client = null, server = null; try { // Wait for a connection on the local port client = ss.accept(); final InputStream streamFromClient = client.getInputStream(); final OutputStream streamToClient = client.getOutputStream(); // Make a connection to the real server. // If we cannot connect to the server, send an error to the // client, disconnect, and continue waiting for connections. try { server = new Socket(host, remoteport); } catch (IOException e) { PrintWriter out = new PrintWriter(streamToClient); out.print("Proxy server cannot connect to " + host + ":" + remoteport + ":\n" + e + "\n"); out.flush(); client.close(); continue; } // Get server streams. final InputStream streamFromServer = server.getInputStream(); final OutputStream streamToServer = server.getOutputStream(); // a thread to read the client's requests and pass them // to the server. A separate thread for asynchronous. Thread t = new Thread() { public void run() { int bytesRead; try { while ((bytesRead = streamFromClient.read(request)) != -1) { streamToServer.write(request, 0, bytesRead); streamToServer.flush(); } } catch (IOException e) { } // the client closed the connection to us, so close our // connection to the server. try { streamToServer.close(); } catch (IOException e) { } } }; // Start the client-to-server request thread running t.start(); // Read the server's responses // and pass them back to the client. int bytesRead; try { while ((bytesRead = streamFromServer.read(reply)) != -1) { streamToClient.write(reply, 0, bytesRead); streamToClient.flush(); } } catch (IOException e) { } // The server closed its connection to us, so we close our // connection to our client. streamToClient.close(); } catch (IOException e) { System.err.println(e); } finally { try { if (server != null) server.close(); if (client != null) client.close(); } catch (IOException e) { } } } }
From source file:SSLSimpleServer.java
public void run() { try {//from w ww . ja va 2s .co m BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream())); PrintWriter pw = new PrintWriter(sock.getOutputStream()); String data = br.readLine(); pw.println(data); pw.close(); sock.close(); } catch (IOException ioe) { // Client disconnected } }
From source file:Main.java
/** * Returns a stack trace of the {@code Throwable} as a {@code String}. * // www .ja v a2 s. co m * @param throwable * The throwable for which to create the stack trace. * @param expectNull * True if null should be returned when {@code throwable} is null or * false to return "" when {@code throwable} is null * @return null if {@code throwable} is null and {@code expectNull} is true, * "" if {@code throwable} is null and {@code expectNull} is false, * otherwise the stack trace for {@code throwable} */ public static String stackTraceToString(final Throwable throwable, final boolean expectNull) { if (throwable == null) { if (expectNull == true) { return null; } return ""; } StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); throwable.printStackTrace(printWriter); printWriter.close(); return stringWriter.toString(); }
From source file:com.github.rnewson.couchdb.lucene.Utils.java
public static String error(final int code, final Throwable t) { final StringWriter writer = new StringWriter(); final PrintWriter printWriter = new PrintWriter(writer); if (t.getMessage() != null) { printWriter.append(t.getMessage()); printWriter.append("\n"); }// ww w.j a v a2 s . c o m t.printStackTrace(printWriter); return new JSONObject().element("code", code).element("body", "<pre>" + writer + "</pre>").toString(); }
From source file:com.aqnote.shared.cryptology.cert.io.PKCSTransformer.java
public static String getCrtFileString(X509Certificate cert) throws Exception { CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE); PEMWriter pemWriter = new PEMWriter(new PrintWriter(cbb.getOutputStream())); cbb.getOutputStream().flush();/*w w w.j a va 2 s .co m*/ cbb.getOutputStream().close(); pemWriter.writeObject(cert); pemWriter.flush(); pemWriter.close(); String crtFile = StreamUtil.stream2Bytes(cbb.getInputStream(), StandardCharsets.UTF_8); cbb.getInputStream().close(); cbb.clear(); return crtFile; }