1 17 18 package org.apache.james.imapserver; 19 20 import java.io.*; 21 import java.util.*; 22 23 27 public class FileStoreHighestUID implements HighestUID { 28 29 private int highestUID; 30 private int whenToWrite; 31 private static final int WRITE_STEP = 3; 32 private File file; 33 34 public FileStoreHighestUID(File f) { 35 file = f; 36 highestUID = 0; 37 38 if (file.exists()) { 39 ObjectInputStream is = null; 40 try { 41 is = new ObjectInputStream(new FileInputStream(file)); 42 Integer i = (Integer ) is.readObject(); 43 highestUID = i.intValue(); 44 is.close(); 45 is = null; 46 } catch (Exception ex) { 47 ex.printStackTrace(); 49 if (is != null) { 50 try { 51 is.close(); 52 } catch (Exception ignored) {} 53 } 54 throw new RuntimeException ("Could not load highestUID!"); 55 } 56 highestUID += WRITE_STEP; 59 } 60 write(); 61 whenToWrite = highestUID+WRITE_STEP; 62 System.out.println("Initialized highestUID="+highestUID); 63 } 64 65 public synchronized int get() { 66 return highestUID; 67 } 68 69 public synchronized void increase() { 70 highestUID++; 71 if (highestUID >= whenToWrite) { 72 whenToWrite = highestUID+WRITE_STEP; 74 write(); 76 } 77 } 78 79 private void write() { 80 ObjectOutputStream os = null; 81 try { 82 os = new ObjectOutputStream( new FileOutputStream(file)); 83 os.writeObject(new Integer (highestUID)); 84 os.close(); 85 os = null; 86 } catch (Exception ex) { 87 ex.printStackTrace(); 89 if (os != null) { 90 try { 91 os.close(); 92 } catch (Exception ignored) {} 93 } 94 throw new RuntimeException ("Failed to save highestUID!"); 95 } 96 } 97 } 98 | Popular Tags |