Merge "Tidy up exception throwing in the media native code."

This commit is contained in:
Elliott Hughes
2011-04-08 18:37:01 -07:00
committed by Android (Google) Code Review
6 changed files with 27 additions and 72 deletions

View File

@@ -49,24 +49,11 @@ struct GsmAmrEncoderState {
int32_t mLastModeUsed;
};
//
// helper function to throw an exception
//
static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data) {
if (jclass cls = env->FindClass(ex)) {
char msg[128];
sprintf(msg, fmt, data);
env->ThrowNew(cls, msg);
env->DeleteLocalRef(cls);
}
}
static jint android_media_AmrInputStream_GsmAmrEncoderNew
(JNIEnv *env, jclass clazz) {
GsmAmrEncoderState* gae = new GsmAmrEncoderState();
if (gae == NULL) {
throwException(env, "java/lang/RuntimeException",
"Out of memory", 0);
jniThrowRuntimeException(env, "Out of memory");
}
return (jint)gae;
}
@@ -76,7 +63,7 @@ static void android_media_AmrInputStream_GsmAmrEncoderInitialize
GsmAmrEncoderState *state = (GsmAmrEncoderState *) gae;
int32_t nResult = AMREncodeInit(&state->mEncState, &state->mSidState, false);
if (nResult != OK) {
throwException(env, "java/lang/IllegalArgumentException",
jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
"GsmAmrEncoder initialization failed %d", nResult);
}
}
@@ -97,7 +84,7 @@ static jint android_media_AmrInputStream_GsmAmrEncoderEncode
(Frame_Type_3GPP*) &state->mLastModeUsed,
AMR_TX_WMF);
if (length < 0) {
throwException(env, "java/io/IOException",
jniThrowExceptionFmt(env, "java/io/IOException",
"Failed to encode a frame with error code: %d", length);
return -1;
}
@@ -148,5 +135,3 @@ int register_android_media_AmrInputStream(JNIEnv *env)
return AndroidRuntime::registerNativeMethods(env,
kClassPathName, gMethods, NELEM(gMethods));
}

View File

@@ -152,7 +152,7 @@ static void android_media_MediaRecorder_setCamera(JNIEnv* env, jobject thiz, job
{
// we should not pass a null camera to get_native_camera() call.
if (camera == NULL) {
jniThrowException(env, "java/lang/NullPointerException", "camera object is a NULL pointer");
jniThrowNullPointerException(env, "camera object is a NULL pointer");
return;
}
sp<Camera> c = get_native_camera(env, camera, NULL);
@@ -505,5 +505,3 @@ int register_android_media_MediaRecorder(JNIEnv *env)
return AndroidRuntime::registerNativeMethods(env,
"android/media/MediaRecorder", gMethods, NELEM(gMethods));
}

View File

@@ -36,19 +36,6 @@
using namespace android;
//
// helper function to throw an exception
//
static void throwException(JNIEnv *env, const char* ex, const char* fmt, int data) {
if (jclass cls = env->FindClass(ex)) {
char msg[1000];
sprintf(msg, fmt, data);
env->ThrowNew(cls, msg);
env->DeleteLocalRef(cls);
}
}
#define FIR_COEF(coef) (short)(0x10000 * coef)
static const short fir21[] = {
FIR_COEF(-0.006965742326),
@@ -90,18 +77,18 @@ static void android_media_ResampleInputStream_fir21(JNIEnv *env, jclass clazz,
jbyteArray jIn, jint jInOffset,
jbyteArray jOut, jint jOutOffset,
jint jNpoints) {
// safety first!
if (nFir21 + jNpoints * 2 > BUF_SIZE) {
throwException(env, "java/lang/IllegalArgumentException",
jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
"FIR+data too long %d", nFir21 + jNpoints);
return;
}
// get input data
short in[BUF_SIZE];
env->GetByteArrayRegion(jIn, jInOffset, (jNpoints * 2 + nFir21 - 1) * 2, (jbyte*)in);
// compute filter
short out[BUF_SIZE];
for (int i = 0; i < jNpoints; i++) {
@@ -132,5 +119,3 @@ int register_android_media_ResampleInputStream(JNIEnv *env)
return AndroidRuntime::registerNativeMethods(env,
kClassPathName, gMethods, NELEM(gMethods));
}

View File

@@ -182,28 +182,23 @@ MyMtpDatabase::MyMtpDatabase(JNIEnv *env, jobject client)
mLongBuffer(NULL),
mStringBuffer(NULL)
{
jintArray intArray;
jlongArray longArray;
jcharArray charArray;
// create buffers for out arguments
// we don't need to be thread-safe so this is OK
intArray = env->NewIntArray(3);
if (!intArray)
goto out_of_memory;
jintArray intArray = env->NewIntArray(3);
if (!intArray) {
return; // Already threw.
}
mIntBuffer = (jintArray)env->NewGlobalRef(intArray);
longArray = env->NewLongArray(2);
if (!longArray)
goto out_of_memory;
jlongArray longArray = env->NewLongArray(2);
if (!longArray) {
return; // Already threw.
}
mLongBuffer = (jlongArray)env->NewGlobalRef(longArray);
charArray = env->NewCharArray(256);
if (!charArray)
goto out_of_memory;
jcharArray charArray = env->NewCharArray(256);
if (!charArray) {
return; // Already threw.
}
mStringBuffer = (jcharArray)env->NewGlobalRef(charArray);
return;
out_of_memory:
env->ThrowNew(env->FindClass("java/lang/OutOfMemoryError"), NULL);
}
void MyMtpDatabase::cleanup(JNIEnv *env) {
@@ -823,7 +818,7 @@ MtpResponseCode MyMtpDatabase::getObjectFilePath(MtpObjectHandle handle,
outFileLength = longValues[0];
outFormat = longValues[1];
env->ReleaseLongArrayElements(mLongBuffer, longValues, 0);
checkAndClearExceptionFromCallback(env, __FUNCTION__);
return result;
}

View File

@@ -72,6 +72,7 @@ LOCAL_C_INCLUDES := \
LOCAL_SHARED_LIBRARIES := libdrm1 \
libnativehelper \
libutils \
libcutils

View File

@@ -24,6 +24,7 @@
#include <jni/drm1_jni.h>
#include <objmng/svc_drm.h>
#include "log.h"
#include "JNIHelp.h"
#define MS_PER_SECOND 1000 /* Milliseconds per second */
@@ -659,23 +660,13 @@ Java_android_drm_mobile1_DrmRawContent_nativeReadContent
jfieldID field;
if (NULL == buf) {
jclass newExcCls = (*env)->FindClass(env, "java/lang/NullPointerException");
if (newExcCls == NULL)
/* Unable to find the exception class, give up. */
return JNI_DRM_FAILURE;
(*env)->ThrowNew(env, newExcCls, "b is null");
jniThrowNullPointerException(env, "b == null");
return JNI_DRM_FAILURE;
}
if (len < 0 || bufOff < 0 || len + bufOff > (*env)->GetArrayLength(env, buf)) {
jclass newExcCls = (*env)->FindClass(env, "java/lang/IndexOutOfBoundsException");
if (newExcCls == NULL)
/* Unable to find the exception class, give up. */
return JNI_DRM_FAILURE;
(*env)->ThrowNew(env, newExcCls, NULL);
jniThrowException(env, "java/lang/IndexOutOfBoundsException", NULL);
return JNI_DRM_FAILURE;
}
if (mediaOff < 0 || len == 0)