Quantcast

[HtmlUnit] SVN: [13742] trunk/htmlunit/src

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[HtmlUnit] SVN: [13742] trunk/htmlunit/src

rbri
Revision: 13742
          http://sourceforge.net/p/htmlunit/code/13742
Author:   rbri
Date:     2017-03-15 20:00:05 +0000 (Wed, 15 Mar 2017)
Log Message:
-----------
reduce the number of temp files created when asking images for width/height

Modified Paths:
--------------
    trunk/htmlunit/src/changes/changes.xml
    trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlImage.java
    trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlImageTest.java

Added Paths:
-----------
    trunk/htmlunit/src/test/resources/testfiles/4x7.jpg

Modified: trunk/htmlunit/src/changes/changes.xml
===================================================================
--- trunk/htmlunit/src/changes/changes.xml 2017-03-15 19:43:00 UTC (rev 13741)
+++ trunk/htmlunit/src/changes/changes.xml 2017-03-15 20:00:05 UTC (rev 13742)
@@ -8,6 +8,9 @@
 
     <body>
         <release version="2.26" date="???" description="Bugfixes">
+            <action type="update" dev="rbri">
+                Reduce the number of temp files created when asking images for width/height.
+            </action>
         </release>
         <release version="2.25" date="February 23, 2017" description="Java 8, InteractivePage and SvgPage removed, PromptHandler, WebRequest.getCharset, ScriptPreProcessor, Bugfixes">
             <action type="update" dev="asashour">

Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlImage.java
===================================================================
--- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlImage.java 2017-03-15 19:43:00 UTC (rev 13741)
+++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/html/HtmlImage.java 2017-03-15 20:00:05 UTC (rev 13742)
@@ -74,6 +74,8 @@
     private int lastClickY_;
     private WebResponse imageWebResponse_;
     private transient ImageData imageData_;
+    private int width_ = -1;
+    private int height_ = -1;
     private boolean downloaded_;
     private boolean onloadInvoked_;
     private boolean createdByJavascript_;
@@ -133,6 +135,12 @@
                 // onload handlers may need to be invoked again, and a new image may need to be downloaded
                 onloadInvoked_ = false;
                 downloaded_ = false;
+                width_ = -1;
+                height_ = -1;
+                if (imageData_ != null) {
+                    imageData_.close();
+                    imageData_ = null;
+                }
 
                 final String readyState = htmlPage.getReadyState();
                 if (READY_STATE_LOADING.equals(readyState)) {
@@ -364,7 +372,10 @@
      * @throws IOException if an error occurs while downloading or reading the image
      */
     public int getHeight() throws IOException {
-        return getImageReader().getHeight(0);
+        if (height_ < 0) {
+            determineWidthAndHeight();
+        }
+        return height_;
     }
 
     /**
@@ -376,7 +387,10 @@
      * @throws IOException if an error occurs while downloading or reading the image
      */
     public int getWidth() throws IOException {
-        return getImageReader().getWidth(0);
+        if (width_ < 0) {
+            determineWidthAndHeight();
+        }
+        return width_;
     }
 
     /**
@@ -392,6 +406,19 @@
         return imageData_.getImageReader();
     }
 
+    private void determineWidthAndHeight() throws IOException {
+        final ImageReader imgReader = getImageReader();
+        width_ = imgReader.getWidth(0);
+        height_ = imgReader.getHeight(0);
+
+        // ImageIO creates temp files; to save file handles
+        // we will cache the values and close this directly to free the resources
+        if (imageData_ != null) {
+            imageData_.close();
+            imageData_ = null;
+        }
+    }
+
     /**
      * <p>Returns the <tt>WebResponse</tt> for the image contained by this image element.</p>
      * <p><span style="color:red">POTENTIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK</span></p>
@@ -432,8 +459,14 @@
                 request.setAdditionalHeader("Referer", page.getUrl().toExternalForm());
                 imageWebResponse_ = webclient.loadWebResponse(request);
             }
-            imageData_ = null;
+
+            if (imageData_ != null) {
+                imageData_.close();
+                imageData_ = null;
+            }
             downloaded_ = true;
+            width_ = -1;
+            height_ = -1;
         }
     }
 
@@ -557,7 +590,7 @@
      * of objects which could all be garbage collected without impacting the ImageReader it is better to
      * wrap it in another class.
      */
-    private static final class ImageData {
+    private static final class ImageData implements AutoCloseable {
 
         private final ImageReader imageReader_;
 
@@ -574,6 +607,12 @@
          */
         @Override
         protected void finalize() throws Throwable {
+            close();
+            super.finalize();
+        }
+
+        @Override
+        public void close() {
             if (imageReader_ != null) {
                 try {
                     try (ImageInputStream stream = (ImageInputStream) imageReader_.getInput()) {
@@ -588,7 +627,6 @@
                     imageReader_.dispose();
                 }
             }
-            super.finalize();
         }
     }
 

Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlImageTest.java
===================================================================
--- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlImageTest.java 2017-03-15 19:43:00 UTC (rev 13741)
+++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/html/HtmlImageTest.java 2017-03-15 20:00:05 UTC (rev 13742)
@@ -27,9 +27,9 @@
 
 import com.gargoylesoftware.htmlunit.BrowserRunner;
 import com.gargoylesoftware.htmlunit.BrowserRunner.NotYetImplemented;
-import com.gargoylesoftware.htmlunit.util.NameValuePair;
 import com.gargoylesoftware.htmlunit.Page;
 import com.gargoylesoftware.htmlunit.SimpleWebTestCase;
+import com.gargoylesoftware.htmlunit.util.NameValuePair;
 
 /**
  * Tests for {@link HtmlImage}.
@@ -197,4 +197,42 @@
         img.saveAs(tempFile);
         FileUtils.deleteQuietly(tempFile);
     }
+
+    /**
+     * @throws Exception if the test fails
+     */
+    @Test
+    public void determineWidthHeigthFromImage() throws Exception {
+        try (InputStream is = getClass().getClassLoader().
+                getResourceAsStream("testfiles/4x7.jpg")) {
+            final byte[] directBytes = IOUtils.toByteArray(is);
+            final URL urlImage = new URL(URL_FIRST, "4x7.jpg");
+            final List<NameValuePair> emptyList = Collections.emptyList();
+            getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", emptyList);
+        }
+        try (InputStream is = getClass().getClassLoader().
+                getResourceAsStream("testfiles/tiny-jpg.img")) {
+            final byte[] directBytes = IOUtils.toByteArray(is);
+            final URL urlImage = new URL(URL_FIRST, "img.jpg");
+            final List<NameValuePair> emptyList = Collections.emptyList();
+            getMockWebConnection().setResponse(urlImage, directBytes, 200, "ok", "image/jpg", emptyList);
+        }
+
+        final String html = "<html><head>\n"
+            + "</head>\n"
+            + "<body>\n"
+            + "  <img id='myImage' src='4x7.jpg' >\n"
+            + "</body></html>";
+
+        final HtmlPage page = loadPage(html);
+
+        final HtmlImage img = page.getHtmlElementById("myImage");
+        assertEquals(4, img.getWidth());
+        assertEquals(7, img.getHeight());
+
+        // source change has to force new determination
+        img.setAttribute("src", "img.jpg");
+        assertEquals(1, img.getWidth());
+        assertEquals(1, img.getHeight());
+    }
 }

Added: trunk/htmlunit/src/test/resources/testfiles/4x7.jpg
===================================================================
(Binary files differ)

Index: trunk/htmlunit/src/test/resources/testfiles/4x7.jpg
===================================================================
--- trunk/htmlunit/src/test/resources/testfiles/4x7.jpg 2017-03-15 19:43:00 UTC (rev 13741)
+++ trunk/htmlunit/src/test/resources/testfiles/4x7.jpg 2017-03-15 20:00:05 UTC (rev 13742)

Property changes on: trunk/htmlunit/src/test/resources/testfiles/4x7.jpg
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
HtmlUnit-develop mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/htmlunit-develop
Loading...