1 21 package au.id.jericho.lib.html; 22 23 import java.util.*; 24 import java.io.*; 25 26 29 public final class Util { 30 private static final int BUFFER_SIZE=2048; 31 private static final String CSVNewLine=System.getProperty("line.separator"); 32 33 private Util() {} 34 35 47 public static String getString(final Reader reader) throws IOException { 48 if (reader==null) return ""; 49 try { 50 final BufferedReader in=new BufferedReader(reader,BUFFER_SIZE); 51 int charsRead; 52 final char[] copyBuffer=new char[BUFFER_SIZE]; 53 final StringBuffer sb=new StringBuffer (); 54 while ((charsRead=in.read(copyBuffer,0,BUFFER_SIZE))!=-1) 55 sb.append(copyBuffer,0,charsRead); 56 in.close(); 57 return sb.toString(); 58 } finally { 59 reader.close(); 60 } 61 } 62 63 99 public static void outputCSVLine(final Writer writer, final String [] values) throws IOException { 100 for (int i=0; i<values.length;) { 101 final String value=values[i]; 102 if (value!=null) { 103 if (value==Config.ColumnValueTrue || value==Config.ColumnValueFalse) { 104 writer.write(value); } else { 106 writer.write('"'); 107 outputValueEscapeQuotes(writer,value); 108 writer.write('"'); 109 } 110 } 111 if (++i!=values.length) writer.write(','); 112 } 113 writer.write(CSVNewLine); 114 } 115 116 private static void outputValueEscapeQuotes(final Writer writer, final String text) throws IOException { 117 for (int i=0; i<text.length(); i++) { 118 final char ch=text.charAt(i); 119 writer.write(ch); 120 if (ch=='"') writer.write(ch); 121 } 122 } 123 124 static StringBuffer appendTo(final StringBuffer sb, final CharSequence s) { 126 return appendTo(sb,s,0,s.length()); 127 } 128 static StringBuffer appendTo(final StringBuffer sb, final CharSequence s, int start, final int end) { 130 while (start<end) { 131 sb.append(s.charAt(start)); 132 start++; 133 } 134 return sb; 135 } 136 static Writer appendTo(final Writer writer, final CharSequence s) throws IOException { 138 return appendTo(writer,s,0,s.length()); 139 } 140 static Writer appendTo(final Writer writer, final CharSequence s, int start, final int end) throws IOException { 142 while (start<end) { 143 writer.write(s.charAt(start)); 144 start++; 145 } 146 return writer; 147 } 148 } 149 | Popular Tags |