From: <rb...@us...> - 2018-02-01 19:20:02
|
Revision: 15106 http://sourceforge.net/p/htmlunit/code/15106 Author: rbri Date: 2018-02-01 19:20:00 +0000 (Thu, 01 Feb 2018) Log Message: ----------- try to no longer 'sweep errors under the mat' Issue 1832 Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java 2018-01-30 19:40:11 UTC (rev 15105) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/HttpWebConnection.java 2018-02-01 19:20:00 UTC (rev 15106) @@ -715,36 +715,37 @@ if (is == null) { return new DownloadedContent.InMemory(null); } - final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - final byte[] buffer = new byte[1024]; - int nbRead; - try { - while ((nbRead = is.read(buffer)) != -1) { - bos.write(buffer, 0, nbRead); - if (bos.size() > maxInMemory) { - // we have exceeded the max for memory, let's write everything to a temporary file - final File file = File.createTempFile("htmlunit", ".tmp"); - file.deleteOnExit(); - try (FileOutputStream fos = new FileOutputStream(file)) { - bos.writeTo(fos); // what we have already read - IOUtils.copyLarge(is, fos); // what remains from the server response + try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { + final byte[] buffer = new byte[1024]; + int nbRead; + try { + while ((nbRead = is.read(buffer)) != -1) { + bos.write(buffer, 0, nbRead); + if (bos.size() > maxInMemory) { + // we have exceeded the max for memory, let's write everything to a temporary file + final File file = File.createTempFile("htmlunit", ".tmp"); + file.deleteOnExit(); + try (FileOutputStream fos = new FileOutputStream(file)) { + bos.writeTo(fos); // what we have already read + IOUtils.copyLarge(is, fos); // what remains from the server response + } + return new DownloadedContent.OnFile(file, true); } - return new DownloadedContent.OnFile(file, true); } } - } - catch (final ConnectionClosedException e) { - LOG.warn("Connection was closed while reading from stream.", e); + catch (final ConnectionClosedException e) { + LOG.warn("Connection was closed while reading from stream.", e); + throw e; + } + catch (final EOFException e) { + // this might happen with broken gzip content + LOG.warn("EOFException while reading from stream.", e); + throw e; + } + return new DownloadedContent.InMemory(bos.toByteArray()); } - catch (final EOFException e) { - // this might happen with broken gzip content - LOG.warn("EOFException while reading from stream.", e); - return new DownloadedContent.InMemory(bos.toByteArray()); - } - - return new DownloadedContent.InMemory(bos.toByteArray()); } /** |