From 1a4778f8f00a6af5a7738a323f6b6d05cbc98c6c Mon Sep 17 00:00:00 2001 From: DRC Date: Tue, 7 Jul 2015 16:39:03 +0000 Subject: [PATCH] Allow TJCompressor and TJDecompressor to be used with a try-with-resources statement in Java 7 and later. git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@1594 632fc199-4ca6-4c93-a231-07263d6284db --- ChangeLog.txt | 4 ++++ .../libjpegturbo/turbojpeg/TJCompressor.html | 18 ++++++++++++++---- .../libjpegturbo/turbojpeg/TJDecompressor.html | 18 ++++++++++++++---- .../libjpegturbo/turbojpeg/TJTransformer.html | 4 ++++ .../libjpegturbo/turbojpeg/package-tree.html | 4 ++-- java/doc/overview-tree.html | 4 ++-- .../libjpegturbo/turbojpeg/TJCompressor.java | 7 ++++--- .../libjpegturbo/turbojpeg/TJDecompressor.java | 7 ++++--- turbojpeg-jni.c | 11 +++++++++-- 9 files changed, 57 insertions(+), 20 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index d9df904..f0c804e 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -11,6 +11,10 @@ use of AltiVec instructions. [2] Added a new libjpeg API function (jpeg_skip_scanlines()) that can be used to partially decode a JPEG image. See libjpeg.txt for more details. +[3] The TJCompressor and TJDecompressor classes in the TurboJPEG Java API now +implement the Closeable interface, so those classes can be used with a +try-with-resources statement. + 1.4.1 ===== diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html b/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html index b7fa3db..88f5644 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html @@ -90,10 +90,15 @@
@@ -828,10 +833,15 @@ public byte[] encodeYUV(java.awt.image.BufferedImage srcImage,
  • close

    public void close()
    -           throws java.lang.Exception
    + throws java.io.IOException
    Free the native structures associated with this compressor instance.
    -
    Throws:
    -
    java.lang.Exception
    +
    +
    Specified by:
    +
    close in interface java.io.Closeable
    +
    Specified by:
    +
    close in interface java.lang.AutoCloseable
    +
    Throws:
    +
    java.io.IOException
  • diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJDecompressor.html b/java/doc/org/libjpegturbo/turbojpeg/TJDecompressor.html index dc1dcbd..73859d5 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJDecompressor.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJDecompressor.html @@ -91,13 +91,18 @@ @@ -1162,10 +1167,15 @@ public byte[] decompressToYUV(int flags)
  • close

    public void close()
    -           throws java.lang.Exception
    + throws java.io.IOException
    Free the native structures associated with this decompressor instance.
    -
    Throws:
    -
    java.lang.Exception
    +
    +
    Specified by:
    +
    close in interface java.io.Closeable
    +
    Specified by:
    +
    close in interface java.lang.AutoCloseable
    +
    Throws:
    +
    java.io.IOException
  • diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJTransformer.html b/java/doc/org/libjpegturbo/turbojpeg/TJTransformer.html index 32c92bb..8366afd 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJTransformer.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJTransformer.html @@ -95,6 +95,10 @@
    • +
      +
      All Implemented Interfaces:
      +
      java.io.Closeable, java.lang.AutoCloseable
      +


      public class TJTransformer
      diff --git a/java/doc/org/libjpegturbo/turbojpeg/package-tree.html b/java/doc/org/libjpegturbo/turbojpeg/package-tree.html
      index 1033ee5..cda6857 100644
      --- a/java/doc/org/libjpegturbo/turbojpeg/package-tree.html
      +++ b/java/doc/org/libjpegturbo/turbojpeg/package-tree.html
      @@ -80,8 +80,8 @@
       
  • org.libjpegturbo.turbojpeg.TJ
  • -
  • org.libjpegturbo.turbojpeg.TJCompressor
  • -
  • org.libjpegturbo.turbojpeg.TJDecompressor +
  • org.libjpegturbo.turbojpeg.TJCompressor (implements java.io.Closeable)
  • +
  • org.libjpegturbo.turbojpeg.TJDecompressor (implements java.io.Closeable) diff --git a/java/doc/overview-tree.html b/java/doc/overview-tree.html index eae18a1..c5a0ab9 100644 --- a/java/doc/overview-tree.html +++ b/java/doc/overview-tree.html @@ -84,8 +84,8 @@
  • org.libjpegturbo.turbojpeg.TJ
  • -
  • org.libjpegturbo.turbojpeg.TJCompressor
  • -
  • org.libjpegturbo.turbojpeg.TJDecompressor +
  • org.libjpegturbo.turbojpeg.TJCompressor (implements java.io.Closeable)
  • +
  • org.libjpegturbo.turbojpeg.TJDecompressor (implements java.io.Closeable) diff --git a/java/org/libjpegturbo/turbojpeg/TJCompressor.java b/java/org/libjpegturbo/turbojpeg/TJCompressor.java index 6ec581a..37fd604 100644 --- a/java/org/libjpegturbo/turbojpeg/TJCompressor.java +++ b/java/org/libjpegturbo/turbojpeg/TJCompressor.java @@ -30,11 +30,12 @@ package org.libjpegturbo.turbojpeg; import java.awt.image.*; import java.nio.*; +import java.io.*; /** * TurboJPEG compressor */ -public class TJCompressor { +public class TJCompressor implements Closeable { private static final String NO_ASSOC_ERROR = "No source image is associated with this instance"; @@ -567,7 +568,7 @@ public class TJCompressor { /** * Free the native structures associated with this compressor instance. */ - public void close() throws Exception { + public void close() throws IOException { if (handle != 0) destroy(); } @@ -583,7 +584,7 @@ public class TJCompressor { private native void init() throws Exception; - private native void destroy() throws Exception; + private native void destroy() throws IOException; // JPEG size in bytes is returned private native int compress(byte[] srcBuf, int width, int pitch, diff --git a/java/org/libjpegturbo/turbojpeg/TJDecompressor.java b/java/org/libjpegturbo/turbojpeg/TJDecompressor.java index 7ec557f..7002247 100644 --- a/java/org/libjpegturbo/turbojpeg/TJDecompressor.java +++ b/java/org/libjpegturbo/turbojpeg/TJDecompressor.java @@ -30,11 +30,12 @@ package org.libjpegturbo.turbojpeg; import java.awt.image.*; import java.nio.*; +import java.io.*; /** * TurboJPEG decompressor */ -public class TJDecompressor { +public class TJDecompressor implements Closeable { private static final String NO_ASSOC_ERROR = "No JPEG image is associated with this instance"; @@ -833,7 +834,7 @@ public class TJDecompressor { /** * Free the native structures associated with this decompressor instance. */ - public void close() throws Exception { + public void close() throws IOException { if (handle != 0) destroy(); } @@ -849,7 +850,7 @@ public class TJDecompressor { private native void init() throws Exception; - private native void destroy() throws Exception; + private native void destroy() throws IOException; private native void decompressHeader(byte[] srcBuf, int size) throws Exception; diff --git a/turbojpeg-jni.c b/turbojpeg-jni.c index 1bf478f..04e2dc3 100644 --- a/turbojpeg-jni.c +++ b/turbojpeg-jni.c @@ -1,5 +1,5 @@ /* - * Copyright (C)2011-2014 D. R. Commander. All Rights Reserved. + * Copyright (C)2011-2015 D. R. Commander. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -46,6 +46,13 @@ goto bailout; \ } +#define _throwio(msg) { \ + jclass _exccls=(*env)->FindClass(env, "java/io/IOException"); \ + if(!_exccls) goto bailout; \ + (*env)->ThrowNew(env, _exccls, msg); \ + goto bailout; \ +} + #define bailif0(f) {if(!(f)) { \ char temps[80]; \ snprintf(temps, 80, "Unexpected NULL condition in line %d", __LINE__); \ @@ -531,7 +538,7 @@ JNIEXPORT void JNICALL Java_org_libjpegturbo_turbojpeg_TJCompressor_destroy gethandle(); - if(tjDestroy(handle)==-1) _throw(tjGetErrorStr()); + if(tjDestroy(handle)==-1) _throwio(tjGetErrorStr()); (*env)->SetLongField(env, obj, _fid, 0); bailout: -- 2.40.0