Class UnicodeEscapeReader

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

    public class UnicodeEscapeReader
    extends java.io.FilterReader
    The UnicodeEscapeReader is a filter for reading \ uxxxx encoded character streams and converting them to their unicode character equivalent.

    Here's an example of how to set up a Reader stream to use this filter:

     BufferedReader in = new BufferedReader(new UnicodeEscapeReader(new FileReader(filename)));
     

    Then, all you need to do is say something like in.readLine() to convert the \ uxxxx characters in the stream to pure unicode.

    Note that there are two escape sequences supported by this reader:

    • \ uxxxx - A unicode character
    • \\ - A backslash character
    See Also:
    UnicodeEscapeWriter
    • Field Summary

      • Fields inherited from class java.io.Reader

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      UnicodeEscapeReader​(java.io.Reader in)
      Create a Reader for filtering unicode escape sequences.
      UnicodeEscapeReader​(java.io.Reader in, boolean escapeSlashes)
      Create a Reader for filtering unicode escape sequences.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean getEscapeSlashes()
      Determine whehter backslashes are being escaped or not
      int read()
      Read a character from the stream and return it.
      int read​(char[] cbuff, int off, int len)
      Read characters from the stream into a buffer.
      void setEscapeSlashes​(boolean escapeSlashes)
      Set whether to escape backslashes passed through this writer or not.
      • Methods inherited from class java.io.FilterReader

        close, mark, markSupported, ready, reset, skip
      • Methods inherited from class java.io.Reader

        nullReader, read, read, transferTo
      • Methods inherited from class java.lang.Object

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

      • UnicodeEscapeReader

        public UnicodeEscapeReader​(java.io.Reader in)
        Create a Reader for filtering unicode escape sequences.
      • UnicodeEscapeReader

        public UnicodeEscapeReader​(java.io.Reader in,
                                   boolean escapeSlashes)
        Create a Reader for filtering unicode escape sequences.
        Parameters:
        in - Input stream to be filtered
        escapeSlashes - Whether or not standalone backslashes should be expanded or not. Default is true.
    • Method Detail

      • read

        public int read​(char[] cbuff,
                        int off,
                        int len)
                 throws java.io.IOException
        Read characters from the stream into a buffer. This version of read will read in data from the stream and convert any escape sequences found into single characters. Escape sequences count as one character for the return value and for the len paramter. Therefore, it's possible for more characters to be physically read from the stream than returned. This is the ideal behavior because the number of characters returned should always be the same as requested, unless the end of the stream is hit during this operation.
        Overrides:
        read in class java.io.FilterReader
        Parameters:
        cbuff - Buffer for copying characters into from the input stream.
        off - Offset in the buffer where the copying should begin.
        len - Number of characters to copy from the stream.
        Returns:
        Count of characters read or -1 if end of stream reached.
        Throws:
        MalformedUnicodeEscapeException - This will be thrown if the input stream contains contains an error in an escape sequence. The errors that can occur are:
        • Incomplete unicode escape sequence. This occurs when there aren't enough characters in the input stream to complete an escape sequence.
        • Invalid character in unicode escape sequence. This occurrs when given \ uxxxx, hex is not a hex digit ([0-9][a-f]). Only lower case hex is supported.
        • Malformed escape sequence. This is occurs when a backslash is not followed by another backslash or 'u'.
        java.io.IOException - Occurs when any other general IO Exception occurs.
        See Also:
        MalformedUnicodeEscapeException
      • read

        public int read()
                 throws java.io.IOException
        Read a character from the stream and return it. This version of read will read in data from the stream and convert any escape sequences found into a single character. Escape sequences count as one character for the return value and for the len paramter. Therefore, it's possible for more characters to be physically read from the stream than returned. This is the ideal behavior because the number of characters returned should always be the same as requested, unless the end of the stream is hit during this operation.
        Overrides:
        read in class java.io.FilterReader
        Returns:
        The character read from the input stream.
        Throws:
        MalformedUnicodeEscapeException - This will be thrown if the input stream contains contains an error in an escape sequence. The errors that can occur are:
        • Incomplete unicode escape sequence. This occurs when there aren't enough characters in the input stream to complete an escape sequence.
        • Invalid character in unicode escape sequence. This occurrs when given \ uxxxx, hex is not a hex digit ([0-9][a-f]). Only lower case hex is supported.
        • Malformed escape sequence. This is occurs when a backslash is not followed by another backslash or 'u'.
        java.io.IOException - Occurs when any other general IO Exception occurs.
        See Also:
        MalformedUnicodeEscapeException
      • setEscapeSlashes

        public void setEscapeSlashes​(boolean escapeSlashes)
        Set whether to escape backslashes passed through this writer or not.
        Parameters:
        escapeSlashes - True to have backslashes escaped in this stream
      • getEscapeSlashes

        public boolean getEscapeSlashes()
        Determine whehter backslashes are being escaped or not
        Returns:
        True if backslashes are being escaped in this stream