1 21 22 27 28 package com.sun.mail.util; 29 30 import java.io.*; 31 32 33 39 public class CRLFOutputStream extends FilterOutputStream { 40 protected int lastb = -1; 41 protected static byte[] newline; 42 static { 43 newline = new byte[2]; 44 newline[0] = (byte)'\r'; 45 newline[1] = (byte)'\n'; 46 } 47 48 public CRLFOutputStream(OutputStream os) { 49 super(os); 50 } 51 52 public void write(int b) throws IOException { 53 if (b == '\r') { 54 out.write(newline); 55 } else if (b == '\n') { 56 if (lastb != '\r') 57 out.write(newline); 58 } else { 59 out.write(b); 60 } 61 lastb = b; 62 } 63 64 public void write(byte b[]) throws IOException { 65 write(b, 0, b.length); 66 } 67 68 public void write(byte b[], int off, int len) throws IOException { 69 int start = off; 70 71 len += off; 72 for (int i = start; i < len ; i++) { 73 if (b[i] == '\r') { 74 out.write(b, start, i - start); 75 out.write(newline); 76 start = i + 1; 77 } else if (b[i] == '\n') { 78 if (lastb != '\r') { 79 out.write(b, start, i - start); 80 out.write(newline); 81 } 82 start = i + 1; 83 } 84 lastb = b[i]; 85 } 86 if ((len - start) > 0) 87 out.write(b, start, len - start); 88 } 89 90 93 public void writeln() throws IOException { 94 out.write(newline); 95 } 96 } 97 | Popular Tags |