jaulib v1.3.0
Jau Support Library (C++, Java, ..)
ByteInStream.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022-2023 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24package org.jau.io;
25
26import java.nio.ByteBuffer;
27
28/**
29 * Abstract byte input stream object.
30 *
31 * Its specializations utilize a native C++ implementation
32 * derived from `jau::io::ByteInStream`.
33 *
34 * @anchor byte_in_stream_properties
35 * ### ByteInStream Properties
36 * The byte input stream can originate from a local source w/o delay,
37 * remote URL like http connection or even from another thread feeding the input buffer.<br />
38 * Both latter asynchronous resources may expose blocking properties
39 * in available().
40 *
41 * Asynchronous resources benefit from knowing their content size,
42 * as their available() implementation may avoid
43 * blocking and waiting for requested bytes available
44 * if the stream is already beyond its scope.
45 *
46 * All method implementations are of `noexcept`.
47 *
48 * One may use fail() to detect whether an error has occurred,
49 * while end_of_data() not only covers the end-of-stream (EOS) case but includes fail().
50 *
51 * @see @ref byte_in_stream_properties "ByteInStream Properties"
52 */
53public interface ByteInStream extends IOStateFunc, AutoCloseable {
54 /** Checks if the stream has an associated file. */
55 boolean is_open();
56
57 /**
58 * Close the stream if supported by the underlying mechanism.
59 *
60 * Native instance will not be disposed.
61 *
62 * {@inheritDoc}
63 */
65
66 /**
67 * Close the stream if supported by the underlying mechanism
68 * and dispose the native instance.
69 *
70 * Instance is unusable after having this method called.
71 *
72 * {@inheritDoc}
73 */
74 @Override
75 void close();
76
77 /**
78 * Return whether n bytes are available in the input stream,
79 * if has_content_size() or using an asynchronous source.
80 *
81 * If !has_content_size() and not being an asynchronous source,
82 * !end_of_data() is returned.
83 *
84 * Method may be blocking when using an asynchronous source
85 * up until the requested bytes are available.
86 *
87 * A subsequent call to read() shall return immediately with at least
88 * the requested numbers of bytes available,
89 * if has_content_size() or using an asynchronous source.
90 *
91 * See details of the implementing class.
92 *
93 * @param n byte count to wait for
94 * @return true if n bytes are available, otherwise false
95 *
96 * @see has_content_size()
97 * @see read()
98 * @see @ref byte_in_stream_properties "ByteInStream Properties"
99 */
100 boolean available(long n);
101
102 /**
103 * Read from the source. Moves the internal offset so that every
104 * call to read will return a new portion of the source.
105 *
106 * Use available() to try to wait for a certain amount of bytes available.
107 *
108 * This method shall only block until `min(available, length)` bytes are transfered.
109 *
110 * See details of the implementing class.
111 *
112 * @param out the byte array to write the result to
113 * @param offset offset to byte array to read into
114 * @param length the length of the byte array out
115 * @return length in bytes that was actually read and put into out
116 *
117 * @see available()
118 * @see @ref byte_in_stream_properties "ByteInStream Properties"
119 */
120 int read(byte out[], final int offset, final int length);
121
122 /**
123 * Read from the source. Moves the internal offset so that every
124 * call to read will return a new portion of the source.
125 *
126 * Use available() to try to wait for a certain amount of bytes available.
127 *
128 * This method shall only block until `min(available, length)` bytes are transfered.
129 *
130 * See details of the implementing class.
131 *
132 * @param out the direct {@link ByteBuffer} to write the result starting at its {@link ByteBuffer#position() position} up to its {@link ByteBuffer#capacity() capacity}.
133 * {@link ByteBuffer#limit() Limit} will be set to {@link ByteBuffer#position() position} + read-bytes.
134 * @return length in bytes that was actually read and put into out,
135 * equal to its {@link ByteBuffer#limit() limit} - {@link ByteBuffer#position() position}, i.e. {@link ByteBuffer#remaining() remaining}.
136 *
137 * @see available()
138 * @see @ref byte_in_stream_properties "ByteInStream Properties"
139 */
140 int read(ByteBuffer out);
141
142 /**
143 * Read from the source but do not modify the internal
144 * offset. Consecutive calls to peek() will return portions of
145 * the source starting at the same position.
146 *
147 * @param out the byte array to write the output to
148 * @param offset offset to byte array to read into
149 * @param length number of in bytes to read into starting at offset
150 * @param peek_offset the offset into the stream to read at
151 * @return length in bytes that was actually read and put into out
152 */
153 int peek(byte out[], final int offset, final int length, final long peek_offset);
154
155 /**
156 * return the id of this data source
157 * @return std::string representing the id of this data source
158 */
159 String id();
160
161 /**
162 * Discard the next N bytes of the data
163 * @param N the number of bytes to discard
164 * @return number of bytes actually discarded
165 */
166 long discard_next(long N);
167
168 /**
169 * Returns the input position indicator, similar to std::basic_istream.
170 *
171 * @return number of bytes read so far.
172 */
173 long tellg();
174
175 /**
176 * Returns true if implementation is aware of content_size(), otherwise false.
177 * @see content_size()
178 */
180
181 /**
182 * Returns the content_size if known.
183 * @see has_content_size()
184 */
186
187
188 @Override
189 String toString();
190}
Abstract byte input stream object.
int peek(byte out[], final int offset, final int length, final long peek_offset)
Read from the source but do not modify the internal offset.
long content_size()
Returns the content_size if known.
boolean available(long n)
Return whether n bytes are available in the input stream, if has_content_size() or using an asynchron...
int read(byte out[], final int offset, final int length)
Read from the source.
int read(ByteBuffer out)
Read from the source.
long discard_next(long N)
Discard the next N bytes of the data.
void close()
Close the stream if supported by the underlying mechanism and dispose the native instance.
boolean has_content_size()
Returns true if implementation is aware of content_size(), otherwise false.
boolean is_open()
Checks if the stream has an associated file.
String id()
return the id of this data source
void closeStream()
Close the stream if supported by the underlying mechanism.
long tellg()
Returns the input position indicator, similar to std::basic_istream.
Supporting std::basic_ios's iostate functionality for all ByteInStream implementations.