Fork me on GitHub
  • API

    Show / Hide Table of Contents

    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
    object
    IOUtils
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Lucene.Net.Util
    Assembly: Lucene.Net.dll
    Syntax
    public static class IOUtils

    Fields

    CHARSET_UTF_8

    UTF-8 Encoding instance to prevent repeated UTF8 lookups and match Java's behavior with respect to a lack of a byte-order mark (BOM).

    Declaration
    public static readonly Encoding CHARSET_UTF_8
    Field Value
    Type Description
    Encoding

    UTF_8

    UTF-8 charset string.

    Where possible, use UTF8 instead, as using the string constant may slow things down.
    Declaration
    public static readonly string UTF_8
    Field Value
    Type Description
    string
    See Also
    UTF8

    Methods

    Close(IEnumerable<IDisposable>)

    Disposes all given IDisposables.

    Declaration
    [Obsolete("Use Dispose(IEnumerable<IDisposable>) instead.")]
    public static void Close(IEnumerable<IDisposable> objects)
    Parameters
    Type Name Description
    IEnumerable<IDisposable> objects
    See Also
    Dispose(params IDisposable[])

    Close(params IDisposable[])

    Disposes all given IDisposables. Some of the 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
    IDisposable[] objects

    Objects to call Dispose() on

    CloseWhileHandlingException(IEnumerable<IDisposable>)

    Disposes all given IDisposables, suppressing all thrown exceptions.

    Declaration
    [Obsolete("Use DisposeWhileHandlingException(IEnumerable<IDisposable>) instead.")]
    public static void CloseWhileHandlingException(IEnumerable<IDisposable> objects)
    Parameters
    Type Name Description
    IEnumerable<IDisposable> objects
    See Also
    DisposeWhileHandlingException(IEnumerable<IDisposable>)
    DisposeWhileHandlingException(params IDisposable[])

    CloseWhileHandlingException(Exception, IEnumerable<IDisposable>)

    Disposes all given 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
    Exception priorException
    IEnumerable<IDisposable> objects
    See Also
    DisposeWhileHandlingException(Exception, params IDisposable[])

    CloseWhileHandlingException(Exception, params IDisposable[])

    Disposes all given IDisposables, suppressing all thrown exceptions. Some of the IDisposables 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
    Exception priorException

    null or an exception that will be rethrown after method completion.

    IDisposable[] objects

    Objects to call Dispose() on.

    CloseWhileHandlingException(params IDisposable[])

    Disposes all given IDisposables, suppressing all thrown exceptions. Some of the 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
    IDisposable[] objects

    Objects to call 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
    FileInfo source
    FileInfo target

    DeleteFilesIgnoringExceptions(Directory, params string[])

    Deletes all given files, suppressing all thrown 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
    string[] files

    Dispose(IEnumerable<IDisposable>)

    Disposes all given IDisposables.

    Declaration
    public static void Dispose(IEnumerable<IDisposable> objects)
    Parameters
    Type Name Description
    IEnumerable<IDisposable> objects
    See Also
    Dispose(params IDisposable[])

    Dispose(params IDisposable[])

    Disposes all given IDisposables. Some of the 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
    IDisposable[] objects

    Objects to call Dispose() on

    DisposeWhileHandlingException(IEnumerable<IDisposable>)

    Disposes all given IDisposables, suppressing all thrown exceptions.

    Declaration
    public static void DisposeWhileHandlingException(IEnumerable<IDisposable> objects)
    Parameters
    Type Name Description
    IEnumerable<IDisposable> objects
    See Also
    DisposeWhileHandlingException(params IDisposable[])

    DisposeWhileHandlingException(Exception, IEnumerable<IDisposable>)

    Disposes all given IDisposables, suppressing all thrown exceptions.

    Declaration
    public static void DisposeWhileHandlingException(Exception priorException, IEnumerable<IDisposable> objects)
    Parameters
    Type Name Description
    Exception priorException
    IEnumerable<IDisposable> objects
    See Also
    DisposeWhileHandlingException(Exception, params IDisposable[])

    DisposeWhileHandlingException(Exception, params IDisposable[])

    Disposes all given IDisposables, suppressing all thrown exceptions. Some of the IDisposables 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
    Exception priorException

    null or an exception that will be rethrown after method completion.

    IDisposable[] objects

    Objects to call Dispose() on.

    DisposeWhileHandlingException(params IDisposable[])

    Disposes all given IDisposables, suppressing all thrown exceptions. Some of the IDisposables may be null, they are ignored.

    Declaration
    public static void DisposeWhileHandlingException(params IDisposable[] objects)
    Parameters
    Type Name Description
    IDisposable[] objects

    Objects to call Dispose() on

    Fsync(string, bool)

    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.

    Declaration
    public static void Fsync(string fileToSync, bool isDir)
    Parameters
    Type Name Description
    string fileToSync
    bool isDir

    GetDecodingReader(FileInfo, Encoding)

    Opens a TextReader for the given FileInfo using a Encoding. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expected 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
    FileInfo file

    The file to open a reader on

    Encoding charSet

    The expected charset

    Returns
    Type Description
    TextReader

    A reader to read the given file

    GetDecodingReader(Stream, Encoding)

    Wrapping the given Stream in a reader using a Encoding. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expected 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
    Stream stream

    The stream to wrap in a reader

    Encoding charSet

    The expected charset

    Returns
    Type Description
    TextReader

    A wrapping reader

    GetDecodingReader(Type, string, Encoding)

    Opens a TextReader for the given resource using a Encoding. Unlike Java's defaults this reader will throw an exception if your it detects the read charset doesn't match the expected 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
    Type clazz

    The class used to locate the resource

    string resource

    The resource name to load

    Encoding charSet

    The expected charset

    Returns
    Type Description
    TextReader

    A reader to read the given file

    ReThrow(Exception)

    Simple utilty method that takes a previously caught Exception and rethrows either 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
    Exception th

    ReThrowUnchecked(Exception)

    Simple utilty method that takes a previously caught 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
    Exception th
    Back to top Copyright © 2024 The Apache Software Foundation, Licensed under the Apache License, Version 2.0
    Apache Lucene.Net, Lucene.Net, Apache, the Apache feather logo, and the Apache Lucene.Net project logo are trademarks of The Apache Software Foundation.
    All other marks mentioned may be trademarks or registered trademarks of their respective owners.