Package com.opengamma.strata.collect.io
Class StringCharSource
- java.lang.Object
-
- com.google.common.io.CharSource
-
- com.opengamma.strata.collect.io.BeanCharSource
-
- com.opengamma.strata.collect.io.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 aString.This implementation allows
IOExceptionto be avoided in many cases, and to be able to create and retrieve the internal array unsafely.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static StringCharSourceEMPTYAn empty source.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ArrayByteSourceasByteSource(Charset charset)ArrayByteSourceasByteSourceUtf8()Converts this char source to a byte source in UTF-8.longcopyTo(CharSink sink)longcopyTo(Appendable appendable)booleanequals(Object obj)static StringCharSourcefrom(CharSource other)Obtains an instance from another char source.static StringCharSourcefrom(CheckedSupplier<? extends Reader> readerSupplier)Obtains an instance from aReader.static StringCharSourcefrom(Readable reader)Obtains an instance from aReadable.static StringCharSourcefromBytes(byte[] bytes, Charset charset)Obtains an instance from a byte array.static StringCharSourcefromBytesUtf8(byte[] bytes)Obtains an instance from a UTF-8 byte array.Optional<String>getFileName()Gets the file name of the source.inthashCode()booleanisEmpty()longlength()Optional<Long>lengthIfKnown()Gets the length, which is always known.StringCharSourceload()Loads the content of the source into memory.org.joda.beans.MetaBeanmetaBean()static StringCharSourceof(String str)Obtains an instance.ReaderopenStream()Stringread()StringtoString()StringCharSourcewithFileName(String fileName)Returns an instance with the file name updated.-
Methods inherited from class com.opengamma.strata.collect.io.BeanCharSource
forEachLine, getFileNameOrThrow, lines, readFirstLine, readLines, readLines
-
Methods inherited from class com.google.common.io.CharSource
concat, concat, concat, empty, openBufferedStream, wrap
-
-
-
-
Field Detail
-
EMPTY
public static final StringCharSource EMPTY
An empty source.
-
-
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 bytescharset- 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 aReader.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 aReadable.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:
metaBeanin interfaceorg.joda.beans.Bean
-
getFileName
public Optional<String> getFileName()
Description copied from class:BeanCharSourceGets the file name of the source.Most sources originate from a file-based location. This is captured and returned here where available.
- Overrides:
getFileNamein classBeanCharSource- 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
-
openStream
public Reader openStream()
- Specified by:
openStreamin classCharSource
-
isEmpty
public boolean isEmpty()
- Overrides:
isEmptyin classBeanCharSource
-
lengthIfKnown
public Optional<Long> lengthIfKnown()
Gets the length, which is always known.- Overrides:
lengthIfKnownin classCharSource- Returns:
- the length, which is always known
-
length
public long length()
- Overrides:
lengthin classBeanCharSource
-
copyTo
public long copyTo(Appendable appendable) throws IOException
- Overrides:
copyToin classCharSource- Throws:
IOException
-
copyTo
public long copyTo(CharSink sink) throws IOException
- Overrides:
copyToin classCharSource- Throws:
IOException
-
read
public String read()
- Overrides:
readin classBeanCharSource
-
load
public StringCharSource load()
Description copied from class:BeanCharSourceLoads the content of the source into memory.- Overrides:
loadin classBeanCharSource- Returns:
- the string-based source
-
asByteSourceUtf8
public ArrayByteSource asByteSourceUtf8()
Description copied from class:BeanCharSourceConverts this char source to a byte source in UTF-8.- Overrides:
asByteSourceUtf8in classBeanCharSource- Returns:
- the equivalent byte source
-
asByteSource
public ArrayByteSource asByteSource(Charset charset)
- Overrides:
asByteSourcein classBeanCharSource
-
-