diff options
-rw-r--r-- | include/ScopedPrimitiveArray.h | 85 | ||||
-rw-r--r-- | luni/src/main/native/BidiWrapper.cpp | 4 | ||||
-rw-r--r-- | luni/src/main/native/NativeConverter.cpp | 8 | ||||
-rw-r--r-- | luni/src/main/native/NativeRegEx.cpp | 20 | ||||
-rw-r--r-- | luni/src/main/native/java_io_File.cpp | 34 | ||||
-rw-r--r-- | luni/src/main/native/java_util_zip_Adler32.cpp | 2 | ||||
-rw-r--r-- | luni/src/main/native/java_util_zip_CRC32.cpp | 2 | ||||
-rw-r--r-- | luni/src/main/native/java_util_zip_Deflater.cpp | 2 | ||||
-rw-r--r-- | luni/src/main/native/java_util_zip_Inflater.cpp | 2 | ||||
-rw-r--r-- | luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp | 18 | ||||
-rw-r--r-- | luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp | 40 | ||||
-rw-r--r-- | luni/src/main/native/org_apache_harmony_luni_util_NumberConvert.cpp | 6 | ||||
-rw-r--r-- | luni/src/main/native/org_apache_harmony_xml_ExpatParser.cpp | 17 | ||||
-rw-r--r-- | luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp | 40 | ||||
-rw-r--r-- | openssl/src/main/native/BNInterface.cpp | 21 |
15 files changed, 152 insertions, 149 deletions
diff --git a/include/ScopedPrimitiveArray.h b/include/ScopedPrimitiveArray.h index b3b16e1..b6dfd95 100644 --- a/include/ScopedPrimitiveArray.h +++ b/include/ScopedPrimitiveArray.h @@ -19,47 +19,82 @@ #include "JNIHelp.h" -// ScopedBooleanArray, ScopedByteArray, ScopedCharArray, ScopedDoubleArray, ScopedFloatArray, -// ScopedIntArray, ScopedLongArray, and ScopedShortArray provide convenient read-only access to -// Java arrays from JNI code. -#define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(PRIMITIVE_TYPE, NAME) \ - class Scoped ## NAME ## Array { \ +// ScopedBooleanArrayRO, ScopedByteArrayRO, ScopedCharArrayRO, ScopedDoubleArrayRO, +// ScopedFloatArrayRO, ScopedIntArrayRO, ScopedLongArrayRO, and ScopedShortArrayRO provide +// convenient read-only access to Java arrays from JNI code. This is cheaper than read-write +// access and should be used by default. +#define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(PRIMITIVE_TYPE, NAME) \ + class Scoped ## NAME ## ArrayRO { \ public: \ - Scoped ## NAME ## Array(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \ + Scoped ## NAME ## ArrayRO(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \ : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \ mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ } \ - ~Scoped ## NAME ## Array() { \ + ~Scoped ## NAME ## ArrayRO() { \ if (mRawArray) { \ mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, JNI_ABORT); \ } \ } \ - const PRIMITIVE_TYPE* get() const { \ - return mRawArray; \ - } \ - const PRIMITIVE_TYPE& operator[](size_t n) const { \ - return mRawArray[n]; \ + const PRIMITIVE_TYPE* get() const { return mRawArray; } \ + const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \ + size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \ + private: \ + JNIEnv* mEnv; \ + PRIMITIVE_TYPE ## Array mJavaArray; \ + PRIMITIVE_TYPE* mRawArray; \ + Scoped ## NAME ## ArrayRO(const Scoped ## NAME ## ArrayRO&); \ + void operator=(const Scoped ## NAME ## ArrayRO&); \ + } + +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jboolean, Boolean); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jbyte, Byte); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jchar, Char); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jdouble, Double); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jfloat, Float); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jint, Int); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jlong, Long); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jshort, Short); + +#undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO + +// ScopedBooleanArrayRW, ScopedByteArrayRW, ScopedCharArrayRW, ScopedDoubleArrayRW, +// ScopedFloatArrayRW, ScopedIntArrayRW, ScopedLongArrayRW, and ScopedShortArrayRW provide +// convenient read-write access to Java arrays from JNI code. These are more expensive, +// since they entail a copy back onto the Java heap, and should only be used when necessary. +#define INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(PRIMITIVE_TYPE, NAME) \ + class Scoped ## NAME ## ArrayRW { \ + public: \ + Scoped ## NAME ## ArrayRW(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \ + : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \ + mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ } \ - size_t size() const { \ - return mEnv->GetArrayLength(mJavaArray); \ + ~Scoped ## NAME ## ArrayRW() { \ + if (mRawArray) { \ + mEnv->Release ## NAME ## ArrayElements(mJavaArray, mRawArray, 0); \ + } \ } \ + const PRIMITIVE_TYPE* get() const { return mRawArray; } \ + const PRIMITIVE_TYPE& operator[](size_t n) const { return mRawArray[n]; } \ + PRIMITIVE_TYPE* get() { return mRawArray; } \ + PRIMITIVE_TYPE& operator[](size_t n) { return mRawArray[n]; } \ + size_t size() const { return mEnv->GetArrayLength(mJavaArray); } \ private: \ JNIEnv* mEnv; \ PRIMITIVE_TYPE ## Array mJavaArray; \ PRIMITIVE_TYPE* mRawArray; \ - Scoped ## NAME ## Array(const Scoped ## NAME ## Array&); \ - void operator=(const Scoped ## NAME ## Array&); \ + Scoped ## NAME ## ArrayRW(const Scoped ## NAME ## ArrayRW&); \ + void operator=(const Scoped ## NAME ## ArrayRW&); \ } -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jboolean, Boolean); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jbyte, Byte); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jchar, Char); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jdouble, Double); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jfloat, Float); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jint, Int); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jlong, Long); -INSTANTIATE_SCOPED_PRIMITIVE_ARRAY(jshort, Short); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jboolean, Boolean); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jbyte, Byte); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jchar, Char); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jdouble, Double); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jfloat, Float); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jint, Int); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jlong, Long); +INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jshort, Short); -#undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY +#undef INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW #endif // SCOPED_PRIMITIVE_ARRAY_H_included diff --git a/luni/src/main/native/BidiWrapper.cpp b/luni/src/main/native/BidiWrapper.cpp index 2811bc7..59dcfaa 100644 --- a/luni/src/main/native/BidiWrapper.cpp +++ b/luni/src/main/native/BidiWrapper.cpp @@ -80,7 +80,7 @@ static void BidiWrapper_ubidi_setPara(JNIEnv* env, jclass, jlong ptr, jcharArray data->setEmbeddingLevels(NULL); } UErrorCode err = U_ZERO_ERROR; - ScopedCharArray chars(env, text); + ScopedCharArrayRO chars(env, text); ubidi_setPara(data->uBiDi(), chars.get(), length, paraLevel, data->embeddingLevels(), &err); icu4jni_error(env, err); } @@ -151,7 +151,7 @@ static jobjectArray BidiWrapper_ubidi_getRuns(JNIEnv* env, jclass, jlong ptr) { } static jintArray BidiWrapper_ubidi_reorderVisual(JNIEnv* env, jclass, jbyteArray javaLevels, jint length) { - ScopedByteArray levelBytes(env, javaLevels); + ScopedByteArrayRO levelBytes(env, javaLevels); const UBiDiLevel* levels = reinterpret_cast<const UBiDiLevel*>(levelBytes.get()); UniquePtr<int[]> indexMap(new int[length]); diff --git a/luni/src/main/native/NativeConverter.cpp b/luni/src/main/native/NativeConverter.cpp index 2c65e74..5683020 100644 --- a/luni/src/main/native/NativeConverter.cpp +++ b/luni/src/main/native/NativeConverter.cpp @@ -98,7 +98,7 @@ static UErrorCode convertCharsToBytes(JNIEnv* env, jclass, jlong handle, jcharA if (!cnv) { return U_ILLEGAL_ARGUMENT_ERROR; } - ScopedCharArray uSource(env, source); + ScopedCharArrayRO uSource(env, source); UErrorCode errorCode = U_ZERO_ERROR; jint* myData = (jint*) env->GetPrimitiveArrayCritical(data, NULL); if (myData) { @@ -172,7 +172,7 @@ static UErrorCode convertBytesToChars(JNIEnv* env, jclass, jlong handle, jbyteAr UErrorCode errorCode =U_ZERO_ERROR; UConverter* cnv = (UConverter*)handle; if (cnv) { - ScopedByteArray uSource(env, source); + ScopedByteArrayRO uSource(env, source); jint* myData = (jint*) env->GetPrimitiveArrayCritical(data,NULL); if(myData) { jint* sourceOffset = &myData[0]; @@ -552,7 +552,7 @@ static jint setCallbackEncode(JNIEnv* env, jclass, jlong handle, jint onMalforme } fromUNewContext->onMalformedInput = getFromUCallback(onMalformedInput); fromUNewContext->onUnmappableInput = getFromUCallback(onUnmappableInput); - ScopedByteArray sub(env, subBytes); + ScopedByteArrayRO sub(env, subBytes); if (sub.get() == NULL) { return U_ILLEGAL_ARGUMENT_ERROR; } @@ -637,7 +637,7 @@ static jint setCallbackDecode(JNIEnv* env, jclass, jlong handle, jint onMalforme } toUNewContext->onMalformedInput = getToUCallback(onMalformedInput); toUNewContext->onUnmappableInput = getToUCallback(onUnmappableInput); - ScopedCharArray sub(env, subChars); + ScopedCharArrayRO sub(env, subChars); if (sub.get() == NULL) { return U_ILLEGAL_ARGUMENT_ERROR; } diff --git a/luni/src/main/native/NativeRegEx.cpp b/luni/src/main/native/NativeRegEx.cpp index 513dc41..d85270d 100644 --- a/luni/src/main/native/NativeRegEx.cpp +++ b/luni/src/main/native/NativeRegEx.cpp @@ -20,6 +20,7 @@ #include <stdio.h> #include <string.h> +#include "ScopedPrimitiveArray.h" #include "unicode/uregex.h" #include "unicode/utypes.h" #include "unicode/parseerr.h" @@ -208,29 +209,20 @@ static jint groupCount(JNIEnv* env, jclass, RegExData* data) return result; } -static void startEnd(JNIEnv* env, jclass, RegExData* data, - jintArray offsets) -{ +static void startEnd(JNIEnv* env, jclass, RegExData* data, jintArray javaOffsets) { UErrorCode status = U_ZERO_ERROR; - - jint * offsetsRaw = env->GetIntArrayElements(offsets, NULL); - + ScopedIntArrayRW offsets(env, javaOffsets); int groupCount = uregex_groupCount(data->regex, &status); for (int i = 0; i <= groupCount && U_SUCCESS(status); i++) { - offsetsRaw[2 * i + 0] = uregex_start(data->regex, i, &status); - offsetsRaw[2 * i + 1] = uregex_end(data->regex, i, &status); + offsets[2 * i + 0] = uregex_start(data->regex, i, &status); + offsets[2 * i + 1] = uregex_end(data->regex, i, &status); } - - env->ReleaseIntArrayElements(offsets, offsetsRaw, 0); - if (!U_SUCCESS(status)) { throwRuntimeException(env, status); } } -static void setRegion(JNIEnv* env, jclass, RegExData* data, jint start, - jint end) -{ +static void setRegion(JNIEnv* env, jclass, RegExData* data, jint start, jint end) { UErrorCode status = U_ZERO_ERROR; uregex_setRegion(data->regex, start, end, &status); if (!U_SUCCESS(status)) { diff --git a/luni/src/main/native/java_io_File.cpp b/luni/src/main/native/java_io_File.cpp index 58905d7..e981f56 100644 --- a/luni/src/main/native/java_io_File.cpp +++ b/luni/src/main/native/java_io_File.cpp @@ -38,12 +38,12 @@ // poor choices of where to divide the work between Java and native // code. -static const char* toPath(const ScopedByteArray& path) { +static const char* toPath(const ScopedByteArrayRO& path) { return reinterpret_cast<const char*>(&path[0]); } static jbyteArray java_io_File_getCanonImpl(JNIEnv* env, jobject, jbyteArray pathBytes) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); // The only thing this native code currently does is truncate the byte[] at // the first NUL. // TODO: this is completely pointless. we should do this in Java, or do all of getCanonicalPath in native code. (realpath(2)?) @@ -54,12 +54,12 @@ static jbyteArray java_io_File_getCanonImpl(JNIEnv* env, jobject, jbyteArray pat } static jboolean java_io_File_deleteImpl(JNIEnv* env, jobject, jbyteArray pathBytes) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); return (remove(toPath(path)) == 0); } static bool doStat(JNIEnv* env, jbyteArray pathBytes, struct stat& sb) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); return (stat(toPath(path), &sb) == 0); } @@ -106,27 +106,27 @@ static jboolean java_io_File_isFileImpl(JNIEnv* env, jobject, jbyteArray pathByt } static jboolean java_io_File_existsImpl(JNIEnv* env, jobject, jbyteArray pathBytes) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); return (access(toPath(path), F_OK) == 0); } static jboolean java_io_File_canExecuteImpl(JNIEnv* env, jobject, jbyteArray pathBytes) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); return (access(toPath(path), X_OK) == 0); } static jboolean java_io_File_canReadImpl(JNIEnv* env, jobject, jbyteArray pathBytes) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); return (access(toPath(path), R_OK) == 0); } static jboolean java_io_File_canWriteImpl(JNIEnv* env, jobject, jbyteArray pathBytes) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); return (access(toPath(path), W_OK) == 0); } static jbyteArray java_io_File_getLinkImpl(JNIEnv* env, jobject, jbyteArray pathBytes) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); // We can't know how big a buffer readlink(2) will need, so we need to // loop until it says "that fit". @@ -153,7 +153,7 @@ static jbyteArray java_io_File_getLinkImpl(JNIEnv* env, jobject, jbyteArray path } static jboolean java_io_File_setLastModifiedImpl(JNIEnv* env, jobject, jbyteArray pathBytes, jlong ms) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); // We want to preserve the access time. struct stat sb; @@ -169,7 +169,7 @@ static jboolean java_io_File_setLastModifiedImpl(JNIEnv* env, jobject, jbyteArra } static jboolean doChmod(JNIEnv* env, jbyteArray pathBytes, mode_t mask, bool set) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); struct stat sb; if (stat(toPath(path), &sb) == -1) { return JNI_FALSE; @@ -194,7 +194,7 @@ static jboolean java_io_File_setWritableImpl(JNIEnv* env, jobject, jbyteArray pa } static bool doStatFs(JNIEnv* env, jbyteArray pathBytes, struct statfs& sb) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); int rc = statfs(toPath(path), &sb); return (rc != -1); } @@ -327,7 +327,7 @@ private: // Reads the directory referred to by 'pathBytes', adding each directory entry // to 'entries'. static bool readDirectory(JNIEnv* env, jbyteArray pathBytes, DirEntries& entries) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); ScopedReaddir dir(toPath(path)); if (dir.isBad()) { return false; @@ -370,13 +370,13 @@ static jobjectArray java_io_File_listImpl(JNIEnv* env, jobject, jbyteArray pathB } static jboolean java_io_File_mkdirImpl(JNIEnv* env, jobject, jbyteArray pathBytes) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); // On Android, we don't want default permissions to allow global access. return (mkdir(toPath(path), S_IRWXU) == 0); } static jboolean java_io_File_createNewFileImpl(JNIEnv* env, jobject, jbyteArray pathBytes) { - ScopedByteArray path(env, pathBytes); + ScopedByteArrayRO path(env, pathBytes); // On Android, we don't want default permissions to allow global access. ScopedFd fd(open(toPath(path), O_CREAT | O_EXCL, 0600)); if (fd.get() != -1) { @@ -392,8 +392,8 @@ static jboolean java_io_File_createNewFileImpl(JNIEnv* env, jobject, jbyteArray } static jboolean java_io_File_renameToImpl(JNIEnv* env, jobject, jbyteArray oldPathBytes, jbyteArray newPathBytes) { - ScopedByteArray oldPath(env, oldPathBytes); - ScopedByteArray newPath(env, newPathBytes); + ScopedByteArrayRO oldPath(env, oldPathBytes); + ScopedByteArrayRO newPath(env, newPathBytes); return (rename(toPath(oldPath), toPath(newPath)) == 0); } diff --git a/luni/src/main/native/java_util_zip_Adler32.cpp b/luni/src/main/native/java_util_zip_Adler32.cpp index 3112600..d7b55be 100644 --- a/luni/src/main/native/java_util_zip_Adler32.cpp +++ b/luni/src/main/native/java_util_zip_Adler32.cpp @@ -21,7 +21,7 @@ #include "zlib.h" static jlong Adler32_updateImpl(JNIEnv* env, jobject, jbyteArray byteArray, int off, int len, jlong crc) { - ScopedByteArray bytes(env, byteArray); + ScopedByteArrayRO bytes(env, byteArray); if (bytes.get() == NULL) { jniThrowNullPointerException(env, NULL); return 0; diff --git a/luni/src/main/native/java_util_zip_CRC32.cpp b/luni/src/main/native/java_util_zip_CRC32.cpp index 3ee3273..596f553 100644 --- a/luni/src/main/native/java_util_zip_CRC32.cpp +++ b/luni/src/main/native/java_util_zip_CRC32.cpp @@ -21,7 +21,7 @@ #include "zlib.h" static jlong CRC32_updateImpl(JNIEnv* env, jobject, jbyteArray byteArray, int off, int len, jlong crc) { - ScopedByteArray bytes(env, byteArray); + ScopedByteArrayRO bytes(env, byteArray); if (bytes.get() == NULL) { jniThrowNullPointerException(env, NULL); return 0; diff --git a/luni/src/main/native/java_util_zip_Deflater.cpp b/luni/src/main/native/java_util_zip_Deflater.cpp index 35d3f6b..b264a6a 100644 --- a/luni/src/main/native/java_util_zip_Deflater.cpp +++ b/luni/src/main/native/java_util_zip_Deflater.cpp @@ -77,7 +77,7 @@ static jint Deflater_deflateImpl(JNIEnv* env, jobject recv, jbyteArray buf, int stream->stream.avail_out = len; jint sin = stream->stream.total_in; jint sout = stream->stream.total_out; - ScopedByteArray out(env, buf); + ScopedByteArrayRO out(env, buf); if (out.get() == NULL) { return -1; } diff --git a/luni/src/main/native/java_util_zip_Inflater.cpp b/luni/src/main/native/java_util_zip_Inflater.cpp index fcedc28..06b4f0a 100644 --- a/luni/src/main/native/java_util_zip_Inflater.cpp +++ b/luni/src/main/native/java_util_zip_Inflater.cpp @@ -76,7 +76,7 @@ static jint Inflater_inflateImpl(JNIEnv* env, jobject recv, jbyteArray buf, int stream->stream.avail_out = len; jint sin = stream->stream.total_in; jint sout = stream->stream.total_out; - ScopedByteArray out(env, buf); + ScopedByteArrayRO out(env, buf); if (out.get() == NULL) { jniThrowOutOfMemoryError(env, NULL); return -1; diff --git a/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp b/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp index 1ab8743..82bc0c6 100644 --- a/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp +++ b/luni/src/main/native/org_apache_harmony_luni_platform_OSFileSystem.cpp @@ -198,9 +198,9 @@ static iovec* initIoVec(JNIEnv* env, jniThrowException(env, "java/lang/OutOfMemoryError", "native heap"); return NULL; } - ScopedIntArray buffers(env, jBuffers); - ScopedIntArray offsets(env, jOffsets); - ScopedIntArray lengths(env, jLengths); + ScopedIntArrayRO buffers(env, jBuffers); + ScopedIntArrayRO offsets(env, jOffsets); + ScopedIntArrayRO lengths(env, jLengths); for (int i = 0; i < size; ++i) { vectors[i].iov_base = reinterpret_cast<void*>(buffers[i] + offsets[i]); vectors[i].iov_len = lengths[i]; @@ -291,16 +291,14 @@ static jlong harmony_io_readImpl(JNIEnv* env, jobject, jint fd, return 0; } - jbyte* bytes = env->GetByteArrayElements(byteArray, NULL); - jlong rc = TEMP_FAILURE_RETRY(read(fd, bytes + offset, nbytes)); - env->ReleaseByteArrayElements(byteArray, bytes, 0); + ScopedByteArrayRW bytes(env, byteArray); + jlong rc = TEMP_FAILURE_RETRY(read(fd, bytes.get() + offset, nbytes)); if (rc == 0) { return -1; } if (rc == -1) { if (errno == EAGAIN) { - jniThrowException(env, "java/io/InterruptedIOException", - "Read timed out"); + jniThrowException(env, "java/io/InterruptedIOException", "Read timed out"); } else { jniThrowIOException(env, errno); } @@ -311,7 +309,7 @@ static jlong harmony_io_readImpl(JNIEnv* env, jobject, jint fd, static jlong harmony_io_writeImpl(JNIEnv* env, jobject, jint fd, jbyteArray byteArray, jint offset, jint nbytes) { - ScopedByteArray bytes(env, byteArray); + ScopedByteArrayRO bytes(env, byteArray); jlong result = TEMP_FAILURE_RETRY(write(fd, bytes.get() + offset, nbytes)); if (result == -1) { if (errno == EAGAIN) { @@ -418,7 +416,7 @@ static jint harmony_io_openImpl(JNIEnv* env, jobject, jbyteArray pathByteArray, flags = EsTranslateOpenFlags(flags); - ScopedByteArray path(env, pathByteArray); + ScopedByteArrayRO path(env, pathByteArray); jint rc = TEMP_FAILURE_RETRY(open(reinterpret_cast<const char*>(&path[0]), flags, mode)); if (rc == -1) { // Get the human-readable form of errno. diff --git a/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp b/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp index 753635c..0bce6f7 100644 --- a/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp +++ b/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp @@ -1121,15 +1121,12 @@ static jint osNetworkSystem_readSocketImpl(JNIEnv* env, jclass, jint timeout) { // LOGD("ENTER readSocketImpl"); - jbyte* bytes = env->GetByteArrayElements(byteArray, NULL); - if (bytes == NULL) { + ScopedByteArrayRW bytes(env, byteArray); + if (bytes.get() == NULL) { return -1; } - jint address = - static_cast<jint>(reinterpret_cast<uintptr_t>(bytes + offset)); - int result = osNetworkSystem_readDirect(env, NULL, - fileDescriptor, address, count, timeout); - env->ReleaseByteArrayElements(byteArray, bytes, 0); + jint address = static_cast<jint>(reinterpret_cast<uintptr_t>(bytes.get() + offset)); + int result = osNetworkSystem_readDirect(env, NULL, fileDescriptor, address, count, timeout); return result; } @@ -1161,14 +1158,12 @@ static jint osNetworkSystem_writeDirect(JNIEnv* env, jobject, static jint osNetworkSystem_write(JNIEnv* env, jobject, jobject fileDescriptor, jbyteArray byteArray, jint offset, jint count) { - jbyte* bytes = env->GetByteArrayElements(byteArray, NULL); - if (bytes == NULL) { + ScopedByteArrayRW bytes(env, byteArray); + if (bytes.get() == NULL) { return -1; } - jint address = static_cast<jint>(reinterpret_cast<uintptr_t>(bytes)); - int result = osNetworkSystem_writeDirect(env, NULL, - fileDescriptor, address, offset, count); - env->ReleaseByteArrayElements(byteArray, bytes, 0); + jint address = static_cast<jint>(reinterpret_cast<uintptr_t>(bytes.get())); + int result = osNetworkSystem_writeDirect(env, NULL, fileDescriptor, address, offset, count); return result; } @@ -1199,8 +1194,8 @@ static jboolean osNetworkSystem_connectWithTimeout(JNIEnv* env, return JNI_FALSE; } - jbyte* contextBytes = env->GetByteArrayElements(passContext, NULL); - selectFDSet* context = reinterpret_cast<selectFDSet*>(contextBytes); + ScopedByteArrayRW contextBytes(env, passContext); + selectFDSet* context = reinterpret_cast<selectFDSet*>(contextBytes.get()); int result = 0; switch (step) { case SOCKET_CONNECT_STEP_START: @@ -1212,7 +1207,6 @@ static jboolean osNetworkSystem_connectWithTimeout(JNIEnv* env, default: assert(false); } - env->ReleaseByteArrayElements(passContext, contextBytes, 0); if (result == 0) { // Connected! @@ -1672,7 +1666,7 @@ static jint osNetworkSystem_sendDatagramDirect(JNIEnv* env, jobject, static jint osNetworkSystem_sendDatagram(JNIEnv* env, jobject, jobject fd, jbyteArray data, jint offset, jint length, jint port, jboolean bindToDevice, jint trafficClass, jobject inetAddress) { - ScopedByteArray bytes(env, data); + ScopedByteArrayRO bytes(env, data); return osNetworkSystem_sendDatagramDirect(env, NULL, fd, reinterpret_cast<uintptr_t>(bytes.get()), offset, length, port, bindToDevice, trafficClass, inetAddress); @@ -1703,7 +1697,7 @@ static jint osNetworkSystem_sendConnectedDatagramDirect(JNIEnv* env, static jint osNetworkSystem_sendConnectedDatagram(JNIEnv* env, jobject, jobject fd, jbyteArray data, jint offset, jint length, jboolean bindToDevice) { - ScopedByteArray bytes(env, data); + ScopedByteArrayRO bytes(env, data); return osNetworkSystem_sendConnectedDatagramDirect(env, NULL, fd, reinterpret_cast<uintptr_t>(bytes.get()), offset, length, bindToDevice); } @@ -1870,14 +1864,12 @@ static jboolean osNetworkSystem_selectImpl(JNIEnv* env, jclass, } // Translate the result into the int[] we're supposed to fill in. - jint* flagArray = env->GetIntArrayElements(outFlags, NULL); - if (flagArray == NULL) { + ScopedIntArrayRW flagArray(env, outFlags); + if (flagArray.get() == NULL) { return JNI_FALSE; } - bool okay = translateFdSet(env, readFDArray, countReadC, readFds, flagArray, 0, SOCKET_OP_READ) && - translateFdSet(env, writeFDArray, countWriteC, writeFds, flagArray, countReadC, SOCKET_OP_WRITE); - env->ReleaseIntArrayElements(outFlags, flagArray, 0); - return okay; + return translateFdSet(env, readFDArray, countReadC, readFds, flagArray.get(), 0, SOCKET_OP_READ) && + translateFdSet(env, writeFDArray, countWriteC, writeFds, flagArray.get(), countReadC, SOCKET_OP_WRITE); } static jobject osNetworkSystem_getSocketLocalAddress(JNIEnv* env, diff --git a/luni/src/main/native/org_apache_harmony_luni_util_NumberConvert.cpp b/luni/src/main/native/org_apache_harmony_luni_util_NumberConvert.cpp index b6586ea..1b3ea75 100644 --- a/luni/src/main/native/org_apache_harmony_luni_util_NumberConvert.cpp +++ b/luni/src/main/native/org_apache_harmony_luni_util_NumberConvert.cpp @@ -20,6 +20,7 @@ #include <stdlib.h> #include "JNIHelp.h" +#include "ScopedPrimitiveArray.h" #include "cbigint.h" #if defined(__linux__) || defined(FREEBSD) @@ -78,7 +79,6 @@ Java_org_apache_harmony_luni_util_NumberConverter_bigIntDigitGeneratorInstImpl ( int high, low, i; jint k, firstK, U; jint getCount, setCount; - jint *uArray; jclass clazz; jfieldID fid; @@ -189,7 +189,7 @@ Java_org_apache_harmony_luni_util_NumberConverter_bigIntDigitGeneratorInstImpl ( clazz = env->GetObjectClass(inst); fid = env->GetFieldID(clazz, "uArray", "[I"); uArrayObject = (jintArray) env->GetObjectField(inst, fid); - uArray = env->GetIntArrayElements(uArrayObject, 0); + ScopedIntArrayRW uArray(env, uArrayObject); getCount = setCount = 0; do @@ -243,8 +243,6 @@ Java_org_apache_harmony_luni_util_NumberConverter_bigIntDigitGeneratorInstImpl ( else uArray[setCount++] = U + 1; - env->ReleaseIntArrayElements(uArrayObject, uArray, 0); - fid = env->GetFieldID(clazz, "setCount", "I"); env->SetIntField(inst, fid, setCount); diff --git a/luni/src/main/native/org_apache_harmony_xml_ExpatParser.cpp b/luni/src/main/native/org_apache_harmony_xml_ExpatParser.cpp index 9df3bfa..aaa052a 100644 --- a/luni/src/main/native/org_apache_harmony_xml_ExpatParser.cpp +++ b/luni/src/main/native/org_apache_harmony_xml_ExpatParser.cpp @@ -391,24 +391,17 @@ static jcharArray ensureCapacity(ParsingContext* parsingContext, int length) { * @param length of characters to copy (in bytes) * @returns number of UTF-16 characters which were copied */ -static size_t fillBuffer(ParsingContext* parsingContext, const char* characters, - int length) { +static size_t fillBuffer(ParsingContext* parsingContext, const char* characters, int length) { JNIEnv* env = parsingContext->env; // Grow buffer if necessary. jcharArray buffer = ensureCapacity(parsingContext, length); if (buffer == NULL) return -1; - // Get a native reference to our buffer. - jchar* nativeBuffer = env->GetCharArrayElements(buffer, NULL); - // Decode UTF-8 characters into our buffer. + ScopedCharArrayRW nativeBuffer(env, buffer); size_t utf16length; - strcpylen8to16((char16_t*) nativeBuffer, characters, length, &utf16length); - - // Release our native reference. - env->ReleaseCharArrayElements(buffer, nativeBuffer, 0); - + strcpylen8to16((char16_t*) nativeBuffer.get(), characters, length, &utf16length); return utf16length; } @@ -1064,14 +1057,14 @@ static void append(JNIEnv* env, jobject object, jint pointer, static void appendBytes(JNIEnv* env, jobject object, jint pointer, jbyteArray xml, jint byteOffset, jint byteCount) { - ScopedByteArray byteArray(env, xml); + ScopedByteArrayRO byteArray(env, xml); const char* bytes = reinterpret_cast<const char*>(byteArray.get()); append(env, object, pointer, bytes, byteOffset, byteCount, XML_FALSE); } static void appendCharacters(JNIEnv* env, jobject object, jint pointer, jcharArray xml, jint charOffset, jint charCount) { - ScopedCharArray charArray(env, xml); + ScopedCharArrayRO charArray(env, xml); const char* bytes = reinterpret_cast<const char*>(charArray.get()); size_t byteOffset = 2 * charOffset; size_t byteCount = 2 * charCount; diff --git a/luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp b/luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp index 787f899..7f52470 100644 --- a/luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp +++ b/luni/src/main/native/org_apache_harmony_xnet_provider_jsse_NativeCrypto.cpp @@ -359,7 +359,7 @@ static SSL_SESSION* to_SSL_SESSION(JNIEnv* env, int ssl_session_address, bool th static BIGNUM* arrayToBignum(JNIEnv* env, jbyteArray source) { // LOGD("Entering arrayToBignum()"); - ScopedByteArray sourceBytes(env, source); + ScopedByteArrayRO sourceBytes(env, source); return BN_bin2bn((unsigned char*) sourceBytes.get(), sourceBytes.size(), NULL); } @@ -576,9 +576,8 @@ static jint NativeCrypto_EVP_DigestFinal(JNIEnv* env, jclass, EVP_MD_CTX* ctx, int result = -1; - jbyte* hashBytes = env->GetByteArrayElements(hash, NULL); - EVP_DigestFinal(ctx, (unsigned char*) (hashBytes + offset), (unsigned int*)&result); - env->ReleaseByteArrayElements(hash, hashBytes, 0); + ScopedByteArrayRW hashBytes(env, hash); + EVP_DigestFinal(ctx, (unsigned char*) (hashBytes.get() + offset), (unsigned int*)&result); throwExceptionIfNecessary(env, "NativeCrypto_EVP_DigestFinal"); @@ -658,7 +657,7 @@ static void NativeCrypto_EVP_DigestUpdate(JNIEnv* env, jclass, EVP_MD_CTX* ctx, return; } - ScopedByteArray bufferBytes(env, buffer); + ScopedByteArrayRO bufferBytes(env, buffer); EVP_DigestUpdate(ctx, (unsigned char*) (bufferBytes.get() + offset), length); throwExceptionIfNecessary(env, "NativeCrypto_EVP_DigestUpdate"); @@ -701,7 +700,7 @@ static void NativeCrypto_EVP_VerifyUpdate(JNIEnv* env, jclass, EVP_MD_CTX* ctx, return; } - ScopedByteArray bufferBytes(env, buffer); + ScopedByteArrayRO bufferBytes(env, buffer); EVP_VerifyUpdate(ctx, (unsigned char*) (bufferBytes.get() + offset), length); throwExceptionIfNecessary(env, "NativeCrypto_EVP_VerifyUpdate"); @@ -719,7 +718,7 @@ static int NativeCrypto_EVP_VerifyFinal(JNIEnv* env, jclass, EVP_MD_CTX* ctx, jb return -1; } - ScopedByteArray bufferBytes(env, buffer); + ScopedByteArrayRO bufferBytes(env, buffer); int result = EVP_VerifyFinal(ctx, (unsigned char*) (bufferBytes.get() + offset), length, pkey); throwExceptionIfNecessary(env, "NativeCrypto_EVP_VerifyFinal"); @@ -814,10 +813,10 @@ static int NativeCrypto_verifysignature(JNIEnv* env, jclass, int result = -1; - ScopedByteArray msgBytes(env, msg); - ScopedByteArray sigBytes(env, sig); - ScopedByteArray modBytes(env, mod); - ScopedByteArray expBytes(env, exp); + ScopedByteArrayRO msgBytes(env, msg); + ScopedByteArrayRO sigBytes(env, sig); + ScopedByteArrayRO modBytes(env, mod); + ScopedByteArrayRO expBytes(env, exp); ScopedUtfChars algorithmChars(env, algorithm); JNI_TRACE("NativeCrypto_verifysignature algorithmChars=%s", algorithmChars.c_str()); @@ -846,7 +845,7 @@ static void NativeCrypto_RAND_seed(JNIEnv* env, jclass, jbyteArray seed) { jniThrowNullPointerException(env, "seed == null"); return; } - ScopedByteArray randseed(env, seed); + ScopedByteArrayRO randseed(env, seed); RAND_seed(randseed.get(), randseed.size()); } @@ -1665,7 +1664,7 @@ static jint NativeCrypto_SSL_new(JNIEnv* env, jclass, jint ssl_ctx_address) * Gets the bytes from a jbyteArray and stores them in a freshly-allocated BIO memory buffer. */ static BIO* jbyteArrayToMemBuf(JNIEnv* env, jbyteArray byteArray) { - ScopedByteArray buf(env, byteArray); + ScopedByteArrayRO buf(env, byteArray); Unique_BIO bio(BIO_new(BIO_s_mem())); if (bio.get() == NULL) { jniThrowRuntimeException(env, "BIO_new failed"); @@ -2358,13 +2357,11 @@ static jint NativeCrypto_SSL_read(JNIEnv* env, jclass, jint return 0; } - jbyte* bytes = env->GetByteArrayElements(dest, NULL); + ScopedByteArrayRW bytes(env, dest); int returnCode = 0; int sslErrorCode = SSL_ERROR_NONE;; - int ret = sslRead(env, ssl, (char*) (bytes + offset), len, &returnCode, &sslErrorCode, timeout); - - env->ReleaseByteArrayElements(dest, bytes, 0); + int ret = sslRead(env, ssl, (char*) (bytes.get() + offset), len, &returnCode, &sslErrorCode, timeout); int result; if (ret == THROW_EXCEPTION) { @@ -2543,7 +2540,7 @@ static void NativeCrypto_SSL_write(JNIEnv* env, jclass, return; } - ScopedByteArray bytes(env, dest); + ScopedByteArrayRO bytes(env, dest); int returnCode = 0; int sslErrorCode = SSL_ERROR_NONE; int ret = sslWrite(env, @@ -2807,10 +2804,9 @@ static jbyteArray NativeCrypto_i2d_SSL_SESSION(JNIEnv* env, jclass, jint ssl_ses jbyteArray bytes = env->NewByteArray(size); if (bytes != NULL) { - jbyte* tmp = env->GetByteArrayElements(bytes, NULL); - unsigned char* ucp = reinterpret_cast<unsigned char*>(tmp); + ScopedByteArrayRW tmp(env, bytes); + unsigned char* ucp = reinterpret_cast<unsigned char*>(tmp.get()); i2d_SSL_SESSION(ssl_session, &ucp); - env->ReleaseByteArrayElements(bytes, tmp, 0); } JNI_TRACE("ssl_session=%p NativeCrypto_i2d_SSL_SESSION => size=%d", ssl_session, size); @@ -2827,7 +2823,7 @@ static jint NativeCrypto_d2i_SSL_SESSION(JNIEnv* env, jclass, jbyteArray bytes, return 0; } - ScopedByteArray tmp(env, bytes); + ScopedByteArrayRO tmp(env, bytes); const unsigned char* ucp = reinterpret_cast<const unsigned char*>(tmp.get()); SSL_SESSION* ssl_session = d2i_SSL_SESSION(NULL, &ucp, size); diff --git a/openssl/src/main/native/BNInterface.cpp b/openssl/src/main/native/BNInterface.cpp index bf3195a..a40192c 100644 --- a/openssl/src/main/native/BNInterface.cpp +++ b/openssl/src/main/native/BNInterface.cpp @@ -178,7 +178,7 @@ static int NativeBN_BN_hex2bn(JNIEnv* env, jclass, BIGNUM* a, jstring str) { */ static jboolean NativeBN_BN_bin2bn(JNIEnv* env, jclass, jbyteArray arr, int len, jboolean neg, BIGNUM* ret) { if (!oneValidHandle(env, ret)) return JNI_FALSE; - ScopedByteArray bytes(env, arr); + ScopedByteArrayRO bytes(env, arr); if (bytes.get() == NULL) { return -1; } @@ -201,7 +201,7 @@ static jboolean NativeBN_litEndInts2bn(JNIEnv* env, jclass, jintArray arr, int l if (!oneValidHandle(env, ret)) return JNI_FALSE; bn_check_top(ret); if (len > 0) { - ScopedIntArray scopedArray(env, arr); + ScopedIntArrayRO scopedArray(env, arr); assert(sizeof(BN_ULONG) == sizeof(jint)); const BN_ULONG* tmpInts = reinterpret_cast<const BN_ULONG*>(scopedArray.get()); if ((tmpInts != NULL) && (bn_wexpand(ret, len) != NULL)) { @@ -287,7 +287,7 @@ static jboolean negBigEndianBytes2bn(JNIEnv*, jclass, const unsigned char* bytes */ static jboolean NativeBN_twosComp2bn(JNIEnv* env, jclass cls, jbyteArray arr, int bytesLen, BIGNUM* ret) { if (!oneValidHandle(env, ret)) return JNI_FALSE; - ScopedByteArray bytes(env, arr); + ScopedByteArrayRO bytes(env, arr); if (bytes.get() == NULL) { return -1; } @@ -384,12 +384,11 @@ static jbyteArray NativeBN_BN_bn2bin(JNIEnv* env, jclass, BIGNUM* a) { if (result == NULL) { return NULL; } - jbyte* bytes = env->GetByteArrayElements(result, NULL); - if (bytes == NULL) { + ScopedByteArrayRW bytes(env, result); + if (bytes.get() == NULL) { return NULL; } - BN_bn2bin(a, reinterpret_cast<unsigned char*>(bytes)); - env->ReleaseByteArrayElements(result, bytes, 0); + BN_bn2bin(a, reinterpret_cast<unsigned char*>(bytes.get())); return result; } @@ -404,12 +403,12 @@ static jintArray NativeBN_bn2litEndInts(JNIEnv* env, jclass, BIGNUM* a) { if (result == NULL) { return NULL; } - BN_ULONG* longs = reinterpret_cast<BN_ULONG*>(env->GetIntArrayElements(result, NULL)); - if (longs == NULL) { + ScopedIntArrayRW ints(env, result); + BN_ULONG* ulongs = reinterpret_cast<BN_ULONG*>(ints.get()); + if (ulongs == NULL) { return NULL; } - int i = len; do { i--; longs[i] = a->d[i]; } while (i > 0); - env->ReleaseIntArrayElements(result, reinterpret_cast<jint*>(longs), 0); + int i = len; do { i--; ulongs[i] = a->d[i]; } while (i > 0); return result; } |