package com.mindprod.welcome; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Arrays; import java.util.HashSet; /** * A persistent cache people who have posted to a given newsgroup. * @since 2002 April 20 */ public class Posters { /* constructor */ public Posters( String newsgroup ) { this.newsgroup = newsgroup; fireup(); } /** * Get list of everyone who has ever posted. * * @return */ public String[] getAllPosters() { String[] result = (String[])(cache.toArray(new String[cache.size()])); Arrays.sort(result); return result; } private String newsgroup; /* allow saving where we are in newsgroup headers as part of serialisation file */ private long lastHeaderNumber; public long getLastHeaderNumber () { return this.lastHeaderNumber; } public void setLastHeaderNumber (long lastHeaderNumber) { this.lastHeaderNumber = lastHeaderNumber; } /** * collection of people who have posted to a given newsgroup * raw names and emails exactly as they posted in FROM: field without any cleanup. */ private HashSet cache; /** * Has this person posted before? * * @param name name/email as appears on FROM: in newsgroup * posting. * @return true if has posted in that newsgroup before. */ public boolean hasPostedBefore(String name) { return cache.contains(name); } /** * Record the fact this person has posted to this newsgroup * It is ok to call hasPosted even if he hasPostedBefore, * though doing so is pointless. * * @param name name/email as appears on FROM: in newsgroup * posting. * @return true if has posted in that newsgroup before. */ public void hasPosted(String name) { if ( ! cache.contains(name) ) { cache.add(name); } } public void hasBeenRead(long headerNumber) { if ( headerNumber > lastHeaderNumber ) { lastHeaderNumber = headerNumber; } } /** * Called when class is loaded to reconstitute the * serialised cache. */ private void fireup() { try { // O P E N FileInputStream fis = new FileInputStream(newsgroup+".ser"); ObjectInputStream ois = new ObjectInputStream(fis); // R E A D lastHeaderNumber = ((Long)ois.readObject()).longValue(); cache = (HashSet) ois.readObject(); // C L O S E ois.close(); } catch ( Exception e ) { System.out.println("Troubles restoring the newsgroup.ser file. Not to worry, starting afresh."); e.printStackTrace(); cache = new HashSet(2053, 0.75f); lastHeaderNumber = 0; } } // end fireup /** * Called to save the cache * in serialised form * so you won't lose what you have so fare */ public void flush() { try { // O P E N FileOutputStream fos = new FileOutputStream(newsgroup+".ser", false /* append */); ObjectOutputStream oos = new ObjectOutputStream(fos); // W R I T E oos.writeObject(new Long(lastHeaderNumber)); oos.writeObject(cache); // C L O S E oos.close(); } catch ( IOException e ) { System.out.println("Troubles writing out the domaincache.ser file"); e.printStackTrace(); } } /** * Called when class is shut down to save the cache * in serialised form. */ public void close() { flush(); } // end close }