jaulib v1.3.0
Jau Support Library (C++, Java, ..)
ByteInStream_File.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 * File based byte input stream, including named file descriptor.
30 *
31 * Implementation mimics std::ifstream via OS level file descriptor (FD) operations,
32 * giving more flexibility, allowing reusing existing FD and enabling openat() operations.
33 *
34 * If source path denotes a named file descriptor, i.e. jau::fs::file_stats::is_fd() returns true,
35 * has_content_size() returns false and available() returns true as long the stream is open and EOS hasn't occurred.
36 *
37 * Instance uses the native C++ object `jau::io::ByteInStream_File`.
38 */
39public final class ByteInStream_File implements ByteInStream {
40 private volatile long nativeInstance;
41 /* pp */ long getNativeInstance() { return nativeInstance; }
42
43 /**
44 * Construct a Stream-Based byte input stream from filesystem path
45 *
46 * In case the given path is a local file URI starting with `file://`, see {@link org.jau.io.UriTk#is_local_file_protocol(String)},
47 * the leading `file://` is cut off and the remainder being used.
48 *
49 * @param path the path to the file, maybe a local file URI
50 */
51 public ByteInStream_File(final String path) {
52 try {
53 nativeInstance = ctorImpl1(path);
54 } catch (final Throwable t) {
55 System.err.println("ByteInStream_File.ctor: native ctor failed: "+t.getMessage());
56 throw t;
57 }
58 }
59 private static native long ctorImpl1(final String path);
60
61 /**
62 * Construct a stream based byte input stream from filesystem path and parent directory file descriptor
63 *
64 * In case the given path is a local file URI starting with `file://`, see jau::io::uri::is_local_file_protocol(),
65 * the leading `file://` is cut off and the remainder being used.
66 *
67 * @param dirfd parent directory file descriptor
68 * @param path the path to the file, maybe a local file URI
69 */
70 public ByteInStream_File(final int dirfd, final String path) {
71 try {
72 nativeInstance = ctorImpl2(dirfd, path);
73 } catch (final Throwable t) {
74 System.err.println("ByteInStream_File.ctor: native ctor failed: "+t.getMessage());
75 throw t;
76 }
77 }
78 private static native long ctorImpl2(final int dirfd, final String path);
79
80 /**
81 * Construct a stream based byte input stream by duplicating given file descriptor
82 *
83 * In case the given path is a local file URI starting with `file://`, see jau::io::uri::is_local_file_protocol(),
84 * the leading `file://` is cut off and the remainder being used.
85 *
86 * @param fd file descriptor to duplicate leaving the given `fd` untouched
87 */
88 public ByteInStream_File(final int fd) {
89 try {
90 nativeInstance = ctorImpl3(fd);
91 } catch (final Throwable t) {
92 System.err.println("ByteInStream_File.ctor: native ctor failed: "+t.getMessage());
93 throw t;
94 }
95 }
96 private static native long ctorImpl3(final int fd);
97
98 @Override
99 public native void closeStream();
100
101 @Override
102 public void close() {
103 final long handle;
104 synchronized( this ) {
105 handle = nativeInstance;
106 nativeInstance = 0;
107 }
108 if( 0 != handle ) {
109 dtorImpl(handle);
110 }
111 }
112 private static native void dtorImpl(final long nativeInstance);
113
114 @Override
115 public void finalize() {
116 close();
117 }
118
119 @Override
120 public native boolean is_open();
121
122 @Override
123 public void clear(final IOState state) {
124 clearImpl( state.mask );
125 }
126 private native void clearImpl(int s);
127
128 /**
129 * Returns the file descriptor if is_open(), otherwise -1 for no file descriptor.
130 *
131 * @see is_open()
132 */
133 public native int fd();
134
135 @Override
136 public IOState rdState() {
137 return new IOState( rdStateImpl() );
138 }
139 private native int rdStateImpl();
140
141 @Override
142 public void setState(final IOState state) {
143 setStateImpl( state.mask );
144 }
145 private native void setStateImpl(int s);
146
147 @Override
148 public native boolean good();
149
150 @Override
151 public native boolean eof();
152
153 @Override
154 public native boolean fail();
155
156 @Override
157 public native boolean bad();
158
159 @Override
160 public native boolean timeout();
161
162 @Override
163 public native boolean available(final long n);
164
165 @Override
166 public native int read(final byte[] out, final int offset, final int length);
167
168 @Override
169 public int read(final ByteBuffer out) {
170 if( !Buffers.isDirect(out) ) {
171 throw new IllegalArgumentException("out buffer not direct");
172 }
173 final int res = read2Impl(out, (int)Buffers.getDirectBufferByteOffset(out));
174 out.limit(out.position() + res);
175 return res;
176 }
177 private native int read2Impl(Object out, int out_offset);
178
179 @Override
180 public native int peek(byte[] out, final int offset, final int length, final long peek_offset);
181
182 @Override
183 public native String id();
184
185 @Override
186 public native long discard_next(long N);
187
188 @Override
189 public native long tellg();
190
191 @Override
192 public native boolean has_content_size();
193
194 @Override
195 public native long content_size();
196
197 @Override
198 public native String toString();
199}
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:43
static boolean isDirect(final Object buf)
Helper routine to tell whether a buffer is direct or not.
Definition: Buffers.java:78
static long getDirectBufferByteOffset(final Object buf)
Helper routine to get the Buffer byte offset by taking into account the Buffer position and the under...
Definition: Buffers.java:96
File based byte input stream, including named file descriptor.
native int read(final byte[] out, final int offset, final int length)
Read from the source.
native boolean available(final long n)
Return whether n bytes are available in the input stream, if has_content_size() or using an asynchron...
native String id()
return the id of this data source
native boolean good()
Checks if no error nor eof() has occurred i.e.
void setState(final IOState state)
Sets state flags, by keeping its previous bits.
native 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.
IOState rdState()
Returns the current state flags.
native boolean bad()
Checks if a non-recoverable error has occurred.
void clear(final IOState state)
Clears state flags by assignment to the given value.
int read(final ByteBuffer out)
Read from the source.
native boolean timeout()
Checks if a timeout (non-recoverable) has occurred.
ByteInStream_File(final int dirfd, final String path)
Construct a stream based byte input stream from filesystem path and parent directory file descriptor.
void close()
Close the stream if supported by the underlying mechanism and dispose the native instance.
native boolean fail()
Checks if an error has occurred.
native long content_size()
Returns the content_size if known.
native int fd()
Returns the file descriptor if is_open(), otherwise -1 for no file descriptor.
native long discard_next(long N)
Discard the next N bytes of the data.
native boolean eof()
Checks if end-of-file has been reached.
ByteInStream_File(final String path)
Construct a Stream-Based byte input stream from filesystem path.
native long tellg()
Returns the input position indicator, similar to std::basic_istream.
native boolean is_open()
Checks if the stream has an associated file.
native boolean has_content_size()
Returns true if implementation is aware of content_size(), otherwise false.
native void closeStream()
Close the stream if supported by the underlying mechanism.
ByteInStream_File(final int fd)
Construct a stream based byte input stream by duplicating given file descriptor.
Mimic std::ios_base::iostate for state functionality, see iostate_func.
Definition: IOState.java:33
Abstract byte input stream object.