List of usage examples for java.io InputStreamReader read
public int read(java.nio.CharBuffer target) throws IOException
From source file:Main.java
/** read a resource as a String * //from ww w . j a va2 s. c o m * @param clazz * @param name * @return * @throws IOException */ public static String readResourceAsString(Class clazz, String name) throws IOException { StringBuffer fileData = new StringBuffer(1000); InputStreamReader reader = new InputStreamReader(clazz.getResourceAsStream(name)); char[] buf = new char[1024]; int numRead = 0; while ((numRead = reader.read(buf)) != -1) { String readData = String.valueOf(buf, 0, numRead); fileData.append(readData); buf = new char[1024]; } reader.close(); return fileData.toString().replace("\r\n", "\n"); }
From source file:Main.java
public static String streamToString(InputStream stream) { InputStreamReader reader = new InputStreamReader(stream); StringBuilder sb = new StringBuilder(); char[] buf = new char[256]; try {// ww w . j a v a 2 s . c o m while (reader.read(buf) != -1) { sb.append(buf); } } catch (IOException e) { // Darn } finally { try { stream.close(); } catch (IOException e) { } } return sb.toString(); }
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 {/* w w w . ja v a2 s. co 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 inputStream2String(InputStream in, String encoding) throws Exception { StringBuffer out = new StringBuffer(); InputStreamReader inread = new InputStreamReader(in, encoding); char[] b = new char[4096]; try {/* w ww . ja v a 2 s .c om*/ for (int n; (n = inread.read(b)) != -1;) { out.append(new String(b, 0, n)); } } catch (IOException e) { } return out.toString(); }
From source file:Main.java
public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception { Process proc = null;/*from ww w . j a v a 2 s .c o m*/ int exitCode = -1; if (runAsRoot) { proc = Runtime.getRuntime().exec("su"); } else { proc = Runtime.getRuntime().exec("sh"); } OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); for (int i = 0; i < cmds.length; i++) { out.write(cmds[i]); out.write("\n"); } out.flush(); out.write("exit\n"); out.flush(); if (waitFor) { final char buf[] = new char[10]; // Consume the "stdout" InputStreamReader reader = new InputStreamReader(proc.getInputStream()); int read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read); } // Consume the "stderr" reader = new InputStreamReader(proc.getErrorStream()); read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read); } exitCode = proc.waitFor(); } return exitCode; }
From source file:Main.java
public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception { Process proc = null;//from w w w .j av a 2s .c om int exitCode = -1; if (runAsRoot) proc = Runtime.getRuntime().exec("su"); else proc = Runtime.getRuntime().exec("sh"); OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); for (int i = 0; i < cmds.length; i++) { out.write(cmds[i]); out.write("\n"); } out.flush(); out.write("exit\n"); out.flush(); if (waitFor) { final char buf[] = new char[10]; // Consume the "stdout" InputStreamReader reader = new InputStreamReader(proc.getInputStream()); int read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read); } // Consume the "stderr" reader = new InputStreamReader(proc.getErrorStream()); read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read); } exitCode = proc.waitFor(); } return exitCode; }
From source file:Main.java
public static int doShellCommand(String[] cmds, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception { Process proc = null;//from w w w.j av a 2s.c om int exitCode = -1; if (runAsRoot) { proc = Runtime.getRuntime().exec("su"); } else { proc = Runtime.getRuntime().exec("sh"); } OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); for (int i = 0; i < cmds.length; i++) { // TorService.logMessage("executing shell cmd: " + cmds[i] + // "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor); out.write(cmds[i]); out.write("\n"); } out.flush(); out.write("exit\n"); out.flush(); if (waitFor) { final char buf[] = new char[10]; // Consume the "stdout" InputStreamReader reader = new InputStreamReader(proc.getInputStream()); int read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) { log.append(buf, 0, read); } } // Consume the "stderr" reader = new InputStreamReader(proc.getErrorStream()); read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) { log.append(buf, 0, read); } } exitCode = proc.waitFor(); } return exitCode; }
From source file:com.barrybecker4.common.util.Base64Codec.java
/** * Take a String and decompress it.//from w w w . j a v a 2 s . c o m * @param data the compressed string to decompress. * @return the decompressed string. */ public static synchronized String decompress(final String data) { // convert from string to bytes for decompressing byte[] compressedDat = Base64.decodeBase64(data.getBytes()); final ByteArrayInputStream in = new ByteArrayInputStream(compressedDat); final Inflater inflater = new Inflater(); final InflaterInputStream iStream = new InflaterInputStream(in, inflater); final char cBuffer[] = new char[4096]; StringBuilder sBuf = new StringBuilder(); try { InputStreamReader iReader = new InputStreamReader(iStream, CONVERTER_UTF8); while (true) { final int numRead = iReader.read(cBuffer); if (numRead == -1) { break; } sBuf.append(cBuffer, 0, numRead); } } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException("Unsupported encoding exception :" + e.getMessage(), e); } catch (IOException e) { throw new IllegalStateException("io error :" + e.getMessage(), e); } return sBuf.toString(); }
From source file:Main.java
/** * Convert InputStream to String//from w ww .j a v a 2 s. c o m * * @param stream inputStream * @return string */ private static String streamToString(InputStream stream) { StringWriter writer = null; if (stream != null) { try { InputStreamReader reader = new InputStreamReader(stream, "UTF-8"); writer = new StringWriter(); int n; char[] buffer = new char[1024 * 4]; while (-1 != (n = reader.read(buffer))) { writer.write(buffer, 0, n); } } catch (IOException e) { // @todo better logging e.printStackTrace(); } } return writer != null ? writer.toString() : ""; }
From source file:Main.java
public static int doShellCommand(String cmd, StringBuilder log, boolean runAsRoot, boolean waitFor) throws Exception { Process proc = null;/*from w w w .j a v a2s .c o m*/ int exitCode = -1; if (runAsRoot) proc = Runtime.getRuntime().exec("su"); else proc = Runtime.getRuntime().exec("sh"); OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); // TorService.logMessage("executing shell cmd: " + cmds[i] + "; runAsRoot=" + runAsRoot + ";waitFor=" + waitFor); out.write(cmd); out.write("\n"); out.flush(); out.write("exit\n"); out.flush(); if (waitFor) { final char buf[] = new char[10]; // Consume the "stdout" InputStreamReader reader = new InputStreamReader(proc.getInputStream()); int read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read); } // Consume the "stderr" reader = new InputStreamReader(proc.getErrorStream()); read = 0; while ((read = reader.read(buf)) != -1) { if (log != null) log.append(buf, 0, read); } exitCode = proc.waitFor(); } return exitCode; }