Merge "Fix data corruption when writing to Bluetooth socket"

This commit is contained in:
Brad Fitzpatrick
2011-04-04 10:49:05 -07:00
committed by Android Code Review

View File

@@ -448,7 +448,7 @@ static jint writeNative(JNIEnv *env, jobject obj, jbyteArray jb, jint offset,
#ifdef HAVE_BLUETOOTH #ifdef HAVE_BLUETOOTH
LOGV(__FUNCTION__); LOGV(__FUNCTION__);
int ret; int ret, total;
jbyte *b; jbyte *b;
int sz; int sz;
struct asocket *s = get_socketData(env, obj); struct asocket *s = get_socketData(env, obj);
@@ -471,15 +471,21 @@ static jint writeNative(JNIEnv *env, jobject obj, jbyteArray jb, jint offset,
return -1; return -1;
} }
ret = asocket_write(s, &b[offset], length, -1); total = 0;
if (ret < 0) { while (length > 0) {
jniThrowIOException(env, errno); ret = asocket_write(s, &b[offset], length, -1);
env->ReleaseByteArrayElements(jb, b, JNI_ABORT); if (ret < 0) {
return -1; jniThrowIOException(env, errno);
env->ReleaseByteArrayElements(jb, b, JNI_ABORT);
return -1;
}
offset += ret;
total += ret;
length -= ret;
} }
env->ReleaseByteArrayElements(jb, b, JNI_ABORT); // no need to commit env->ReleaseByteArrayElements(jb, b, JNI_ABORT); // no need to commit
return (jint)ret; return (jint)total;
#endif #endif
jniThrowIOException(env, ENOSYS); jniThrowIOException(env, ENOSYS);