----- Original Message ----- From: "Dave Brosius" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, August 24, 2004 9:58 PM Subject: patch to make sure streams get closed
> This patch makes sure streams are closed even on exception paths. > Hmmm, the list strips attachments? ---------------------------- Index: src/share/org/apache/struts/taglib/bean/IncludeTag.java =================================================================== RCS file: /home/cvspublic/jakarta-struts/src/share/org/apache/struts/taglib/bean/Inclu deTag.java,v retrieving revision 1.30 diff -u -r1.30 IncludeTag.java --- src/share/org/apache/struts/taglib/bean/IncludeTag.java 14 Mar 2004 06:23:45 -0000 1.30 +++ src/share/org/apache/struts/taglib/bean/IncludeTag.java 25 Aug 2004 01:49:33 -0000 @@ -154,13 +154,13 @@ protected boolean useLocalEncoding = false; - public boolean isUseLocalEncoding() { - return useLocalEncoding; - } - - public void setUseLocalEncoding(boolean b) { - useLocalEncoding = b; - } + public boolean isUseLocalEncoding() { + return useLocalEncoding; + } + + public void setUseLocalEncoding(boolean b) { + useLocalEncoding = b; + } // --------------------------------------------------------- Public Methods @@ -221,9 +221,10 @@ // Copy the contents of this URL StringBuffer sb = new StringBuffer(); + InputStreamReader in = null; try { BufferedInputStream is = new BufferedInputStream(conn.getInputStream()); - InputStreamReader in = new InputStreamReader(is); // FIXME - encoding + in = new InputStreamReader(is); // FIXME - encoding char buffer[] = new char[BUFFER_SIZE]; int n = 0; while (true) { @@ -232,11 +233,17 @@ break; sb.append(buffer, 0, n); } - in.close(); } catch (Exception e) { TagUtils.getInstance().saveException(pageContext, e); throw new JspException( messages.getMessage("include.read", url.toString(), e.toString())); + } + finally { + try { + if (in != null) + in.close(); + } catch (Exception ioe) { + } } // Define the retrieved content as a page scope attribute Index: src/share/org/apache/struts/taglib/bean/ResourceTag.java =================================================================== RCS file: /home/cvspublic/jakarta-struts/src/share/org/apache/struts/taglib/bean/Resou rceTag.java,v retrieving revision 1.16 diff -u -r1.16 ResourceTag.java --- src/share/org/apache/struts/taglib/bean/ResourceTag.java 14 Mar 2004 06:23:45 -0000 1.16 +++ src/share/org/apache/struts/taglib/bean/ResourceTag.java 25 Aug 2004 01:49:35 -0000 @@ -120,9 +120,10 @@ } // Accumulate the contents of this resource into a StringBuffer + InputStreamReader reader = null; try { StringBuffer sb = new StringBuffer(); - InputStreamReader reader = new InputStreamReader(stream); + reader = new InputStreamReader(stream); char buffer[] = new char[BUFFER_SIZE]; int n = 0; while (true) { @@ -132,12 +133,18 @@ } sb.append(buffer, 0, n); } - reader.close(); pageContext.setAttribute(id, sb.toString()); } catch (IOException e) { TagUtils.getInstance().saveException(pageContext, e); throw new JspException(messages.getMessage("resource.get", name)); + } + finally { + try { + if (reader != null) + reader.close(); + } catch (Exception ioe) { + } } return (SKIP_BODY); Index: src/share/org/apache/struts/upload/DiskFile.java =================================================================== RCS file: /home/cvspublic/jakarta-struts/src/share/org/apache/struts/upload/DiskFile.j ava,v retrieving revision 1.7 diff -u -r1.7 DiskFile.java --- src/share/org/apache/struts/upload/DiskFile.java 14 Mar 2004 06:23:48 -0000 1.7 +++ src/share/org/apache/struts/upload/DiskFile.java 25 Aug 2004 01:51:53 -0000 @@ -74,9 +74,15 @@ byte[] bytes = new byte[getFileSize()]; - FileInputStream fis = new FileInputStream(filePath); - fis.read(bytes); - fis.close(); + FileInputStream fis = null; + + try { + fis = new FileInputStream(filePath); + fis.read(bytes); + } finally { + if (fis != null) + fis.close(); + } return bytes; } @@ -92,27 +98,33 @@ public byte[] getFileData(int bufferSize) throws FileNotFoundException, IOException { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); - FileInputStream fis = new FileInputStream(filePath); - - int readLength = 0; - int totalLength = 0; - int offset = 0; + FileInputStream fis = null; - byte[] bytes = new byte[bufferSize]; + try { + fis = new FileInputStream(filePath); - while ((readLength = fis.read(bytes, offset, bufferSize)) != -1) { + int readLength = 0; + int totalLength = 0; + int offset = 0; - byteStream.write(bytes, offset, bufferSize); - totalLength += readLength; - offset += readLength; - } - - bytes = byteStream.toByteArray(); - - fis.close(); - byteStream.close(); - - return bytes; + byte[] bytes = new byte[bufferSize]; + + while ((readLength = fis.read(bytes, offset, bufferSize)) != -1 ) { + + byteStream.write(bytes, offset, bufferSize); + totalLength += readLength; + offset += readLength; + } + + bytes = byteStream.toByteArray(); + + byteStream.close(); + + return bytes; + } finally { + if (fis != null) + fis.close(); + } } Index: src/share/org/apache/struts/upload/MultipartIterator.java =================================================================== RCS file: /home/cvspublic/jakarta-struts/src/share/org/apache/struts/upload/MultipartI terator.java,v retrieving revision 1.27 diff -u -r1.27 MultipartIterator.java --- src/share/org/apache/struts/upload/MultipartIterator.java 14 Mar 2004 06:23:48 -0000 1.27 +++ src/share/org/apache/struts/upload/MultipartIterator.java 25 Aug 2004 01:53:11 -0000 @@ -402,15 +402,20 @@ protected File createLocalFile() throws IOException { File tempFile = File.createTempFile(FILE_PREFIX, null, new File(this.tempDir)); - BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(tempFile), this.diskBufferSize); - int read = 0; - byte buffer[] = new byte[this.diskBufferSize]; - while ((read = this.inputStream.read(buffer, 0, this.diskBufferSize)) > 0) - { - fos.write(buffer, 0, read); + BufferedOutputStream fos = null; + + try { + fos = new BufferedOutputStream(new FileOutputStream(tempFile), this.diskBufferSize); + int read = 0; + byte buffer[] = new byte[this.diskBufferSize]; + while ((read = this.inputStream.read(buffer, 0, this.diskBufferSize)) > 0) + { + fos.write(buffer, 0, read); + } + return tempFile; + } finally { + if (fos != null) + fos.close(); } - fos.flush(); - fos.close(); - return tempFile; } } Index: src/share/org/apache/struts/util/ModuleUtils.java =================================================================== RCS file: /home/cvspublic/jakarta-struts/src/share/org/apache/struts/util/ModuleUtils. java,v retrieving revision 1.9 diff -u -r1.9 ModuleUtils.java --- src/share/org/apache/struts/util/ModuleUtils.java 22 Jul 2004 13:29:44 -0000 1.9 +++ src/share/org/apache/struts/util/ModuleUtils.java 3 Aug 2004 00:48:15 -0000 @@ -243,20 +243,20 @@ if (config != null) { request.setAttribute(Globals.MODULE_KEY, config); + MessageResourcesConfig[] mrConfig = config.findMessageResourcesConfigs(); + for(int i = 0; i < mrConfig.length; i++) { + String key = mrConfig[i].getKey(); + MessageResources resources = + (MessageResources) context.getAttribute(key + prefix); + if (resources != null) { + request.setAttribute(key, resources); + } else { + request.removeAttribute(key); + } + } } else { request.removeAttribute(Globals.MODULE_KEY); } - MessageResourcesConfig[] mrConfig = config.findMessageResourcesConfigs(); - for(int i = 0; i < mrConfig.length; i++) { - String key = mrConfig[i].getKey(); - MessageResources resources = - (MessageResources) context.getAttribute(key + prefix); - if (resources != null) { - request.setAttribute(key, resources); - } else { - request.removeAttribute(key); - } - } } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]