📄 nntpstore.java
字号:
throw new MessagingException(command+" failed: "+response);
}
} catch (IOException e) {
throw new MessagingException(command+" failed", e);
} catch (NumberFormatException e) {
throw new MessagingException(command+" failed", e);
}
overviewFormat = new String[vector.size()]; vector.copyInto(overviewFormat);
strippedOverviewFormat = new String[vector2.size()]; vector2.copyInto(strippedOverviewFormat);
if (session.getDebug()) {
StringBuffer buffer = new StringBuffer();
buffer.append("DEBUG: nntp: overview format: ");
for (int i=0; i<strippedOverviewFormat.length;i++) {
if (i>0)
buffer.append(", ");
buffer.append(strippedOverviewFormat[i]);
}
System.out.println(buffer.toString());
}
}
}
if (strip)
return strippedOverviewFormat;
else
return overviewFormat;
}
// Prefetches header information for the specified messages, if possible
Article[] getOverview(Newsgroup newsgroup) throws MessagingException {
String name = newsgroup.getName();
String[] format = getOverviewFormat(false);
String command = "GROUP "+name;
if (current==null || !name.equals(current.getName())) { // select the group
synchronized (this) {
try {
send(command);
switch (getResponse()) {
case GROUP_SELECTED:
try {
updateGroup(newsgroup, response);
current = newsgroup;
} catch(NumberFormatException e) {
throw new MessagingException("NNTP protocol exception: "+response, e);
}
case NO_SUCH_GROUP:
throw new MessagingException("No such group");
case SERVER_ERROR:
if (response.toLowerCase().indexOf("timeout")>-1) {
close();
connect();
return getOverview(newsgroup);
}
default:
throw new MessagingException(command+" failed: "+response);
}
} catch (IOException e) {
throw new MessagingException(command+" failed", e);
}
}
}
command = "XOVER "+newsgroup.first+"-"+newsgroup.last;
Vector av = new Vector(Math.max(newsgroup.last-newsgroup.first, 10));
synchronized (this) {
try {
send(command);
switch (getResponse()) {
case LISTING_OVERVIEW:
String line;
int count = 0, length = (newsgroup.last-newsgroup.first);
processStatusEvent(new StatusEvent(this, StatusEvent.OPERATION_START, "Retrieving messages", 0, length, 0));
for (line=in.readLine(); line!=null && !".".equals(line); line = in.readLine()) {
int tabIndex = line.indexOf('\t');
if (tabIndex>-1) {
int msgnum = Integer.parseInt(line.substring(0, tabIndex));
Article article = new Article(newsgroup, msgnum);
article.addXoverHeaders(getOverviewHeaders(format, line, tabIndex));
av.addElement(article);
} else
throw new ProtocolException("Invalid overview line format");
if ((++count%50) == 0)
processStatusEvent(new StatusEvent(this, StatusEvent.OPERATION_UPDATE, "Retrieved "+count+" of "+length+" messages", 0, length, count));
}
processStatusEvent(new StatusEvent(this, StatusEvent.OPERATION_END, "Done", 0, length, length));
break;
case NO_ARTICLE_SELECTED:
case PERMISSION_DENIED:
break;
case NO_GROUP_SELECTED:
case SERVER_ERROR:
default:
throw new MessagingException(command+" failed: "+response);
}
} catch (IOException e) {
throw new MessagingException(command+" failed", e);
} catch (NumberFormatException e) {
throw new MessagingException(command+" failed", e);
}
}
Article[] articles = new Article[av.size()]; av.copyInto(articles);
return articles;
}
// Returns an InternetHeaders object representing the headers stored in an xover response line.
InternetHeaders getOverviewHeaders(String[] format, String line, int startIndex) {
InternetHeaders headers = new InternetHeaders();
for (int i=0; i<format.length; i++) {
int colonIndex = format[i].indexOf(':');
String key = format[i].substring(0, colonIndex);
boolean full = "full".equals(format[i].substring(colonIndex+1, format[i].length()));
int tabIndex = line.indexOf('\t', startIndex+1);
if (tabIndex<0) tabIndex = line.length();
String value = line.substring(startIndex+1, tabIndex);
if (full)
value = value.substring(value.indexOf(':')+1).trim();
headers.addHeader(key, value);
startIndex = tabIndex;
}
return headers;
}
boolean validateOverviewHeader(String key) throws MessagingException {
String[] format = getOverviewFormat(true);
for (int i=0; i<format.length; i++) {
if (key.equalsIgnoreCase(format[i]))
return true;
}
return false;
}
public void addStatusListener(StatusListener l) {
synchronized (statusListeners) {
statusListeners.addElement(l);
}
}
public void removeStatusListener(StatusListener l) {
synchronized (statusListeners) {
statusListeners.removeElement(l);
}
}
protected void processStatusEvent(StatusEvent event) {
StatusListener[] listeners;
synchronized (statusListeners) {
listeners = new StatusListener[statusListeners.size()];
statusListeners.copyInto(listeners);
}
switch (event.getType()) {
case StatusEvent.OPERATION_START:
for (int i=0; i<listeners.length; i++)
listeners[i].statusOperationStarted(event);
break;
case StatusEvent.OPERATION_UPDATE:
for (int i=0; i<listeners.length; i++)
listeners[i].statusProgressUpdate(event);
break;
case StatusEvent.OPERATION_END:
for (int i=0; i<listeners.length; i++)
listeners[i].statusOperationEnded(event);
break;
}
}
/*
* Manages multiplexing of store connections.
*/
static Hashtable stores;
static void addStore(NNTPStore store) {
if (stores==null)
stores = new Hashtable();
stores.put(store.socket, store);
}
static void removeStore(NNTPStore store) {
stores.remove(store.socket);
}
static NNTPStore getStore(InetAddress address, int port) {
if (stores==null)
return null;
for (Enumeration enum = stores.keys(); enum.hasMoreElements(); ) {
Socket ss = (Socket)enum.nextElement();
InetAddress sa = ss.getInetAddress();
int sp = ss.getPort();
if (sp==port && sa.equals(address))
return (NNTPStore)stores.get(ss);
}
return null;
}
/**
* The root holds the newsgroups in an NNTPStore.
*/
class Root extends Folder {
/**
* Constructor.
*/
protected Root(Store store) { super(store); }
/**
* Returns the name of this folder.
*/
public String getName() { return "/"; }
/**
* Returns the full name of this folder.
*/
public String getFullName() { return getName(); }
/**
* Returns the type of this folder.
*/
public int getType() throws MessagingException { return HOLDS_FOLDERS; }
/**
* Indicates whether this folder exists.
*/
public boolean exists() throws MessagingException { return true; }
/**
* Indicates whether this folder contains any new articles.
*/
public boolean hasNewMessages() throws MessagingException { return false; }
/**
* Opens this folder.
*/
public void open(int mode) throws MessagingException {
if (mode!=this.READ_ONLY) throw new MessagingException("Folder is read-only");
}
/**
* Closes this folder.
*/
public void close(boolean expunge) throws MessagingException {}
/**
* Expunges this folder.
*/
public Message[] expunge() throws MessagingException { return new Message[0]; }
/**
* Indicates whether this folder is open.
*/
public boolean isOpen() { return true; }
/**
* Returns the permanent flags for this folder.
*/
public Flags getPermanentFlags() { return new Flags(); }
/**
* Returns the number of articles in this folder.
*/
public int getMessageCount() throws MessagingException { return 0; }
/**
* Returns the articles in this folder.
*/
public Message[] getMessages() throws MessagingException {
throw new MessagingException("Folder can't contain messages");
}
/**
* Returns the specified message in this folder.
* Since NNTP articles are not stored in sequential order,
* the effect is just to reference articles returned by getMessages().
*/
public Message getMessage(int msgnum) throws MessagingException {
throw new MessagingException("Folder can't contain messages");
}
/**
* Root folder is read-only.
*/
public void appendMessages(Message aarticle[]) throws MessagingException {
throw new MessagingException("Folder is read-only");
}
/**
* Does nothing.
*/
public void fetch(Message articles[], FetchProfile fetchprofile) throws MessagingException {
}
/**
* This folder does not have a parent.
*/
public Folder getParent() throws MessagingException { return null; }
/**
* Returns the newsgroups on the server.
*/
public Folder[] list() throws MessagingException {
return getNewsgroups("LIST ACTIVE", false);
}
/**
* Returns the newsgroups on the server.
*/
public Folder[] list(String pattern) throws MessagingException {
return getNewsgroups(pattern, false);
}
/**
* Returns the subscribed newsgroups on the server.
*/
public Folder[] listSubscribed() throws MessagingException {
Vector groups = new Vector();
for (Enumeration enum = newsgroups.elements(); enum.hasMoreElements(); ) {
Newsgroup group = (Newsgroup)enum.nextElement();
if (group.subscribed)
groups.addElement(group);
}
Folder[] list = new Folder[groups.size()]; groups.copyInto(list);
return list;
}
/**
* Returns the subscribed newsgroups on the server.
*/
public Folder[] listSubscribed(String pattern) throws MessagingException {
return listSubscribed();
}
/**
* Returns the newsgroup with the specified name.
*/
public Folder getFolder(String name) throws MessagingException {
return getNewsgroup(name);
}
/**
* Returns the separator character.
*/
public char getSeparator() throws MessagingException {
return '.';
}
/**
* Root folders cannot be created, deleted, or renamed.
*/
public boolean create(int i) throws MessagingException {
throw new MessagingException("Folder cannot be created");
}
/**
* Root folders cannot be created, deleted, or renamed.
*/
public boolean delete(boolean flag) throws MessagingException {
throw new MessagingException("Folder cannot be deleted");
}
/**
* Root folders cannot be created, deleted, or renamed.
*/
public boolean renameTo(Folder folder) throws MessagingException {
throw new MessagingException("Folder cannot be renamed");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -