PageRenderTime 34ms CodeModel.GetById 11ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/hudson-core/src/main/java/hudson/util/LineEndNormalizingWriter.java

http://github.com/hudson/hudson
Java | 104 lines | 53 code | 14 blank | 37 comment | 8 complexity | 01def6deb1c53178f7c1d25d2a83766e MD5 | raw file
  1/*
  2 * The MIT License
  3 * 
  4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
  5 * 
  6 * Permission is hereby granted, free of charge, to any person obtaining a copy
  7 * of this software and associated documentation files (the "Software"), to deal
  8 * in the Software without restriction, including without limitation the rights
  9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10 * copies of the Software, and to permit persons to whom the Software is
 11 * furnished to do so, subject to the following conditions:
 12 * 
 13 * The above copyright notice and this permission notice shall be included in
 14 * all copies or substantial portions of the Software.
 15 * 
 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22 * THE SOFTWARE.
 23 */
 24package hudson.util;
 25
 26import java.io.FilterWriter;
 27import java.io.Writer;
 28import java.io.IOException;
 29
 30/**
 31 * Finds the lone LF and converts that to CR+LF.
 32 *
 33 * <p>
 34 * Internet Explorer's <tt>XmlHttpRequest.responseText</tt> seems to
 35 * normalize the line end, and if we only send LF without CR, it will
 36 * not recognize that as a new line. To work around this problem,
 37 * we use this filter to always convert LF to CR+LF.
 38 *
 39 * @author Kohsuke Kawaguchi
 40 * @deprecated since 2008-05-28. moved to stapler
 41 */
 42public class LineEndNormalizingWriter extends FilterWriter {
 43
 44    private boolean seenCR;
 45
 46    public LineEndNormalizingWriter(Writer out) {
 47        super(out);
 48    }
 49
 50    public void write(char cbuf[]) throws IOException {
 51        write(cbuf, 0, cbuf.length);
 52    }
 53
 54    public void write(String str) throws IOException {
 55        write(str,0,str.length());
 56    }
 57
 58    public void write(int c) throws IOException {
 59        if(!seenCR && c==LF)
 60            super.write("\r\n");
 61        else
 62            super.write(c);
 63        seenCR = (c==CR);
 64    }
 65
 66    public void write(char cbuf[], int off, int len) throws IOException {
 67        int end = off+len;
 68        int writeBegin = off;
 69
 70        for( int i=off; i<end; i++ ) {
 71            char ch = cbuf[i];
 72            if(!seenCR && ch==LF) {
 73                // write up to the char before LF
 74                super.write(cbuf,writeBegin,i-writeBegin);
 75                super.write("\r\n");
 76                writeBegin=i+1;
 77            }
 78            seenCR = (ch==CR);
 79        }
 80
 81        super.write(cbuf,writeBegin,end-writeBegin);
 82    }
 83
 84    public void write(String str, int off, int len) throws IOException {
 85        int end = off+len;
 86        int writeBegin = off;
 87
 88        for( int i=off; i<end; i++ ) {
 89            char ch = str.charAt(i);
 90            if(!seenCR && ch==LF) {
 91                // write up to the char before LF
 92                super.write(str,writeBegin,i-writeBegin);
 93                super.write("\r\n");
 94                writeBegin=i+1;
 95            }
 96            seenCR = (ch==CR);
 97        }
 98
 99        super.write(str,writeBegin,end-writeBegin);
100    }
101
102    private static final int CR = 0x0D;
103    private static final int LF = 0x0A;
104}