snichol 2004/04/09 06:19:04
Modified: java/samples/doclit GetQuote.java java/src/org/apache/soap/util/net HTTPUtils.java Log: Always clean up socket resources. Revision Changes Path 1.3 +1 -0 ws-soap/java/samples/doclit/GetQuote.java Index: GetQuote.java =================================================================== RCS file: /home/cvs/ws-soap/java/samples/doclit/GetQuote.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- GetQuote.java 7 Apr 2004 20:51:34 -0000 1.2 +++ GetQuote.java 9 Apr 2004 13:19:04 -0000 1.3 @@ -76,6 +76,7 @@ e.getFaultCode() + "): " + e.getMessage()); System.err.println(e.toString()); + e.printStackTrace(System.err); return; } 1.46 +25 -8 ws-soap/java/src/org/apache/soap/util/net/HTTPUtils.java Index: HTTPUtils.java =================================================================== RCS file: /home/cvs/ws-soap/java/src/org/apache/soap/util/net/HTTPUtils.java,v retrieving revision 1.45 retrieving revision 1.46 diff -u -r1.45 -r1.46 --- HTTPUtils.java 7 Apr 2004 20:51:40 -0000 1.45 +++ HTTPUtils.java 9 Apr 2004 13:19:04 -0000 1.46 @@ -404,6 +404,8 @@ String protocol = url.getProtocol(); Socket s = null; boolean proxyUsed = false; + BufferedOutputStream bOutStream = null; + BufferedInputStream bInStream = null; /* Open the connection */ try { @@ -421,10 +423,16 @@ if (timeout > 0) // Should be redundant but not every JVM likes this s.setSoTimeout(timeout); - outStream = s.getOutputStream (); - inStream = s.getInputStream (); + outStream = s.getOutputStream(); + inStream = s.getInputStream(); } catch (Exception e) { + if (inStream != null) + inStream.close(); + if (outStream != null) + outStream.close(); + if (s != null) + s.close(); if (e instanceof SOAPException) throw (SOAPException) e; @@ -438,6 +446,7 @@ "Error opening socket: " + t, t); } + try { /* Compute the Request URI */ String URI = (!proxyUsed ? url.getFile() : url.toString()); if (URI.length() == 0) URI = "/"; @@ -460,7 +469,7 @@ headerbuf.append("\r\n"); /* Send the request. */ - BufferedOutputStream bOutStream = new BufferedOutputStream(outStream, outputBufferSize); + bOutStream = new BufferedOutputStream(outStream, outputBufferSize); bOutStream.write( headerbuf.toString().getBytes(Constants.HEADERVAL_DEFAULT_CHARSET)); request.writeTo(bOutStream); @@ -473,7 +482,7 @@ bOutStream.flush(); - BufferedInputStream bInStream = new BufferedInputStream(inStream); + bInStream = new BufferedInputStream(inStream); byte[] linebuf = new byte[1024]; char[] cbuf = new char[256]; int count = 0; @@ -604,8 +613,11 @@ URL newURL = new URL(url, newLocation); /* Close current streams */ bOutStream.close(); + bOutStream = null; bInStream.close(); + bInStream = null; s.close(); + s = null; // Post to new location return post(newURL, request, timeout, httpProxyHost, httpProxyPort, outputBufferSize, tcpNoDelay, null, responseCopy, numRedirects); @@ -643,11 +655,16 @@ responseCopy.append(new String(response.getBytes())); /* May get junk due to actual encoding */ } - /* All done here! */ - bOutStream.close(); - bInStream.close(); - s.close(); return response; + } finally { + /* clean up socket resources */ + if (bOutStream != null) + bOutStream.close(); + if (bInStream != null) + bInStream.close(); + if (s != null) + s.close(); + } } /**