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
IOException
to 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 StringCharSource
EMPTY
An empty source.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ArrayByteSource
asByteSource(Charset charset)
ArrayByteSource
asByteSourceUtf8()
Converts this char source to a byte source in UTF-8.long
copyTo(CharSink sink)
long
copyTo(Appendable appendable)
boolean
equals(Object obj)
static StringCharSource
from(CharSource other)
Obtains an instance from another char source.static StringCharSource
from(CheckedSupplier<? extends Reader> readerSupplier)
Obtains an instance from aReader
.static StringCharSource
from(Readable reader)
Obtains an instance from aReadable
.static StringCharSource
fromBytes(byte[] bytes, Charset charset)
Obtains an instance from a byte array.static StringCharSource
fromBytesUtf8(byte[] bytes)
Obtains an instance from a UTF-8 byte array.Optional<String>
getFileName()
Gets the file name of the source.int
hashCode()
boolean
isEmpty()
long
length()
Optional<Long>
lengthIfKnown()
Gets the length, which is always known.StringCharSource
load()
Loads the content of the source into memory.org.joda.beans.MetaBean
metaBean()
static StringCharSource
of(String str)
Obtains an instance.Reader
openStream()
String
read()
String
toString()
StringCharSource
withFileName(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:
metaBean
in interfaceorg.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 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:
openStream
in classCharSource
-
isEmpty
public boolean isEmpty()
- Overrides:
isEmpty
in classBeanCharSource
-
lengthIfKnown
public Optional<Long> lengthIfKnown()
Gets the length, which is always known.- Overrides:
lengthIfKnown
in classCharSource
- Returns:
- the length, which is always known
-
length
public long length()
- Overrides:
length
in classBeanCharSource
-
copyTo
public long copyTo(Appendable appendable) throws IOException
- Overrides:
copyTo
in classCharSource
- Throws:
IOException
-
copyTo
public long copyTo(CharSink sink) throws IOException
- Overrides:
copyTo
in classCharSource
- Throws:
IOException
-
read
public String read()
- Overrides:
read
in classBeanCharSource
-
load
public StringCharSource load()
Description copied from class:BeanCharSource
Loads the content of the source into memory.- Overrides:
load
in classBeanCharSource
- Returns:
- the string-based source
-
asByteSourceUtf8
public ArrayByteSource asByteSourceUtf8()
Description copied from class:BeanCharSource
Converts this char source to a byte source in UTF-8.- Overrides:
asByteSourceUtf8
in classBeanCharSource
- Returns:
- the equivalent byte source
-
asByteSource
public ArrayByteSource asByteSource(Charset charset)
- Overrides:
asByteSource
in classBeanCharSource
-
-