1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-26 09:38:52 -05:00

Fixed EOLConvertingOutputStream.

write('\r');flush();write('\n'); would lead to "\r\n\r\n" instead of simply "\r\n";
This commit is contained in:
cketti 2010-02-05 02:36:52 +00:00
parent 0422cae33e
commit 14ee970b18

View File

@ -6,7 +6,8 @@ import java.io.OutputStream;
public class EOLConvertingOutputStream extends FilterOutputStream public class EOLConvertingOutputStream extends FilterOutputStream
{ {
int lastChar; private int lastChar;
private boolean ignoreNextIfLF = false;
public EOLConvertingOutputStream(OutputStream out) public EOLConvertingOutputStream(OutputStream out)
{ {
@ -16,16 +17,17 @@ public class EOLConvertingOutputStream extends FilterOutputStream
@Override @Override
public void write(int oneByte) throws IOException public void write(int oneByte) throws IOException
{ {
if (oneByte == '\n') if (!ignoreNextIfLF)
{ {
if (lastChar != '\r') if ((oneByte == '\n') && (lastChar != '\r'))
{ {
super.write('\r'); super.write('\r');
} }
}
super.write(oneByte); super.write(oneByte);
lastChar = oneByte; lastChar = oneByte;
} }
ignoreNextIfLF = false;
}
@Override @Override
public void flush() throws IOException public void flush() throws IOException
@ -34,6 +36,11 @@ public class EOLConvertingOutputStream extends FilterOutputStream
{ {
super.write('\n'); super.write('\n');
lastChar = '\n'; lastChar = '\n';
// 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;
} }
super.flush(); super.flush();
} }