Further exception cleanup

Use a new checked exception type (TJException) when passing through
errors from the underlying C library. This gives the application a
choice of catching all exceptions or just those from TurboJPEG.

Throw IllegalArgumentException at the JNI level when arguments to the
JNI function are incorrect, and when one of the TurboJPEG "utility"
functions returns an error (because, per the C API specification, those
functions will only return an error if one of their arguments is out of
range.)

Remove "throws Exception" from the signature of any methods that no
longer pass through an error from the TurboJPEG C library.

Credit Viktor for the new code

Code formatting tweaks
This commit is contained in:
DRC
2015-07-21 09:34:02 -05:00
parent 10dc5cfeff
commit 739edeb8a6
31 changed files with 663 additions and 305 deletions

View File

@@ -46,9 +46,7 @@
goto bailout; \
}
#define _throwio(msg) _throw(msg, "java/io/IOException")
#define _throwtj() _throw(tjGetErrorStr(), "java/lang/Exception")
#define _throwtj() _throw(tjGetErrorStr(), "org/libjpegturbo/turbojpeg/TJException")
#define _throwarg(msg) _throw(msg, "java/lang/IllegalArgumentException")
@@ -107,7 +105,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSize
(JNIEnv *env, jclass cls, jint width, jint height, jint jpegSubsamp)
{
jint retval=(jint)tjBufSize(width, height, jpegSubsamp);
if(retval==-1) _throwtj();
if(retval==-1) _throwarg(tjGetErrorStr());
bailout:
return retval;
@@ -118,7 +116,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_bufSizeYUV__IIII
(JNIEnv *env, jclass cls, jint width, jint pad, jint height, jint subsamp)
{
jint retval=(jint)tjBufSizeYUV2(width, pad, height, subsamp);
if(retval==-1) _throwtj();
if(retval==-1) _throwarg(tjGetErrorStr());
bailout:
return retval;
@@ -139,7 +137,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeSizeYUV__IIIII
{
jint retval=(jint)tjPlaneSizeYUV(componentID, width, stride, height,
subsamp);
if(retval==-1) _throwtj();
if(retval==-1) _throwarg(tjGetErrorStr());
bailout:
return retval;
@@ -150,7 +148,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeWidth__III
(JNIEnv *env, jclass cls, jint componentID, jint width, jint subsamp)
{
jint retval=(jint)tjPlaneWidth(componentID, width, subsamp);
if(retval==-1) _throwtj();
if(retval==-1) _throwarg(tjGetErrorStr());
bailout:
return retval;
@@ -161,7 +159,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJ_planeHeight__III
(JNIEnv *env, jclass cls, jint componentID, jint height, jint subsamp)
{
jint retval=(jint)tjPlaneHeight(componentID, height, subsamp);
if(retval==-1) _throwtj();
if(retval==-1) _throwarg(tjGetErrorStr());
bailout:
return retval;
@@ -323,7 +321,7 @@ JNIEXPORT jint JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_compressFrom
int pw=tjPlaneWidth(i, width, subsamp);
if(planeSize<0 || pw<0)
_throwtj();
_throwarg(tjGetErrorStr());
if(srcOffsets[i]<0)
_throwarg("Invalid argument in compressFromYUV()");
@@ -402,7 +400,7 @@ static void TJCompressor_encodeYUV
int pw=tjPlaneWidth(i, width, subsamp);
if(planeSize<0 || pw<0)
_throwtj();
_throwarg(tjGetErrorStr());
if(dstOffsets[i]<0)
_throwarg("Invalid argument in encodeYUV()");
@@ -537,7 +535,7 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy
gethandle();
if(tjDestroy(handle)==-1) _throwio(tjGetErrorStr());
if(tjDestroy(handle)==-1) _throwtj();
(*env)->SetLongField(env, obj, _fid, 0);
bailout:
@@ -572,7 +570,7 @@ JNIEXPORT jobjectArray JNICALL Java_org_libjpegturbo_turbojpeg_TJ_getScalingFact
jobjectArray sfjava=NULL;
if((sf=tjGetScalingFactors(&n))==NULL || n==0)
_throwtj();
_throwarg(tjGetErrorStr());
bailif0(sfcls=(*env)->FindClass(env, "org/libjpegturbo/turbojpeg/TJScalingFactor"));
bailif0(sfjava=(jobjectArray)(*env)->NewObjectArray(env, n, sfcls, 0));
@@ -751,7 +749,7 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress
if(height==0) height=jpegHeight;
sf=tjGetScalingFactors(&nsf);
if(!sf || nsf<1)
_throwtj();
_throwarg(tjGetErrorStr());
for(i=0; i<nsf; i++)
{
scaledWidth=TJSCALED(jpegWidth, sf[i]);
@@ -769,7 +767,7 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJDecompressor_decompress
int pw=tjPlaneWidth(i, scaledWidth, jpegSubsamp);
if(planeSize<0 || pw<0)
_throwtj();
_throwarg(tjGetErrorStr());
if(dstOffsets[i]<0)
_throwarg("Invalid argument in decompressToYUV()");
@@ -882,7 +880,7 @@ static void TJDecompressor_decodeYUV
int pw=tjPlaneWidth(i, width, subsamp);
if(planeSize<0 || pw<0)
_throwtj();
_throwarg(tjGetErrorStr());
if(srcOffsets[i]<0)
_throwarg("Invalid argument in decodeYUV()");