1 19 20 package com.maverick.util; 21 22 import java.io.*; 23 24 34 public class TextConversionStream extends FilterOutputStream { 35 36 40 public final static int TEXT_SYSTEM = 0; 41 45 public final static int TEXT_WINDOWS = 1; 46 50 public final static int TEXT_DOS = 1; 51 55 public final static int TEXT_CRLF = 1; 56 60 public final static int TEXT_UNIX = 2; 61 65 public final static int TEXT_LF = 2; 66 70 public final static int TEXT_MAC = 3; 71 75 public final static int TEXT_CR = 3; 76 77 82 public final static int TEXT_ALL = 4; 83 84 byte[] lineEnding; 85 String systemNL = System.getProperty("line.separator"); 86 boolean stripCR; 87 boolean stripLF; 88 boolean stripCRLF; 89 boolean encounteredBinary = false; 90 91 boolean lastCharacterWasCR = false; 92 93 public TextConversionStream(int inputStyle, int outputStyle, OutputStream out) { 94 95 super(out); 96 97 switch (inputStyle) { 98 case TEXT_CRLF: { 99 stripCR = false; 100 stripLF = false; 101 stripCRLF = true; 102 } break; 103 case TEXT_CR: { 104 stripCR = true; 105 stripLF = false; 106 stripCRLF = false; 107 } break; 108 case TEXT_LF: { 109 stripCR = false; 110 stripLF = true; 111 stripCRLF = false; 112 } break; 113 case TEXT_ALL: { 114 stripCR = true; 115 stripLF = true; 116 stripCRLF = true; 117 } break; 118 default: { 119 throw new IllegalArgumentException ("Unknown text style: " + outputStyle); 120 } 121 } 122 123 124 switch (outputStyle) { 125 case TEXT_SYSTEM: { 126 lineEnding = systemNL.getBytes(); 127 } break; 128 case TEXT_CRLF: { 129 lineEnding = new byte[]{(byte)'\r',(byte)'\n'}; 130 } break; 131 case TEXT_CR: { 132 lineEnding = new byte[]{(byte)'\r'}; 133 } break; 134 case TEXT_LF: { 135 lineEnding = new byte[]{(byte)'\n'}; 136 } break; 137 case TEXT_ALL: { 138 throw new IllegalArgumentException ("TEXT_ALL cannot be used for an output style"); 139 } 140 default: { 141 throw new IllegalArgumentException ("Unknown text style: " + outputStyle); 142 } 143 } 144 } 145 146 151 public boolean hasBinary() { 152 return encounteredBinary; 153 } 154 155 public void write(int b) throws IOException { 156 write(new byte[] { (byte)b }); 157 } 158 159 public void close() throws IOException { 160 161 if(lastCharacterWasCR && !stripCR) 162 out.write('\r'); 163 164 super.close(); 165 } 166 167 175 public void write(byte[] buf, 176 int off, 177 int len) 178 throws IOException { 179 180 181 BufferedInputStream bin = new BufferedInputStream( 182 new ByteArrayInputStream(buf, off, len), 32768); 183 184 int b; 185 while((b = bin.read()) != -1){ 186 187 if (b == '\r') { 188 189 if(stripCRLF) { 190 bin.mark(1); 191 int ch = bin.read(); 192 if(ch==-1) { 193 lastCharacterWasCR = true; 194 break; 195 } 196 if(ch == '\n') { 197 out.write(lineEnding); 199 200 } else { 201 bin.reset(); 203 if(stripCR) { 204 out.write(lineEnding); 205 } else 206 out.write(b); 207 } 208 } else { 209 if (stripCR) 211 out.write(lineEnding); 212 else 213 out.write(b); 214 215 } 216 } else if(b == '\n') { 217 218 if(lastCharacterWasCR) { 220 out.write(lineEnding); 221 lastCharacterWasCR = false; 222 } else { 223 if (stripLF) 225 out.write(lineEnding); 226 else 227 out.write(b); 228 } 229 } else { 230 231 if(lastCharacterWasCR) { 233 if(stripCR) { 234 out.write(lineEnding); 235 } else 236 out.write(b); 237 } 238 239 if(b != 't' 241 && b != '\f' 242 && (b & 0xff) < 32) { 243 encounteredBinary = true; 244 } 245 246 out.write(b); 247 } 248 } 249 250 } 251 252 253 public static void main(String [] args) { 254 255 try { 256 TextConversionStream t = new TextConversionStream( 257 TEXT_CRLF, 258 TEXT_CR, 259 new FileOutputStream("C:\\TEXT.txt")); 260 261 262 t.write("1234567890\r".getBytes()); 263 t.write("\n01234567890\r\n".getBytes()); 264 t.write("\r\n12323445546657".getBytes()); 265 t.write("21344356545656\r".getBytes()); 266 267 t.close(); 268 269 } catch(Exception e) { 270 } 271 272 } 273 274 275 276 } 277 | Popular Tags |