diff options
Diffstat (limited to 'java_jni/org')
-rw-r--r-- | java_jni/org/jau/io/ByteInStream.java | 38 | ||||
-rw-r--r-- | java_jni/org/jau/io/ByteInStream_Feed.java | 5 | ||||
-rw-r--r-- | java_jni/org/jau/io/ByteInStream_File.java | 3 | ||||
-rw-r--r-- | java_jni/org/jau/io/ByteInStream_URL.java | 5 | ||||
-rw-r--r-- | java_jni/org/jau/io/ByteOutStream.java | 105 | ||||
-rw-r--r-- | java_jni/org/jau/io/ByteOutStream_File.java | 188 | ||||
-rw-r--r-- | java_jni/org/jau/io/IOStateFunc.java | 59 |
7 files changed, 369 insertions, 34 deletions
diff --git a/java_jni/org/jau/io/ByteInStream.java b/java_jni/org/jau/io/ByteInStream.java index 407f08b..9303635 100644 --- a/java_jni/org/jau/io/ByteInStream.java +++ b/java_jni/org/jau/io/ByteInStream.java @@ -50,7 +50,10 @@ import java.nio.ByteBuffer; * * @see @ref byte_in_stream_properties "ByteInStream Properties" */ -public interface ByteInStream extends AutoCloseable { +public interface ByteInStream extends IOStateFunc, AutoCloseable { + /** Checks if the stream has an associated file. */ + boolean is_open(); + /** * Close the stream if supported by the underlying mechanism. * @@ -71,35 +74,6 @@ public interface ByteInStream extends AutoCloseable { @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(). * @@ -147,7 +121,7 @@ public interface ByteInStream extends AutoCloseable { * 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 offset offset to 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 * @@ -182,7 +156,7 @@ public interface ByteInStream extends AutoCloseable { * 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 offset offset to 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 diff --git a/java_jni/org/jau/io/ByteInStream_Feed.java b/java_jni/org/jau/io/ByteInStream_Feed.java index 96faa3f..ceb9127 100644 --- a/java_jni/org/jau/io/ByteInStream_Feed.java +++ b/java_jni/org/jau/io/ByteInStream_Feed.java @@ -26,7 +26,7 @@ package org.jau.io; import java.nio.ByteBuffer; /** - * This class represents a Ringbuffer-Based byte input stream with an externally provisioned data feed. + * Ringbuffer-Based byte input stream with an externally provisioned data feed. * * Instance uses the native C++ object `jau::io::ByteInStream_Feed`. */ @@ -71,6 +71,9 @@ public final class ByteInStream_Feed implements ByteInStream { } @Override + public native boolean is_open(); + + @Override public void clear(final IOState state) { clearImpl( state.mask ); } diff --git a/java_jni/org/jau/io/ByteInStream_File.java b/java_jni/org/jau/io/ByteInStream_File.java index ff2448f..de75de6 100644 --- a/java_jni/org/jau/io/ByteInStream_File.java +++ b/java_jni/org/jau/io/ByteInStream_File.java @@ -117,6 +117,9 @@ public final class ByteInStream_File implements ByteInStream { } @Override + public native boolean is_open(); + + @Override public void clear(final IOState state) { clearImpl( state.mask ); } diff --git a/java_jni/org/jau/io/ByteInStream_URL.java b/java_jni/org/jau/io/ByteInStream_URL.java index e4d0a68..a9e79ad 100644 --- a/java_jni/org/jau/io/ByteInStream_URL.java +++ b/java_jni/org/jau/io/ByteInStream_URL.java @@ -26,7 +26,7 @@ package org.jau.io; import java.nio.ByteBuffer; /** - * This class represents a Ringbuffer-Based byte input stream with a URL connection provisioned data feed. + * Ringbuffer-Based byte input stream with a URL connection provisioned data feed. * * Instance uses the native C++ object `jau::io::ByteInStream_URL`. * @@ -75,6 +75,9 @@ public final class ByteInStream_URL implements ByteInStream { } @Override + public native boolean is_open(); + + @Override public void clear(final IOState state) { clearImpl( state.mask ); } diff --git a/java_jni/org/jau/io/ByteOutStream.java b/java_jni/org/jau/io/ByteOutStream.java new file mode 100644 index 0000000..9862906 --- /dev/null +++ b/java_jni/org/jau/io/ByteOutStream.java @@ -0,0 +1,105 @@ +/** + * Author: Sven Gothel <[email protected]> + * 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 output stream object, + * to write data to a sink. + * + * Its specializations utilize a native C++ implementation + * derived from `jau::io::ByteOutStream`. + * + * All method implementations are of `noexcept`. + * + * One may use fail() to detect whether an error has occurred. + */ +public interface ByteOutStream extends IOStateFunc, AutoCloseable { + /** Checks if the stream has an associated file. */ + boolean is_open(); + + /** + * 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(); + + /** + * Write to the data sink. Moves the internal offset so that every + * call to write will be appended to the sink. + * + * This method is not blocking beyond the transfer length bytes. + * + * @param in the input bytes to write out + * @param offset offset to byte array to write out + * @param length the length of the byte array in + * @return length in bytes that were actually written + */ + int write(byte in[], final int offset, final int length); + + /** + * Write to the data sink. Moves the internal offset so that every + * call to write will be appended to the sink. + * + * This method is not blocking beyond the transfer length bytes. + * + * @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}, + * i.e. {@link ByteBuffer#remaining() remaining}. + * {@link ByteBuffer#position() Position} will be set to {@link ByteBuffer#position() position} + written-bytes. + * @return length in bytes that were actually written, + * equal to its current {@link ByteBuffer#position() position} - previous {@link ByteBuffer#position() position}. + */ + int write(ByteBuffer in); + + /** + * return the id of this data source + * @return std::string representing the id of this data source + */ + String id(); + + /** + * Returns the output position indicator. + * + * @return number of bytes written so far. + */ + long tellp(); + + @Override + String toString(); +} diff --git a/java_jni/org/jau/io/ByteOutStream_File.java b/java_jni/org/jau/io/ByteOutStream_File.java new file mode 100644 index 0000000..53ca9d7 --- /dev/null +++ b/java_jni/org/jau/io/ByteOutStream_File.java @@ -0,0 +1,188 @@ +/** + * Author: Sven Gothel <[email protected]> + * 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; + +import org.jau.fs.FMode; + +/** + * File based byte output stream, including named file descriptor. + * + * Implementation mimics std::ifstream via OS level file descriptor (FD) operations, + * giving more flexibility, allowing reusing existing FD and enabling openat() operations. + * + * Instance uses the native C++ object `jau::io::ByteOutStream_File`. + */ +public final class ByteOutStream_File implements ByteOutStream { + private volatile long nativeInstance; + /* pp */ long getNativeInstance() { return nativeInstance; } + + /** + * Construct a stream based byte output stream from filesystem path, + * either an existing or new file. + * + * In case the file already exists, the underlying file offset is positioned at the end of the file. + * + * In case the given path is a local file URI starting with `file://`, see jau::io::uri::is_local_file_protocol(), + * the leading `file://` is cut off and the remainder being used. + * + * @param path the path to the file, maybe a local file URI + * @param mode file protection mode for a new file, otherwise ignored, may use {@link FMode#def_file}. + */ + public ByteOutStream_File(final String path, final FMode mode) { + try { + nativeInstance = ctorImpl1(path, mode.mask); + } catch (final Throwable t) { + System.err.println("ByteOutStream_File.ctor: native ctor failed: "+t.getMessage()); + throw t; + } + } + private static native long ctorImpl1(final String path, int mode); + + /** + * Construct a stream based byte output stream from filesystem path and parent directory file descriptor, + * either an existing or new file. + * + * In case the file already exists, the underlying file offset is positioned at the end of the file. + * + * In case the given path is a local file URI starting with `file://`, see jau::io::uri::is_local_file_protocol(), + * the leading `file://` is cut off and the remainder being used. + * + * @param dirfd parent directory file descriptor + * @param path the path to the file, maybe a local file URI + * @param mode file protection mode for a new file, otherwise ignored, may use {@link FMode#def_file}. + */ + public ByteOutStream_File(final int dirfd, final String path, final FMode mode) { + try { + nativeInstance = ctorImpl2(dirfd, path, mode.mask); + } catch (final Throwable t) { + System.err.println("ByteOutStream_File.ctor: native ctor failed: "+t.getMessage()); + throw t; + } + } + private static native long ctorImpl2(final int dirfd, final String path, int mode); + + /** + * Construct a stream based byte output stream by duplicating given file descriptor + * + * In case the given path is a local file URI starting with `file://`, see jau::io::uri::is_local_file_protocol(), + * the leading `file://` is cut off and the remainder being used. + * + * @param fd file descriptor to duplicate leaving the given `fd` untouched + */ + public ByteOutStream_File(final int fd) { + try { + nativeInstance = ctorImpl3(fd); + } catch (final Throwable t) { + System.err.println("ByteOutStream_File.ctor: native ctor failed: "+t.getMessage()); + throw t; + } + } + private static native long ctorImpl3(final int fd); + + @Override + public native void closeStream(); + + @Override + public void close() { + final long handle; + synchronized( this ) { + handle = nativeInstance; + nativeInstance = 0; + } + if( 0 != handle ) { + dtorImpl(handle); + } + } + private static native void dtorImpl(final long nativeInstance); + + @Override + public void finalize() { + close(); + } + + @Override + public native boolean is_open(); + + @Override + public void clear(final IOState state) { + clearImpl( state.mask ); + } + private native void clearImpl(int s); + + /** + * Returns the file descriptor if is_open(), otherwise -1 for no file descriptor. + * + * @see is_open() + */ + public native int fd(); + + @Override + public IOState rdState() { + return new IOState( rdStateImpl() ); + } + private native int rdStateImpl(); + + @Override + public void setState(final IOState state) { + setStateImpl( state.mask ); + } + private native void setStateImpl(int s); + + @Override + public native boolean good(); + + @Override + public native boolean eof(); + + @Override + public native boolean fail(); + + @Override + public native boolean bad(); + + @Override + public native int write(final byte[] in, final int offset, final int length); + + @Override + public int write(final ByteBuffer in) { + if( !Buffers.isDirect(in) ) { + throw new IllegalArgumentException("in buffer not direct"); + } + final int res = write2Impl(in, (int)Buffers.getDirectBufferByteOffset(in), (int)Buffers.getDirectBufferByteLimit(in)); + in.position(in.position() + res); + return res; + } + private native int write2Impl(Object in, int in_offset, int in_limit); + + @Override + public native String id(); + + @Override + public native long tellp(); + + @Override + public native String toString(); +} diff --git a/java_jni/org/jau/io/IOStateFunc.java b/java_jni/org/jau/io/IOStateFunc.java new file mode 100644 index 0000000..02fa759 --- /dev/null +++ b/java_jni/org/jau/io/IOStateFunc.java @@ -0,0 +1,59 @@ +/** + * Author: Sven Gothel <[email protected]> + * 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; + +/** + * Supporting std::basic_ios's iostate functionality for all ByteInStream implementations. + */ +public interface IOStateFunc { + + /** 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(); +} |