1 38 39 import java.io.*; 40 41 49 public class NewlineOutputStream extends FilterOutputStream { 50 private int lastb = -1; 51 private static byte[] newline; 52 53 public NewlineOutputStream(OutputStream os) { 54 super(os); 55 if (newline == null) { 56 String s = System.getProperty("line.separator"); 57 if (s == null || s.length() <= 0) 58 s = "\n"; 59 newline = new byte[s.length()]; 60 s.getBytes(0, s.length(), newline, 0); 61 } 62 } 63 64 public void write(int b) throws IOException { 65 if (b == '\r') { 66 out.write(newline); 67 } else if (b == '\n') { 68 if (lastb != '\r') 69 out.write(newline); 70 } else { 71 out.write(b); 72 } 73 lastb = b; 74 } 75 76 public void write(byte b[]) throws IOException { 77 write(b, 0, b.length); 78 } 79 80 public void write(byte b[], int off, int len) throws IOException { 81 for (int i = 0 ; i < len ; i++) { 82 write(b[off + i]); 83 } 84 } 85 } 86 | Popular Tags |