You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/04/15 07:41:15 UTC
svn commit: r1092592 - in /commons/sandbox/runtime/trunk/src/main:
java/org/apache/commons/runtime/NioByteBuffer.java
java/org/apache/commons/runtime/Posix.java native/os/unix/posixapi.c
native/shared/nbb.c
Author: mturk
Date: Fri Apr 15 05:41:14 2011
New Revision: 1092592
URL: http://svn.apache.org/viewvc?rev=1092592&view=rev
Log:
Use integer length for NioByteBuffers
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/NioByteBuffer.java
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Posix.java
commons/sandbox/runtime/trunk/src/main/native/os/unix/posixapi.c
commons/sandbox/runtime/trunk/src/main/native/shared/nbb.c
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/NioByteBuffer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/NioByteBuffer.java?rev=1092592&r1=1092591&r2=1092592&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/NioByteBuffer.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/NioByteBuffer.java Fri Apr 15 05:41:14 2011
@@ -40,22 +40,17 @@ public final class NioByteBuffer
// No class instance
}
- private static native ByteBuffer malloc0(long size)
+ private static native ByteBuffer malloc0(int size)
throws OutOfMemoryError, IllegalArgumentException;
-
- private static native ByteBuffer calloc0(long size)
+ private static native ByteBuffer calloc0(int size)
throws OutOfMemoryError, IllegalArgumentException;
-
- private static native ByteBuffer attach0(long ptr, long len)
+ private static native ByteBuffer attach0(long ptr, int len)
throws OutOfMemoryError;
private static native long addr0(ByteBuffer buf);
-
- private static native void copy0(ByteBuffer src, long srcPos, ByteBuffer dst,
- long dstPos, int count)
- throws NullPointerException, IllegalArgumentException,
- IndexOutOfBoundsException;
-
+ private static native void copy0(ByteBuffer src, int srcPos, ByteBuffer dst,
+ int dstPos, int count)
+ throws NullPointerException;
/**
* Allocate a new {@code ByteBuffer} from {@link Pointer} memory area.
* <p>
@@ -76,13 +71,13 @@ public final class NioByteBuffer
* @throws IndexOutOfBoundsException if copying would cause access of
* data outside array bounds.
*/
- public static ByteBuffer allocate(Pointer ptr, long off, long len)
+ public static ByteBuffer allocate(Pointer ptr, long off, int len)
throws NullPointerException, IllegalArgumentException,
IndexOutOfBoundsException, OutOfMemoryError
{
if (ptr == null || ptr.POINTER == 0L)
throw new NullPointerException();
- if (off < 0L || len < 1L)
+ if (off < 0L || len < 1)
throw new IllegalArgumentException();
if (off + len >= ptr.PLENGTH)
throw new IndexOutOfBoundsException();
@@ -109,16 +104,11 @@ public final class NioByteBuffer
{
if (ptr == null || ptr.POINTER == 0L)
throw new NullPointerException();
- return attach0(ptr.POINTER, ptr.PLENGTH);
+ return attach0(ptr.POINTER, (int)ptr.PLENGTH);
}
- /**
- * Returns the allocated memory size of the ByteBuffer.
- * @param buf Previously allocated ByteBuffer.
- */
- public static native long size(ByteBuffer buf)
+ public static native void set0(ByteBuffer buf, int c, int count)
throws NullPointerException;
-
/**
* Set ByteBuffer to specified character.
* @param buf The ByteBuffer to use
@@ -130,9 +120,19 @@ public final class NioByteBuffer
* @throws IndexOutOfBoundsException if copying would cause access of
* data outside ByteBuffer bounds.
*/
- public static native void set(ByteBuffer buf, int c, int count)
+ public static void set(ByteBuffer buf, int c, int count)
throws NullPointerException, IllegalArgumentException,
- IndexOutOfBoundsException;
+ IndexOutOfBoundsException
+ {
+ if (buf == null)
+ throw new NullPointerException();
+ int ss = buf.capacity();
+ if (ss < 1)
+ throw new IllegalArgumentException();
+ if (count > ss)
+ throw new IndexOutOfBoundsException();
+ set0(buf, c, count);
+ }
/**
* Copy an ByteBuffer content from specified source ByteBuffer.
@@ -151,6 +151,14 @@ public final class NioByteBuffer
{
if (src == null || dst == null)
throw new NullPointerException();
+ int ss = src.capacity();
+ int ds = dst.capacity();
+ if (ss < 1 || ds < 1)
+ throw new IllegalArgumentException();
+ if (length > ss)
+ throw new IndexOutOfBoundsException();
+ if (length > ds)
+ throw new IndexOutOfBoundsException();
copy0(src, 0, dst, 0, length);
}
@@ -167,13 +175,21 @@ public final class NioByteBuffer
* @throws IndexOutOfBoundsException if copying would cause access of
* data outside ByteBuffer bounds.
*/
- public static void copy(ByteBuffer src, long srcPos, ByteBuffer dst,
- long dstPos, int length)
+ public static void copy(ByteBuffer src, int srcPos, ByteBuffer dst,
+ int dstPos, int length)
throws NullPointerException, IllegalArgumentException,
IndexOutOfBoundsException
{
if (src == null || dst == null)
throw new NullPointerException();
+ int ss = src.capacity();
+ int ds = dst.capacity();
+ if (ss < 1 || ds < 1)
+ throw new IllegalArgumentException();
+ if ((srcPos + length) > ss)
+ throw new IndexOutOfBoundsException();
+ if ((dstPos + length) > ds)
+ throw new IndexOutOfBoundsException();
copy0(src, srcPos, dst, dstPos, length);
}
@@ -189,7 +205,7 @@ public final class NioByteBuffer
* @throws OutOfMemoryError if memory cannot be allocated from the system.
* @throws IllegalArgumentException if {@code size} is invalid.
*/
- public static ByteBuffer allocate(long size)
+ public static ByteBuffer allocate(int size)
throws OutOfMemoryError, IllegalArgumentException
{
return malloc0(size);
@@ -204,7 +220,7 @@ public final class NioByteBuffer
* @throws OutOfMemoryError if memory cannot be allocated from the system.
* @throws IllegalArgumentException if {@code size} is invalid.
*/
- public static ByteBuffer allocateAndClear(long size)
+ public static ByteBuffer allocateAndClear(int size)
throws OutOfMemoryError, IllegalArgumentException
{
return calloc0(size);
@@ -221,7 +237,7 @@ public final class NioByteBuffer
* @throws IllegalArgumentException if {@code offset} or
* {@code size} are invalid.
*/
- public static ByteBuffer allocate(ByteBuffer buf, long offset, long size)
+ public static ByteBuffer allocate(ByteBuffer buf, int offset, int size)
throws NullPointerException, IllegalArgumentException
{
if (buf == null)
@@ -229,9 +245,9 @@ public final class NioByteBuffer
long mem = addr0(buf);
if (mem == 0L)
throw new NullPointerException();
- if (offset < 0L || size < 1L)
+ if (offset < 0 || size < 1)
throw new IllegalArgumentException();
- long len = size(buf);
+ int len = buf.capacity();
if (offset + size > len)
throw new IndexOutOfBoundsException();
return attach0(mem + offset, size);
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Posix.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Posix.java?rev=1092592&r1=1092591&r2=1092592&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Posix.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Posix.java Fri Apr 15 05:41:14 2011
@@ -37,5 +37,13 @@ final class Posix
public static native int close(int fd);
public static native int unlink(String name);
+ public static native int chmod(String name, int mode);
+ public static native int chown(String name, int uid, int gid);
+ public static native int fchmod(int fd, int mode);
+ public static native int fchown(int fd, int uid, int gid);
+ public static native int chdir(String path);
+ public static native int fchdir(int fd);
+ public static native String getcwd()
+ throws SystemException;
}
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/posixapi.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/posixapi.c?rev=1092592&r1=1092591&r2=1092592&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/posixapi.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/posixapi.c Fri Apr 15 05:41:14 2011
@@ -42,3 +42,81 @@ ACR_JNI_EXPORT(jint, Posix, unlink)(JNI_
return rc;
}
+
+ACR_JNI_EXPORT(jint, Posix, chmod)(JNI_STDARGS, jstring name, jint mode)
+{
+ int rc = EINVAL;
+
+ WITH_CSTR(name) {
+ if (chmod(J2S(name), (mode_t)mode) == 0)
+ rc = 0;
+ else
+ rc = errno;
+ } DONE_WITH_STR(name);
+
+ return rc;
+}
+
+ACR_JNI_EXPORT(jint, Posix, chown)(JNI_STDARGS, jstring name, jint uid, jint gid)
+{
+ int rc = EINVAL;
+
+ WITH_CSTR(name) {
+ if (chown(J2S(name), (uid_t)uid, (gid_t)gid) == 0)
+ rc = 0;
+ else
+ rc = errno;
+ } DONE_WITH_STR(name);
+
+ return rc;
+}
+
+ACR_JNI_EXPORT(jint, Posix, chdir)(JNI_STDARGS, jstring path)
+{
+ int rc = EINVAL;
+
+ WITH_CSTR(path) {
+ if (chdir(J2S(path)) == 0)
+ rc = 0;
+ else
+ rc = errno;
+ } DONE_WITH_STR(path);
+
+ return rc;
+}
+
+ACR_JNI_EXPORT(jint, Posix, fchdir)(JNI_STDARGS, jint fd)
+{
+ if (fchdir(fd) == -1)
+ return errno;
+ else
+ return 0;
+}
+
+ACR_JNI_EXPORT(jint, Posix, fchown)(JNI_STDARGS, jint fd, jint uid, jint gid)
+{
+ if (fchown(fd, (uid_t)uid, (gid_t)gid) == -1)
+ return errno;
+ else
+ return 0;
+}
+
+ACR_JNI_EXPORT(jint, Posix, fchmod)(JNI_STDARGS, jint fd, jint mode)
+{
+ if (fchmod(fd, (mode_t)mode) == -1)
+ return errno;
+ else
+ return 0;
+}
+
+ACR_JNI_EXPORT(jstring, Posix, getcwd)(JNI_STDARGS)
+{
+ char path[ACR_PATH_MAX];
+
+ if (getcwd(path, ACR_PATH_MAX) == 0) {
+ ACR_THROW_SYS_ERROR();
+ return 0;
+ }
+ else
+ return CSTR_TO_JSTRING(path);
+}
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/nbb.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/nbb.c?rev=1092592&r1=1092591&r2=1092592&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/nbb.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/nbb.c Fri Apr 15 05:41:14 2011
@@ -26,12 +26,12 @@
* NioByteBuffer utilities
*/
ACR_JNI_EXPORT(jobject, NioByteBuffer, malloc0)(JNI_STDARGS,
- jlong size)
+ jint size)
{
void *mem;
- jlong sz = ACR_ALIGN_DEFAULT(size);
+ jint sz = ACR_ALIGN_DEFAULT(size);
- if (size < 1L || size > SIZE_T_MAX) {
+ if (size < 1) {
ACR_THROW(ACR_EX_EINVAL, 0);
return 0;
}
@@ -46,12 +46,12 @@ ACR_JNI_EXPORT(jobject, NioByteBuffer, m
}
ACR_JNI_EXPORT(jobject, NioByteBuffer, calloc0)(JNI_STDARGS,
- jlong size)
+ jint size)
{
- jlong sz = ACR_ALIGN_DEFAULT(size);
+ jint sz = ACR_ALIGN_DEFAULT(size);
void *mem;
- if (size < 1L || size > SIZE_T_MAX) {
+ if (size < 1) {
ACR_THROW(ACR_EX_EINVAL, 0);
return 0;
}
@@ -66,7 +66,7 @@ ACR_JNI_EXPORT(jobject, NioByteBuffer, c
}
ACR_JNI_EXPORT(jobject, NioByteBuffer, attach0)(JNI_STDARGS,
- jlong src, jlong len)
+ jlong src, jint len)
{
return (*env)->NewDirectByteBuffer(env, J2P(src, char *), len);
}
@@ -94,37 +94,12 @@ ACR_JNI_EXPORT(jlong, NioByteBuffer, add
return P2J((*env)->GetDirectBufferAddress(env, bb));
}
-ACR_JNI_EXPORT(jlong, NioByteBuffer, size)(JNI_STDARGS,
- jobject bb)
-{
- if (bb == 0) {
- ACR_THROW(ACR_EX_ENULL, 0);
- return -1;
- }
- return (*env)->GetDirectBufferCapacity(env, bb);
-}
-
-ACR_JNI_EXPORT(void, NioByteBuffer, set)(JNI_STDARGS,
- jobject bb,
- jint c,
- jint count)
+ACR_JNI_EXPORT(void, NioByteBuffer, set0)(JNI_STDARGS,
+ jobject bb,
+ jint c,
+ jint count)
{
void *m;
- jlong sz;
-
- if (bb == 0) {
- ACR_THROW(ACR_EX_ENULL, 0);
- return;
- }
- sz = (*env)->GetDirectBufferCapacity(env, bb);
- if (count < 1) {
- ACR_THROW(ACR_EX_EINVAL, 0);
- return;
- }
- if ((jlong)count > sz) {
- ACR_THROW(ACR_EX_EINVAL, ACR_ERANGE);
- return;
- }
if ((m = (*env)->GetDirectBufferAddress(env, bb))) {
memset(m, c, count);
}
@@ -135,31 +110,14 @@ ACR_JNI_EXPORT(void, NioByteBuffer, set)
ACR_JNI_EXPORT(void, NioByteBuffer, copy0)(JNI_STDARGS,
jobject srcb,
- jlong srco,
+ jint srco,
jobject dstb,
- jlong dsto,
+ jint dsto,
jint count)
{
char *d;
char *s;
- jlong ss;
- jlong ds;
-
- ss = (*env)->GetDirectBufferCapacity(env, srcb);
- ds = (*env)->GetDirectBufferCapacity(env, dstb);
- if (ss < 1 || ds < 1) {
- ACR_THROW(ACR_EX_EINVAL, ACR_EINVALSIZ);
- return;
- }
- if ((count + srco) > ss) {
- ACR_THROW(ACR_EX_EINDEX, ACR_ERANGE);
- return;
- }
- if ((count + dsto) > ds) {
- ACR_THROW(ACR_EX_EINDEX, ACR_ERANGE);
- return;
- }
s = (char *)(*env)->GetDirectBufferAddress(env, srcb);
if (s == 0 ) {
ACR_THROW(ACR_EX_ENULL, 0);