我已经从mime4j
下载了subversion
0.8.0快照,并使用maven
构建了它。
我生成的相关jar可以在here中找到。
现在,我尝试从mime4j
测试中解析a toy mbox file。
我用这个sample code。简要地:
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());
}
输出为:
消息为空发送者:为空发送至:为空
消息为空发送者:为空发送至:为空
消息为空发送者:为空发送至:为空
消息为空发送者:为空发送至:为空
消息为空发送者:为空发送至:为空
找到5则讯息
确实有5条消息,但是为什么所有字段都为空?
最佳答案
基于@zvisofer的答案,我在BufferedLineReaderInputStream
中找到了guilty piece of code:
@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文件:)