Class IOUtils
This class emulates the new Java 7 "Try-With-Resources" statement. Remove once Lucene is on Java 7.
Note
This API is for internal purposes only and might change in incompatible ways in the next release.
Inheritance
Inherited Members
Namespace: Lucene.Net.Util
Assembly: Lucene.Net.dll
Syntax
public static class IOUtils
Fields
| Improve this Doc View SourceCHARSET_UTF_8
UTF-8 System.Text.Encoding instance to prevent repeated System.Text.Encoding.UTF8 lookups
Declaration
[Obsolete("Use Encoding.UTF8 instead.")]
public static readonly Encoding CHARSET_UTF_8
Field Value
Type | Description |
---|---|
System.Text.Encoding |
UTF_8
UTF-8 charset string.
Where possible, use System.Text.Encoding.UTF8 instead, as using the System.String constant may slow things down.
Declaration
public static readonly string UTF_8
Field Value
Type | Description |
---|---|
System.String |
See Also
Methods
| Improve this Doc View SourceClose(IEnumerable<IDisposable>)
Disposes all given System.IDisposables.
Declaration
[Obsolete("Use Dispose(IEnumerable<IDisposable>) instead.")]
public static void Close(IEnumerable<IDisposable> objects)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.IDisposable> | objects |
See Also
| Improve this Doc View SourceClose(IDisposable[])
Disposes all given System.IDisposables. Some of the
System.IDisposables 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.
Declaration
[Obsolete("Use Dispose(params IDisposable[]) instead.")]
public static void Close(params IDisposable[] objects)
Parameters
Type | Name | Description |
---|---|---|
System.IDisposable[] | objects | Objects to call System.IDisposable.Dispose() on |
CloseWhileHandlingException(IEnumerable<IDisposable>)
Disposes all given System.IDisposables, suppressing all thrown exceptions.
Declaration
[Obsolete("Use DisposeWhileHandlingException(IEnumerable<IDisposable>) instead.")]
public static void CloseWhileHandlingException(IEnumerable<IDisposable> objects)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.IDisposable> | objects |
See Also
CloseWhileHandlingException(Exception, IEnumerable<IDisposable>)
Disposes all given System.IDisposables, suppressing all thrown exceptions.
Declaration
[Obsolete("Use DisposeWhileHandlingException(Exception, IEnumerable<IDisposable>) instead.")]
public static void CloseWhileHandlingException(Exception priorException, IEnumerable<IDisposable> objects)
Parameters
Type | Name | Description |
---|---|---|
System.Exception | priorException | |
System.Collections.Generic.IEnumerable<System.IDisposable> | objects |
See Also
| Improve this Doc View SourceCloseWhileHandlingException(Exception, IDisposable[])
Disposes all given IDisposable
s, suppressing all thrown exceptions. Some of the IDisposable
s
may be null
, they are ignored. After everything is disposed, method either throws priorException
,
if one is supplied, or the first of suppressed exceptions, or completes normally.
Sample usage:
IDisposable 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
{
IOUtils.CloseWhileHandlingException(priorE, resource1, resource2, resource3);
}
Declaration
[Obsolete("Use DisposeWhileHandlingException(Exception, params IDisposable[]) instead.")]
public static void CloseWhileHandlingException(Exception priorException, params IDisposable[] objects)
Parameters
Type | Name | Description |
---|---|---|
System.Exception | priorException |
|
System.IDisposable[] | objects | Objects to call System.IDisposable.Dispose() on. |
CloseWhileHandlingException(IDisposable[])
Disposes all given System.IDisposables, suppressing all thrown exceptions.
Some of the System.IDisposables may be null
, they are ignored.
Declaration
[Obsolete("Use DisposeWhileHandlingException(params IDisposable[]) instead.")]
public static void CloseWhileHandlingException(params IDisposable[] objects)
Parameters
Type | Name | Description |
---|---|---|
System.IDisposable[] | objects | Objects to call System.IDisposable.Dispose() on |
Copy(FileInfo, FileInfo)
Copy one file's contents to another file. The target will be overwritten if it exists. The source must exist.
Declaration
public static void Copy(FileInfo source, FileInfo target)
Parameters
Type | Name | Description |
---|---|---|
System.IO.FileInfo | source | |
System.IO.FileInfo | target |
DeleteFilesIgnoringExceptions(Directory, String[])
Deletes all given files, suppressing all thrown System.Exceptions.
Note that the files should not be null
.
Declaration
public static void DeleteFilesIgnoringExceptions(Directory dir, params string[] files)
Parameters
Type | Name | Description |
---|---|---|
Directory | dir | |
System.String[] | files |
Dispose(IEnumerable<IDisposable>)
Disposes all given System.IDisposables.
Declaration
public static void Dispose(IEnumerable<IDisposable> objects)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.IDisposable> | objects |
See Also
| Improve this Doc View SourceDispose(IDisposable[])
Disposes all given System.IDisposables. Some of the
System.IDisposables 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.
Declaration
public static void Dispose(params IDisposable[] objects)
Parameters
Type | Name | Description |
---|---|---|
System.IDisposable[] | objects | Objects to call System.IDisposable.Dispose() on |
DisposeWhileHandlingException(IEnumerable<IDisposable>)
Disposes all given System.IDisposables, suppressing all thrown exceptions.
Declaration
public static void DisposeWhileHandlingException(IEnumerable<IDisposable> objects)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.IDisposable> | objects |
See Also
| Improve this Doc View SourceDisposeWhileHandlingException(Exception, IEnumerable<IDisposable>)
Disposes all given System.IDisposables, suppressing all thrown exceptions.
Declaration
public static void DisposeWhileHandlingException(Exception priorException, IEnumerable<IDisposable> objects)
Parameters
Type | Name | Description |
---|---|---|
System.Exception | priorException | |
System.Collections.Generic.IEnumerable<System.IDisposable> | objects |
See Also
| Improve this Doc View SourceDisposeWhileHandlingException(Exception, IDisposable[])
Disposes all given IDisposable
s, suppressing all thrown exceptions. Some of the IDisposable
s
may be null
, they are ignored. After everything is disposed, method either throws priorException
,
if one is supplied, or the first of suppressed exceptions, or completes normally.
Sample usage:
IDisposable 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
{
IOUtils.DisposeWhileHandlingException(priorE, resource1, resource2, resource3);
}
Declaration
public static void DisposeWhileHandlingException(Exception priorException, params IDisposable[] objects)
Parameters
Type | Name | Description |
---|---|---|
System.Exception | priorException |
|
System.IDisposable[] | objects | Objects to call System.IDisposable.Dispose() on. |
DisposeWhileHandlingException(IDisposable[])
Disposes all given System.IDisposables, suppressing all thrown exceptions.
Some of the System.IDisposables may be null
, they are ignored.
Declaration
public static void DisposeWhileHandlingException(params IDisposable[] objects)
Parameters
Type | Name | Description |
---|---|---|
System.IDisposable[] | objects | Objects to call System.IDisposable.Dispose() on |
GetDecodingReader(FileInfo, Encoding)
Opens a System.IO.TextReader for the given System.IO.FileInfo using a System.Text.Encoding. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expected System.Text.Encoding.
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.
Declaration
public static TextReader GetDecodingReader(FileInfo file, Encoding charSet)
Parameters
Type | Name | Description |
---|---|---|
System.IO.FileInfo | file | The file to open a reader on |
System.Text.Encoding | charSet | The expected charset |
Returns
Type | Description |
---|---|
System.IO.TextReader | A reader to read the given file |
GetDecodingReader(Stream, Encoding)
Wrapping the given System.IO.Stream in a reader using a System.Text.Encoding. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expected System.Text.Encoding.
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.
Declaration
public static TextReader GetDecodingReader(Stream stream, Encoding charSet)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | stream | The stream to wrap in a reader |
System.Text.Encoding | charSet | The expected charset |
Returns
Type | Description |
---|---|
System.IO.TextReader | A wrapping reader |
GetDecodingReader(Type, String, Encoding)
Opens a System.IO.TextReader for the given resource using a System.Text.Encoding. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expected System.Text.Encoding.
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.
Declaration
public static TextReader GetDecodingReader(Type clazz, string resource, Encoding charSet)
Parameters
Type | Name | Description |
---|---|---|
System.Type | clazz | The class used to locate the resource |
System.String | resource | The resource name to load |
System.Text.Encoding | charSet | The expected charset |
Returns
Type | Description |
---|---|
System.IO.TextReader | A reader to read the given file |
ReThrow(Exception)
Simple utilty method that takes a previously caught
System.Exception and rethrows either
System.IO.IOException or an unchecked exception. If the
argument is null
then this method does nothing.
Declaration
public static void ReThrow(Exception th)
Parameters
Type | Name | Description |
---|---|---|
System.Exception | th |
ReThrowUnchecked(Exception)
Simple utilty method that takes a previously caught
System.Exception and rethrows it as an unchecked exception.
If the argument is null
then this method does nothing.
Declaration
public static void ReThrowUnchecked(Exception th)
Parameters
Type | Name | Description |
---|---|---|
System.Exception | th |