1 19 20 package com.maverick.util; 21 22 import java.io.*; 23 24 34 public class EOLProcessor { 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 = false;; 87 boolean stripLF = false; 88 boolean stripCRLF = false; 89 boolean encounteredBinary = false; 90 91 boolean lastCharacterWasCR = false; 92 93 OutputStream out; 94 95 public EOLProcessor(int inputStyle, int outputStyle, OutputStream out) throws IOException { 96 97 this.out = out; 98 99 switch (inputStyle) { 100 case TEXT_CRLF: { 101 stripCRLF = true; 102 } break; 103 case TEXT_CR: { 104 stripCR = true; 105 } break; 106 case TEXT_LF: { 107 stripLF = true; 108 } break; 109 case TEXT_ALL: { 110 stripCR = true; 111 stripLF = true; 112 stripCRLF = true; 113 } break; 114 case TEXT_SYSTEM: 115 { 116 byte[] tmp = systemNL.getBytes(); 117 if(tmp.length == 2 && tmp[0] == '\r' && tmp[1] == '\n') { 118 stripCRLF = true; 119 } else if(tmp.length==1 && tmp[0] == '\r') { 120 stripCR = true; 121 } else if(tmp.length==1 && tmp[0] == '\n') { 122 stripLF = true; 123 } else { 124 throw new IOException("Unsupported system EOL mode"); 125 } 126 break; 127 } 128 default: { 129 throw new IllegalArgumentException ("Unknown text style: " + outputStyle); 130 } 131 } 132 133 134 switch (outputStyle) { 135 case TEXT_SYSTEM: { 136 lineEnding = systemNL.getBytes(); 137 } break; 138 case TEXT_CRLF: { 139 lineEnding = new byte[]{(byte)'\r',(byte)'\n'}; 140 } break; 141 case TEXT_CR: { 142 lineEnding = new byte[]{(byte)'\r'}; 143 } break; 144 case TEXT_LF: { 145 lineEnding = new byte[]{(byte)'\n'}; 146 } break; 147 case TEXT_ALL: { 148 throw new IllegalArgumentException ("TEXT_ALL cannot be used for an output style"); 149 } 150 default: { 151 throw new IllegalArgumentException ("Unknown text style: " + outputStyle); 152 } 153 } 154 } 155 156 161 public boolean hasBinary() { 162 return encounteredBinary; 163 } 164 165 public void close() throws IOException { 166 167 if(lastCharacterWasCR && !stripCR) 168 out.write('\r'); 169 170 out.close(); 171 } 172 173 181 public void processBytes(byte[] buf, 182 int off, 183 int len) 184 throws IOException { 185 186 187 BufferedInputStream bin = new BufferedInputStream( 188 new ByteArrayInputStream(buf, off, len), 32768); 189 190 int b; 191 while((b = bin.read()) != -1){ 192 193 if (b == '\r') { 194 195 if(stripCRLF) { 196 bin.mark(1); 197 int ch = bin.read(); 198 if(ch==-1) { 199 lastCharacterWasCR = true; 200 break; 201 } 202 if(ch == '\n') { 203 out.write(lineEnding); 205 206 } else { 207 bin.reset(); 209 if(stripCR) { 210 out.write(lineEnding); 211 } else 212 out.write(b); 213 } 214 } else { 215 if (stripCR) 217 out.write(lineEnding); 218 else 219 out.write(b); 220 221 } 222 } else if(b == '\n') { 223 224 if(lastCharacterWasCR) { 226 out.write(lineEnding); 227 lastCharacterWasCR = false; 228 } else { 229 if (stripLF) 231 out.write(lineEnding); 232 else 233 out.write(b); 234 } 235 } else { 236 237 if(lastCharacterWasCR) { 239 if(stripCR) { 240 out.write(lineEnding); 241 } else 242 out.write(b); 243 } 244 245 if(b != 't' 247 && b != '\f' 248 && (b & 0xff) < 32) { 249 encounteredBinary = true; 250 } 251 252 out.write(b); 253 } 254 } 255 256 } 257 258 259 public static OutputStream createOutputStream(int inputStyle, int outputStyle, OutputStream out) throws IOException { 260 return new EOLProcessorOutputStream(inputStyle, outputStyle, out); 261 } 262 263 public static InputStream createInputStream(int inputStyle, int outputStyle, InputStream in) 264 throws IOException { 265 return new EOLProcessorInputStream(inputStyle, outputStyle, in); 266 } 267 268 269 270 271 } 272 | Popular Tags |