All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class SecureFileOutputStreamextends FileOutputStream
Same as FileOutputStream but sets the file mode so it can only be read and written by the owner only (i.e. 600 on POSIX). Best effort: on filesystems without POSIX permission support, such as Windows, only the read-only attribute can be affected.
  • Field Details

    • oneDotSix

      private static final boolean oneDotSix
      True on Java 6, where the file-open options differ
  • Constructor Details

    • SecureFileOutputStream

      public SecureFileOutputStream(String file) throws FileNotFoundException
      Tries to set output file to mode 600.
      Parameters:
      file - the path to open for writing, truncating any existing file
      Throws:
      FileNotFoundException - if the file cannot be opened for writing
    • SecureFileOutputStream

      public SecureFileOutputStream(String file, boolean append) throws FileNotFoundException
      Tries to set output file to mode 600 whether append = true or false.
      Parameters:
      file - the path to open for writing
      append - true to write at the end of an existing file, false to truncate
      Throws:
      FileNotFoundException - if the file cannot be opened for writing
    • SecureFileOutputStream

      public SecureFileOutputStream(File file) throws FileNotFoundException
      Tries to set output file to mode 600.
      Parameters:
      file - the file to open for writing, truncating any existing content
      Throws:
      FileNotFoundException - if the file cannot be opened for writing
    • SecureFileOutputStream

      public SecureFileOutputStream(File file, boolean append) throws FileNotFoundException
      Tries to set output file to mode 600 whether append = true or false.
      Parameters:
      file - the file to open for writing
      append - true to write at the end of an existing file, false to truncate
      Throws:
      FileNotFoundException - if the file cannot be opened for writing
  • Method Details

    • canSetPerms

      static boolean canSetPerms()
      Whether it is worth trying to tighten the mode of a file this class opens. False on a pre-1.6 JVM, and false when the router is configured with i2p.insecureFiles, since the setting is then left alone.
      Returns:
      true if the permissions should be set, false to skip
    • setPerms

      public static void setPerms(File f)
      Tries to set the permissions to 600, ignores errors.
      Parameters:
      f - the file to make readable and writable by the owner only
    • setGroupPerms

      public static void setGroupPerms(File f)
      Tries to set the permissions to 660 (owner+group rw), ignores errors, including on filesystems without POSIX permission support (e.g. Windows). Uses the PosixFilePermission API.
      Parameters:
      f - the file to make readable and writable by owner and group