public abstract class ByteStream extends StreamBase
Abstract base class for byte
streams.
Modifier and Type | Field and Description |
---|---|
static ByteStream |
empty |
Constructor and Description |
---|
ByteStream() |
Modifier and Type | Method and Description |
---|---|
CharStream |
asText()
Assuming this stream is a UTF-8 byte stream, map it to a character stream (of UTF-16 code units).
|
void |
copyTo(ByteStream target)
|
void |
copyTo(ByteStream target,
boolean closeTarget)
Copy all remaining data until the end of this stream into a target stream, then close this stream.
|
void |
copyToFile(java.lang.String file)
Copy all remaining data until the end of the stream into a file, then close this stream.
|
static ByteStream |
fileWriter(java.lang.String file)
Return a stream that will write the contents of a data file.
|
static ByteStream |
fromBinary(byte[] data)
|
static ByteStream |
fromBinary(byte[] data,
int offset)
|
static ByteStream |
fromBinary(byte[] data,
int offset,
int length)
Return a new stream for reading from a binary value.
|
static ByteStream |
fromFile(java.lang.String file)
Return a stream that will read the contents of a data file.
|
static ByteStream |
fromInput(java.io.InputStream input) |
static ByteStream |
fromStream(StreamBase stream)
Return
stream (if stream is a ByteStream) or ByteStream.fromText(stream) (if stream is a CharStream). |
static ByteStream |
fromText(CharStream stream)
Return a new UTF-8 byte stream which reads from an underlying UTF-16 character stream.
|
boolean |
getCanUndo()
Return does this stream support
ByteStream.undoRead ? |
DataType |
getDataType()
Return data type with a
DataType.code of DataType.BYTE_STREAM. |
byte[] |
readAndClose()
Read all remaining data until the end of the stream, then close this stream.
|
byte[] |
readBinary()
See readBinary(int).
|
byte[] |
readBinary(int length)
Read up to
length bytes from this stream (may read less, even if the end of stream is not reached). |
int |
readByte()
Read a single byte from this stream.
|
static ByteStream |
toBuffer(com.sap.cloud.server.odata.core.ByteBuffer buffer)
Return a new byte stream which writes to a provided buffer.
|
static java.io.InputStream |
toInput(ByteStream stream) |
java.lang.String |
toString()
Convert this data value to a string.
|
void |
undoRead(byte value)
Undo read of one byte.
|
ByteStream |
withUndo()
Return a wrapper (if needed) of this stream supporting
ByteStream.undoRead , or this stream if it already supports undoRead. |
void |
writeBinary(byte[] data)
Write to this stream the portion of
data from the specified offset with the specified length . |
void |
writeByte(byte value)
Write a single byte to this stream.
|
close, flush, getEntityTag, getFileName, getMediaType, setEntityTag, setFileName, setMediaType
cloneMutable, equals, getTypeCode, hashCode
public static final ByteStream empty
public static ByteStream fromInput(java.io.InputStream input)
public static java.io.InputStream toInput(ByteStream stream)
public CharStream asText()
Assuming this stream is a UTF-8 byte stream, map it to a character stream (of UTF-16 code units).
public void copyTo(ByteStream target)
target
- Target parameter.public void copyTo(ByteStream target, boolean closeTarget)
Copy all remaining data until the end of this stream into a target stream, then close this stream.
Also propagate the ByteStream.entityTag
and ByteStream.mediaType
of this stream into the target stream.
target
- Output stream.closeTarget
- Close target stream? (defaults to true
)public void copyToFile(java.lang.String file)
Copy all remaining data until the end of the stream into a file, then close this stream.
file
- Output file name.public static ByteStream fileWriter(java.lang.String file)
Return a stream that will write the contents of a data file.
file
- Output file name.public static ByteStream fromBinary(byte[] data)
data
- Data parameter.public static ByteStream fromBinary(byte[] data, int offset)
data
- Data parameter.offset
- Offset parameter.public static ByteStream fromBinary(byte[] data, int offset, int length)
Return a new stream for reading from a binary value.
data
- Source data.offset
- Starting offset (zero-based).length
- Maximum number of bytes to read.public static ByteStream fromFile(java.lang.String file)
Return a stream that will read the contents of a data file.
file
- Input file name.public static ByteStream fromStream(StreamBase stream)
Return stream
(if stream
is a ByteStream) or ByteStream.fromText(stream)
(if stream
is a CharStream).
stream
- Underlying stream.stream
(if stream
is a ByteStream) or ByteStream.fromText(stream)
(if stream
is a CharStream).public static ByteStream fromText(CharStream stream)
Return a new UTF-8 byte stream which reads from an underlying UTF-16 character stream.
stream
- Underlying character stream.public boolean getCanUndo()
Return does this stream support ByteStream.undoRead
?
ByteStream.undoRead
?public DataType getDataType()
Return data type with a DataType.code
of DataType.BYTE_STREAM.
getDataType
in class DataValue
DataType.code
of DataType.BYTE_STREAM.public byte[] readAndClose()
Read all remaining data until the end of the stream, then close this stream.
Caution: this function loads all stream content into memory at once. Consider reading a chunk-at-a-time in a loop, using ByteStream.readBinary
.
public byte[] readBinary()
public byte[] readBinary(int length)
Read up to length
bytes from this stream (may read less, even if the end of stream is not reached).
length
- Maximum number of bytes to read (must be greater than zero). Defaults to 20,000.length
bytes, or null
if there is no remaining data in the stream.public int readByte()
Read a single byte from this stream.
-1
at end of stream.public static ByteStream toBuffer(com.sap.cloud.server.odata.core.ByteBuffer buffer)
Return a new byte stream which writes to a provided buffer.
buffer
- Buffer to be written to.public java.lang.String toString()
Convert this data value to a string.
If the ByteStream.dataType
is defined by XML Schema Part 2: Datatypes, then the corresponding lexical format is used.
JSON format is used for structured values (arrays and objects).
public void undoRead(byte value)
Undo read of one byte.
DataStreamException
if undo is not supported by this stream.
value
- Byte value that was previously read by the caller and is to be made available to read again.public ByteStream withUndo()
Return a wrapper (if needed) of this stream supporting ByteStream.undoRead
, or this stream if it already supports undoRead.
ByteStream.undoRead
, or this stream if it already supports undoRead.public void writeBinary(byte[] data)
Write to this stream the portion of data
from the specified offset
with the specified length
.
data
- Source data.public void writeByte(byte value)
Write a single byte to this stream.
value
- Byte value to be written.