1 38 39 import java.io.*; 40 41 50 public class CRLFOutputStream extends FilterOutputStream { 51 protected int lastb = -1; 52 protected static byte[] newline; 53 static { 54 newline = new byte[2]; 55 newline[0] = (byte)'\r'; 56 newline[1] = (byte)'\n'; 57 } 58 59 public CRLFOutputStream(OutputStream os) { 60 super(os); 61 } 62 63 public void write(int b) throws IOException { 64 if (b == '\r') { 65 out.write(newline); 66 } else if (b == '\n') { 67 if (lastb != '\r') 68 out.write(newline); 69 } else { 70 out.write(b); 71 } 72 lastb = b; 73 } 74 75 public void write(byte b[]) throws IOException { 76 write(b, 0, b.length); 77 } 78 79 public void write(byte b[], int off, int len) throws IOException { 80 int start = off; 81 82 len += off; 83 for (int i = start; i < len ; i++) { 84 if (b[i] == '\r') { 85 out.write(b, start, i - start); 86 out.write(newline); 87 start = i + 1; 88 } else if (b[i] == '\n') { 89 if (lastb != '\r') { 90 out.write(b, start, i - start); 91 out.write(newline); 92 } 93 start = i + 1; 94 } 95 lastb = b[i]; 96 } 97 if ((len - start) > 0) 98 out.write(b, start, len - start); 99 } 100 } 101 | Popular Tags |