反序列化 MimeMessages JavaMail
在我的时区下午好。
我正在使用 exJello Provider 的 JavaMail api。我正在使用 SearchTerm 类来过滤检索到的消息,但搜索方法平均需要 1 分钟以上才能返回结果。因此,我决定以这种方式序列化一组消息,这样我就不必等待那么久了。所以我有一个理论问题和一个具体问题。 1)只有实现 Serialized 接口的类才能被序列化,所以我用来“序列化”此消息的方式并不是“真正”的序列化,对吗? 我的代码片段:message.writeTo("OutputStream");
2)现在我正在处理的问题: 代码片段:
messages = inbox.search(new AndTerm(terms));
ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream("serializer.txt"));
for(Message msg : messages){
msg.writeTo(stream);
}
在该过程结束时,我在文件“serializer.txt”中序列化了多条消息。我的问题是如何反序列化这些消息。我已经能够反序列化一条消息,但是如果文件包含多条消息,则只有第一条消息会被反序列化。 代码:
ObjectInputStream file = new ObjectInputStream(new FileInputStream("serializer.txt"));
new MimeMessage(session,file);
此代码将仅反序列化一条消息,但如果我制作一个 cicle,则只有第一条消息将再次反序列化。所以任何机构都面临着同样的问题。 PS->如果我尝试从任何 InputStream 使用 readObject 方法,它将检索异常,唯一的方法是使用 Message 构造函数。
致以最诚挚的问候
Good afternoon in my timezone.
I am working with JavaMail api with exJello Provider. I am using SearchTerm class to filter the retrieved messages but it takes in average more than 1 minute to the search method returns results.So i decided to serialize a set of messages this way i did not have to wait so long.so i have one theoretical issue and one specif issue.
1) Only the classes that implements the Serializable interface could be serialize so the way i use to "serialize" this messages is not a "really" serialization, right ?
Snippet of my code : message.writeTo("OutputStream");
2) Now the problem that i am dealing with:
Snippet of code:
messages = inbox.search(new AndTerm(terms));
ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream("serializer.txt"));
for(Message msg : messages){
msg.writeTo(stream);
}
In the end of the process i had serialize more than one message in the file "serializer.txt". My question is how can i deserialize those messages.I already am able to deserialize one messeage , but if the file contains more than one message only the first one get deserialize.
Code:
ObjectInputStream file = new ObjectInputStream(new FileInputStream("serializer.txt"));
new MimeMessage(session,file);
This code will deserialize just one message , but if i make a cicle only the first one will be again deserialize. So any body had face the same issue.
PS-> If i try to use the method readObject from any InputStream it will retrieve an exception, the only way is to use the Message constructor.
With the best regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试 ObjectInputStream.readObject( )
You can try ObjectInputStream.readObject()
您可以使用可序列化或可外部化的接口进行序列化。 Serialized 是一个标记接口,java 将为您完成序列化。如果您使用外部化,您可以编写自己的序列化方法。
如果我们按照序列化的定义,那么您所做的就可以称为序列化。因为您正在将对象写入文件并能够从中恢复。
对于下一部分:: 不要将所有序列化对象写入同一个文件中。为每个序列化对象动态创建一个文件。使用命名结构来识别您想要反序列化的正确文件,这将解决您的问题
You can use either serilizable or externalizable interface for serialization . Serializable is a marker interface and java will do the serialization for you . If you use externalizable you can write your own serialization method .
If we go according to the definition of serialization then what you are doing can be termed serialization . Coz you are writing your object to a file and are able to restore from it.
For the next part :: do not write all serialized objects in the same file . Create a file dynamically for each serialized object. Use a naming structure for identifying the correct file you want to desirialize and that will solve your problem