Java Mime4j:DefaultMessageBuilder无法解析mbox内容
我从Java Mime4j:DefaultMessageBuilder无法解析mbox内容,java,apache,parsing,mbox,mime4j,Java,Apache,Parsing,Mbox,Mime4j,我从subversion下载了mime4j0.8.0快照,并用maven构建了它。 可以找到我生成的相关jar 现在我尝试从mime4jtest解析 我用这个。简而言之: final File mbox = new File("c:\\mbox.rlug"); int count = 0; for (CharBufferWrapper message : MboxIterator.fromFile(mbox).charset(ENCODER.charset()).build()) { S
subversion
下载了mime4j
0.8.0快照,并用maven
构建了它。
可以找到我生成的相关jar
现在我尝试从mime4j
test解析
我用这个。简而言之:
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");
+
输出为:
消息null由:null发送到:null
消息null由:null发送到:null
消息null由:null发送到:null
消息null由:null发送到:null
消息null由:null发送到:null
找到5条消息
确实有5条消息,但为什么所有字段都为空?我下载了您的jar文件、您指向的示例代码和您指向的示例mbox文件,编译了示例(没有任何更改),并针对示例mbox文件运行了它 它按预期工作(字段包含预期数据,而不是空值)。 这是在Mac上使用的Java 1.6_0_65和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 windows主机、LINUX主机连接信息(la ZAPP) 发送人:rlug-bounce@lug.ro致:[rlug@lug.ro] 消息Re:RH 8.0引导软盘发送人:rlug-bounce@lug.ro 致:[rlug@lug.ro] 已发送消息Qmail mysql virtualusers+ssl+smtp验证+pop3 作者:rlug-bounce@lug.ro致:[rlug@lug.ro] 信息回复:Din windows ma pot、Din LINUX NU ma pot conecta(la ZAPP) 发送人:rlug-bounce@lug.ro致:[rlug@lug.ro] 消息LSTP问题-已解决发送人:rlug-bounce@lug.ro 致:[rlug@lug.ro] 在:108毫秒内发现5条消息 我发现了问题
DefaultMessageBuilder
无法解析具有windows行分隔符的mbox文件\r\n
。
用UNIX行分隔符替换它们时,解析工作正常
这是一个关键问题,因为从
Gmail
下载的mbox文件使用\r\n
基于@zvisofer-answer,我在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 by tearraybuffer 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文件:)我在两台windows机器上都失败了。我会试试其他的方法,你能在循环中打印原始消息,看看它的构造是否正确吗<代码>系统输出打印项次(消息)代码>您可能希望发布apache james项目的更改请求。我在社区的经验很好。这段代码导致5个构建测试失败(其中一个是错误)。如果“\r”后面没有“\n”我想它会失败是的,我想我的修复程序可以改进,在处理\r单独使用时:byte[]microsofstucks={(byte)'\r',(byte)'\n}代码>inti=indexOf(microsoftSucks)代码>修复了3个测试,但仍有两个测试失败
@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;
}
}