⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gnu_java_nio_channels_filechannelimpl.c

📁 gcc的组建
💻 C
📖 第 1 页 / 共 2 页
字号:
/* gnu_java_nio_channels_FileChannelImpl.c -   Copyright (C) 2003, 2004, 2005  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. *//* do not move; needed here because of some macro definitions */#include <config.h>#include <stdlib.h>#include <errno.h>#include <jni.h>#include <jcl.h>#include "target_native.h"#ifndef WITHOUT_FILESYSTEM#include "target_native_file.h"#endif#include "target_native_math_int.h"#include "gnu_java_nio_channels_FileChannelImpl.h"#ifdef HAVE_FCNTL_H#include <fcntl.h>#endif /* HAVE_FCNTL_H */#ifdef HAVE_SYS_MMAN_H#include <sys/mman.h>#endif /* HAVE_SYS_MMAN_H *//* These values must be kept in sync with FileChannelImpl.java.  */#define FILECHANNELIMPL_READ   1#define FILECHANNELIMPL_WRITE  2#define FILECHANNELIMPL_APPEND 4/* These values must be kept in sync with FileChannelImpl.java.  *//* #define FILECHANNELIMPL_FILESEEK_SET          0 *//* #define FILECHANNELIMPL_FILESEEK_CUR          1 *//* #define FILECHANNELIMPL_FILESEEK_END          2 */#define FILECHANNELIMPL_FILEOPEN_FLAG_READ    1#define FILECHANNELIMPL_FILEOPEN_FLAG_WRITE   2#define FILECHANNELIMPL_FILEOPEN_FLAG_APPEND  4#define FILECHANNELIMPL_FILEOPEN_FLAG_EXCL    8#define FILECHANNELIMPL_FILEOPEN_FLAG_SYNC   16#define FILECHANNELIMPL_FILEOPEN_FLAG_DSYNC  32#define IO_EXCEPTION "java/io/IOException"/* FIXME: This can't be right.  Need converter macros. */#define CONVERT_JLONG_TO_INT(x) TARGET_NATIVE_MATH_INT_INT64_TO_INT32(x)#define CONVERT_INT_TO_JLONG(x) TARGET_NATIVE_MATH_INT_INT32_TO_INT64(x)/* FIXME: This can't be right.  Need converter macros. */#define CONVERT_JLONG_TO_OFF_T(x) TARGET_NATIVE_MATH_INT_INT64_TO_INT32(x)#define CONVERT_OFF_T_TO_JLONG(x) TARGET_NATIVE_MATH_INT_INT32_TO_INT64(x)/* FIXME: This can't be right.  Need converter macros */#define CONVERT_JINT_TO_INT(x) ((int)(x & 0xFFFFFFFF))#define CONVERT_INT_TO_JINT(x) ((int)(x & 0xFFFFFFFF))/* FIXME: This can't be right.  Need converter macros. */#define CONVERT_SSIZE_T_TO_JINT(x) ((jint)(x & 0xFFFFFFFF))#define CONVERT_JINT_TO_SSIZE_T(x) (x)/* Align a value up or down to a multiple of the pagesize. */#define ALIGN_DOWN(p,s) ((p) - ((p) % (s)))#define ALIGN_UP(p,s) ((p) + ((s) - ((p) % (s))))/* cached fieldID of gnu.java.nio.channels.FileChannelImpl.fd */static jfieldID native_fd_fieldID;static jintget_native_fd (JNIEnv * env, jobject obj){  return (*env)->GetIntField (env, obj, native_fd_fieldID);}/* * Library initialization routine.  Called as part of java.io.FileDescriptor * static initialization. */JNIEXPORT void JNICALLJava_gnu_java_nio_channels_FileChannelImpl_init (JNIEnv * env,						jclass clazz						__attribute__ ((__unused__))){  jclass clazz_fc;  jfieldID field;  /* Initialize native_fd_fieldID so we only compute it once! */  clazz_fc = (*env)->FindClass (env, "gnu/java/nio/channels/FileChannelImpl");  if (!clazz_fc)    {      JCL_ThrowException (env, IO_EXCEPTION, "Internal error");      return;    }  field = (*env)->GetFieldID (env, clazz_fc, "fd", "I");  if (!field)    {      JCL_ThrowException (env, IO_EXCEPTION, "Internal error");      return;    }  native_fd_fieldID = field;}/* * Open the specified file and return a native file descriptor */JNIEXPORT jint JNICALLJava_gnu_java_nio_channels_FileChannelImpl_open (JNIEnv * env,						 jobject obj						 __attribute__ ((__unused__)),						 jstring name, jint mode){  const char *filename;  int flags;  int permissions;  int native_fd;  int result;  filename = JCL_jstring_to_cstring (env, name);  if (filename == NULL)    return (-1);		/* Exception will already have been thrown */  /* get file/permission flags for open() */  if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_READ)      && (mode & FILECHANNELIMPL_FILEOPEN_FLAG_WRITE))    {      /* read/write */      flags =	TARGET_NATIVE_FILE_FILEFLAG_CREATE |	TARGET_NATIVE_FILE_FILEFLAG_READWRITE;      permissions = TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL;    }  else if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_READ))    {      /* read */      flags = TARGET_NATIVE_FILE_FILEFLAG_READ;      permissions = TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL;    }  else    {      /* write */      flags =	TARGET_NATIVE_FILE_FILEFLAG_CREATE |	TARGET_NATIVE_FILE_FILEFLAG_WRITE;      if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_APPEND))	{	  flags |= TARGET_NATIVE_FILE_FILEFLAG_APPEND;	}      else	{	  flags |= TARGET_NATIVE_FILE_FILEFLAG_TRUNCATE;	}      permissions = TARGET_NATIVE_FILE_FILEPERMISSION_NORMAL;    }  if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_SYNC))    {      flags |= TARGET_NATIVE_FILE_FILEFLAG_SYNC;    }  if ((mode & FILECHANNELIMPL_FILEOPEN_FLAG_DSYNC))    {      flags |= TARGET_NATIVE_FILE_FILEFLAG_DSYNC;    }#ifdef O_BINARY  flags |= TARGET_NATIVE_FILE_FILEFLAG_BINARY;#endif  TARGET_NATIVE_FILE_OPEN (filename, native_fd, flags, permissions, result);  if (result != TARGET_NATIVE_OK)    {      char message[256]; /* Fixed size we don't need to malloc. */      char *error_string = TARGET_NATIVE_LAST_ERROR_STRING ();      snprintf(message, 256, "%s: %s", error_string, filename);      /* We are only allowed to throw FileNotFoundException.  */      JCL_ThrowException (env,			  "java/io/FileNotFoundException",			  message);      JCL_free_cstring (env, name, filename);      return TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1;    }  JCL_free_cstring (env, name, filename);  return native_fd;}/* * Closes the specified file descriptor and return status code. * Exception on error */JNIEXPORT void JNICALLJava_gnu_java_nio_channels_FileChannelImpl_implCloseChannel (JNIEnv * env,							     jobject obj){  int native_fd;  int result;  native_fd = get_native_fd (env, obj);  do    {      TARGET_NATIVE_FILE_CLOSE (native_fd, result);      if (result != TARGET_NATIVE_OK	  && (TARGET_NATIVE_LAST_ERROR ()	      != TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))	{	  JCL_ThrowException (env, IO_EXCEPTION,			      TARGET_NATIVE_LAST_ERROR_STRING ());	  return;	}    }  while (result != TARGET_NATIVE_OK);}/* * Return number of bytes that can be read from the file w/o blocking. * Exception on error */JNIEXPORT jint JNICALLJava_gnu_java_nio_channels_FileChannelImpl_available (JNIEnv * env,						      jobject obj){  int native_fd;  jlong bytes_available;  int result;  native_fd = get_native_fd (env, obj);  do    {      TARGET_NATIVE_FILE_AVAILABLE (native_fd, bytes_available, result);      if (result != TARGET_NATIVE_OK	  && (TARGET_NATIVE_LAST_ERROR ()	      != TARGET_NATIVE_ERROR_INTERRUPT_FUNCTION_CALL))	{	  JCL_ThrowException (env, IO_EXCEPTION,			      TARGET_NATIVE_LAST_ERROR_STRING ());	  return 0;	}    }  while (result != TARGET_NATIVE_OK);  /* FIXME NYI ??? why only jint and not jlong? */  return TARGET_NATIVE_MATH_INT_INT64_TO_INT32 (bytes_available);}JNIEXPORT jlong JNICALLJava_gnu_java_nio_channels_FileChannelImpl_size (JNIEnv * env, jobject obj){  int native_fd;  jlong file_size;  int result;  native_fd = get_native_fd (env, obj);  TARGET_NATIVE_FILE_SIZE (native_fd, file_size, result);  if (result != TARGET_NATIVE_OK)    {      JCL_ThrowException (env, IO_EXCEPTION,			  TARGET_NATIVE_LAST_ERROR_STRING ());      return TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1;    }  return file_size;}/* * Return the current position of the file pointer * Exception on error */JNIEXPORT jlong JNICALLJava_gnu_java_nio_channels_FileChannelImpl_implPosition (JNIEnv * env,							 jobject obj){  int native_fd;  jlong current_offset;  int result;  native_fd = get_native_fd (env, obj);  TARGET_NATIVE_FILE_TELL (native_fd, current_offset, result);  if (result != TARGET_NATIVE_OK)    {      JCL_ThrowException (env, IO_EXCEPTION,			  TARGET_NATIVE_LAST_ERROR_STRING ());      return TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1;    }  return current_offset;}/* * Wrapper around lseek call.  Return new file position * Exception on error */JNIEXPORT void JNICALLJava_gnu_java_nio_channels_FileChannelImpl_seek (JNIEnv * env, jobject obj,						 jlong offset){  int native_fd;  jlong new_offset;  int result;  native_fd = get_native_fd (env, obj);#if 0  /* Should there be such an exception? All native layer macros should     be accepting 64bit-values if needed. It some target is not able     to handle such values it should simply operate with 32bit-values     and convert 64bit-values appriopated. In this case I assume     problems should not occurre: if some specific target is not able     to handle 64bit-values the system is limited to 32bit at all, thus     the application can not do a seek() or something else beyond the     32bit limit. It this true?   */  /* FIXME: What do we do if offset > the max value of off_t on this 32bit   * system?  How do we detect that and what do we do? */  if (CONVERT_OFF_T_TO_JLONG (native_offset) != offset)    {      JCL_ThrowException (env, IO_EXCEPTION,			  "Cannot represent position correctly on this system");    }#endif /* 0 */  result = TARGET_NATIVE_ERROR;  new_offset = TARGET_NATIVE_MATH_INT_INT64_CONST_MINUS_1;  TARGET_NATIVE_FILE_SEEK_BEGIN (native_fd, offset, new_offset, result);  if (result != TARGET_NATIVE_OK)    {      JCL_ThrowException (env, IO_EXCEPTION,			  TARGET_NATIVE_LAST_ERROR_STRING ());    }}/* * Set the length of the file * Exception on error */JNIEXPORT void JNICALLJava_gnu_java_nio_channels_FileChannelImpl_implTruncate (JNIEnv * env,							 jobject obj,							 jlong len){  int native_fd;  jlong file_size;  int bytes_written;  jlong save_offset, new_offset;  char data;  int result;  native_fd = get_native_fd (env, obj);#if 0  /* Should there be such an exception? All native layer macros should     be accepting 64bit-values if needed. It some target is not able     to handle such values it should simply operate with 32bit-values     and convert 64bit-values appriopated. In this case I assume     problems should not occurre: if some specific target is not able     to handle 64bit-values the system is limited to 32bit at all, thus     the application can not do a seek() or something else beyond the     32bit limit. It this true?   */  /* FIXME: What do we do if len > the max value of off_t on this 32bit   * system?  How do we detect that and what do we do? */  if (CONVERT_OFF_T_TO_JLONG (native_len) != len)    {      JCL_ThrowException (env, IO_EXCEPTION,			  "Cannot represent position correctly on this system");      return;    }#endif /* 0 */  /* get file size */  TARGET_NATIVE_FILE_SIZE (native_fd, file_size, result);  if (result != TARGET_NATIVE_OK)    {      JCL_ThrowException (env, IO_EXCEPTION,			  TARGET_NATIVE_LAST_ERROR_STRING ());      return;    }  /* Save off current position */  TARGET_NATIVE_FILE_TELL (native_fd, save_offset, result);  if (result != TARGET_NATIVE_OK)    {      JCL_ThrowException (env, IO_EXCEPTION,			  TARGET_NATIVE_LAST_ERROR_STRING ());      return;    }  if (TARGET_NATIVE_MATH_INT_INT64_LT (file_size, len))    {      /* File is too short -- seek to one byte short of where we want,       * then write a byte */      /* move to position n-1 */      TARGET_NATIVE_FILE_SEEK_BEGIN (native_fd,				     TARGET_NATIVE_MATH_INT_INT64_SUB (len,								       1),				     new_offset, result);      if (result != TARGET_NATIVE_OK)	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -