Package org.bzdev.io

Class DelayedFileOutputStream

java.lang.Object
org.bzdev.io.DelayedFileOutputStream

public class DelayedFileOutputStream extends Object
Class to provide delayed opening of a file output stream. This class is intended to allow classes that have permission to open a file for writing to delay opening the file until a later time. The file may be opened by a class that has weaker permissions.

An example of when this class may be useful is when an application has to open a long list of files, perhaps passed to the application on the command line, and these files have to be processed later (the original use case was on in which the files were processed after a security manager was installed, but security managers have been removed from recent versions of Java). Each java virtual machine has a limit on the number of files that can be simultaneously opened, so opening all the files (if there are enough of them) and then processing them will not work.

  • Constructor Details

    • DelayedFileOutputStream

      public DelayedFileOutputStream(String name) throws FileNotFoundException
      Constructor given a file name.
      Parameters:
      name - the name of the file to open for writing
      Throws:
      FileNotFoundException - the file could not be found
    • DelayedFileOutputStream

      public DelayedFileOutputStream(File file) throws FileNotFoundException
      Constructor given a file.
      Parameters:
      file - the file to open for writing.
      Throws:
      FileNotFoundException - the file could not be found
    • DelayedFileOutputStream

      public DelayedFileOutputStream(String name, boolean append) throws FileNotFoundException
      Constructor given a file name and a flag to indicate if one should append to the file rather than replace it.
      Parameters:
      name - the name of the file to open for writing
      append - true if the file should be opened for appending data; false if the file is overridden
      Throws:
      FileNotFoundException - the file could not be found
    • DelayedFileOutputStream

      public DelayedFileOutputStream(File file, boolean append) throws FileNotFoundException
      Constructor given a file and a flag to indicate if one should append to the file rather than replace it.
      Parameters:
      file - the file to open for writing.
      append - true if the file is opened in append mode; false otherwise
      Throws:
      FileNotFoundException - the file could not be found
  • Method Details

    • open

      Open the file specified by a constructor. This method can be called at most one time. The file actually opened is the file with the same canonical path name as the file passed to the constructor.
      Returns:
      the output stream for writing the file
      Throws:
      FileNotFoundException - the file could not be found