Class IOUtils
- java.lang.Object
-
- org.apache.lucene.util.IOUtils
-
public final class IOUtils extends java.lang.Object
This class emulates the new Java 7 "Try-With-Resources" statement. Remove once Lucene is on Java 7.
-
-
Field Summary
Fields Modifier and Type Field Description static java.nio.charset.Charset
CHARSET_UTF_8
UTF-8Charset
instance to prevent repeatedCharset.forName(String)
lookupsstatic java.lang.String
UTF_8
UTF-8 charset string
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
close(java.io.Closeable... objects)
Closes all given Closeables.static void
close(java.lang.Iterable<? extends java.io.Closeable> objects)
Closes all given Closeables.static <E extends java.lang.Exception>
voidcloseWhileHandlingException(E priorException, java.io.Closeable... objects)
Closes all given Closeables, suppressing all thrown exceptions.static <E extends java.lang.Exception>
voidcloseWhileHandlingException(E priorException, java.lang.Iterable<? extends java.io.Closeable> objects)
Closes all given Closeables, suppressing all thrown exceptions.static void
closeWhileHandlingException(java.io.Closeable... objects)
Closes all given Closeables, suppressing all thrown exceptions.static void
closeWhileHandlingException(java.lang.Iterable<? extends java.io.Closeable> objects)
Closes all given Closeables, suppressing all thrown exceptions.static void
copy(java.io.File source, java.io.File target)
Copy one file's contents to another file.static void
deleteFilesIgnoringExceptions(Directory dir, java.lang.String... files)
Deletes all given files, suppressing all thrown IOExceptions.static java.io.Reader
getDecodingReader(java.io.File file, java.nio.charset.Charset charSet)
Opens a Reader for the givenFile
using aCharsetDecoder
.static java.io.Reader
getDecodingReader(java.io.InputStream stream, java.nio.charset.Charset charSet)
Wrapping the givenInputStream
in a reader using aCharsetDecoder
.static java.io.Reader
getDecodingReader(java.lang.Class<?> clazz, java.lang.String resource, java.nio.charset.Charset charSet)
Opens a Reader for the given resource using aCharsetDecoder
.static void
reThrow(java.lang.Throwable th)
Simple utilty method that takes a previously caughtThrowable
and rethrows eitherIOException
or an unchecked exception.static void
reThrowUnchecked(java.lang.Throwable th)
Simple utilty method that takes a previously caughtThrowable
and rethrows it as an unchecked exception.
-
-
-
Field Detail
-
UTF_8
public static final java.lang.String UTF_8
UTF-8 charset string- See Also:
Charset.forName(String)
, Constant Field Values
-
CHARSET_UTF_8
public static final java.nio.charset.Charset CHARSET_UTF_8
UTF-8Charset
instance to prevent repeatedCharset.forName(String)
lookups
-
-
Method Detail
-
closeWhileHandlingException
public static <E extends java.lang.Exception> void closeWhileHandlingException(E priorException, java.io.Closeable... objects) throws E extends java.lang.Exception, java.io.IOException
Closes all given Closeables, suppressing all thrown exceptions. Some of the Closeables may be null, they are ignored. After everything is closed, method either throws priorException, if one is supplied, or the first of suppressed exceptions, or completes normally.
Sample usage:
Closeable resource1 = null, resource2 = null, resource3 = null; ExpectedException priorE = null; try { resource1 = ...; resource2 = ...; resource3 = ...; // Acquisition may throw ExpectedException ..do..stuff.. // May throw ExpectedException } catch (ExpectedException e) { priorE = e; } finally { closeWhileHandlingException(priorE, resource1, resource2, resource3); }
- Parameters:
priorException
- null or an exception that will be rethrown after method completionobjects
- objects to call close() on- Throws:
E extends java.lang.Exception
java.io.IOException
-
closeWhileHandlingException
public static <E extends java.lang.Exception> void closeWhileHandlingException(E priorException, java.lang.Iterable<? extends java.io.Closeable> objects) throws E extends java.lang.Exception, java.io.IOException
Closes all given Closeables, suppressing all thrown exceptions.- Throws:
E extends java.lang.Exception
java.io.IOException
- See Also:
closeWhileHandlingException(Exception, Closeable...)
-
close
public static void close(java.io.Closeable... objects) throws java.io.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:
java.io.IOException
-
close
public static void close(java.lang.Iterable<? extends java.io.Closeable> objects) throws java.io.IOException
Closes all given Closeables.- Throws:
java.io.IOException
- See Also:
close(Closeable...)
-
closeWhileHandlingException
public static void closeWhileHandlingException(java.io.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
-
closeWhileHandlingException
public static void closeWhileHandlingException(java.lang.Iterable<? extends java.io.Closeable> objects)
Closes all given Closeables, suppressing all thrown exceptions.
-
getDecodingReader
public static java.io.Reader getDecodingReader(java.io.InputStream stream, java.nio.charset.Charset charSet)
Wrapping the givenInputStream
in a reader using aCharsetDecoder
. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expectedCharset
.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 readercharSet
- the expected charset- Returns:
- a wrapping reader
-
getDecodingReader
public static java.io.Reader getDecodingReader(java.io.File file, java.nio.charset.Charset charSet) throws java.io.IOException
Opens a Reader for the givenFile
using aCharsetDecoder
. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expectedCharset
.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:
file
- the file to open a reader oncharSet
- the expected charset- Returns:
- a reader to read the given file
- Throws:
java.io.IOException
-
getDecodingReader
public static java.io.Reader getDecodingReader(java.lang.Class<?> clazz, java.lang.String resource, java.nio.charset.Charset charSet) throws java.io.IOException
Opens a Reader for the given resource using aCharsetDecoder
. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expectedCharset
.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 resourceresource
- the resource name to loadcharSet
- the expected charset- Returns:
- a reader to read the given file
- Throws:
java.io.IOException
-
deleteFilesIgnoringExceptions
public static void deleteFilesIgnoringExceptions(Directory dir, java.lang.String... files)
Deletes all given files, suppressing all thrown IOExceptions.Note that the files should not be null.
-
copy
public static void copy(java.io.File source, java.io.File target) throws java.io.IOException
Copy one file's contents to another file. The target will be overwritten if it exists. The source must exist.- Throws:
java.io.IOException
-
reThrow
public static void reThrow(java.lang.Throwable th) throws java.io.IOException
Simple utilty method that takes a previously caughtThrowable
and rethrows eitherIOException
or an unchecked exception. If the argument is null then this method does nothing.- Throws:
java.io.IOException
-
reThrowUnchecked
public static void reThrowUnchecked(java.lang.Throwable th)
Simple utilty method that takes a previously caughtThrowable
and rethrows it as an unchecked exception. If the argument is null then this method does nothing.
-
-