Encode mbox files

pull/197/head
M66B 4 years ago
parent ff0f874b91
commit c6825d4e1d

@ -907,17 +907,63 @@ public class FragmentFolders extends FragmentBase {
Message imessage = MessageHelper.from(context, message, null, isession, false); Message imessage = MessageHelper.from(context, message, null, isession, false);
imessage.writeTo(new FilterOutputStream(out) { imessage.writeTo(new FilterOutputStream(out) {
private int pos = 0;
private boolean cr = false;
private boolean lf = false;
private byte[] buffer = new byte[998];
@Override @Override
public void write(int b) throws IOException { public void write(int b) throws IOException {
if (b != 13) if (b == 13) {
out.write(b); if (cr)
line();
else
cr = true;
} else if (b == 10) {
if (lf)
line();
else
lf = true;
} else {
if (cr || lf)
line();
if (pos == buffer.length)
throw new IOException("Line too long");
buffer[pos++] = (byte) b;
}
} }
@Override @Override
public void flush() throws IOException { public void flush() throws IOException {
if (pos > 0 || cr || lf)
line();
out.write(10); out.write(10);
super.flush(); super.flush();
} }
private void line() throws IOException {
int i = 0;
for (; i < pos; i++)
if (buffer[i] != '>')
break;
if (i + 4 < pos &&
buffer[i + 0] == 'F' &&
buffer[i + 1] == 'r' &&
buffer[i + 2] == 'o' &&
buffer[i + 3] == 'm' &&
buffer[i + 4] == ' ')
out.write('>');
for (i = 0; i < pos; i++)
out.write(buffer[i]);
out.write(10);
pos = 0;
cr = false;
lf = false;
}
}); });
} }
} }

Loading…
Cancel
Save