📄 maildirfolder.java
字号:
*/
public synchronized void deleteMessages(String[] msglist) throws MessagingException {
if (msglist == null || msglist.length <=0) throw new MessagingException("Error Message List");
for(int i = 0;i < msglist.length; i++) {
int index = -1;
if ((index = hasMessage(msglist[i])) >= 0) {
File file = new File(this.getFullName() + this.getSeparator() + msglist[i]);
file.delete();
msgsummarys.remove(index);
messages.remove(index);
}
else throw new MessagingException("Error Message Name :" + msglist[i]);
}
refresh();
}
/**
* 更新消息数和新消息数
*/
public void refresh() {
summary.setMessageCount(msgsummarys.size());
for(int i = 0;i < summary.getMessageCount(); i++)
if (((MaildirMessageSummary)msgsummarys.get(i)).isNewMessage())
summary.incNewMessageCount();
}
/**
* 移动消息到目标邮件夹
* @param folder 目标邮件夹
* @param msglist 消息列表
*/
public synchronized void Moveto(Folder folder,String[] msglist) throws MessagingException {
if (folder !=null && folder.exists()) {
if (msglist == null || msglist.length <=0) throw new MessagingException("Error Message List");
for(int i = 0;i < msglist.length; i++) {
int index = -1;
if ((index = hasMessage(msglist[i])) >= 0) {
File file = new File(getFullName() + getSeparator() + msglist[i]);
file.renameTo(new File(folder.getFullName() + folder.getSeparator() + msglist[i]));
if (folder.isOpen()) {//插入队列
((MaildirFolder)folder).appendMessage((Message)messages.get(index));
}
msgsummarys.remove(index);
messages.remove(index);
//DEBUG
if (((MaildirStore)store).getSession().getDebug())
System.err.println("DEBUG: Message " + msglist[i] + " has been moved to Folder " + folder.getName());
}
else throw new MessagingException("Error Message Name");
}
refresh();
}
else throw new MessagingException("Folder isn't exists");
}
/**
* 移动消息到目标邮件夹
* @param folder 目标邮件夹
* @param msgname 消息名称
*/
public void Moveto(Folder folder,String msgname) throws MessagingException {
if (folder!=null && folder.exists()) {
if (msgname == null) throw new MessagingException("Error Message Name");
String[] msglist = { msgname };
Moveto(folder,msglist);
}
else throw new MessagingException("Folder isn't exists");
}
/**
* 邮件夹是否打开
* @return boolean 是否打开
*/
public boolean isOpen() {
return summary.isOpen();
}
/**
* 接收消息
*/
public void popMessages() throws MessagingException {
int origcount = getMessageCount();
int count = summary.list(new MaildirFilenameFilter('*')).length;
if (count > origcount) {
int start = 0,end = count - origcount - 1; //实际要取到数据库里的消息起始和结尾索引号
MaildirMessageSummary[] mms = new MaildirMessageSummary[end - start + 1]; //需要调入的消息
listMessageSummarys();
msgsummarys.subList(start,end+1).toArray(mms);
if (mms != null) {
for(int i = mms.length - 1; i >= 0; i--) {
MaildirMessage m = new MaildirMessage(this,mms[i],i);
messages.insertElementAt(m,0);
}
}
}
}
/**
* 返回索引号位msgnum的消息
* @param msgnum 消息索引号
*/
public Message getMessage(String msgname) throws MessagingException {
if (messages.size()<=0) throw new MessagingException("ERROR:No mail in the Folder or it have not been load");
int index;
if ((index = hasMessage(msgname)) >= 0) {
return (MaildirMessage)messages.get(index);
}
throw new MessagingException("No such message or the message has not been load");
}
/**
* 返回下一消息的消息名
* @param msgname 消息名
* @return String 下一消息名
*/
public String getNextMessageID(String msgname) throws MessagingException {
if (messages.size()<=0) throw new MessagingException("ERROR:No mail in the Folder or it have not been load");
int index;
if ((index = hasMessage(msgname)) >= 0) {
if (index < messages.size()-1) {
return ((MaildirMessage)messages.get(index+1)).getMessageID();
}
else if ( index < summary.getMessageCount() - 1) { //调出下一消息
getMessages(messages.size(),index+1);
return ((MaildirMessage)messages.get(messages.size()-1)).getMessageID();
}
else return null;
}
throw new MessagingException("No such message");
}
/**
* 返回上一消息的消息名
* @param msgname 消息名
* @return String 上一消息名
*/
public String getPrevMessageID(String msgname) throws MessagingException {
if (messages.size()<=0) throw new MessagingException("ERROR:No mail in the Folder or it have not been load");
int index;
if ((index = hasMessage(msgname)) >= 0) {
if (index == 0) return null;
else return ((MaildirMessage)messages.get(index-1)).getMessageID();
}
throw new MessagingException("No such message");
}
/**
* 返回消息索引号start -- end的消息
* @param start 起始索引号
* @param end 结尾索引号
* @return Message[] 消息数组
*/
public Message[] getMessages(int start,int end) throws MessagingException {
if (!isOpen()) this.open(summary.getType());
if (summary.getMessageCount() <= 0) return new Message[0];
if (start< 0 || end <= start && end >= summary.getMessageCount()) throw new MessagingException("Error start index or end index");
if (start < messages.size() && end < messages.size()) { //已调出
Message[] m = new MaildirMessage[(end-start+1)];
for(int i = start; i <= end; i++) {
m[i - start] = (Message)messages.get(i);
}
return m;
}
else {//未调出
int newStart = messages.size(),newEnd = end; //实际要取到数据库里的消息起始和结尾索引号
for(int i = newStart; i <= newEnd;i++) {
Message tempMess = new MaildirMessage(this,(MaildirMessageSummary)msgsummarys.get(i),i);
messages.add(tempMess);
}
Message[] m = new MaildirMessage[end - start + 1];
for(int i = start;i <= end;i++) {
m[i - start] = (Message)messages.get(i);
}
return m;
}
}
/**
* 取得所有消息
* @return Message[] 所有消息
*/
public Message[] getMessages() throws MessagingException {
if (!isOpen()) this.open(summary.getType());
if (summary.getMessageCount() <= 0) return new Message[0];
messages.removeAllElements();
return getMessages(0,summary.getMessageCount()-1);
}
/**
* 保存消息
* @param m 消息
* @param flag 标志位
* @return String 消息ID
*/
public String save(Message m,char flag) throws MessagingException {
String filename = String.valueOf(System.currentTimeMillis()/1000) + '.' + flag + '.' +
java.lang.Math.abs(new java.util.Random().nextInt()) % 10000;
if (saveas(m,flag,filename))
return filename;
else
return null;
}
/**
* 保存消息为MessageID
* @param m 消息
* @param flag 标志位
* @param filename 消息名称
* @return boolean 是否成功
*/
public boolean saveas(Message m,char flag,String filename) throws MessagingException{
try{
File file = new File(getFullName() + getSeparator() + filename);
if (file.exists()&&file.isFile()) file.delete();
FileOutputStream fos = null;
try {
fos= new FileOutputStream(file);
m.writeTo(fos);
fos.close();
return true;
}
catch(Exception e) {
e.printStackTrace();
return false;
}
finally{
if(fos!=null) fos.close();
}
}catch(Exception e){
e.printStackTrace();
return false;
}
}
/**
* 获取新的邮件(不支持)
*/
public void fetch(Message[] msglist,FetchProfile fetchprofile) throws MessagingException {
throw new MessagingException("Not Support");
}
/**
* 返回上一级邮件夹
* @return Folder 上一级邮件夹
*/
public Folder getParent() throws MessagingException {
File file = new File(summary.folderdir.getParent());
if (((MaildirStore)store).hasFolder(file.getName()))
return store.getFolder(file.getName());
else return null;
}
/**
* 返回邮件夹分隔符
* @return char 分隔符
*/
public char getSeparator() {
return File.separatorChar;
}
/**
* 不支持此方法
*/
public boolean renameTo(Folder folder) throws MessagingException {
throw new MessagingException("Not Support the Method");
}
/**
* 不支持此方法
*/
public Flags getPermanentFlags() {
//throw new MessagingException("Not Support the Method");
return null;
}
/**
* 不支持此方法
*/
public boolean create(int type) throws MessagingException{
throw new MessagingException("Not Support the Method");
}
/**
* 不支持此方法
*/
public Message[] expunge() throws MessagingException {
throw new MessagingException("Not Support the Method");
}
/**
* 不支持此方法
*/
public boolean delete(boolean sure) throws MessagingException {
throw new MessagingException("Not Support the Method");
}
/**
* 不支持此方法
*/
public Message getMessage(int msgnum) throws MessagingException {
throw new MessagingException("Not Support the Method");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -