You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by mt...@apache.org on 2005/05/19 19:11:01 UTC
cvs commit: jakarta-tomcat-connectors/jni/native/src file.c
mturk 2005/05/19 10:11:01
Modified: jni/java/org/apache/tomcat/jni File.java
jni/native/src file.c
Log:
Added readb and writeb (ByteBuffer) to File object.
Revision Changes Path
1.4 +46 -2 jakarta-tomcat-connectors/jni/java/org/apache/tomcat/jni/File.java
Index: File.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-connectors/jni/java/org/apache/tomcat/jni/File.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- File.java 15 Apr 2005 10:15:28 -0000 1.3
+++ File.java 19 May 2005 17:11:01 -0000 1.4
@@ -15,7 +15,8 @@
*/
package org.apache.tomcat.jni;
-
+/* Import needed classes */
+import java.nio.ByteBuffer;
/** File
*
* @author Mladen Turk
@@ -346,12 +347,31 @@
* be returned. APR_EINTR is never returned.
* @param thefile The file descriptor to write to.
* @param buf The buffer which contains the data.
+ * @param offset Start offset in buf
* @param nbytes The number of bytes to write; (-1) for full array.
* @return The number of bytes written.
*/
public static native int write(long thefile, byte[] buf, int offset, int nbytes);
/**
+ * Write data to the specified file.
+ *
+ * Write will write up to the specified number of
+ * bytes, but never more. If the OS cannot write that many bytes, it
+ * will write as many as it can. The third argument is modified to
+ * reflect the * number of bytes written.
+ *
+ * It is possible for both bytes to be written and an error to
+ * be returned. APR_EINTR is never returned.
+ * @param thefile The file descriptor to write to.
+ * @param buf The direct Byte buffer which contains the data.
+ * @param offset Start offset in buf
+ * @param nbytes The number of bytes to write
+ * @return The number of bytes written.
+ */
+ public static native int writeb(long thefile, ByteBuffer buf, int offset, int nbytes);
+
+ /**
* Write data to the specified file, ensuring that all of the data is
* written before returning.
*
@@ -368,6 +388,7 @@
* APR_EINTR is never returned.
* @param thefile The file descriptor to write to.
* @param buf The buffer which contains the data.
+ * @param offset Start offset in buf
* @param nbytes The number of bytes to write; (-1) for full array.
* @return The number of bytes written.
*/
@@ -413,12 +434,33 @@
* or other error to be returned. APR_EINTR is never returned.
* @param thefile The file descriptor to read from.
* @param buf The buffer to store the data to.
+ * @param offset Start offset in buf
* @param nbytes The number of bytes to read (-1) for full array.
* @return the number of bytes read.
*/
public static native int read(long thefile, byte[] buf, int offset, int nbytes);
/**
+ * Read data from the specified file.
+ *
+ * apr_file_read will read up to the specified number of
+ * bytes, but never more. If there isn't enough data to fill that
+ * number of bytes, all of the available data is read. The third
+ * argument is modified to reflect the number of bytes read. If a
+ * char was put back into the stream via ungetc, it will be the first
+ * character returned.
+ *
+ * It is not possible for both bytes to be read and an APR_EOF
+ * or other error to be returned. APR_EINTR is never returned.
+ * @param thefile The file descriptor to read from.
+ * @param buf The direct Byte buffer to store the data to.
+ * @param offset Start offset in buf
+ * @param nbytes The number of bytes to read.
+ * @return the number of bytes read.
+ */
+ public static native int readb(long thefile, ByteBuffer buf, int offset, int nbytes);
+
+ /**
* Read data from the specified file, ensuring that the buffer is filled
* before returning.
*
@@ -435,6 +477,7 @@
* APR_EINTR is never returned.
* @param thefile The file descriptor to read from.
* @param buf The buffer to store the data to.
+ * @param offset Start offset in buf
* @param nbytes The number of bytes to read (-1) for full array.
* @return the number of bytes read.
*/
@@ -444,6 +487,7 @@
* Read a string from the specified file.
* The buffer will be NUL-terminated if any characters are stored.
* @param buf The buffer to store the string in.
+ * @param offset Start offset in buf
* @param thefile The file descriptor to read from
*/
public static native int gets(byte[] buf, int offset, long thefile);
1.4 +46 -14 jakarta-tomcat-connectors/jni/native/src/file.c
Index: file.c
===================================================================
RCS file: /home/cvs/jakarta-tomcat-connectors/jni/native/src/file.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- file.c 15 Apr 2005 10:14:20 -0000 1.3
+++ file.c 19 May 2005 17:11:01 -0000 1.4
@@ -282,13 +282,13 @@
jbyteArray buf, jint offset, jint towrite)
{
apr_file_t *f = J2P(file, apr_file_t *);
- apr_size_t nbytes = (*e)->GetArrayLength(e, buf);
+ apr_size_t nbytes = (apr_size_t)towrite;
jbyte *bytes = (*e)->GetPrimitiveArrayCritical(e, buf, NULL);
apr_status_t ss;
UNREFERENCED(o);
- if (towrite > 0)
- nbytes = min(nbytes - offset, (apr_size_t)towrite);
+ if (towrite < 0)
+ towrite = (*e)->GetArrayLength(e, buf);
ss = apr_file_write(f, bytes + offset, &nbytes);
(*e)->ReleasePrimitiveArrayCritical(e, buf, bytes, JNI_ABORT);
@@ -298,18 +298,36 @@
return -(jint)ss;
}
+TCN_IMPLEMENT_CALL(jint, File, writeb)(TCN_STDARGS, jlong file,
+ jobject buf, jint offset, jint towrite)
+{
+ apr_file_t *f = J2P(file, apr_file_t *);
+ apr_size_t nbytes = (apr_size_t)towrite;
+ char *bytes = (char *)(*e)->GetDirectBufferAddress(e, buf);
+ apr_status_t ss = APR_EINVAL;
+
+ UNREFERENCED(o);
+ if (bytes)
+ ss = apr_file_write(f, bytes + offset, &nbytes);
+
+ if (ss == APR_SUCCESS)
+ return (jint)nbytes;
+ else
+ return -(jint)ss;
+}
+
TCN_IMPLEMENT_CALL(jint, File, writeFull)(TCN_STDARGS, jlong file,
jbyteArray buf, jint offset, jint towrite)
{
apr_file_t *f = J2P(file, apr_file_t *);
- apr_size_t nbytes = (*e)->GetArrayLength(e, buf);
+ apr_size_t nbytes = (apr_size_t)towrite;
apr_size_t written = 0;
apr_status_t ss;
jbyte *bytes = (*e)->GetByteArrayElements(e, buf, NULL);
UNREFERENCED(o);
- if (towrite > 0)
- nbytes = min(nbytes + offset, (apr_size_t)towrite);
+ if (towrite < 0)
+ towrite = (*e)->GetArrayLength(e, buf);
ss = apr_file_write_full(f, bytes + offset, nbytes, &written);
(*e)->ReleaseByteArrayElements(e, buf, bytes, JNI_ABORT);
@@ -396,14 +414,11 @@
jint toread)
{
apr_file_t *f = J2P(file, apr_file_t *);
- apr_size_t nbytes = (*e)->GetArrayLength(e, buf);
+ apr_size_t nbytes = (apr_size_t)toread;
jbyte *bytes = (*e)->GetByteArrayElements(e, buf, NULL);
apr_status_t ss;
UNREFERENCED(o);
- if (toread > 0)
- nbytes = min(nbytes - offset, (apr_size_t)toread);
-
ss = apr_file_read(f, bytes + offset, &nbytes);
(*e)->ReleaseByteArrayElements(e, buf, bytes,
@@ -414,19 +429,36 @@
return -(jint)ss;
}
+TCN_IMPLEMENT_CALL(jint, File, readb)(TCN_STDARGS, jlong file,
+ jobject buf, jint offset,
+ jint toread)
+{
+ apr_file_t *f = J2P(file, apr_file_t *);
+ apr_size_t nbytes = (apr_size_t)toread;
+ char *bytes = (char *)(*e)->GetDirectBufferAddress(e, buf);
+ apr_status_t ss = APR_EINVAL;
+
+ UNREFERENCED(o);
+ if (bytes)
+ ss = apr_file_read(f, bytes + offset, &nbytes);
+
+ if (ss == APR_SUCCESS)
+ return (jint)nbytes;
+ else
+ return -(jint)ss;
+}
+
TCN_IMPLEMENT_CALL(jint, File, readFull)(TCN_STDARGS, jlong file,
jbyteArray buf, jint offset,
jint toread)
{
apr_file_t *f = J2P(file, apr_file_t *);
- apr_size_t nbytes = (*e)->GetArrayLength(e, buf);
+ apr_size_t nbytes = (apr_size_t)toread;
apr_size_t nread;
apr_status_t ss;
jbyte *bytes = (*e)->GetByteArrayElements(e, buf, NULL);
UNREFERENCED(o);
- if (toread > 0)
- nbytes = min(nbytes - offset, (apr_size_t)toread);
ss = apr_file_read_full(f, bytes + offset, nbytes, &nread);
(*e)->ReleaseByteArrayElements(e, buf, bytes,
@@ -446,7 +478,7 @@
jbyte *bytes = (*e)->GetByteArrayElements(e, buf, NULL);
UNREFERENCED(o);
- rv = apr_file_gets(bytes + offset, nbytes, f);
+ rv = apr_file_gets(bytes + offset, nbytes - offset, f);
(*e)->ReleaseByteArrayElements(e, buf, bytes,
rv == APR_SUCCESS ? 0 : JNI_ABORT);
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-dev-help@jakarta.apache.org