Author: markt Date: Fri Feb 19 22:53:50 2016 New Revision: 1731306 URL: http://svn.apache.org/viewvc?rev=1731306&view=rev Log: Back-port the send file unit test
Added: tomcat/tc7.0.x/trunk/test/org/apache/catalina/connector/TestSendFile.java - copied, changed from r1731286, tomcat/tc8.0.x/trunk/test/org/apache/catalina/connector/TestSendFile.java Copied: tomcat/tc7.0.x/trunk/test/org/apache/catalina/connector/TestSendFile.java (from r1731286, tomcat/tc8.0.x/trunk/test/org/apache/catalina/connector/TestSendFile.java) URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/test/org/apache/catalina/connector/TestSendFile.java?p2=tomcat/tc7.0.x/trunk/test/org/apache/catalina/connector/TestSendFile.java&p1=tomcat/tc8.0.x/trunk/test/org/apache/catalina/connector/TestSendFile.java&r1=1731286&r2=1731306&rev=1731306&view=diff ============================================================================== --- tomcat/tc8.0.x/trunk/test/org/apache/catalina/connector/TestSendFile.java (original) +++ tomcat/tc7.0.x/trunk/test/org/apache/catalina/connector/TestSendFile.java Fri Feb 19 22:53:50 2016 @@ -68,7 +68,7 @@ public class TestSendFile extends Tomcat tomcat.start(); ByteChunk bc = new ByteChunk(); - Map<String, List<String>> respHeaders = new HashMap<>(); + Map<String, List<String>> respHeaders = new HashMap<String, List<String>>(); for (int i=0; i<ITERATIONS; i++) { long start = System.currentTimeMillis(); int rc = getUrl("http://localhost:" + getPort() + "/servlet" + i, bc, null, respHeaders); @@ -88,7 +88,11 @@ public class TestSendFile extends Tomcat public File generateFile(String dir, String suffix, int size) throws IOException { String name = "testSendFile-" + System.currentTimeMillis() + suffix + ".txt"; File f = new File(dir, name); - try (FileWriter fw = new FileWriter(f, false); BufferedWriter w = new BufferedWriter(fw);) { + FileWriter fw = null; + BufferedWriter w = null; + try { + fw = new FileWriter(f, false); + w = new BufferedWriter(fw); int defSize = 8192; while (size > 0) { int bytes = Math.min(size, defSize); @@ -98,6 +102,13 @@ public class TestSendFile extends Tomcat size = size - bytes; } w.flush(); + } finally { + if (w != null) { + w.close(); + } + if (fw != null) { + fw.close(); + } } System.out.println("Created file:" + f.getAbsolutePath() + " with " + f.length() + " bytes."); @@ -122,15 +133,16 @@ public class TestSendFile extends Tomcat resp.setContentType("'application/octet-stream"); resp.setCharacterEncoding("ISO-8859-1"); - resp.setContentLengthLong(f.length()); + resp.setContentLength((int) f.length()); if (Boolean.TRUE.equals(req.getAttribute(Globals.SENDFILE_SUPPORTED_ATTR))) { req.setAttribute(Globals.SENDFILE_FILENAME_ATTR, f.getAbsolutePath()); req.setAttribute(Globals.SENDFILE_FILE_START_ATTR, new Long(0)); req.setAttribute(Globals.SENDFILE_FILE_END_ATTR, new Long(f.length())); } else { byte[] c = new byte[8192]; - try (BufferedInputStream in = new BufferedInputStream( - new FileInputStream(f))) { + BufferedInputStream in = null; + try { + in = new BufferedInputStream(new FileInputStream(f)); int len = 0; int written = 0; long start = System.currentTimeMillis(); @@ -142,6 +154,10 @@ public class TestSendFile extends Tomcat } } while (len > 0); System.out.println("Server Wrote "+written + " bytes in "+(System.currentTimeMillis()-start)+" ms."); + } finally { + if (in != null) { + in.close(); + } } } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org