Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2004-2010 Oracle Corporation. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Kohsuke Kawaguchi /
package org.kohsuke.stapler.framework.io;
Finds the lone LF and converts that to CR+LF.

Internet Explorer's XmlHttpRequest.responseText seems to normalize the line end, and if we only send LF without CR, it will not recognize that as a new line. To work around this problem, we use this filter to always convert LF to CR+LF.

Author(s):
Kohsuke Kawaguchi
public /*for now, until Hudson migration completes*/ class LineEndNormalizingWriter extends FilterWriter {
    private boolean seenCR;
    public LineEndNormalizingWriter(Writer out) {
        super(out);
    }
    public void write(char cbuf[]) throws IOException {
        write(cbuf, 0, cbuf.length);
    }
    public void write(String strthrows IOException {
        write(str,0,str.length());
    }
    public void write(int cthrows IOException {
        if(! && c==)
            super.write("\r\n");
        else
            super.write(c);
         = (c==);
    }
    public void write(char cbuf[], int offint lenthrows IOException {
        int end = off+len;
        int writeBegin = off;
        forint i=offi<endi++ ) {
            char ch = cbuf[i];
            if(! && ch==) {
                // write up to the char before LF
                super.write(cbuf,writeBegin,i-writeBegin);
                super.write("\r\n");
                writeBegin=i+1;
            }
             = (ch==);
        }
        super.write(cbuf,writeBegin,end-writeBegin);
    }
    public void write(String strint offint lenthrows IOException {
        int end = off+len;
        int writeBegin = off;
        forint i=offi<endi++ ) {
            char ch = str.charAt(i);
            if(! && ch==) {
                // write up to the char before LF
                super.write(str,writeBegin,i-writeBegin);
                super.write("\r\n");
                writeBegin=i+1;
            }
             = (ch==);
        }
        super.write(str,writeBegin,end-writeBegin);
    }
    private static final int CR = 0x0D;
    private static final int LF = 0x0A;
New to GrepCode? Check out our FAQ X