[ 
https://issues.apache.org/jira/browse/HDFS-923?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12805809#action_12805809
 ] 

Ruyue Ma commented on HDFS-923:
-------------------------------

The latest modification of hdfsRead api impl:

{noformat}

tSize hdfsRead(hdfsFS fs, hdfsFile f, void* buffer, tSize length)
{
    // JAVA EQUIVALENT:
    //  byte [] bR = new byte[length];
    //  fis.read(bR);

    //Get the JNIEnv* corresponding to current thread
    JNIEnv* env = getJNIEnv();
    if (env == NULL) {
      errno = EINTERNAL;
      return -1;
    }

    //Parameters
    jobject jInputStream = (jobject)(f ? f->file : NULL);

    jbyteArray jbRarray;
    jint noReadBytes = 0;
    jvalue jVal;
    jthrowable jExc = NULL;

    int hasReadBytes = 0;

    //Sanity check
    if (!f || f->type == UNINITIALIZED) {
        errno = EBADF;
        return -1;
    }

    //Error checking... make sure that this file is 'readable'
    if (f->type != INPUT) {
        fprintf(stderr, "Cannot read from a non-InputStream object!\n");
        errno = EINVAL;
        return -1;
    }

     
/////////////////////////////////////////////////////////////////////////////////////////////////
    // > OUR MODIFICATION
    int exception = 0;
    jbRarray = (*env)->NewByteArray(env, length);
    while (hasReadBytes < length) {
        if (invokeMethod(env, &jVal, &jExc, INSTANCE, jInputStream, 
HADOOP_ISTRM,
                         "read", JMETHOD3("[B", "I", "I", "I") , jbRarray, 
hasReadBytes, length-hasReadBytes) != 0) {
            errno = errnoFromException(jExc, env, "org.apache.hadoop.fs."
                                       "FSDataInputStream::read");
            exception = 1;
            break;
        }
        else {
            noReadBytes = jVal.i;
            if (noReadBytes >= 0) {
                (*env)->GetByteArrayRegion(env, jbRarray, 0, noReadBytes, 
buffer+hasReadBytes);
                hasReadBytes += noReadBytes;
            }  else {
                //This is a valid case: there aren't any bytes left to read!
                break;
            }
            errno = 0;
        }


    
///////////////////////////////////////////////////////////////////////////////////////

    }

    destroyLocalReference(env, jbRarray);
    if (exception == 1) return -1;
    return hasReadBytes;
     // > OUR MODIFICATION
}
{noformat}

> libhdfs hdfs_read example uses hdfsRead wrongly
> -----------------------------------------------
>
>                 Key: HDFS-923
>                 URL: https://issues.apache.org/jira/browse/HDFS-923
>             Project: Hadoop HDFS
>          Issue Type: Bug
>          Components: contrib/libhdfs
>    Affects Versions: 0.20.1
>            Reporter: Ruyue Ma
>            Assignee: Ruyue Ma
>             Fix For: 0.21.0
>
>
> In the examples of libhdfs,  the hdfs_read.c uses hdfsRead wrongly. 
> {noformat}
>     // read from the file
>     tSize curSize = bufferSize;
>     for (; curSize == bufferSize;) {
>         curSize = hdfsRead(fs, readFile, (void*)buffer, curSize);
>     }
> {noformat} 
> the condition curSize == bufferSize has problem.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to