Class ResourceReader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class ResourceReader
    extends java.lang.Object
    implements java.io.Closeable
    A reader for text resource data in the current package or the package of a given class object. The resource data is loaded through the class loader, so it will typically be a file in the same directory as the *.class files, or a file within a JAR file in the corresponding subdirectory. The file must be a text file in one of the supported encodings; when the resource is opened by constructing a ResourceReader object the encoding is specified.

    2015-sep-03 TODO: Only used in com.ibm.icu.dev.test.format, move there.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String encoding  
      private int lineNo
      The one-based line number.
      private java.io.BufferedReader reader  
      private java.lang.String resourceName  
      private java.lang.Class<?> root  
    • Constructor Summary

      Constructors 
      Constructor Description
      ResourceReader​(java.io.InputStream is, java.lang.String resourceName)
      Construct a reader object for the input stream associated with the given resource name.
      ResourceReader​(java.io.InputStream is, java.lang.String resourceName, java.lang.String encoding)
      Construct a reader object for the input stream associated with the given resource name.
      ResourceReader​(java.lang.Class<?> rootClass, java.lang.String resourceName)
      Construct a reader object for the text file of the given name in the given class's package, using the default encoding.
      ResourceReader​(java.lang.Class<?> rootClass, java.lang.String resourceName, java.lang.String encoding)
      Construct a reader object for the text file of the given name in the given class's package, using the given encoding.
      ResourceReader​(java.lang.String resourceName)
      Construct a reader object for the text file of the given name in this package, using the default encoding.
      ResourceReader​(java.lang.String resourceName, java.lang.String encoding)
      Construct a reader object for the text file of the given name in this package, using the given encoding.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void _reset()
      Reset to the start by reconstructing the stream and readers.
      void close()
      Closes the underlying reader and releases any system resources associated with it.
      java.lang.String describePosition()
      Return a string description of the position of the last line returned by readLine() or readLineSkippingComments().
      int getLineNumber()
      Return the one-based line number of the last line returned by readLine() or readLineSkippingComments().
      java.lang.String readLine()
      Read and return the next line of the file or null if the end of the file has been reached.
      java.lang.String readLineSkippingComments()
      Read a line, ignoring blank lines and lines that start with '#'.
      java.lang.String readLineSkippingComments​(boolean trim)
      Read a line, ignoring blank lines and lines that start with '#'.
      void reset()
      Reset this reader so that the next call to readLine() returns the first line of the file again.
      • Methods inherited from class java.lang.Object

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

      • reader

        private java.io.BufferedReader reader
      • resourceName

        private java.lang.String resourceName
      • encoding

        private java.lang.String encoding
      • root

        private java.lang.Class<?> root
      • lineNo

        private int lineNo
        The one-based line number. Has the special value -1 before the object is initialized. Has the special value 0 after initialization but before the first line is read.
    • Constructor Detail

      • ResourceReader

        public ResourceReader​(java.lang.String resourceName,
                              java.lang.String encoding)
                       throws java.io.UnsupportedEncodingException
        Construct a reader object for the text file of the given name in this package, using the given encoding.
        Parameters:
        resourceName - the name of the text file located in this package's ".data" subpackage.
        encoding - the encoding of the text file; if unsupported an exception is thrown
        Throws:
        java.io.UnsupportedEncodingException - if encoding is not supported by the JDK.
      • ResourceReader

        public ResourceReader​(java.lang.String resourceName)
        Construct a reader object for the text file of the given name in this package, using the default encoding.
        Parameters:
        resourceName - the name of the text file located in this package's ".data" subpackage.
      • ResourceReader

        public ResourceReader​(java.lang.Class<?> rootClass,
                              java.lang.String resourceName,
                              java.lang.String encoding)
                       throws java.io.UnsupportedEncodingException
        Construct a reader object for the text file of the given name in the given class's package, using the given encoding.
        Parameters:
        resourceName - the name of the text file located in the given class's package.
        encoding - the encoding of the text file; if unsupported an exception is thrown
        Throws:
        java.io.UnsupportedEncodingException - if encoding is not supported by the JDK.
      • ResourceReader

        public ResourceReader​(java.io.InputStream is,
                              java.lang.String resourceName,
                              java.lang.String encoding)
        Construct a reader object for the input stream associated with the given resource name.
        Parameters:
        is - the input stream of the resource
        resourceName - the name of the resource
      • ResourceReader

        public ResourceReader​(java.io.InputStream is,
                              java.lang.String resourceName)
        Construct a reader object for the input stream associated with the given resource name.
        Parameters:
        is - the input stream of the resource
        resourceName - the name of the resource
      • ResourceReader

        public ResourceReader​(java.lang.Class<?> rootClass,
                              java.lang.String resourceName)
        Construct a reader object for the text file of the given name in the given class's package, using the default encoding.
        Parameters:
        resourceName - the name of the text file located in the given class's package.
    • Method Detail

      • readLine

        public java.lang.String readLine()
                                  throws java.io.IOException
        Read and return the next line of the file or null if the end of the file has been reached.
        Throws:
        java.io.IOException
      • readLineSkippingComments

        public java.lang.String readLineSkippingComments​(boolean trim)
                                                  throws java.io.IOException
        Read a line, ignoring blank lines and lines that start with '#'.
        Parameters:
        trim - if true then trim leading Pattern_White_Space.
        Throws:
        java.io.IOException
      • readLineSkippingComments

        public java.lang.String readLineSkippingComments()
                                                  throws java.io.IOException
        Read a line, ignoring blank lines and lines that start with '#'. Do not trim leading Pattern_White_Space.
        Throws:
        java.io.IOException
      • getLineNumber

        public int getLineNumber()
        Return the one-based line number of the last line returned by readLine() or readLineSkippingComments(). Should only be called after a call to one of these methods; otherwise the return value is undefined.
      • describePosition

        public java.lang.String describePosition()
        Return a string description of the position of the last line returned by readLine() or readLineSkippingComments().
      • reset

        public void reset()
        Reset this reader so that the next call to readLine() returns the first line of the file again. This is a somewhat expensive call, however, calling reset() after calling it the first time does nothing if readLine() has not been called in between.
      • _reset

        private void _reset()
                     throws java.io.UnsupportedEncodingException
        Reset to the start by reconstructing the stream and readers. We could also use mark() and reset() on the stream or reader, but that would cause them to keep the stream data around in memory. We don't want that because some of the resource files are large, e.g., 400k.
        Throws:
        java.io.UnsupportedEncodingException
      • close

        public void close()
                   throws java.io.IOException
        Closes the underlying reader and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException