Class StringCharSource

  • All Implemented Interfaces:
    Serializable, org.joda.beans.Bean, org.joda.beans.ImmutableBean

    public final class StringCharSource
    extends BeanCharSource
    implements org.joda.beans.ImmutableBean, Serializable
    A char source implementation that explicitly wraps a String.

    This implementation allows IOException to be avoided in many cases, and to be able to create and retrieve the internal array unsafely.

    See Also:
    Serialized Form
    • Method Detail

      • of

        public static StringCharSource of​(String str)
        Obtains an instance.
        Parameters:
        str - the string
        Returns:
        the char source
      • fromBytesUtf8

        public static StringCharSource fromBytesUtf8​(byte[] bytes)
        Obtains an instance from a UTF-8 byte array.
        Parameters:
        bytes - the UTF-8 bytes
        Returns:
        the char source
      • fromBytes

        public static StringCharSource fromBytes​(byte[] bytes,
                                                 Charset charset)
        Obtains an instance from a byte array.
        Parameters:
        bytes - the bytes
        charset - the encoding
        Returns:
        the char source
      • from

        public static StringCharSource from​(CharSource other)
        Obtains an instance from another char source.
        Parameters:
        other - the other char source
        Returns:
        the char source
        Throws:
        UncheckedIOException - if an IO error occurs
      • from

        public static StringCharSource from​(CheckedSupplier<? extends Reader> readerSupplier)
        Obtains an instance from a Reader.

        This method use the supplier to open the reader, extract the chars and close the reader. It is intended that invoking the supplier opens the reader. It is not intended that an already open reader is supplied.

        Parameters:
        readerSupplier - the supplier of the reader
        Returns:
        the char source
        Throws:
        UncheckedIOException - if an IO error occurs
      • from

        public static StringCharSource from​(Readable reader)
                                     throws IOException
        Obtains an instance from a Readable.

        This method uses an already open reader, extracting the chars. The stream is not closed - that is the responsibility of the caller.

        Parameters:
        reader - the open reader, which will not be closed
        Returns:
        the char source
        Throws:
        IOException - if an IO error occurs
      • metaBean

        public org.joda.beans.MetaBean metaBean()
        Specified by:
        metaBean in interface org.joda.beans.Bean
      • getFileName

        public Optional<String> getFileName()
        Description copied from class: BeanCharSource
        Gets the file name of the source.

        Most sources originate from a file-based location. This is captured and returned here where available.

        Overrides:
        getFileName in class BeanCharSource
        Returns:
        the file name, empty if not known
      • withFileName

        public StringCharSource withFileName​(String fileName)
        Returns an instance with the file name updated.

        If a path is passed in, only the file name is retained.

        Parameters:
        fileName - the file name, an empty string can be used to remove the file name
        Returns:
        a source with the specified file name
      • lengthIfKnown

        public Optional<Long> lengthIfKnown()
        Gets the length, which is always known.
        Overrides:
        lengthIfKnown in class CharSource
        Returns:
        the length, which is always known
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object