2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9.mail.transport;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
import java.io.FilterOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public class EOLConvertingOutputStream extends FilterOutputStream
|
|
|
|
{
|
2010-02-04 21:36:52 -05:00
|
|
|
private int lastChar;
|
|
|
|
private boolean ignoreNextIfLF = false;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public EOLConvertingOutputStream(OutputStream out)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
super(out);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void write(int oneByte) throws IOException
|
|
|
|
{
|
2010-02-04 21:36:52 -05:00
|
|
|
if (!ignoreNextIfLF)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2010-02-04 21:36:52 -05:00
|
|
|
if ((oneByte == '\n') && (lastChar != '\r'))
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
super.write('\r');
|
|
|
|
}
|
2010-02-04 21:36:52 -05:00
|
|
|
super.write(oneByte);
|
|
|
|
lastChar = oneByte;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2010-02-04 21:36:52 -05:00
|
|
|
ignoreNextIfLF = false;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void flush() throws IOException
|
|
|
|
{
|
|
|
|
if (lastChar == '\r')
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
super.write('\n');
|
|
|
|
lastChar = '\n';
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-02-04 21:36:52 -05:00
|
|
|
// We have to ignore the next character if it is <LF>. Otherwise it
|
|
|
|
// will be expanded to an additional <CR><LF> sequence although it
|
|
|
|
// belongs to the one just completed.
|
|
|
|
ignoreNextIfLF = true;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
super.flush();
|
|
|
|
}
|
|
|
|
}
|