Class FileOp

  • Direct Known Subclasses:
    FileOpImpl

    public abstract class FileOp
    extends java.lang.Object
    Deprecated.
    Use Paths, CancellableFileIo and (for testing) InMemoryFileSystems directly.
    Wraps some common File operations on files and folders.

    This makes it possible to override/mock/stub some file operations in unit tests. Uses CancellableFileIo to check for cancellation before read I/O operations.

    • Constructor Summary

      Constructors 
      Constructor Description
      FileOp()
      Deprecated.
       
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      boolean canExecute​(java.io.File file)
      Deprecated.
       
      boolean canWrite​(java.io.File file)
      Deprecated.
      Invokes CancellableFileIo.isWritable(Path)on the given file.
      void copyFile​(java.io.File source, java.io.File dest)
      Deprecated.
       
      boolean createNewFile​(java.io.File file)
      Deprecated.
      Creates a new file.
      boolean delete​(java.io.File file)
      Deprecated.
      Invokes Files.delete(Path) on the given file.
      void deleteFileOrFolder​(java.io.File fileOrFolder)
      Deprecated.
      Helper to delete a file or a directory.
      abstract void deleteOnExit​(java.io.File file)
      Deprecated.
      The application may not exit for a very long time.
      abstract java.io.File ensureRealFile​(java.io.File in)
      Deprecated.
      If in is an in-memory file, write it out as a proper file and return it.
      boolean exists​(java.io.File file)
      Deprecated.
      Invokes CancellableFileIo.exists(Path, LinkOption...) on the given file.
      abstract java.nio.file.FileSystem getFileSystem()
      Deprecated.
      Returns the FileSystem this is based on.
      boolean isDirectory​(java.io.File file)
      Deprecated.
      Invokes CancellableFileIo.isDirectory(Path, LinkOption...) (Path, LinkOption...)} on the given file.
      boolean isFile​(java.io.File file)
      Deprecated.
      Invokes CancellableFileIo.isRegularFile(Path, LinkOption...) on the given file.
      boolean isSameFile​(java.io.File file1, java.io.File file2)
      Deprecated.
      Checks whether 2 binary files are the same.
      long lastModified​(java.io.File file)
      Deprecated.
      Returns the lastModified attribute of the file.
      long length​(java.io.File file)
      Deprecated.
      Invokes CancellableFileIo.size(Path) on the given file.
      java.lang.String[] list​(java.io.File folder, java.io.FilenameFilter filenameFilter)
      Deprecated.
       
      java.io.File[] listFiles​(java.io.File file)
      Deprecated.
      Invokes CancellableFileIo.list(Path) on the given file.
      java.io.File[] listFiles​(java.io.File folder, java.io.FilenameFilter filenameFilter)
      Deprecated.
       
      boolean mkdirs​(java.io.File file)
      Deprecated.
      Invokes Files.createDirectories(Path, FileAttribute[]) on the given file.
      java.io.InputStream newFileInputStream​(java.io.File file)
      Deprecated.
      Creates a new InputStream for the given file.
      java.io.InputStream newFileInputStream​(java.nio.file.Path path)
      Deprecated.
      Creates a new InputStream for the given path.
      java.io.OutputStream newFileOutputStream​(java.io.File file)
      Deprecated.
      Creates a new OutputStream for the given file.
      java.io.OutputStream newFileOutputStream​(java.io.File file, boolean append)
      Deprecated.
      Creates a new OutputStream for the given file, either truncating an existing file or appending to it.
      java.lang.String readText​(java.io.File f)
      Deprecated.
       
      boolean renameTo​(java.io.File oldFile, java.io.File newFile)
      Deprecated.
      Uses Files.move(Path, Path, CopyOption...) to rename oldFile to {
      void setExecutablePermission​(java.io.File file)
      Deprecated.
      Sets the executable Unix permission (+x) on a file or folder.
      boolean setLastModified​(java.io.File file, long time)
      Deprecated.
       
      boolean setLastModified​(java.nio.file.Path file, long time)
      Deprecated.
       
      void setReadOnly​(java.io.File file)
      Deprecated.
      Sets the file or directory as read-only.
      abstract java.io.File toFile​(java.nio.file.Path path)
      Deprecated.
      Temporary functionality to help with File-to-Path migration.
      java.nio.file.Path toPath​(java.io.File file)
      Deprecated.
      Convert the given File into a Path, using some means appropriate to this FileOp.
      java.nio.file.Path toPath​(java.lang.String path)
      Deprecated.
      Convert the given String into a Path, using some means appropriate to this FileOp.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileOp

        public FileOp()
        Deprecated.
    • Method Detail

      • getFileSystem

        public abstract java.nio.file.FileSystem getFileSystem()
        Deprecated.
        Returns the FileSystem this is based on.
      • deleteFileOrFolder

        public final void deleteFileOrFolder​(@NonNull
                                             java.io.File fileOrFolder)
        Deprecated.
        Helper to delete a file or a directory. For a directory, recursively deletes all of its content. Files that cannot be deleted right away are marked for deletion on exit. It's ok for the file or folder to not exist at all.
      • setExecutablePermission

        public final void setExecutablePermission​(@NonNull
                                                  java.io.File file)
                                           throws java.io.IOException
        Deprecated.
        Sets the executable Unix permission (+x) on a file or folder.

        Caller must make sure to not invoke this under Windows.

        Parameters:
        file - The file to set permissions on.
        Throws:
        java.io.IOException - If an I/O error occurs
      • setReadOnly

        public final void setReadOnly​(@NonNull
                                      java.io.File file)
                               throws java.io.IOException
        Deprecated.
        Sets the file or directory as read-only.
        Parameters:
        file - The file or directory to set permissions on.
        Throws:
        java.io.IOException
      • copyFile

        public void copyFile​(@NonNull
                             java.io.File source,
                             @NonNull
                             java.io.File dest)
                      throws java.io.IOException
        Deprecated.
        Throws:
        java.io.IOException
      • isSameFile

        public final boolean isSameFile​(@NonNull
                                        java.io.File file1,
                                        @NonNull
                                        java.io.File file2)
                                 throws java.io.IOException
        Deprecated.
        Checks whether 2 binary files are the same.
        Parameters:
        file1 - the source file to copy
        file2 - the destination file to write
        Throws:
        java.io.IOException - if there's a problem reading the files.
      • exists

        public final boolean exists​(@NonNull
                                    java.io.File file)
        Deprecated.
        Invokes CancellableFileIo.exists(Path, LinkOption...) on the given file.
      • isFile

        public final boolean isFile​(@NonNull
                                    java.io.File file)
        Deprecated.
        Invokes CancellableFileIo.isRegularFile(Path, LinkOption...) on the given file.
      • isDirectory

        public final boolean isDirectory​(@NonNull
                                         java.io.File file)
        Deprecated.
        Invokes CancellableFileIo.isDirectory(Path, LinkOption...) (Path, LinkOption...)} on the given file.
      • canWrite

        public boolean canWrite​(@NonNull
                                java.io.File file)
        Deprecated.
        Invokes CancellableFileIo.isWritable(Path)on the given file.

        TODO: make this final so we can migrate from FileOp to using Paths directly

      • length

        public final long length​(@NonNull
                                 java.io.File file)
                          throws java.io.IOException
        Deprecated.
        Invokes CancellableFileIo.size(Path) on the given file.
        Throws:
        java.io.IOException
      • delete

        public boolean delete​(@NonNull
                              java.io.File file)
        Deprecated.
        Invokes Files.delete(Path) on the given file. Note: for a recursive folder version, consider deleteFileOrFolder(File).

        TODO: make this final so we can migrate from FileOp to using Paths directly

      • mkdirs

        public final boolean mkdirs​(@NonNull
                                    java.io.File file)
        Deprecated.
        Invokes Files.createDirectories(Path, FileAttribute[]) on the given file.
      • listFiles

        @NonNull
        public final java.io.File[] listFiles​(@NonNull
                                              java.io.File file)
        Deprecated.
        Invokes CancellableFileIo.list(Path) on the given file. Contrary to the Java API, this returns an empty array instead of null when the directory does not exist.
      • renameTo

        public boolean renameTo​(@NonNull
                                java.io.File oldFile,
                                @NonNull
                                java.io.File newFile)
        Deprecated.
        Uses Files.move(Path, Path, CopyOption...) to rename oldFile to {
      • newFileOutputStream

        @NonNull
        public java.io.OutputStream newFileOutputStream​(@NonNull
                                                        java.io.File file)
                                                 throws java.io.IOException
        Deprecated.
        Creates a new OutputStream for the given file.

        TODO: make this final so we can migrate from FileOp to using Paths directly

        Throws:
        java.io.IOException
      • newFileOutputStream

        @NonNull
        public java.io.OutputStream newFileOutputStream​(@NonNull
                                                        java.io.File file,
                                                        boolean append)
                                                 throws java.io.IOException
        Deprecated.
        Creates a new OutputStream for the given file, either truncating an existing file or appending to it.

        TODO: make this final so we can migrate from FileOp to using Paths directly

        Throws:
        java.io.IOException
      • newFileInputStream

        @NonNull
        public final java.io.InputStream newFileInputStream​(@NonNull
                                                            java.io.File file)
                                                     throws java.io.IOException
        Deprecated.
        Creates a new InputStream for the given file.
        Throws:
        java.io.IOException
      • newFileInputStream

        @NonNull
        public final java.io.InputStream newFileInputStream​(@NonNull
                                                            java.nio.file.Path path)
                                                     throws java.io.IOException
        Deprecated.
        Creates a new InputStream for the given path.
        Throws:
        java.io.IOException
      • lastModified

        public final long lastModified​(@NonNull
                                       java.io.File file)
        Deprecated.
        Returns the lastModified attribute of the file.
        Parameters:
        file - The non-null file of which to retrieve the lastModified attribute.
        Returns:
        The last-modified attribute of the file, in milliseconds since The Epoch.
        See Also:
        Files.getLastModifiedTime(Path, LinkOption...)
      • createNewFile

        public final boolean createNewFile​(@NonNull
                                           java.io.File file)
                                    throws java.io.IOException
        Deprecated.
        Creates a new file. See Files.createFile(Path, FileAttribute[]).
        Throws:
        java.io.IOException
      • canExecute

        public final boolean canExecute​(@NonNull
                                        java.io.File file)
        Deprecated.
        See Also:
        Files.isExecutable(Path)
      • ensureRealFile

        public abstract java.io.File ensureRealFile​(@NonNull
                                                    java.io.File in)
                                             throws java.io.IOException
        Deprecated.
        If in is an in-memory file, write it out as a proper file and return it. Otherwise just return in.
        Throws:
        java.io.IOException
      • readText

        @NonNull
        public final java.lang.String readText​(@NonNull
                                               java.io.File f)
                                        throws java.io.IOException
        Deprecated.
        Throws:
        java.io.IOException
      • list

        public final java.lang.String[] list​(@NonNull
                                             java.io.File folder,
                                             @Nullable
                                             java.io.FilenameFilter filenameFilter)
        Deprecated.
        See Also:
        File.list(FilenameFilter)
      • listFiles

        public final java.io.File[] listFiles​(@NonNull
                                              java.io.File folder,
                                              @Nullable
                                              java.io.FilenameFilter filenameFilter)
        Deprecated.
        See Also:
        File.listFiles(FilenameFilter)
      • deleteOnExit

        @Deprecated
        public abstract void deleteOnExit​(java.io.File file)
        Deprecated.
        The application may not exit for a very long time. Prefer explicit cleanup.
        See Also:
        File.deleteOnExit()
      • setLastModified

        public final boolean setLastModified​(@NonNull
                                             java.io.File file,
                                             long time)
                                      throws java.io.IOException
        Deprecated.
        Throws:
        java.io.IOException - if there is an error setting the modification time.
        See Also:
        Files.setLastModifiedTime(Path, FileTime)
      • setLastModified

        public final boolean setLastModified​(@NonNull
                                             java.nio.file.Path file,
                                             long time)
                                      throws java.io.IOException
        Deprecated.
        Throws:
        java.io.IOException - if there is an error setting the modification time.
        See Also:
        Files.setLastModifiedTime(Path, FileTime)
      • toPath

        @NonNull
        public java.nio.file.Path toPath​(@NonNull
                                         java.io.File file)
        Deprecated.
        Convert the given File into a Path, using some means appropriate to this FileOp.

        TODO: make final once tests can work on windows without special-casing.

      • toPath

        @NonNull
        public java.nio.file.Path toPath​(@NonNull
                                         java.lang.String path)
        Deprecated.
        Convert the given String into a Path, using some means appropriate to this FileOp.
      • toFile

        @NonNull
        public abstract java.io.File toFile​(@NonNull
                                            java.nio.file.Path path)
        Deprecated.
        Temporary functionality to help with File-to-Path migration. Should only be called when a FileOp is not available and with a Path that is backed by the default FileSystem (notably not in the context of any tests that use MockFileOp or jimfs).
        Throws:
        java.lang.UnsupportedOperationException - if the Path is backed by a non-default FileSystem.