Skip to content

Commit

Permalink
Further exception cleanup
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dcommander committed Jul 27, 2015
1 parent 8026b0f commit a23542e
Show file tree
Hide file tree
Showing 31 changed files with 663 additions and 305 deletions.
3 changes: 2 additions & 1 deletion ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ try-with-resources statement.

[4] The TurboJPEG Java classes now throw unchecked idiomatic exceptions
(IllegalArgumentException, IllegalStateException) for unrecoverable errors
caused by incorrect API usage.
caused by incorrect API usage, and those classes throw a new checked exception
type (TJException) for errors that are passed through from the C library.


1.4.1
Expand Down
1 change: 1 addition & 0 deletions java/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set(JAVA_CLASSNAMES org/libjpegturbo/turbojpeg/TJ
org/libjpegturbo/turbojpeg/TJCompressor
org/libjpegturbo/turbojpeg/TJCustomFilter
org/libjpegturbo/turbojpeg/TJDecompressor
org/libjpegturbo/turbojpeg/TJException
org/libjpegturbo/turbojpeg/TJScalingFactor
org/libjpegturbo/turbojpeg/TJTransform
org/libjpegturbo/turbojpeg/TJTransformer
Expand Down
2 changes: 2 additions & 0 deletions java/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ JAVASOURCES = org/libjpegturbo/turbojpeg/TJ.java \
org/libjpegturbo/turbojpeg/TJCompressor.java \
org/libjpegturbo/turbojpeg/TJCustomFilter.java \
org/libjpegturbo/turbojpeg/TJDecompressor.java \
org/libjpegturbo/turbojpeg/TJException.java \
org/libjpegturbo/turbojpeg/TJScalingFactor.java \
org/libjpegturbo/turbojpeg/TJTransform.java \
org/libjpegturbo/turbojpeg/TJTransformer.java \
Expand All @@ -31,6 +32,7 @@ JAVA_CLASSES = org/libjpegturbo/turbojpeg/TJ.class \
org/libjpegturbo/turbojpeg/TJCompressor.class \
org/libjpegturbo/turbojpeg/TJCustomFilter.class \
org/libjpegturbo/turbojpeg/TJDecompressor.class \
org/libjpegturbo/turbojpeg/TJException.class \
org/libjpegturbo/turbojpeg/TJLoader.class \
org/libjpegturbo/turbojpeg/TJScalingFactor.class \
org/libjpegturbo/turbojpeg/TJTransform.class \
Expand Down
4 changes: 2 additions & 2 deletions java/TJExample.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C)2011-2012, 2014 D. R. Commander. All Rights Reserved.
* Copyright (C)2011-2012, 2014-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:
Expand Down Expand Up @@ -351,7 +351,7 @@ else if (argv[i].length() > 2 &&
public void customFilter(ShortBuffer coeffBuffer, Rectangle bufferRegion,
Rectangle planeRegion, int componentIndex,
int transformIndex, TJTransform transform)
throws Exception {
throws TJException {
for (int i = 0; i < bufferRegion.width * bufferRegion.height; i++) {
coeffBuffer.put(i, (short)(-coeffBuffer.get(i)));
}
Expand Down
1 change: 1 addition & 0 deletions java/doc/allclasses-frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ <h1 class="bar">All Classes</h1>
<li><a href="org/libjpegturbo/turbojpeg/TJCompressor.html" title="class in org.libjpegturbo.turbojpeg" target="classFrame">TJCompressor</a></li>
<li><a href="org/libjpegturbo/turbojpeg/TJCustomFilter.html" title="interface in org.libjpegturbo.turbojpeg" target="classFrame"><i>TJCustomFilter</i></a></li>
<li><a href="org/libjpegturbo/turbojpeg/TJDecompressor.html" title="class in org.libjpegturbo.turbojpeg" target="classFrame">TJDecompressor</a></li>
<li><a href="org/libjpegturbo/turbojpeg/TJException.html" title="class in org.libjpegturbo.turbojpeg" target="classFrame">TJException</a></li>
<li><a href="org/libjpegturbo/turbojpeg/TJScalingFactor.html" title="class in org.libjpegturbo.turbojpeg" target="classFrame">TJScalingFactor</a></li>
<li><a href="org/libjpegturbo/turbojpeg/TJTransform.html" title="class in org.libjpegturbo.turbojpeg" target="classFrame">TJTransform</a></li>
<li><a href="org/libjpegturbo/turbojpeg/TJTransformer.html" title="class in org.libjpegturbo.turbojpeg" target="classFrame">TJTransformer</a></li>
Expand Down
1 change: 1 addition & 0 deletions java/doc/allclasses-noframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ <h1 class="bar">All Classes</h1>
<li><a href="org/libjpegturbo/turbojpeg/TJCompressor.html" title="class in org.libjpegturbo.turbojpeg">TJCompressor</a></li>
<li><a href="org/libjpegturbo/turbojpeg/TJCustomFilter.html" title="interface in org.libjpegturbo.turbojpeg"><i>TJCustomFilter</i></a></li>
<li><a href="org/libjpegturbo/turbojpeg/TJDecompressor.html" title="class in org.libjpegturbo.turbojpeg">TJDecompressor</a></li>
<li><a href="org/libjpegturbo/turbojpeg/TJException.html" title="class in org.libjpegturbo.turbojpeg">TJException</a></li>
<li><a href="org/libjpegturbo/turbojpeg/TJScalingFactor.html" title="class in org.libjpegturbo.turbojpeg">TJScalingFactor</a></li>
<li><a href="org/libjpegturbo/turbojpeg/TJTransform.html" title="class in org.libjpegturbo.turbojpeg">TJTransform</a></li>
<li><a href="org/libjpegturbo/turbojpeg/TJTransformer.html" title="class in org.libjpegturbo.turbojpeg">TJTransformer</a></li>
Expand Down
10 changes: 10 additions & 0 deletions java/doc/index-all.html
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,16 @@ <h2 class="title">T</h2>
source image stored in <code>yuvImage</code> with the newly created
instance.</div>
</dd>
<dt><a href="./org/libjpegturbo/turbojpeg/TJException.html" title="class in org.libjpegturbo.turbojpeg"><span class="strong">TJException</span></a> - Exception in <a href="./org/libjpegturbo/turbojpeg/package-summary.html">org.libjpegturbo.turbojpeg</a></dt>
<dd>&nbsp;</dd>
<dt><span class="strong"><a href="./org/libjpegturbo/turbojpeg/TJException.html#TJException()">TJException()</a></span> - Constructor for exception org.libjpegturbo.turbojpeg.<a href="./org/libjpegturbo/turbojpeg/TJException.html" title="class in org.libjpegturbo.turbojpeg">TJException</a></dt>
<dd>&nbsp;</dd>
<dt><span class="strong"><a href="./org/libjpegturbo/turbojpeg/TJException.html#TJException(java.lang.String,%20java.lang.Throwable)">TJException(String, Throwable)</a></span> - Constructor for exception org.libjpegturbo.turbojpeg.<a href="./org/libjpegturbo/turbojpeg/TJException.html" title="class in org.libjpegturbo.turbojpeg">TJException</a></dt>
<dd>&nbsp;</dd>
<dt><span class="strong"><a href="./org/libjpegturbo/turbojpeg/TJException.html#TJException(java.lang.String)">TJException(String)</a></span> - Constructor for exception org.libjpegturbo.turbojpeg.<a href="./org/libjpegturbo/turbojpeg/TJException.html" title="class in org.libjpegturbo.turbojpeg">TJException</a></dt>
<dd>&nbsp;</dd>
<dt><span class="strong"><a href="./org/libjpegturbo/turbojpeg/TJException.html#TJException(java.lang.Throwable)">TJException(Throwable)</a></span> - Constructor for exception org.libjpegturbo.turbojpeg.<a href="./org/libjpegturbo/turbojpeg/TJException.html" title="class in org.libjpegturbo.turbojpeg">TJException</a></dt>
<dd>&nbsp;</dd>
<dt><a href="./org/libjpegturbo/turbojpeg/TJScalingFactor.html" title="class in org.libjpegturbo.turbojpeg"><span class="strong">TJScalingFactor</span></a> - Class in <a href="./org/libjpegturbo/turbojpeg/package-summary.html">org.libjpegturbo.turbojpeg</a></dt>
<dd>
<div class="block">Fractional scaling factor</div>
Expand Down
47 changes: 13 additions & 34 deletions java/doc/org/libjpegturbo/turbojpeg/TJ.html
Original file line number Diff line number Diff line change
Expand Up @@ -1075,16 +1075,13 @@ <h4>getBlueOffset</h4>
<h4>bufSize</h4>
<pre>public static&nbsp;int&nbsp;bufSize(int&nbsp;width,
int&nbsp;height,
int&nbsp;jpegSubsamp)
throws java.lang.Exception</pre>
int&nbsp;jpegSubsamp)</pre>
<div class="block">Returns the maximum size of the buffer (in bytes) required to hold a JPEG
image with the given width, height, and level of chrominance subsampling.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>width</code> - the width (in pixels) of the JPEG image</dd><dd><code>height</code> - the height (in pixels) of the JPEG image</dd><dd><code>jpegSubsamp</code> - the level of chrominance subsampling to be used when
generating the JPEG image (one of <a href="../../../org/libjpegturbo/turbojpeg/TJ.html" title="class in org.libjpegturbo.turbojpeg"><code>TJ.SAMP_*</code></a>)</dd>
<dt><span class="strong">Returns:</span></dt><dd>the maximum size of the buffer (in bytes) required to hold a JPEG
image with the given width, height, and level of chrominance subsampling.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
image with the given width, height, and level of chrominance subsampling.</dd></dl>
</li>
</ul>
<a name="bufSizeYUV(int, int, int, int)">
Expand All @@ -1096,17 +1093,14 @@ <h4>bufSizeYUV</h4>
<pre>public static&nbsp;int&nbsp;bufSizeYUV(int&nbsp;width,
int&nbsp;pad,
int&nbsp;height,
int&nbsp;subsamp)
throws java.lang.Exception</pre>
int&nbsp;subsamp)</pre>
<div class="block">Returns the size of the buffer (in bytes) required to hold a YUV planar
image with the given width, height, and level of chrominance subsampling.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>width</code> - the width (in pixels) of the YUV image</dd><dd><code>pad</code> - the width of each line in each plane of the image is padded to
the nearest multiple of this number of bytes (must be a power of 2.)</dd><dd><code>height</code> - the height (in pixels) of the YUV image</dd><dd><code>subsamp</code> - the level of chrominance subsampling used in the YUV
image (one of <a href="../../../org/libjpegturbo/turbojpeg/TJ.html" title="class in org.libjpegturbo.turbojpeg"><code>TJ.SAMP_*</code></a>)</dd>
<dt><span class="strong">Returns:</span></dt><dd>the size of the buffer (in bytes) required to hold a YUV planar
image with the given width, height, and level of chrominance subsampling.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
image with the given width, height, and level of chrominance subsampling.</dd></dl>
</li>
</ul>
<a name="bufSizeYUV(int, int, int)">
Expand All @@ -1118,11 +1112,8 @@ <h4>bufSizeYUV</h4>
<pre>@Deprecated
public static&nbsp;int&nbsp;bufSizeYUV(int&nbsp;width,
int&nbsp;height,
int&nbsp;subsamp)
throws java.lang.Exception</pre>
int&nbsp;subsamp)</pre>
<div class="block"><span class="strong">Deprecated.</span>&nbsp;<i>Use <a href="../../../org/libjpegturbo/turbojpeg/TJ.html#bufSizeYUV(int,%20int,%20int,%20int)"><code>bufSizeYUV(int, int, int, int)</code></a> instead.</i></div>
<dl><dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
</li>
</ul>
<a name="planeSizeYUV(int, int, int, int, int)">
Expand All @@ -1135,8 +1126,7 @@ <h4>planeSizeYUV</h4>
int&nbsp;width,
int&nbsp;stride,
int&nbsp;height,
int&nbsp;subsamp)
throws java.lang.Exception</pre>
int&nbsp;subsamp)</pre>
<div class="block">Returns the size of the buffer (in bytes) required to hold a YUV image
plane with the given parameters.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>componentID</code> - ID number of the image plane (0 = Y, 1 = U/Cb,
Expand All @@ -1145,9 +1135,7 @@ <h4>planeSizeYUV</h4>
height of the whole image, not the plane height.</dd><dd><code>subsamp</code> - the level of chrominance subsampling used in the YUV
image (one of <a href="../../../org/libjpegturbo/turbojpeg/TJ.html" title="class in org.libjpegturbo.turbojpeg"><code>TJ.SAMP_*</code></a>)</dd>
<dt><span class="strong">Returns:</span></dt><dd>the size of the buffer (in bytes) required to hold a YUV planar
image with the given parameters.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
image with the given parameters.</dd></dl>
</li>
</ul>
<a name="planeWidth(int, int, int)">
Expand All @@ -1158,16 +1146,13 @@ <h4>planeSizeYUV</h4>
<h4>planeWidth</h4>
<pre>public static&nbsp;int&nbsp;planeWidth(int&nbsp;componentID,
int&nbsp;width,
int&nbsp;subsamp)
throws java.lang.Exception</pre>
int&nbsp;subsamp)</pre>
<div class="block">Returns the plane width of a YUV image plane with the given parameters.
Refer to <a href="../../../org/libjpegturbo/turbojpeg/YUVImage.html" title="class in org.libjpegturbo.turbojpeg"><code>YUVImage</code></a> for a description of plane width.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>componentID</code> - ID number of the image plane (0 = Y, 1 = U/Cb,
2 = V/Cr)</dd><dd><code>width</code> - width (in pixels) of the YUV image</dd><dd><code>subsamp</code> - the level of chrominance subsampling used in the YUV image
(one of <a href="../../../org/libjpegturbo/turbojpeg/TJ.html" title="class in org.libjpegturbo.turbojpeg"><code>TJ.SAMP_*</code></a>)</dd>
<dt><span class="strong">Returns:</span></dt><dd>the plane width of a YUV image plane with the given parameters.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dt><span class="strong">Returns:</span></dt><dd>the plane width of a YUV image plane with the given parameters.</dd></dl>
</li>
</ul>
<a name="planeHeight(int, int, int)">
Expand All @@ -1178,16 +1163,13 @@ <h4>planeWidth</h4>
<h4>planeHeight</h4>
<pre>public static&nbsp;int&nbsp;planeHeight(int&nbsp;componentID,
int&nbsp;height,
int&nbsp;subsamp)
throws java.lang.Exception</pre>
int&nbsp;subsamp)</pre>
<div class="block">Returns the plane height of a YUV image plane with the given parameters.
Refer to <a href="../../../org/libjpegturbo/turbojpeg/YUVImage.html" title="class in org.libjpegturbo.turbojpeg"><code>YUVImage</code></a> for a description of plane height.</div>
<dl><dt><span class="strong">Parameters:</span></dt><dd><code>componentID</code> - ID number of the image plane (0 = Y, 1 = U/Cb,
2 = V/Cr)</dd><dd><code>height</code> - height (in pixels) of the YUV image</dd><dd><code>subsamp</code> - the level of chrominance subsampling used in the YUV image
(one of <a href="../../../org/libjpegturbo/turbojpeg/TJ.html" title="class in org.libjpegturbo.turbojpeg"><code>TJ.SAMP_*</code></a>)</dd>
<dt><span class="strong">Returns:</span></dt><dd>the plane height of a YUV image plane with the given parameters.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
<dt><span class="strong">Returns:</span></dt><dd>the plane height of a YUV image plane with the given parameters.</dd></dl>
</li>
</ul>
<a name="getScalingFactors()">
Expand All @@ -1196,14 +1178,11 @@ <h4>planeHeight</h4>
<ul class="blockListLast">
<li class="blockList">
<h4>getScalingFactors</h4>
<pre>public static&nbsp;<a href="../../../org/libjpegturbo/turbojpeg/TJScalingFactor.html" title="class in org.libjpegturbo.turbojpeg">TJScalingFactor</a>[]&nbsp;getScalingFactors()
throws java.lang.Exception</pre>
<pre>public static&nbsp;<a href="../../../org/libjpegturbo/turbojpeg/TJScalingFactor.html" title="class in org.libjpegturbo.turbojpeg">TJScalingFactor</a>[]&nbsp;getScalingFactors()</pre>
<div class="block">Returns a list of fractional scaling factors that the JPEG decompressor in
this implementation of TurboJPEG supports.</div>
<dl><dt><span class="strong">Returns:</span></dt><dd>a list of fractional scaling factors that the JPEG decompressor in
this implementation of TurboJPEG supports.</dd>
<dt><span class="strong">Throws:</span></dt>
<dd><code>java.lang.Exception</code></dd></dl>
this implementation of TurboJPEG supports.</dd></dl>
</li>
</ul>
</li>
Expand Down
Loading

0 comments on commit a23542e

Please sign in to comment.