- public StoredMessage get(int messagenum) throws IOException {
- File f = new File(path + File.separatorChar + messagenum + ".");
- if (!f.exists()) throw new FileNotFoundException(f);
- return StoredMessage.undump(new FileInputStream(f));
- }
+ public int[] list() {
+ String[] names = new File(path).list();
+ int[] ret = new int[names.length];
+ for(int i=0, j=0; j<ret.length; i++, j++) {
+ try {
+ ret[j] = Integer.parseInt(names[i].substring(0, names[i].length - 1));
+ } catch (NumberFormatException nfe) {
+ Log.warn(FileBased.class, "NumberFormatException: " + names[i].substring(0, names[i].length - 1));
+ j--;
+ int[] newret = new int[ret.length - 1];
+ System.arrayCopy(ret, 0, newret, 0, newret.length);
+ ret = newret;
+ }
+ }
+ return ret;
+ }
+
+ /** returns a message identifier */
+ public synchronized int add(StoredMessage message) throws IOException {
+ int[] all = list();
+ int max = 0;
+ for(int i=0; i<all.length; i++) max = Math.max(max, all[i]);
+ int target = max++;
+ File f = new File(path + File.separatorChar + max + ".-");
+ FileOutputStream fo = new FileOutputStream(f);
+ message.dump(fo);
+ fo.close();
+ f.renameTo(path + File.separatorChar + max + ".");
+ return target;
+ }
+
+ public StoredMessage get(int messagenum) throws IOException {
+ File f = new File(path + File.separatorChar + messagenum + ".");
+ if (!f.exists()) throw new FileNotFoundException(f);
+ return StoredMessage.undump(new FileInputStream(f));
+ }
+
+ // query types: stringmatch (headers, body), header element, deletion status, date range, message size
+ public StoredMessage[] query(int maxResults) {
+ throw new RuntimeException("FileBased.query() not implemented yet");
+ }