/** * Acquires an open file slot for the calling instance and registers it accordingly. Transparently * closes the last opened file of another instance, if limit is to be exceeded. * * @param customer Calling instance requiring an open file slot(<code>this</code> reference) * @throws IOException */ protected static synchronized void retrieveOpenFileSlot(ManagedRandomAccessFile customer) throws IOException { // probe, whether maximum open files limit has been exceeded if (openFilesCounter >= maxOpenFiles) { // close the last opened file ManagedRandomAccessFile victim = (ManagedRandomAccessFile) openFilesList.remove(0); victim.closeHandle(); // adjust counter openFilesCounter--; } // provide slot and save reference openFilesList.add(customer); // adjust counter openFilesCounter++; }
/** * Creates a new managed instance, which is based on the given file. The created instance will be * an exact copy, or clone, of the given template instance. Modifications are not synchronized * between template and clone, i.e. they behave as truly separate instances that are only exactly * the same after construction. * * @param template The template to create a clone from. * @param aFile File which is to be backing the clone to be created. * @throws IOException */ public ManagedRandomAccessFile(ManagedRandomAccessFile template) throws IOException { file = ManagedRandomAccessFile.createTempFile(); raf = null; currentFilePointer = template.currentFilePointer; isOpen = false; copyFile(template.file, file); }
/** * Closes this instance virtually (flushes and releases the managed file) * * @throws IOException */ public synchronized void close() throws IOException { if (isOpen) { raf().close(); ManagedRandomAccessFile.releaseOpenFileSlot(this); isOpen = false; } }
/** * Creates a new managed instance, which is based on the given file. * * @param aFile The file which to access in a random fashion. * @throws IOException */ public ManagedRandomAccessFile() throws IOException { // initialize file = ManagedRandomAccessFile.createTempFile(); raf = null; currentFilePointer = 0; isOpen = false; }
/** * Re-opens the managed random access file, implies retrieving an open file slot from the static * management facilities. */ protected synchronized void reOpen() throws IOException { // retrieve open file slot ManagedRandomAccessFile.retrieveOpenFileSlot(this); // adjust internal state isOpen = true; // restore random access file raf = new RandomAccessFile(file, "rw"); }