1 20 21 package com.tonicsystems.jarjar; 22 23 import java.io.*; 24 25 class IoUtils 26 { 27 public static final String LINE_SEPARATOR = System.getProperty("line.separator"); 28 29 private IoUtils() { 30 } 31 32 public static String readIntoString(InputStream in) throws IOException { 33 StringBuffer sb = new StringBuffer (); 34 BufferedReader r = new BufferedReader(new InputStreamReader(in, "UTF-8")); 35 String line = null; 36 while ((line = r.readLine()) != null) 37 sb.append(line).append(LINE_SEPARATOR); 38 return sb.toString(); 39 } 40 41 public static String escapeStringLiteral(String value) { 42 StringBuffer sb = new StringBuffer (); 43 sb.append("\""); 44 char[] chars = value.toCharArray(); 45 for (int i = 0, size = chars.length; i < size; i++) { 46 char ch = chars[i]; 47 switch (ch) { 48 case '\n': sb.append("\\n"); break; 49 case '\r': sb.append("\\r"); break; 50 case '\b': sb.append("\\b"); break; 51 case '\f': sb.append("\\f"); break; 52 case '\t': sb.append("\\t"); break; 53 case '\"': sb.append("\\\""); break; 54 case '\\': sb.append("\\\\"); break; 55 default: 56 sb.append(ch); 57 } 58 } 59 sb.append("\""); 60 return sb.toString(); 61 } 62 63 public static byte[] toByteArray(InputStream is, byte[] buf) throws IOException { 64 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 65 pipe(is, baos, buf); 66 return baos.toByteArray(); 67 } 68 69 public static void pipe(InputStream is, OutputStream out, byte[] buf) throws IOException { 70 for (;;) { 71 int amt = is.read(buf); 72 if (amt < 0) 73 break; 74 out.write(buf, 0, amt); 75 } 76 } 77 } 78 | Popular Tags |