Path: blob/master/src/java.base/share/native/libjava/io_util.h
41149 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include "jni.h"26#include "jni_util.h"2728extern jfieldID IO_fd_fdID;29extern jfieldID IO_handle_fdID;30extern jfieldID IO_append_fdID;3132#ifdef _ALLBSD_SOURCE33#include <fcntl.h>34#ifndef O_SYNC35#define O_SYNC O_FSYNC36#endif37#ifndef O_DSYNC38#define O_DSYNC O_FSYNC39#endif40#elif !defined(O_DSYNC) || !defined(O_SYNC)41#define O_SYNC (0x0800)42#define O_DSYNC (0x2000)43#endif4445/*46* IO helper functions47*/4849jint readSingle(JNIEnv *env, jobject this, jfieldID fid);50jint readBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off,51jint len, jfieldID fid);52void writeSingle(JNIEnv *env, jobject this, jint byte, jboolean append, jfieldID fid);53void writeBytes(JNIEnv *env, jobject this, jbyteArray bytes, jint off,54jint len, jboolean append, jfieldID fid);55void fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags);56void throwFileNotFoundException(JNIEnv *env, jstring path);5758/*59* Macros for managing platform strings. The typical usage pattern is:60*61* WITH_PLATFORM_STRING(env, string, var) {62* doSomethingWith(var);63* } END_PLATFORM_STRING(env, var);64*65* where env is the prevailing JNIEnv,66* string is a JNI reference to a java.lang.String object, and67* var is the char * variable that will point to the string,68* after being converted into the platform encoding.69*70* The related macro WITH_FIELD_PLATFORM_STRING first extracts the string from71* a given field of a given object:72*73* WITH_FIELD_PLATFORM_STRING(env, object, id, var) {74* doSomethingWith(var);75* } END_PLATFORM_STRING(env, var);76*77* where env is the prevailing JNIEnv,78* object is a jobject,79* id is the field ID of the String field to be extracted, and80* var is the char * variable that will point to the string.81*82* Uses of these macros may be nested as long as each WITH_.._STRING macro83* declares a unique variable.84*/8586#define WITH_PLATFORM_STRING(env, strexp, var) \87if (1) { \88const char *var; \89jstring _##var##str = (strexp); \90if (_##var##str == NULL) { \91JNU_ThrowNullPointerException((env), NULL); \92goto _##var##end; \93} \94var = JNU_GetStringPlatformChars((env), _##var##str, NULL); \95if (var == NULL) goto _##var##end;9697#define WITH_FIELD_PLATFORM_STRING(env, object, id, var) \98WITH_PLATFORM_STRING(env, \99((object == NULL) \100? NULL \101: (*(env))->GetObjectField((env), (object), (id))), \102var)103104#define END_PLATFORM_STRING(env, var) \105JNU_ReleaseStringPlatformChars(env, _##var##str, var); \106_##var##end: ; \107} else ((void)NULL)108109110/* Macros for transforming Java Strings into native Unicode strings.111* Works analogously to WITH_PLATFORM_STRING.112*/113114#define WITH_UNICODE_STRING(env, strexp, var) \115if (1) { \116const jchar *var; \117jstring _##var##str = (strexp); \118if (_##var##str == NULL) { \119JNU_ThrowNullPointerException((env), NULL); \120goto _##var##end; \121} \122var = (*(env))->GetStringChars((env), _##var##str, NULL); \123if (var == NULL) goto _##var##end;124125#define END_UNICODE_STRING(env, var) \126(*(env))->ReleaseStringChars(env, _##var##str, var); \127_##var##end: ; \128} else ((void)NULL)129130131