aboutsummaryrefslogtreecommitdiffstats
path: root/java_jni/org/jau/io/ByteInStream.java
blob: 407f08bc5c5a5a5baebb0143ce063f61bf5ec878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2022 Gothel Software e.K.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.jau.io;

import java.nio.ByteBuffer;

/**
 * Abstract byte input stream object.
 *
 * Its specializations utilize a native C++ implementation
 * derived from `jau::io::ByteInStream`.
 *
 * @anchor byte_in_stream_properties
 * ### ByteInStream Properties
 * The byte input stream can originate from a local source w/o delay,
 * remote URL like http connection or even from another thread feeding the input buffer.<br />
 * Both latter asynchronous resources may expose blocking properties
 * in available().
 *
 * Asynchronous resources benefit from knowing their content size,
 * as their available() implementation may avoid
 * blocking and waiting for requested bytes available
 * if the stream is already beyond its scope.
 *
 * All method implementations are of `noexcept`.
 *
 * One may use fail() to detect whether an error has occurred,
 * while end_of_data() not only covers the end-of-stream (EOS) case but includes fail().
 *
 * @see @ref byte_in_stream_properties "ByteInStream Properties"
 */
public interface ByteInStream extends AutoCloseable  {
    /**
     * Close the stream if supported by the underlying mechanism.
     *
     * Native instance will not be disposed.
     *
     * {@inheritDoc}
     */
    void closeStream();

    /**
     * Close the stream if supported by the underlying mechanism
     * and dispose the native instance.
     *
     * Instance is unusable after having this method called.
     *
     * {@inheritDoc}
     */
    @Override
    void close();

    /** Clears state flags by assignment to the given value. */
    void clear(final IOState state);

    /**
     * Returns the current state flags.
     *
     * Method is marked `virtual` to allow implementations with asynchronous resources
     * to determine or update the current iostate.
     *
     * Method is used throughout all query members and setstate(),
     * hence they all will use the updated state from a potential override implementation.
     */
    IOState rdState();

    /** Sets state flags, by keeping its previous bits. */
    void setState(final IOState state);

    /** Checks if no error nor eof() has occurred i.e. I/O operations are available. */
    boolean good();

    /** Checks if end-of-file has been reached. */
    boolean eof();

    /** Checks if an error has occurred. */
    boolean fail();

    /** Checks if a non-recoverable error has occurred. */
    boolean bad();

    /**
     * Test whether the source still has data that can be read, synonym for !good().
     *
     * Hence this includes errors in the underlying implementation, see fail()
     *
     * @return true if there is no more data to read, false otherwise
     * @see good()
     * @see fail()
     */
    boolean end_of_data();

    /**
     * Return whether n bytes are available in the input stream,
     * if has_content_size() or using an asynchronous source.
     *
     * If !has_content_size() and not being an asynchronous source,
     * !end_of_data() is returned.
     *
     * Method may be blocking when using an asynchronous source
     * up until the requested bytes are available.
     *
     * A subsequent call to read() shall return immediately with at least
     * the requested numbers of bytes available,
     * if has_content_size() or using an asynchronous source.
     *
     * See details of the implementing class.
     *
     * @param n byte count to wait for
     * @return true if n bytes are available, otherwise false
     *
     * @see has_content_size()
     * @see read()
     * @see @ref byte_in_stream_properties "ByteInStream Properties"
     */
    boolean available(long n);

    /**
     * Read from the source. Moves the internal offset so that every
     * call to read will return a new portion of the source.
     *
     * Use available() to try to wait for a certain amount of bytes available.
     *
     * This method shall only block until `min(available, length)` bytes are transfered.
     *
     * See details of the implementing class.
     *
     * @param out the byte array to write the result to
     * @param offset offset to in byte array to read into
     * @param length the length of the byte array out
     * @return length in bytes that was actually read and put into out
     *
     * @see available()
     * @see @ref byte_in_stream_properties "ByteInStream Properties"
     */
    int read(byte out[], final int offset, final int length);

    /**
     * Read from the source. Moves the internal offset so that every
     * call to read will return a new portion of the source.
     *
     * Use available() to try to wait for a certain amount of bytes available.
     *
     * This method shall only block until `min(available, length)` bytes are transfered.
     *
     * See details of the implementing class.
     *
     * @param out the direct {@link ByteBuffer} to write the result starting at its {@link ByteBuffer#position() position} up to its {@link ByteBuffer#capacity() capacity}.
     *            {@link ByteBuffer#limit() Limit} will be set to {@link ByteBuffer#position() position} + read-bytes.
     * @return length in bytes that was actually read and put into out,
     *         equal to its {@link ByteBuffer#limit() limit} - {@link ByteBuffer#position() position}, i.e. {@link ByteBuffer#remaining() remaining}.
     *
     * @see available()
     * @see @ref byte_in_stream_properties "ByteInStream Properties"
     */
    int read(ByteBuffer out);

    /**
     * Read from the source but do not modify the internal
     * offset. Consecutive calls to peek() will return portions of
     * the source starting at the same position.
     *
     * @param out the byte array to write the output to
     * @param offset offset to in byte array to read into
     * @param length number of in bytes to read into starting at offset
     * @param peek_offset the offset into the stream to read at
     * @return length in bytes that was actually read and put into out
     */
    int peek(byte out[], final int offset, final int length, final long peek_offset);

    /**
     * return the id of this data source
     * @return std::string representing the id of this data source
     */
    String id();

    /**
     * Discard the next N bytes of the data
     * @param N the number of bytes to discard
     * @return number of bytes actually discarded
     */
    long discard_next(long N);

    /**
     * Returns the input position indicator, similar to std::basic_istream.
     *
     * @return number of bytes read so far.
     */
    long tellg();

    /**
     * Returns true if implementation is aware of content_size(), otherwise false.
     * @see content_size()
     */
    boolean has_content_size();

    /**
     * Returns the content_size if known.
     * @see has_content_size()
     */
    long content_size();


    @Override
    String toString();
}