aboutsummaryrefslogtreecommitdiffstats
path: root/java_jni/jni/jau/ByteInStream_File.cxx
blob: eb4421fd14588142522fccd74173a7c797b4cb35 (plain)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * 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.
 */

#include "jau/byte_stream.hpp"

#include <jau/debug.hpp>

#include "jau/jni/helper_jni.hpp"

// #include "org_jau_io_IOState.h"
#include "org_jau_io_ByteInStream_File.h"

//
// IOState
//

jstring Java_org_jau_io_IOState_to_1string(JNIEnv *env, jclass cls, jint mask) {
    (void)cls;
    try {
        const std::string s = jau::io::to_string(static_cast<jau::io::iostate>(mask));
        return jau::jni::from_string_to_jstring(env, s);
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return nullptr;
}

//
// ByteInStream_File
//

jlong Java_org_jau_io_ByteInStream_1File_ctorImpl1(JNIEnv *env, jclass cls, jstring jpath) {
    try {
        (void)cls;
        // new instance
        const std::string _path = jau::jni::from_jstring_to_string(env, jpath);
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref( new jau::io::ByteInStream_File( _path ) );
        return ref.release_to_jlong();
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return (jlong) (intptr_t)nullptr;
}

jlong Java_org_jau_io_ByteInStream_1File_ctorImpl2(JNIEnv *env, jclass cls, jint dirfd, jstring jpath) {
    try {
        (void)cls;
        // new instance
        const std::string path = jau::jni::from_jstring_to_string(env, jpath);
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref( new jau::io::ByteInStream_File(dirfd, path) );
        return ref.release_to_jlong();
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return (jlong) (intptr_t)nullptr;
}

jlong Java_org_jau_io_ByteInStream_1File_ctorImpl3(JNIEnv *env, jclass cls, jint fd) {
    try {
        (void)cls;
        // new instance
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref( new jau::io::ByteInStream_File(fd) );
        return ref.release_to_jlong();
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return (jlong) (intptr_t)nullptr;
}

void Java_org_jau_io_ByteInStream_1File_closeStream(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        ref->close();
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
}

void Java_org_jau_io_ByteInStream_1File_dtorImpl(JNIEnv *env, jclass clazz, jlong nativeInstance) {
    (void)clazz;
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done
        if( nullptr != sref.pointer() ) {
            std::shared_ptr<jau::io::ByteInStream_File>* sref_ptr = jau::jni::castInstance<jau::io::ByteInStream_File>(nativeInstance);
            delete sref_ptr;
        }
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
}

jboolean Java_org_jau_io_ByteInStream_1File_is_1open(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        return ref->is_open() ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return JNI_FALSE;
}

void Java_org_jau_io_ByteInStream_1File_clearImpl(JNIEnv *env, jobject obj, jint mask) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        ref->clear( static_cast<jau::io::iostate>(mask) );
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
}

jint Java_org_jau_io_ByteInStream_1File_fd(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        return ref->fd();
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return -1;
}

jint Java_org_jau_io_ByteInStream_1File_rdStateImpl(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        return static_cast<jint>( ref->rdstate() );
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return static_cast<jint>(jau::io::iostate::failbit);
}

void Java_org_jau_io_ByteInStream_1File_setStateImpl(JNIEnv *env, jobject obj, jint mask) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        ref->setstate( static_cast<jau::io::iostate>(mask) );
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
}

jboolean Java_org_jau_io_ByteInStream_1File_good(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        return ref->good() ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return JNI_FALSE;
}

jboolean Java_org_jau_io_ByteInStream_1File_eof(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        return ref->eof() ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return JNI_TRUE;
}

jboolean Java_org_jau_io_ByteInStream_1File_fail(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        return ref->fail() ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return JNI_TRUE;
}

jboolean Java_org_jau_io_ByteInStream_1File_bad(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        return ref->bad() ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return JNI_FALSE;
}

jboolean Java_org_jau_io_ByteInStream_1File_available(JNIEnv *env, jobject obj, jlong n) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        return ref->available((size_t)n) ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return JNI_FALSE;
}

jint Java_org_jau_io_ByteInStream_1File_read(JNIEnv *env, jobject obj, jbyteArray jout, jint joffset, jint jlength) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done

        if( nullptr == jout ) {
            throw jau::IllegalArgumentException("out buffer null", E_FILE_LINE);
        }
        const size_t in_size = env->GetArrayLength(jout);
        if( (size_t)joffset + (size_t)jlength > in_size ) {
            throw jau::IllegalArgumentException("output byte size "+std::to_string(in_size)+" < "+std::to_string(joffset)+" + "+std::to_string(jlength), E_FILE_LINE);
        }
        jau::jni::JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
        uint8_t * out_ptr = criticalArray.get(jout, criticalArray.Mode::UPDATE_AND_RELEASE);
        if( NULL == out_ptr ) {
            throw jau::InternalError("GetPrimitiveArrayCritical(address byte array) is null", E_FILE_LINE);
        }
        return (jint) ref->read(out_ptr + joffset, jlength);
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return 0;
}

jint Java_org_jau_io_ByteInStream_1File_read2Impl(JNIEnv *env, jobject obj, jobject jout, jint out_offset) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done

        if( nullptr == jout ) {
            throw jau::IllegalArgumentException("out buffer null", E_FILE_LINE);
        }
        const jlong out_cap = env->GetDirectBufferCapacity(jout);
        uint8_t * out_ptr = static_cast<uint8_t *>( env->GetDirectBufferAddress(jout) );
        if( 0 > out_cap || nullptr == out_ptr ) {
            throw jau::IllegalArgumentException("out buffer access failure", E_FILE_LINE);
        }
        return (jint) ref->read(out_ptr + out_offset, out_cap - out_offset);
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return 0;
}

jint Java_org_jau_io_ByteInStream_1File_peek(JNIEnv *env, jobject obj, jbyteArray jout, jint joffset, jint jlength, jlong jpeek_offset) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done

        if( nullptr == jout ) {
            throw jau::IllegalArgumentException("out buffer null", E_FILE_LINE);
        }
        const size_t in_size = env->GetArrayLength(jout);
        if( (size_t)joffset + (size_t)jlength > in_size ) {
            throw jau::IllegalArgumentException("output byte size "+std::to_string(in_size)+" < "+std::to_string(joffset)+" + "+std::to_string(jlength), E_FILE_LINE);
        }
        jau::jni::JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
        uint8_t * out_ptr = criticalArray.get(jout, criticalArray.Mode::UPDATE_AND_RELEASE);
        if( NULL == out_ptr ) {
            throw jau::InternalError("GetPrimitiveArrayCritical(address byte array) is null", E_FILE_LINE);
        }
        const size_t res = ref->peek(out_ptr + joffset, jlength, jpeek_offset);
        return (jint)res;
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return 0;
}

jstring Java_org_jau_io_ByteInStream_1File_id(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        return jau::jni::from_string_to_jstring(env, ref->id());
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return nullptr;
}

jlong Java_org_jau_io_ByteInStream_1File_discard_1next(JNIEnv *env, jobject obj, jlong n) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done

        const size_t res = ref->discard(n);
        return (jlong)res;
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return 0;
}

jlong Java_org_jau_io_ByteInStream_1File_tellg(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        return static_cast<jlong>( ref->tellg() );
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return 0;
}

jboolean Java_org_jau_io_ByteInStream_1File_has_1content_1size(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        return ref->has_content_size() ? JNI_TRUE : JNI_FALSE;
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return JNI_FALSE;
}

jlong Java_org_jau_io_ByteInStream_1File_content_1size(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj); // hold until done
        return static_cast<jlong>( ref->content_size() );
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return 0;
}

jstring Java_org_jau_io_ByteInStream_1File_toString(JNIEnv *env, jobject obj) {
    try {
        jau::jni::shared_ptr_ref<jau::io::ByteInStream_File> ref(env, obj, false /* throw_on_nullptr */); // hold until done
        std::string str = ref.is_null() ? "null" : ref->to_string();
        return jau::jni::from_string_to_jstring(env, str);
    } catch(...) {
        rethrow_and_raise_java_exception_jau(env);
    }
    return nullptr;
}