commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r889907 - /commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysrw.c
Date Sat, 12 Dec 2009 09:44:17 GMT
Author: mturk
Date: Sat Dec 12 09:44:16 2009
New Revision: 889907

URL: http://svn.apache.org/viewvc?rev=889907&view=rev
Log:
Add native posix api for reading and writting at given file offest

Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysrw.c

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysrw.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysrw.c?rev=889907&r1=889906&r2=889907&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysrw.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysrw.c Sat Dec 12 09:44:16 2009
@@ -370,6 +370,294 @@
     return -1;
 }
 
+ACR_IO_EXPORT_DECLARE(jint, FileSystemIo, read4)(ACR_JNISTDARGS,
+                                                 jint file,
+                                                 jlong pos)
+{
+    unsigned char c;
+    ssize_t rd;
+    int     rc = 0;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_INVALID_DESC, ACR_EFTYPE);
+        return -1;
+    }
+    if (IS_INVALID_HANDLE(f)) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_CLOSED_DESC, ACR_EBADF);
+        return -1;
+    }
+    if (f->eof) {
+        return -1;
+    }
+    rd = r_pread(f->fd, &c, 1, (off_t)pos);
+    if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
+        f->timeout != 0) {
+        if ((rc = wait_for_io_or_timeout(f, 1)) == 0) {
+            rd = r_pread(f->fd, &c, 1, (off_t)pos);
+        }
+    }
+    if (rd == -1) {
+        rc = rc ? rc : ACR_GET_OS_ERROR();
+        if (ACR_STATUS_IS_EAGAIN(rc))
+            rc = ACR_TIMEUP;
+    }
+    if (rd == 0) {
+        ACR_DescriptorSetErrEx(_E, f->descriptor, ACR_EOF);
+        f->eof = 1;
+        return  -1;
+    }
+    if (rd > 0)
+        return (jint)rd;
+
+    ACR_DescriptorSetErrEx(_E, f->descriptor, rc);
+    switch (rc) {
+        case 0:
+        case ACR_TIMEUP:
+            ACR_THROW_EX_IF_ERR(ACR_EX_ETIMEOUT, rc);
+        break;
+        case EBADF:
+            ACR_THROW_EX_IF_ERR(ACR_EX_ACLOSED_DESC, rc);
+        break;
+        default:
+            ACR_THROW_IO_IF_ERR(rc);
+        break;
+    }
+    return -1;
+}
+
+ACR_IO_EXPORT_DECLARE(jint, FileSystemIo, read5)(ACR_JNISTDARGS,
+                                                 jint file,
+                                                 jbyteArray buf,
+                                                 jint off,
+                                                 jint len,
+                                                 jlong pos)
+{
+    jbyte  *bb = NULL;
+    jbyte  *bc = NULL;
+    size_t  po = (size_t)off;
+    size_t  cs = (size_t)len;
+    jbyte   onstack[ACR_PBUFF_SIZ];
+    ssize_t rd;
+    int     rc = 0;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_INVALID_DESC, ACR_EFTYPE);
+        return -1;
+    }
+    if (IS_INVALID_HANDLE(f)) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_CLOSED_DESC, ACR_EBADF);
+        return -1;
+    }
+    if (f->eof) {
+        return -1;
+    }
+    if (cs > sizeof(onstack)) {
+        if (cs > (1024 * 1024)) {
+            bc = (*_E)->GetByteArrayElements(_E, buf, NULL);
+            if (bc)
+                bb = bc + po;
+        }
+        else
+            bb = ACR_Malloc(_E, THROW_FMARK, cs);
+    }
+    else
+        bb = onstack;
+    if (!bb) {
+        /* Exception was already thrown */
+        return -1;
+    }
+    rd = r_pread(f->fd, bb, cs, (off_t)pos);
+    if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
+        f->timeout != 0) {
+        if ((rc = wait_for_io_or_timeout(f, 1)) == 0) {
+            rd = r_pread(f->fd, bb, cs, (off_t)pos);
+        }
+    }
+    if (rd == -1) {
+        rc = rc ? rc : ACR_GET_OS_ERROR();
+        if (ACR_STATUS_IS_EAGAIN(rc))
+            rc = ACR_TIMEUP;
+    }
+    if (rd > 0) {
+        if (bc) {
+            (*_E)->ReleaseByteArrayElements(_E, buf, bc, 0);
+            return (jint)rd;
+        }
+        else {
+            (*_E)->SetByteArrayRegion(_E, buf, (jsize)po, (jsize)rd, bb);
+        }
+    }
+    if (bb != onstack) {
+        if (bc)
+            (*_E)->ReleaseByteArrayElements(_E, buf, bc, JNI_ABORT);
+        else
+            x_free(bb);
+    }
+    if (rd > 0)
+        return (jint)rd;
+
+    ACR_DescriptorSetErrEx(_E, f->descriptor, rc);
+    switch (rc) {
+        case 0:
+            f->eof = 1;
+        break;
+        case ACR_TIMEUP:
+            return 0;
+        break;
+        case EBADF:
+            ACR_THROW_EX_IF_ERR(ACR_EX_ACLOSED_DESC, rc);
+        break;
+        default:
+            ACR_THROW_IO_IF_ERR(rc);
+        break;
+    }
+    return -1;
+
+}
+
+ACR_IO_EXPORT_DECLARE(jlong, FileSystemIo, read6)(ACR_JNISTDARGS,
+                                                  jint file,
+                                                  jobject ptr,
+                                                  jlong off,
+                                                  jlong len,
+                                                  jlong pos)
+{
+    size_t  pl;
+    size_t  po = (size_t)off;
+    size_t  cs = (size_t)len;
+    jbyte  *pb;
+    ssize_t rd;
+    int     rc = 0;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_INVALID_DESC, ACR_EFTYPE);
+        return -1;
+    }
+    if (IS_INVALID_HANDLE(f)) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_CLOSED_DESC, ACR_EBADF);
+        return -1;
+    }
+    if (f->eof) {
+        return -1;
+    }
+    pb = (jbyte *)ACR_PointerGet(_E, ptr, &pl);
+    if (!pb) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
+        return -1;
+    }
+    if ((po + cs) > pl) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+        return -1;
+    }
+    rd = r_pread(f->fd, pb + po, cs, (off_t)pos);
+    if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
+        f->timeout != 0) {
+        if ((rc = wait_for_io_or_timeout(f, 1)) == 0) {
+            rd = r_pread(f->fd, pb + po, cs, (off_t)pos);
+        }
+    }
+    if (rd == -1) {
+        rc = rc ? rc : ACR_GET_OS_ERROR();
+        if (ACR_STATUS_IS_EAGAIN(rc))
+            rc = ACR_TIMEUP;
+    }
+    if (rd > 0)
+        return (jlong)rd;
+
+    ACR_DescriptorSetErrEx(_E, f->descriptor, rc);
+    switch (rc) {
+        case 0:
+            f->eof = 1;
+        break;
+        case ACR_TIMEUP:
+            return 0;
+        break;
+        case EBADF:
+            ACR_THROW_EX_IF_ERR(ACR_EX_ACLOSED_DESC, rc);
+        break;
+        default:
+            ACR_THROW_IO_IF_ERR(rc);
+        break;
+    }
+    return -1;
+}
+
+ACR_IO_EXPORT_DECLARE(jint, FileSystemIo, read7)(ACR_JNISTDARGS,
+                                                 jint file,
+                                                 jobject dbb,
+                                                 jint off,
+                                                 jint len,
+                                                 jlong pos)
+{
+#if defined(_DEBUG)
+    size_t  pl;
+#endif
+    size_t  po = (size_t)off;
+    size_t  cs = (size_t)len;
+    jbyte  *pb;
+    ssize_t rd;
+    int     rc = 0;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_INVALID_DESC, ACR_EFTYPE);
+        return -1;
+    }
+    if (IS_INVALID_HANDLE(f)) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_CLOSED_DESC, ACR_EBADF);
+        return -1;
+    }
+    if (f->eof) {
+        return -1;
+    }
+    pb = (jbyte *)(*_E)->GetDirectBufferAddress(_E, dbb);
+    if (!pb) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
+        return -1;
+    }
+#if defined(_DEBUG)
+    pl = (size_t)(*_E)->GetDirectBufferCapacity(_E, dbb);
+    if ((po + cs) > pl) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+        return -1;
+    }
+#endif
+    rd = r_pread(f->fd, pb + po, cs, (off_t)pos);
+    if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
+        f->timeout != 0) {
+        if ((rc = wait_for_io_or_timeout(f, 1)) == 0) {
+            rd = r_pread(f->fd, pb + po, cs, (off_t)pos);
+        }
+    }
+    if (rd == -1) {
+        rc = rc ? rc : ACR_GET_OS_ERROR();
+        if (ACR_STATUS_IS_EAGAIN(rc))
+            rc = ACR_TIMEUP;
+    }
+    if (rd > 0)
+        return (jint)rd;
+
+    ACR_DescriptorSetErrEx(_E, f->descriptor, rc);
+    switch (rc) {
+        case 0:
+            f->eof = 1;
+        break;
+        case ACR_TIMEUP:
+            return 0;
+        break;
+        case EBADF:
+            ACR_THROW_EX_IF_ERR(ACR_EX_ACLOSED_DESC, rc);
+        break;
+        default:
+            ACR_THROW_IO_IF_ERR(rc);
+        break;
+    }
+    return -1;
+}
+
 ACR_IO_EXPORT_DECLARE(jint, FileSystemIo, write0)(ACR_JNISTDARGS,
                                                   jint file,
                                                   jint b)
@@ -780,6 +1068,245 @@
     return -1;
 }
 
+ACR_IO_EXPORT_DECLARE(jint, FileSystemIo, write6)(ACR_JNISTDARGS,
+                                                  jint file,
+                                                  jint b,
+                                                  jlong pos)
+{
+    unsigned char c = (unsigned char)(b & 0xFF);
+    ssize_t wr;
+    int     rc = 0;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_INVALID_DESC, ACR_EFTYPE);
+        return -1;
+    }
+    if (IS_INVALID_HANDLE(f)) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_CLOSED_DESC, ACR_EBADF);
+        return -1;
+    }
+    wr = r_pwrite(f->fd, &c, 1, (off_t)pos);
+    if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
+        f->timeout != 0) {
+        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+            wr = r_pwrite(f->fd, &c, 1, (off_t)pos);
+        }
+    }
+    if (wr < 0) {
+        rc = rc ? rc : ACR_GET_OS_ERROR();
+        if (ACR_STATUS_IS_EAGAIN(rc))
+            rc = ACR_TIMEUP;
+    }
+    if (wr > 0)
+        return (jint)wr;
+
+    ACR_DescriptorSetErrEx(_E, f->descriptor, rc);
+    switch (rc) {
+        case 0:
+        case ACR_TIMEUP:
+            return 0;
+        break;
+        case EBADF:
+            ACR_THROW_EX_IF_ERR(ACR_EX_ACLOSED_DESC, rc);
+        break;
+        default:
+            ACR_THROW_IO_IF_ERR(rc);
+        break;
+    }
+    return -1;
+}
+
+ACR_IO_EXPORT_DECLARE(jint, FileSystemIo, write7)(ACR_JNISTDARGS,
+                                                  jint file,
+                                                  jbyteArray buf,
+                                                  jint off,
+                                                  jint len,
+                                                  jlong pos)
+{
+    jbyte  *bb;
+    size_t  po = (size_t)off;
+    size_t  cs = (size_t)len;
+    ssize_t wr;
+    int     rc = 0;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_INVALID_DESC, ACR_EFTYPE);
+        return -1;
+    }
+    if (IS_INVALID_HANDLE(f)) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_CLOSED_DESC, ACR_EBADF);
+        return -1;
+    }
+    bb = (*_E)->GetByteArrayElements(_E, buf, NULL);
+    if (!bb) {
+        return -1;
+    }
+
+    wr = r_pwrite(f->fd, bb + po, cs, (off_t)pos);
+    if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
+        f->timeout != 0) {
+        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+            wr = r_pwrite(f->fd, bb + po, cs, (off_t)pos);
+        }
+    }
+    if (wr < 0) {
+        rc = rc ? rc : ACR_GET_OS_ERROR();
+        if (ACR_STATUS_IS_EAGAIN(rc))
+            rc = ACR_TIMEUP;
+    }
+   (*_E)->ReleaseByteArrayElements(_E, buf, bb, JNI_ABORT);
+    if (wr > 0)
+        return (jint)wr;
+
+    ACR_DescriptorSetErrEx(_E, f->descriptor, rc);
+    switch (rc) {
+        case 0:
+        case ACR_TIMEUP:
+            return 0;
+        break;
+        case EBADF:
+            ACR_THROW_EX_IF_ERR(ACR_EX_ACLOSED_DESC, rc);
+        break;
+        default:
+            ACR_THROW_IO_IF_ERR(rc);
+        break;
+    }
+    return -1;
+}
+
+ACR_IO_EXPORT_DECLARE(jlong, FileSystemIo, write8)(ACR_JNISTDARGS,
+                                                   jint file,
+                                                   jobject ptr,
+                                                   jlong off,
+                                                   jlong len,
+                                                   jlong pos)
+{
+    size_t  pl;
+    size_t  po = (size_t)off;
+    size_t  cs = (size_t)len;
+    jbyte  *pb;
+    ssize_t wr;
+    int     rc = 0;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_INVALID_DESC, ACR_EFTYPE);
+        return -1;
+    }
+    if (IS_INVALID_HANDLE(f)) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_CLOSED_DESC, ACR_EBADF);
+        return -1;
+    }
+    pb = (jbyte *)ACR_PointerGet(_E, ptr, &pl);
+    if (!pb) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
+        return -1;
+    }
+    if ((po + cs) > pl) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+        return -1;
+    }
+    wr = r_pwrite(f->fd, pb + po, cs, (off_t)pos);
+    if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
+        f->timeout != 0) {
+        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+            wr = r_pwrite(f->fd, pb + po, cs, (off_t)pos);
+        }
+    }
+    if (wr < 0) {
+        rc = rc ? rc : ACR_GET_OS_ERROR();
+        if (ACR_STATUS_IS_EAGAIN(rc))
+            rc = ACR_TIMEUP;
+    }
+    if (wr > 0)
+        return (jlong)wr;
+
+    ACR_DescriptorSetErrEx(_E, f->descriptor, rc);
+    switch (rc) {
+        case 0:
+        case ACR_TIMEUP:
+            return 0;
+        break;
+        case EBADF:
+            ACR_THROW_EX_IF_ERR(ACR_EX_ACLOSED_DESC, rc);
+        break;
+        default:
+            ACR_THROW_IO_IF_ERR(rc);
+        break;
+    }
+    return -1;
+}
+
+ACR_IO_EXPORT_DECLARE(jint, FileSystemIo, write9)(ACR_JNISTDARGS,
+                                                  jint file,
+                                                  jobject dbb,
+                                                  jlong off,
+                                                  jlong len,
+                                                  jlong pos)
+{
+#if defined(_DEBUG)
+    size_t  pl;
+#endif
+    size_t  po = (size_t)off;
+    size_t  cs = (size_t)len;
+    jbyte  *pb;
+    ssize_t wr;
+    int     rc = 0;
+    acr_file_t *f = (acr_file_t *)ACR_IOH_FDATA(file);
+
+    if (ACR_IOH_FTYPE(file) != ACR_DT_FILE) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_INVALID_DESC, ACR_EFTYPE);
+        return -1;
+    }
+    if (IS_INVALID_HANDLE(f)) {
+        ACR_THROW_EX_IF_ERR(ACR_EX_CLOSED_DESC, ACR_EBADF);
+        return -1;
+    }
+    pb = (jbyte *)(*_E)->GetDirectBufferAddress(_E, dbb);
+    if (!pb) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
+        return -1;
+    }
+#if defined(_DEBUG)
+    pl = (size_t)(*_E)->GetDirectBufferCapacity(_E, dbb);
+    if ((po + cs) > pl) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+        return -1;
+    }
+#endif
+    wr = r_pwrite(f->fd, pb + po, cs, (off_t)pos);
+    if (wr == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
+        f->timeout != 0) {
+        if ((rc = wait_for_io_or_timeout(f, 0)) == 0) {
+            wr = r_pwrite(f->fd, pb + po, cs, (off_t)pos);
+        }
+    }
+    if (wr < 0) {
+        rc = rc ? rc : ACR_GET_OS_ERROR();
+        if (ACR_STATUS_IS_EAGAIN(rc))
+            rc = ACR_TIMEUP;
+    }
+    if (wr > 0)
+        return (jint)wr;
+
+    ACR_DescriptorSetErrEx(_E, f->descriptor, rc);
+    switch (rc) {
+        case 0:
+        case ACR_TIMEUP:
+            return 0;
+        break;
+        case EBADF:
+            ACR_THROW_EX_IF_ERR(ACR_EX_ACLOSED_DESC, rc);
+        break;
+        default:
+            ACR_THROW_IO_IF_ERR(rc);
+        break;
+    }
+    return -1;
+}
+
 ACR_IO_EXPORT_DECLARE(jint, FileSystemIo, fullw0)(ACR_JNISTDARGS,
                                                   jint file,
                                                   jbyteArray buf,
@@ -996,3 +1523,4 @@
     }
     return -1;
 }
+



Mime
View raw message