jaulib v1.3.0
Jau Support Library (C++, Java, ..)
ByteOutStream.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 output stream object,
30 * to write data to a sink.
31 *
32 * Its specializations utilize a native C++ implementation
33 * derived from `jau::io::ByteOutStream`.
34 *
35 * All method implementations are of `noexcept`.
36 *
37 * One may use fail() to detect whether an error has occurred.
38 */
39public interface ByteOutStream extends IOStateFunc, AutoCloseable {
40 /** Checks if the stream has an associated file. */
41 boolean is_open();
42
43 /**
44 * Close the stream if supported by the underlying mechanism.
45 *
46 * Native instance will not be disposed.
47 *
48 * {@inheritDoc}
49 */
51
52 /**
53 * Close the stream if supported by the underlying mechanism
54 * and dispose the native instance.
55 *
56 * Instance is unusable after having this method called.
57 *
58 * {@inheritDoc}
59 */
60 @Override
61 void close();
62
63 /**
64 * Write to the data sink. Moves the internal offset so that every
65 * call to write will be appended to the sink.
66 *
67 * This method is not blocking beyond the transfer length bytes.
68 *
69 * @param in the input bytes to write out
70 * @param offset offset to byte array to write out
71 * @param length the length of the byte array in
72 * @return length in bytes that were actually written
73 */
74 int write(byte in[], final int offset, final int length);
75
76 /**
77 * Write to the data sink. Moves the internal offset so that every
78 * call to write will be appended to the sink.
79 *
80 * This method is not blocking beyond the transfer length bytes.
81 *
82 * @param in the direct {@link ByteBuffer} source to be written to the sink from {@link ByteBuffer#position() position} up to its {@link ByteBuffer#limit() limit},
83 * i.e. {@link ByteBuffer#remaining() remaining}.
84 * {@link ByteBuffer#position() Position} will be set to {@link ByteBuffer#position() position} + written-bytes.
85 * @return length in bytes that were actually written,
86 * equal to its current {@link ByteBuffer#position() position} - previous {@link ByteBuffer#position() position}.
87 */
88 int write(ByteBuffer in);
89
90 /**
91 * return the id of this data source
92 * @return std::string representing the id of this data source
93 */
94 String id();
95
96 /**
97 * Returns the output position indicator.
98 *
99 * @return number of bytes written so far.
100 */
101 long tellp();
102
103 @Override
104 String toString();
105}
Abstract byte output stream object, to write data to a sink.
boolean is_open()
Checks if the stream has an associated file.
long tellp()
Returns the output position indicator.
void closeStream()
Close the stream if supported by the underlying mechanism.
int write(byte in[], final int offset, final int length)
Write to the data sink.
int write(ByteBuffer in)
Write to the data sink.
void close()
Close the stream if supported by the underlying mechanism and dispose the native instance.
String id()
return the id of this data source
Supporting std::basic_ios's iostate functionality for all ByteInStream implementations.