Class XIOUtils



  • public final class XIOUtils
    extends Object
    This class emulates the new Java 7 "Try-With-Resources" statement. Remove once Lucene is on Java 7.
    • Method Detail

      • close

        public static void close(Closeable... objects)
                          throws IOException
        Closes all given Closeables. Some of the Closeables may be null; they are ignored. After everything is closed, the method either throws the first exception it hit while closing, or completes normally if there were no exceptions.
        Parameters:
        objects - objects to call close() on
        Throws:
        IOException
      • closeWhileHandlingException

        public static void closeWhileHandlingException(Closeable... objects)
        Closes all given Closeables, suppressing all thrown exceptions. Some of the Closeables may be null, they are ignored.
        Parameters:
        objects - objects to call close() on
      • getDecodingReader

        public static Reader getDecodingReader(InputStream stream,
                                               Charset charSet)
        Wrapping the given InputStream in a reader using a CharsetDecoder. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expected Charset.

        Decoding readers are useful to load configuration files, stopword lists or synonym files to detect character set problems. However, its not recommended to use as a common purpose reader.

        Parameters:
        stream - the stream to wrap in a reader
        charSet - the expected charset
        Returns:
        a wrapping reader
      • getDecodingReader

        public static Reader getDecodingReader(Class<?> clazz,
                                               String resource,
                                               Charset charSet)
                                        throws IOException
        Opens a Reader for the given resource using a CharsetDecoder. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expected Charset.

        Decoding readers are useful to load configuration files, stopword lists or synonym files to detect character set problems. However, its not recommended to use as a common purpose reader.

        Parameters:
        clazz - the class used to locate the resource
        resource - the resource name to load
        charSet - the expected charset
        Returns:
        a reader to read the given file
        Throws:
        IOException
      • deleteFilesIgnoringExceptions

        public static void deleteFilesIgnoringExceptions(org.apache.lucene.store.Directory dir,
                                                         String... files)
        Deletes all given files, suppressing all thrown IOExceptions.

        Note that the files should not be null.

      • deleteFilesIgnoringExceptions

        public static void deleteFilesIgnoringExceptions(Path... files)
        Deletes all given files, suppressing all thrown IOExceptions.

        Some of the files may be null, if so they are ignored.

      • deleteFilesIgnoringExceptions

        public static void deleteFilesIgnoringExceptions(Collection<? extends Path> files)
        Deletes all given files, suppressing all thrown IOExceptions.

        Some of the files may be null, if so they are ignored.

      • deleteFilesIfExist

        public static void deleteFilesIfExist(Path... files)
                                       throws IOException
        Deletes all given Paths, if they exist. Some of the Files may be null; they are ignored. After everything is deleted, the method either throws the first exception it hit while deleting, or completes normally if there were no exceptions.
        Parameters:
        files - files to delete
        Throws:
        IOException
      • deleteFilesIfExist

        public static void deleteFilesIfExist(Collection<? extends Path> files)
                                       throws IOException
        Deletes all given Paths, if they exist. Some of the Files may be null; they are ignored. After everything is deleted, the method either throws the first exception it hit while deleting, or completes normally if there were no exceptions.
        Parameters:
        files - files to delete
        Throws:
        IOException
      • rm

        public static void rm(Path... locations)
                       throws IOException
        Deletes one or more files or directories (and everything underneath it).
        Throws:
        IOException - if any of the given files (or their subhierarchy files in case of directories) cannot be removed.
      • reThrow

        public static void reThrow(Throwable th)
                            throws IOException
        Simple utility method that takes a previously caught Throwable and rethrows either IOException or an unchecked exception. If the argument is null then this method does nothing.
        Throws:
        IOException
      • reThrowUnchecked

        public static void reThrowUnchecked(Throwable th)
        Simple utility method that takes a previously caught Throwable and rethrows it as an unchecked exception. If the argument is null then this method does nothing.
      • fsync

        public static void fsync(Path fileToSync,
                                 boolean isDir)
                          throws IOException
        Ensure that any writes to the given file is written to the storage device that contains it.
        Parameters:
        fileToSync - the file to fsync
        isDir - if true, the given file is a directory (we open for read and ignore IOExceptions, because not all file systems and operating systems allow to fsync on a directory)
        Throws:
        IOException