Author: violetagg Date: Mon Aug 29 18:10:36 2016 New Revision: 1758276 URL: http://svn.apache.org/viewvc?rev=1758276&view=rev Log: Introduce a new method CoyoteOutputStream.write(ByteBuffer)
Added: tomcat/trunk/test/org/apache/catalina/connector/test_content.txt (with props) Modified: tomcat/trunk/java/org/apache/catalina/connector/CoyoteOutputStream.java tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java tomcat/trunk/test/org/apache/catalina/connector/TestCoyoteOutputStream.java Modified: tomcat/trunk/java/org/apache/catalina/connector/CoyoteOutputStream.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/connector/CoyoteOutputStream.java?rev=1758276&r1=1758275&r2=1758276&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/connector/CoyoteOutputStream.java (original) +++ tomcat/trunk/java/org/apache/catalina/connector/CoyoteOutputStream.java Mon Aug 29 18:10:36 2016 @@ -17,6 +17,7 @@ package org.apache.catalina.connector; import java.io.IOException; +import java.nio.ByteBuffer; import javax.servlet.ServletOutputStream; import javax.servlet.WriteListener; @@ -97,6 +98,15 @@ public class CoyoteOutputStream extends if (nonBlocking) { checkRegisterForWrite(); } + } + + + public void write(ByteBuffer from) throws IOException { + boolean nonBlocking = checkNonBlockingWrite(); + ob.write(from); + if (nonBlocking) { + checkRegisterForWrite(); + } } Modified: tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java?rev=1758276&r1=1758275&r2=1758276&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java (original) +++ tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java Mon Aug 29 18:10:36 2016 @@ -430,6 +430,17 @@ public class OutputBuffer extends Writer } + public void write(ByteBuffer from) throws IOException { + + if (suspended) { + return; + } + + writeBytes(from); + + } + + private void writeBytes(byte b[], int off, int len) throws IOException { @@ -442,6 +453,24 @@ public class OutputBuffer extends Writer // if called from within flush(), then immediately flush // remaining bytes + if (doFlush) { + bb.flushBuffer(); + } + + } + + + private void writeBytes(ByteBuffer from) throws IOException { + + if (closed) { + return; + } + + bb.append(from); + bytesWritten += from.remaining(); + + // if called from within flush(), then immediately flush + // remaining bytes if (doFlush) { bb.flushBuffer(); } Modified: tomcat/trunk/test/org/apache/catalina/connector/TestCoyoteOutputStream.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/connector/TestCoyoteOutputStream.java?rev=1758276&r1=1758275&r2=1758276&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/connector/TestCoyoteOutputStream.java (original) +++ tomcat/trunk/test/org/apache/catalina/connector/TestCoyoteOutputStream.java Mon Aug 29 18:10:36 2016 @@ -16,7 +16,10 @@ */ package org.apache.catalina.connector; +import java.io.File; import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.channels.FileChannel.MapMode; import java.nio.charset.StandardCharsets; import java.util.concurrent.atomic.AtomicInteger; @@ -99,6 +102,27 @@ public class TestCoyoteOutputStream exte doNonBlockingTest(2, 1, false); } + @Test + public void testWriteWithByteBuffer() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + Context root = tomcat.addContext("", TEMP_DIR); + Tomcat.addServlet(root, "testServlet", new TestServlet()); + root.addServletMapping("/", "testServlet"); + + tomcat.start(); + + ByteChunk bc = new ByteChunk(); + int rc = getUrl("http://localhost:" + getPort() + "/", bc, null, null); + Assert.assertEquals(HttpServletResponse.SC_OK, rc); + File file = new File("test/org/apache/catalina/connector/test_content.txt"); + try (RandomAccessFile raf = new RandomAccessFile(file, "r");) { + ByteChunk expected = new ByteChunk(); + expected.append(raf.getChannel().map(MapMode.READ_ONLY, 0, file.length())); + Assert.assertTrue(expected.equals(bc)); + } + } + private void doNonBlockingTest(int asyncWriteTarget, int syncWriteTarget, boolean useContainerThreadToSetListener) throws Exception { @@ -250,4 +274,20 @@ public class TestCoyoteOutputStream exte } } } + + private static final class TestServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + CoyoteOutputStream os = (CoyoteOutputStream) resp.getOutputStream(); + File file = new File("test/org/apache/catalina/connector/test_content.txt"); + try (RandomAccessFile raf = new RandomAccessFile(file, "r");) { + os.write(raf.getChannel().map(MapMode.READ_ONLY, 0, file.length())); + } + } + + } } Added: tomcat/trunk/test/org/apache/catalina/connector/test_content.txt URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/connector/test_content.txt?rev=1758276&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/connector/test_content.txt (added) +++ tomcat/trunk/test/org/apache/catalina/connector/test_content.txt Mon Aug 29 18:10:36 2016 @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is a test file for the WebappServiceLoader +# It contains comment lines and blank lines + +test content Propchange: tomcat/trunk/test/org/apache/catalina/connector/test_content.txt ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org