You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by hi...@apache.org on 2007/10/11 11:02:59 UTC
svn commit: r583744 - in /harmony/enhanced/classlib/trunk/modules:
luni/src/main/native/luni/shared/nethelp.c
portlib/src/main/native/common/shared/iohelp.c
Author: hindessm
Date: Thu Oct 11 02:02:59 2007
New Revision: 583744
URL: http://svn.apache.org/viewvc?rev=583744&view=rev
Log:
Move functions used only in luni from the common static library (included
in all native code dlls) to the luni dll. I've tried to make this change
as close to a verbatim copy as possible - only changing JCL_CACHE_* to
HARMONY_CACHE_* - but there are several more refactoring steps to come.
Modified:
harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/nethelp.c
harmony/enhanced/classlib/trunk/modules/portlib/src/main/native/common/shared/iohelp.c
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/nethelp.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/nethelp.c?rev=583744&r1=583743&r2=583744&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/nethelp.c (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/nethelp.c Thu Oct 11 02:02:59 2007
@@ -21,6 +21,8 @@
#include "harmonyglob.h"
#include "exceptions.h"
+jfieldID getJavaIoFileDescriptorDescriptorFID (JNIEnv * env);
+
/**
* Set up JNI ID Caches.
*
@@ -1426,4 +1428,302 @@
(*env)->SetByteArrayRegion(env,addr_array,(jsize)0,(jsize)4,localAddr);
addrarray_field = HARMONY_CACHE_GET (env, FID_java_net_InetAddress_address);
(*env)->SetObjectField(env, localAddr_object, addrarray_field, addr_array);
+}
+
+/**
+ * This will write count bytes from buffer starting at offset
+ */
+void
+ioh_writebytesImpl (JNIEnv * env, jobject recv, jbyteArray buffer,
+ jint offset, jint count, IDATA descriptor)
+{
+ I_32 result = 0;
+ jbyte *buf;
+ PORT_ACCESS_FROM_ENV (env);
+ jsize len;
+ char *errorMessage = NULL;
+
+/* TODO: ARRAY PINNING */
+#define INTERNAL_MAX 512
+ jbyte internalBuffer[INTERNAL_MAX];
+
+ if (buffer == NULL)
+ {
+ throwNPException (env, "buffer is null");
+ return;
+ }
+
+ len = (*env)->GetArrayLength (env, buffer);
+
+ /**
+ * If offset is negative, or count is negative, or offset+count is greater
+ * than the length of the array b, then an IndexOutOfBoundsException is thrown.
+ * Must test offset > len, or len - offset < count to avoid int overflow caused
+ * by offset + count
+ */
+ if (offset < 0 || count < 0 || offset > len || (len - offset) < count)
+ {
+ throwIndexOutOfBoundsException (env);
+ return;
+ }
+
+ /* If len or count is zero, just return 0 */
+ if (len == 0 || count == 0)
+ return;
+
+ if (descriptor == -1)
+ {
+ throwJavaIoIOExceptionClosed (env);
+ return;
+ }
+ if (count > INTERNAL_MAX)
+ {
+ buf = jclmem_allocate_memory (env, count);
+ }
+ else
+ {
+ buf = internalBuffer;
+ }
+
+ if (buf == NULL)
+ {
+ throwNewOutOfMemoryError (env, "");
+ return;
+ }
+ ((*env)->GetByteArrayRegion (env, buffer, offset, count, buf));
+
+ result = hyfile_write (descriptor, buf, count);
+
+ /**
+ * if there is an error, find the error message before calling free in case
+ * hymem_free_memory changes the error code
+ */
+ if (result < 0)
+ errorMessage = ioLookupErrorString (env, result);
+
+ if (buf != internalBuffer)
+ {
+ jclmem_free_memory (env, buf);
+ }
+#undef INTERNAL_MAX
+
+ if (result < 0)
+ throwJavaIoIOException (env, errorMessage);
+}
+
+/**
+ * This will read a up to count bytes into buffer starting at offset
+ */
+jint
+ioh_readbytesImpl (JNIEnv * env, jobject recv, jbyteArray buffer, jint offset,
+ jint count, IDATA descriptor)
+{
+ I_32 result;
+ jsize len;
+ jbyte *buf;
+
+/* TODO: ARRAY PINNING */
+#define INTERNAL_MAX 2048
+ jbyte internalBuffer[INTERNAL_MAX];
+
+ PORT_ACCESS_FROM_ENV (env);
+
+ if (buffer == NULL)
+ {
+ throwNPException (env, "buffer is null");
+ return 0;
+ }
+
+ len = (*env)->GetArrayLength (env, buffer);
+ /**
+ * Throw IndexOutOfBoundsException according to spec.
+ * Must test offset > len, or len - offset < count to avoid
+ * int overflow caused by offset + count
+ */
+ if (offset < 0 || count < 0 || offset > len || (len - offset) < count)
+ {
+ throwIndexOutOfBoundsException (env);
+ return 0;
+ }
+ /* If len is 0, simply return 0 (even if it is closed) */
+ if (len == 0 || count == 0)
+ return 0;
+
+ if (descriptor == -1)
+ {
+ throwJavaIoIOExceptionClosed (env);
+ return 0;
+ }
+ if (len >= INTERNAL_MAX)
+ {
+ buf = jclmem_allocate_memory (env, len);
+ }
+ else
+ {
+ buf = internalBuffer;
+ }
+
+ if (buf == NULL)
+ {
+ throwNewOutOfMemoryError (env, "");
+ return 0;
+ }
+ /* Must FREE buffer before returning */
+
+ if (descriptor == 0)
+ {
+ /* hytty_get_chars() returns zero on EOF */
+ if ((result = hytty_get_chars (buf, count)) == 0)
+ result = -1;
+ }
+ else
+ {
+ result = hyfile_read (descriptor, buf, count);
+ }
+ if (result > 0)
+ (*env)->SetByteArrayRegion (env, buffer, offset, result, buf);
+
+ if (buf != internalBuffer)
+ {
+ jclmem_free_memory (env, buf);
+ }
+#undef INTERNAL_MAX
+
+ return result;
+}
+
+/**
+ * This will close a file descriptor
+ */
+void
+new_ioh_close (JNIEnv * env, jobject recv, jfieldID fdFID)
+{
+ jobject fd;
+ jfieldID descriptorFID;
+ IDATA descriptor;
+ PORT_ACCESS_FROM_ENV (env);
+
+ descriptorFID = getJavaIoFileDescriptorDescriptorFID (env);
+ if (NULL == descriptorFID)
+ {
+ return;
+ }
+
+ /* fetch the fd field from the object */
+ fd = (*env)->GetObjectField (env, recv, fdFID);
+
+ /* dereference the C pointer from the wrapper object */
+ descriptor = (IDATA) getJavaIoFileDescriptorContentsAsPointer (env, fd);
+
+ /* Check for closed file, in, out, and err */
+ if (descriptor >= -1 && descriptor <= 2)
+ {
+ return;
+ }
+
+ hyfile_close (descriptor);
+ setJavaIoFileDescriptorContentsAsPointer (env, fd, (void *) -1);
+ return;
+}
+
+/**
+ * This will retrieve the 'descriptor' field value from a java.io.FileDescriptor
+ */
+void *
+getJavaIoFileDescriptorContentsAsPointer (JNIEnv * env, jobject fd)
+{
+ jfieldID descriptorFID = getJavaIoFileDescriptorDescriptorFID (env);
+ if (NULL == descriptorFID)
+ {
+ return (void *) -1;
+ }
+ return (void *)(IDATA) ((*env)->GetLongField (env, fd, descriptorFID));
+}
+
+/**
+ * This will set the 'descriptor' field value in the java.io.FileDescriptor
+ * @fd to the value @desc
+ */
+void
+setJavaIoFileDescriptorContentsAsPointer (JNIEnv * env, jobject fd,
+ void *value)
+{
+ jfieldID fid = getJavaIoFileDescriptorDescriptorFID (env);
+ if (NULL != fid)
+ {
+ (*env)->SetLongField (env, fd, fid, (IDATA)value);
+ }
+}
+
+/**
+ * Answer the errorString corresponding to the errorNumber, if available.
+ * This function will answer a default error string, if the errorNumber is not
+ * recognized.
+ *
+ * This function will have to be reworked to handle internationalization properly, removing
+ * the explicit strings.
+ *
+ * @param anErrorNum the error code to resolve to a human readable string
+ *
+ * @return a human readable error string
+ */
+
+char *
+ioLookupErrorString (JNIEnv * env, I_32 anErrorNum)
+{
+ PORT_ACCESS_FROM_ENV (env);
+ switch (anErrorNum)
+ {
+ case HYPORT_ERROR_FILE_NOTFOUND:
+ return "File not found";
+ case HYPORT_ERROR_FILE_NOPERMISSION:
+ return "Lacking proper permissions to perform the operation";
+ case HYPORT_ERROR_FILE_DISKFULL:
+ return "Disk is full";
+ case HYPORT_ERROR_FILE_NOENT:
+ return "A component of the path name does not exist";
+ case HYPORT_ERROR_FILE_NOTDIR:
+ return "A component of the path name is not a directory";
+ case HYPORT_ERROR_FILE_BADF:
+ return "File descriptor invalid";
+ case HYPORT_ERROR_FILE_EXIST:
+ return "File already exists";
+ case HYPORT_ERROR_FILE_INVAL:
+ return "A parameter is invalid";
+ case HYPORT_ERROR_FILE_LOOP:
+ return "Followed too many symbolic links, possibly stuck in loop";
+ case HYPORT_ERROR_FILE_NAMETOOLONG:
+ return "Filename exceeds maximum length";
+ default:
+ return (char *) hyfile_error_message ();
+ }
+}
+
+/**
+ * This will retrieve the 'descriptor' field value from a java.io.FileDescriptor
+ */
+jfieldID
+getJavaIoFileDescriptorDescriptorFID (JNIEnv * env)
+{
+ jclass descriptorCLS;
+ jfieldID descriptorFID;
+
+ descriptorFID =
+ HARMONY_CACHE_GET (env, FID_java_io_FileDescriptor_descriptor);
+ if (NULL != descriptorFID)
+ {
+ return descriptorFID;
+ }
+ descriptorCLS = (*env)->FindClass (env, "java/io/FileDescriptor");
+ if (NULL == descriptorCLS)
+ {
+ return NULL;
+ }
+ descriptorFID = (*env)->GetFieldID (env, descriptorCLS, "descriptor", "J");
+ if (NULL == descriptorFID)
+ {
+ return NULL;
+ }
+ HARMONY_CACHE_SET (env, FID_java_io_FileDescriptor_descriptor, descriptorFID);
+ return descriptorFID;
}
Modified: harmony/enhanced/classlib/trunk/modules/portlib/src/main/native/common/shared/iohelp.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/portlib/src/main/native/common/shared/iohelp.c?rev=583744&r1=583743&r2=583744&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/portlib/src/main/native/common/shared/iohelp.c (original)
+++ harmony/enhanced/classlib/trunk/modules/portlib/src/main/native/common/shared/iohelp.c Thu Oct 11 02:02:59 2007
@@ -19,8 +19,6 @@
#include "exceptions.h"
#include "jclglob.h"
-jfieldID getJavaIoFileDescriptorDescriptorFID (JNIEnv * env);
-
/**
* This will convert all separators to the proper platform separator
* and remove duplicates on non POSIX platforms.
@@ -71,301 +69,4 @@
}
/* This will have to handle extra \'s but currently doesn't */
-}
-
-/**
- * This will write count bytes from buffer starting at offset
- */
-void
-ioh_writebytesImpl (JNIEnv * env, jobject recv, jbyteArray buffer,
- jint offset, jint count, IDATA descriptor)
-{
- I_32 result = 0;
- jbyte *buf;
- PORT_ACCESS_FROM_ENV (env);
- jsize len;
- char *errorMessage = NULL;
-
-/* TODO: ARRAY PINNING */
-#define INTERNAL_MAX 512
- jbyte internalBuffer[INTERNAL_MAX];
-
- if (buffer == NULL)
- {
- throwNPException (env, "buffer is null");
- return;
- }
-
- len = (*env)->GetArrayLength (env, buffer);
-
- /**
- * If offset is negative, or count is negative, or offset+count is greater
- * than the length of the array b, then an IndexOutOfBoundsException is thrown.
- * Must test offset > len, or len - offset < count to avoid int overflow caused
- * by offset + count
- */
- if (offset < 0 || count < 0 || offset > len || (len - offset) < count)
- {
- throwIndexOutOfBoundsException (env);
- return;
- }
-
- /* If len or count is zero, just return 0 */
- if (len == 0 || count == 0)
- return;
-
- if (descriptor == -1)
- {
- throwJavaIoIOExceptionClosed (env);
- return;
- }
- if (count > INTERNAL_MAX)
- {
- buf = jclmem_allocate_memory (env, count);
- }
- else
- {
- buf = internalBuffer;
- }
-
- if (buf == NULL)
- {
- throwNewOutOfMemoryError (env, "");
- return;
- }
- ((*env)->GetByteArrayRegion (env, buffer, offset, count, buf));
-
- result = hyfile_write (descriptor, buf, count);
-
- /**
- * if there is an error, find the error message before calling free in case
- * hymem_free_memory changes the error code
- */
- if (result < 0)
- errorMessage = ioLookupErrorString (env, result);
-
- if (buf != internalBuffer)
- {
- jclmem_free_memory (env, buf);
- }
-#undef INTERNAL_MAX
-
- if (result < 0)
- throwJavaIoIOException (env, errorMessage);
-}
-
-/**
- * This will read a up to count bytes into buffer starting at offset
- */
-jint
-ioh_readbytesImpl (JNIEnv * env, jobject recv, jbyteArray buffer, jint offset,
- jint count, IDATA descriptor)
-{
- I_32 result;
- jsize len;
- jbyte *buf;
-
-/* TODO: ARRAY PINNING */
-#define INTERNAL_MAX 2048
- jbyte internalBuffer[INTERNAL_MAX];
-
- PORT_ACCESS_FROM_ENV (env);
-
- if (buffer == NULL)
- {
- throwNPException (env, "buffer is null");
- return 0;
- }
-
- len = (*env)->GetArrayLength (env, buffer);
- /**
- * Throw IndexOutOfBoundsException according to spec.
- * Must test offset > len, or len - offset < count to avoid
- * int overflow caused by offset + count
- */
- if (offset < 0 || count < 0 || offset > len || (len - offset) < count)
- {
- throwIndexOutOfBoundsException (env);
- return 0;
- }
- /* If len is 0, simply return 0 (even if it is closed) */
- if (len == 0 || count == 0)
- return 0;
-
- if (descriptor == -1)
- {
- throwJavaIoIOExceptionClosed (env);
- return 0;
- }
- if (len >= INTERNAL_MAX)
- {
- buf = jclmem_allocate_memory (env, len);
- }
- else
- {
- buf = internalBuffer;
- }
-
- if (buf == NULL)
- {
- throwNewOutOfMemoryError (env, "");
- return 0;
- }
- /* Must FREE buffer before returning */
-
- if (descriptor == 0)
- {
- /* hytty_get_chars() returns zero on EOF */
- if ((result = hytty_get_chars (buf, count)) == 0)
- result = -1;
- }
- else
- {
- result = hyfile_read (descriptor, buf, count);
- }
- if (result > 0)
- (*env)->SetByteArrayRegion (env, buffer, offset, result, buf);
-
- if (buf != internalBuffer)
- {
- jclmem_free_memory (env, buf);
- }
-#undef INTERNAL_MAX
-
- return result;
-}
-
-/**
- * This will close a file descriptor
- */
-void
-new_ioh_close (JNIEnv * env, jobject recv, jfieldID fdFID)
-{
- jobject fd;
- jfieldID descriptorFID;
- IDATA descriptor;
- PORT_ACCESS_FROM_ENV (env);
-
- descriptorFID = getJavaIoFileDescriptorDescriptorFID (env);
- if (NULL == descriptorFID)
- {
- return;
- }
-
- /* fetch the fd field from the object */
- fd = (*env)->GetObjectField (env, recv, fdFID);
-
- /* dereference the C pointer from the wrapper object */
- descriptor = (IDATA) getJavaIoFileDescriptorContentsAsPointer (env, fd);
-
- /* Check for closed file, in, out, and err */
- if (descriptor >= -1 && descriptor <= 2)
- {
- return;
- }
-
- hyfile_close (descriptor);
- setJavaIoFileDescriptorContentsAsPointer (env, fd, (void *) -1);
- return;
-}
-
-/**
- * This will retrieve the 'descriptor' field value from a java.io.FileDescriptor
- */
-void *
-getJavaIoFileDescriptorContentsAsPointer (JNIEnv * env, jobject fd)
-{
- jfieldID descriptorFID = getJavaIoFileDescriptorDescriptorFID (env);
- if (NULL == descriptorFID)
- {
- return (void *) -1;
- }
- return (void *)(IDATA) ((*env)->GetLongField (env, fd, descriptorFID));
-}
-
-/**
- * This will set the 'descriptor' field value in the java.io.FileDescriptor
- * @fd to the value @desc
- */
-void
-setJavaIoFileDescriptorContentsAsPointer (JNIEnv * env, jobject fd,
- void *value)
-{
- jfieldID fid = getJavaIoFileDescriptorDescriptorFID (env);
- if (NULL != fid)
- {
- (*env)->SetLongField (env, fd, fid, (IDATA)value);
- }
-}
-
-/**
- * Answer the errorString corresponding to the errorNumber, if available.
- * This function will answer a default error string, if the errorNumber is not
- * recognized.
- *
- * This function will have to be reworked to handle internationalization properly, removing
- * the explicit strings.
- *
- * @param anErrorNum the error code to resolve to a human readable string
- *
- * @return a human readable error string
- */
-
-char *
-ioLookupErrorString (JNIEnv * env, I_32 anErrorNum)
-{
- PORT_ACCESS_FROM_ENV (env);
- switch (anErrorNum)
- {
- case HYPORT_ERROR_FILE_NOTFOUND:
- return "File not found";
- case HYPORT_ERROR_FILE_NOPERMISSION:
- return "Lacking proper permissions to perform the operation";
- case HYPORT_ERROR_FILE_DISKFULL:
- return "Disk is full";
- case HYPORT_ERROR_FILE_NOENT:
- return "A component of the path name does not exist";
- case HYPORT_ERROR_FILE_NOTDIR:
- return "A component of the path name is not a directory";
- case HYPORT_ERROR_FILE_BADF:
- return "File descriptor invalid";
- case HYPORT_ERROR_FILE_EXIST:
- return "File already exists";
- case HYPORT_ERROR_FILE_INVAL:
- return "A parameter is invalid";
- case HYPORT_ERROR_FILE_LOOP:
- return "Followed too many symbolic links, possibly stuck in loop";
- case HYPORT_ERROR_FILE_NAMETOOLONG:
- return "Filename exceeds maximum length";
- default:
- return (char *) hyfile_error_message ();
- }
-}
-
-/**
- * This will retrieve the 'descriptor' field value from a java.io.FileDescriptor
- */
-jfieldID
-getJavaIoFileDescriptorDescriptorFID (JNIEnv * env)
-{
- jclass descriptorCLS;
- jfieldID descriptorFID;
-
- descriptorFID = JCL_CACHE_GET (env, FID_java_io_FileDescriptor_descriptor);
- if (NULL != descriptorFID)
- {
- return descriptorFID;
- }
- descriptorCLS = (*env)->FindClass (env, "java/io/FileDescriptor");
- if (NULL == descriptorCLS)
- {
- return NULL;
- }
- descriptorFID = (*env)->GetFieldID (env, descriptorCLS, "descriptor", "J");
- if (NULL == descriptorFID)
- {
- return NULL;
- }
- JCL_CACHE_SET (env, FID_java_io_FileDescriptor_descriptor, descriptorFID);
- return descriptorFID;
}