List of usage examples for java.io StringWriter write
public void write(String str, int off, int len)
From source file:Main.java
public static void main(String[] args) { String s = "tutorial from java2s.com"; StringWriter sw = new StringWriter(); // write strings sw.write(s, 0, 4); sw.write(s, 5, 6);/* www. j a va 2s. c o m*/ System.out.println(sw.toString()); }
From source file:com.asual.lesscss.LessEngineCli.java
public static void main(String[] args) throws LessException, URISyntaxException { Options cmdOptions = new Options(); cmdOptions.addOption(LessOptions.CHARSET_OPTION, true, "Input file charset encoding. Defaults to UTF-8."); cmdOptions.addOption(LessOptions.COMPRESS_OPTION, false, "Flag that enables compressed CSS output."); cmdOptions.addOption(LessOptions.CSS_OPTION, false, "Flag that enables compilation of .css files."); cmdOptions.addOption(LessOptions.LESS_OPTION, true, "Path to a custom less.js for Rhino version."); try {/*from w ww . ja va 2s. c o m*/ CommandLineParser cmdParser = new GnuParser(); CommandLine cmdLine = cmdParser.parse(cmdOptions, args); LessOptions options = new LessOptions(); if (cmdLine.hasOption(LessOptions.CHARSET_OPTION)) { options.setCharset(cmdLine.getOptionValue(LessOptions.CHARSET_OPTION)); } if (cmdLine.hasOption(LessOptions.COMPRESS_OPTION)) { options.setCompress(true); } if (cmdLine.hasOption(LessOptions.CSS_OPTION)) { options.setCss(true); } if (cmdLine.hasOption(LessOptions.LESS_OPTION)) { options.setLess(new File(cmdLine.getOptionValue(LessOptions.LESS_OPTION)).toURI().toURL()); } LessEngine engine = new LessEngine(options); if (System.in.available() != 0) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringWriter sw = new StringWriter(); char[] buffer = new char[1024]; int n = 0; while (-1 != (n = in.read(buffer))) { sw.write(buffer, 0, n); } String src = sw.toString(); if (!src.isEmpty()) { System.out.println(engine.compile(src, null, options.isCompress())); System.exit(0); } } String[] files = cmdLine.getArgs(); if (files.length == 1) { System.out.println(engine.compile(new File(files[0]), options.isCompress())); System.exit(0); } if (files.length == 2) { engine.compile(new File(files[0]), new File(files[1]), options.isCompress()); System.exit(0); } } catch (IOException ioe) { System.err.println("Error opening input file."); } catch (ParseException pe) { System.err.println("Error parsing arguments."); } String[] paths = LessEngine.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath() .split(File.separator); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar " + paths[paths.length - 1] + " input [output] [options]", cmdOptions); System.exit(1); }
From source file:de.akquinet.dustjs.DustEngine.java
public static void main(String[] args) throws URISyntaxException { Options cmdOptions = new Options(); cmdOptions.addOption(DustOptions.CHARSET_OPTION, true, "Input file charset encoding. Defaults to UTF-8."); cmdOptions.addOption(DustOptions.DUST_OPTION, true, "Path to a custom dust.js for Rhino version."); try {/*from w w w. ja v a2 s.c o m*/ CommandLineParser cmdParser = new GnuParser(); CommandLine cmdLine = cmdParser.parse(cmdOptions, args); DustOptions options = new DustOptions(); if (cmdLine.hasOption(DustOptions.CHARSET_OPTION)) { options.setCharset(cmdLine.getOptionValue(DustOptions.CHARSET_OPTION)); } if (cmdLine.hasOption(DustOptions.DUST_OPTION)) { options.setDust(new File(cmdLine.getOptionValue(DustOptions.DUST_OPTION)).toURI().toURL()); } DustEngine engine = new DustEngine(options); String[] files = cmdLine.getArgs(); String src = null; if (files == null || files.length == 0) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringWriter sw = new StringWriter(); char[] buffer = new char[1024]; int n = 0; while (-1 != (n = in.read(buffer))) { sw.write(buffer, 0, n); } src = sw.toString(); } if (src != null && !src.isEmpty()) { System.out.println(engine.compile(src, "test")); return; } if (files.length == 1) { System.out.println(engine.compile(new File(files[0]))); return; } if (files.length == 2) { engine.compile(new File(files[0]), new File(files[1])); return; } } catch (IOException ioe) { System.err.println("Error opening input file."); } catch (ParseException pe) { System.err.println("Error parsing arguments."); } HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java -jar dust-engine.jar input [output] [options]", cmdOptions); System.exit(1); }
From source file:Main.java
public static String convertStreamToString(InputStream is) throws IOException { InputStreamReader r = new InputStreamReader(is); StringWriter sw = new StringWriter(); char[] buffer = new char[1024]; try {//from w ww . j a v a2 s .c o m for (int n; (n = r.read(buffer)) != -1;) sw.write(buffer, 0, n); } finally { try { is.close(); } catch (IOException e1) { e1.printStackTrace(); } } return sw.toString(); }
From source file:Main.java
public static String getDtdAsString(String uri) throws IOException { Reader in = null;// ww w .ja va2 s. com if ((uri.startsWith("http")) || (uri.startsWith("ftp")) || (uri.startsWith("file:"))) in = new InputStreamReader(new URL(uri).openStream()); else { in = new FileReader(uri); } StringWriter out = new StringWriter(); char[] buffer = new char[4096]; for (int count = in.read(buffer); count != -1; count = in.read(buffer)) { out.write(buffer, 0, count); } return out.getBuffer().toString(); }
From source file:Main.java
private static String getStreamAsString(InputStream stream, String charset) throws IOException { try {/*from ww w.java2 s .c o m*/ BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset)); StringWriter writer = new StringWriter(); char[] chars = new char[256]; int count = 0; while ((count = reader.read(chars)) > 0) { writer.write(chars, 0, count); } return writer.toString(); } finally { if (stream != null) { stream.close(); } } }
From source file:Main.java
static String readFully(Reader reader) throws IOException { try {// w w w.j a v a 2s . c o m StringWriter writer = new StringWriter(); char[] buffer = new char[1024]; int count; while ((count = reader.read(buffer)) != -1) { writer.write(buffer, 0, count); } return writer.toString(); } finally { reader.close(); } }
From source file:org.lucasr.layoutsamples.util.RawResource.java
public static JSONArray getAsJSON(Context context, int id) throws IOException { InputStreamReader reader = null; try {// www . j a v a2 s . c om final Resources res = context.getResources(); final InputStream is = res.openRawResource(id); if (is == null) { return null; } reader = new InputStreamReader(is); final char[] buffer = new char[1024]; final StringWriter s = new StringWriter(); int n; while ((n = reader.read(buffer, 0, buffer.length)) != -1) { s.write(buffer, 0, n); } return new JSONArray(s.toString()); } catch (JSONException e) { return null; } finally { if (reader != null) { reader.close(); } } }
From source file:Main.java
/** Returns the remainder of 'reader' as a string, closing it when done. */ public static String readFully(Reader reader) throws IOException { try {/*from w w w. j a va 2s . co m*/ StringWriter writer = new StringWriter(); char[] buffer = new char[1024]; int count; while ((count = reader.read(buffer)) != -1) { writer.write(buffer, 0, count); } return writer.toString(); } finally { reader.close(); } }
From source file:Main.java
public static String request(String url, Map<String, String> cookies, Map<String, String> parameters) throws Exception { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"); if (cookies != null && !cookies.isEmpty()) { StringBuilder cookieHeader = new StringBuilder(); for (String cookie : cookies.values()) { if (cookieHeader.length() > 0) { cookieHeader.append(";"); }/*w ww. ja v a 2 s. c o m*/ cookieHeader.append(cookie); } connection.setRequestProperty("Cookie", cookieHeader.toString()); } connection.setDoInput(true); if (parameters != null && !parameters.isEmpty()) { connection.setDoOutput(true); OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream()); osw.write(parametersToWWWFormURLEncoded(parameters)); osw.flush(); osw.close(); } if (cookies != null) { for (Map.Entry<String, List<String>> headerEntry : connection.getHeaderFields().entrySet()) { if (headerEntry != null && headerEntry.getKey() != null && headerEntry.getKey().equalsIgnoreCase("Set-Cookie")) { for (String header : headerEntry.getValue()) { for (HttpCookie httpCookie : HttpCookie.parse(header)) { cookies.put(httpCookie.getName(), httpCookie.toString()); } } } } } Reader r = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringWriter w = new StringWriter(); char[] buffer = new char[1024]; int n = 0; while ((n = r.read(buffer)) != -1) { w.write(buffer, 0, n); } r.close(); return w.toString(); }