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