我从subversion下载了subversion 0.8.0快照,并使用maven构建了它。我生成的相关jars可以找到这里。
现在,我尝试从玩具mbox文件测试中解析mime4j。
我使用这个样本代码。简要说明:
final File mbox = new File("c:\\mbox.rlug");
int count = 0;
for (CharBufferWrapper message : MboxIterator.fromFile(mbox).charset(ENCODER.charset()).build()) {
System.out.println(messageSummary(message.asInputStream(ENCODER.charset())));
count++;
}
System.out.println("Found " + count + " messages");+
private static String messageSummary(InputStream messageBytes) throws IOException, MimeException {
MessageBuilder builder = new DefaultMessageBuilder();
Message message = builder.parseMessage(messageBytes);
return String.format("\nMessage %s \n" +
"Sent by:\t%s\n" +
"To:\t%s\n",
message.getSubject(),
message.getSender(),
message.getTo());
}产出如下:
消息null由: null发送到: null 消息null由: null发送到: null 消息null由: null发送到: null 消息null由: null发送到: null 消息null由: null发送到: null 找到5条消息
确实有5条消息,但为什么所有字段都为空?
发布于 2014-12-02 13:40:35
我发现了问题。
DefaultMessageBuilder无法解析带有windows行分隔符\r\n的mbox文件。用UNIX行分隔符\n替换它们时,解析工作。
这是一个关键问题,因为从Gmail下载的mbox文件使用\r\n。
发布于 2014-12-03 12:09:53
基于@zvisofer的回答,我在BufferedLineReaderInputStream中找到了BufferedLineReaderInputStream中的
@Override
public int readLine(final ByteArrayBuffer dst)
throws MaxLineLimitException, IOException {
if (dst == null) {
throw new IllegalArgumentException("Buffer may not be null");
}
if (!readAllowed()) return -1;
int total = 0;
boolean found = false;
int bytesRead = 0;
while (!found) {
if (!hasBufferedData()) {
bytesRead = fillBuffer();
if (bytesRead == -1) {
break;
}
}
int i = indexOf((byte)'\n');
int chunk;
if (i != -1) {
found = true;
chunk = i + 1 - pos();
} else {
chunk = length();
}
if (chunk > 0) {
dst.append(buf(), pos(), chunk);
skip(chunk);
total += chunk;
}
if (this.maxLineLen > 0 && dst.length() >= this.maxLineLen) {
throw new MaxLineLimitException("Maximum line length limit exceeded");
}
}
if (total == 0 && bytesRead == -1) {
return -1;
} else {
return total;
}
}最好的办法是报告错误,但这里有一个修复,有点脏,但它运行良好
在项目中创建类org.apache.james.mime4j.io.BufferedLineReaderInputStream
用以下方法替换public int readLine(final ByteArrayBuffer dst)方法:
@Override
public int readLine(final ByteArrayBuffer dst)
throws MaxLineLimitException, IOException {
if (dst == null) {
throw new IllegalArgumentException("Buffer may not be null");
}
if (!readAllowed()) return -1;
int total = 0;
boolean found = false;
int bytesRead = 0;
while (!found) {
if (!hasBufferedData()) {
bytesRead = fillBuffer();
if (bytesRead == -1) {
break;
}
}
int chunk;
int i = indexOf((byte)'\r');
if (i != -1) {
found = true;
chunk = i + 2 - pos();
} else {
i = indexOf((byte)'\n');
if (i != -1) {
found = true;
chunk = i + 1 - pos();
} else {
chunk = length();
}
}
if (chunk > 0) {
dst.append(buf(), pos(), chunk);
skip(chunk);
total += chunk;
}
if (this.maxLineLen > 0 && dst.length() >= this.maxLineLen) {
throw new MaxLineLimitException("Maximum line length limit exceeded");
}
}
if (total == 0 && bytesRead == -1) {
return -1;
} else {
return total;
}
}同时享受unix和dos文件:)
发布于 2014-12-01 14:04:22
我下载了jar文件、指向的示例代码和指向的示例mbox文件,编译了示例(没有任何更改),并针对示例mbox文件运行它。
它按预期工作(字段包含预期数据,而不是空值)。这是在带有Java 1.6__65的Mac上,也在1.8.0_11上。
产出如下:
$ java -cp .:apache-mime4j-core-0.8.0-SNAPSHOT.jar:apache-mime4j-dom-0.8.0-SNAPSHOT.jar:apache-mime4j-mbox-iterator-0.8.0-SNAPSHOT.jar IterateOverMbox mbox.rlug.txt 消息Din windows ma pot,din LINUX NU ma pot conecta (la ZAPP),由rlug-弹跳@lug.ro发送到: rlug@lug.ro 消息Re: RH 8.0引导软盘,由:rlug-弹跳@lug.ro发送到: rlug@lug.ro 消息Qmail mysql虚拟用户+ssl + smtp auth +pop3 3发送的:rlug-弹跳@lug.ro到: rlug@lug.ro 消息Re: Din windows ma pot,din LINUX NU ma pot conecta (la ZAPP),发送地址:rlug-弹跳@lug.ro到: rlug@lug.ro 消息LSTP问题-已解决,由:rlug-弹跳@lug.ro发送到: rlug@lug.ro 在108毫秒内找到5条消息
https://stackoverflow.com/questions/27201605
复制相似问题