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
|
/**
* 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;
/**
* File based byte input 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.
*
* If source path denotes a named file descriptor, i.e. jau::fs::file_stats::is_fd() returns true,
* has_content_size() returns false and available() returns true as long the stream is open and EOS hasn't occurred.
*
* Instance uses the native C++ object `jau::io::ByteInStream_File`.
*/
public final class ByteInStream_File implements ByteInStream {
private volatile long nativeInstance;
/* pp */ long getNativeInstance() { return nativeInstance; }
/**
* Construct a Stream-Based byte input stream from filesystem path
*
* In case the given path is a local file URI starting with `file://`, see {@link org.jau.io.UriTk#is_local_file_protocol(String)},
* the leading `file://` is cut off and the remainder being used.
*
* @param path the path to the file, maybe a local file URI
*/
public ByteInStream_File(final String path) {
try {
nativeInstance = ctorImpl1(path);
} catch (final Throwable t) {
System.err.println("ByteInStream_File.ctor: native ctor failed: "+t.getMessage());
throw t;
}
}
private static native long ctorImpl1(final String path);
/**
* Construct a stream based byte input stream from filesystem path and parent directory 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 dirfd parent directory file descriptor
* @param path the path to the file, maybe a local file URI
*/
public ByteInStream_File(final int dirfd, final String path) {
try {
nativeInstance = ctorImpl2(dirfd, path);
} catch (final Throwable t) {
System.err.println("ByteInStream_File.ctor: native ctor failed: "+t.getMessage());
throw t;
}
}
private static native long ctorImpl2(final int dirfd, final String path);
/**
* Construct a stream based byte input 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 ByteInStream_File(final int fd) {
try {
nativeInstance = ctorImpl3(fd);
} catch (final Throwable t) {
System.err.println("ByteInStream_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 boolean end_of_data() { return !good(); }
@Override
public native boolean available(final long n);
@Override
public native int read(final byte[] out, final int offset, final int length);
@Override
public int read(final ByteBuffer out) {
if( !Buffers.isDirect(out) ) {
throw new IllegalArgumentException("out buffer not direct");
}
final int res = read2Impl(out, (int)Buffers.getDirectBufferByteOffset(out));
out.limit(out.position() + res);
return res;
}
private native int read2Impl(Object out, int out_offset);
@Override
public native int peek(byte[] out, final int offset, final int length, final long peek_offset);
@Override
public native String id();
@Override
public native long discard_next(long N);
@Override
public native long tellg();
@Override
public native boolean has_content_size();
@Override
public native long content_size();
@Override
public native String toString();
}
|