1 package hudson.util; 2 3 import java.io.FilterWriter ; 4 import java.io.Writer ; 5 import java.io.IOException ; 6 7 18 public class LineEndNormalizingWriter extends FilterWriter { 19 20 private boolean seenCR; 21 22 public LineEndNormalizingWriter(Writer out) { 23 super(out); 24 } 25 26 public void write(char cbuf[]) throws IOException { 27 write(cbuf, 0, cbuf.length); 28 } 29 30 public void write(String str) throws IOException { 31 write(str,0,str.length()); 32 } 33 34 public void write(int c) throws IOException { 35 if(!seenCR && c==LF) 36 super.write("\r\n"); 37 else 38 super.write(c); 39 seenCR = (c==CR); 40 } 41 42 public void write(char cbuf[], int off, int len) throws IOException { 43 int end = off+len; 44 int writeBegin = off; 45 46 for( int i=off; i<end; i++ ) { 47 char ch = cbuf[i]; 48 if(!seenCR && ch==LF) { 49 super.write(cbuf,writeBegin,i-writeBegin); 51 super.write("\r\n"); 52 writeBegin=i+1; 53 } 54 seenCR = (ch==CR); 55 } 56 57 super.write(cbuf,writeBegin,end-writeBegin); 58 } 59 60 public void write(String str, int off, int len) throws IOException { 61 int end = off+len; 62 int writeBegin = off; 63 64 for( int i=off; i<end; i++ ) { 65 char ch = str.charAt(i); 66 if(!seenCR && ch==LF) { 67 super.write(str,writeBegin,i-writeBegin); 69 super.write("\r\n"); 70 writeBegin=i+1; 71 } 72 seenCR = (ch==CR); 73 } 74 75 super.write(str,writeBegin,end-writeBegin); 76 } 77 78 private static final int CR = 0x0D; 79 private static final int LF = 0x0A; 80 } 81 | Popular Tags |