FileDocCategorySizeDatePackage
EOLConvertingInputStream.javaAPI DocAndroid 1.5 API3843Wed May 06 22:42:46 BST 2009org.apache.james.mime4j

EOLConvertingInputStream

public class EOLConvertingInputStream extends InputStream
InputStream which converts \r bytes not followed by \n and \n not preceded by \r to \r\n.
version
$Id: EOLConvertingInputStream.java,v 1.4 2004/11/29 13:15:42 ntherning Exp $

Fields Summary
public static final int
CONVERT_CR
Converts single '\r' to '\r\n'
public static final int
CONVERT_LF
Converts single '\n' to '\r\n'
public static final int
CONVERT_BOTH
Converts single '\r' and '\n' to '\r\n'
private PushbackInputStream
in
private int
previous
private int
flags
Constructors Summary
public EOLConvertingInputStream(InputStream in)
Creates a new EOLConvertingInputStream instance converting bytes in the given InputStream. The flag CONVERT_BOTH is the default.

param
in the InputStream to read from.

    
                                  
       
        this(in, CONVERT_BOTH);
    
public EOLConvertingInputStream(InputStream in, int flags)
Creates a new EOLConvertingInputStream instance converting bytes in the given InputStream.

param
in the InputStream to read from.
param
flags one of CONVERT_CR, CONVERT_LF or CONVERT_BOTH.

        super();
        
        this.in = new PushbackInputStream(in, 2);
        this.flags = flags;
    
Methods Summary
public voidclose()
Closes the underlying stream.

throws
IOException on I/O errors.

        in.close();
    
public intread()

see
java.io.InputStream#read()

        int b = in.read();
        
        if (b == -1) {
            return -1;
        }
        
        if ((flags & CONVERT_CR) != 0 && b == '\r") {
            int c = in.read();
            if (c != -1) {
                in.unread(c);
            }
            if (c != '\n") {
                in.unread('\n");
            }
        } else if ((flags & CONVERT_LF) != 0 && b == '\n" && previous != '\r") {
            b = '\r";
            in.unread('\n");
        }
        
        previous = b;
        
        return b;