You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by vi...@apache.org on 2016/09/08 11:43:33 UTC
svn commit: r1759785 - in /tomcat/trunk: java/org/apache/catalina/connector/
java/org/apache/tomcat/util/buf/ test/org/apache/catalina/connector/
Author: violetagg
Date: Thu Sep 8 11:43:32 2016
New Revision: 1759785
URL: http://svn.apache.org/viewvc?rev=1759785&view=rev
Log:
Introduce a new method CoyoteInputStream.read(ByteBuffer).
Added:
tomcat/trunk/test/org/apache/catalina/connector/TestCoyoteInputStream.java (with props)
Modified:
tomcat/trunk/java/org/apache/catalina/connector/CoyoteInputStream.java
tomcat/trunk/java/org/apache/catalina/connector/InputBuffer.java
tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java
Modified: tomcat/trunk/java/org/apache/catalina/connector/CoyoteInputStream.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/connector/CoyoteInputStream.java?rev=1759785&r1=1759784&r2=1759785&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/connector/CoyoteInputStream.java (original)
+++ tomcat/trunk/java/org/apache/catalina/connector/CoyoteInputStream.java Thu Sep 8 11:43:32 2016
@@ -17,6 +17,7 @@
package org.apache.catalina.connector;
import java.io.IOException;
+import java.nio.ByteBuffer;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
@@ -183,6 +184,47 @@ public class CoyoteInputStream extends S
}
}
+
+ /**
+ * Transfers bytes from the buffer to the specified ByteBuffer. After the
+ * operation the position of the ByteBuffer will be returned to the one
+ * before the operation, the limit will be the position incremented by
+ * the number of the transfered bytes.
+ *
+ * @param to the ByteBuffer into which bytes are to be written.
+ * @return an integer specifying the actual number of bytes read, or -1 if
+ * the end of the stream is reached
+ * @throws IOException if an input or output exception has occurred
+ */
+ public int read(final ByteBuffer b) throws IOException {
+ checkNonBlockingRead();
+
+ if (SecurityUtil.isPackageProtectionEnabled()) {
+ try {
+ Integer result = AccessController
+ .doPrivileged(new PrivilegedExceptionAction<Integer>() {
+
+ @Override
+ public Integer run() throws IOException {
+ Integer integer = Integer.valueOf(ib.read(b));
+ return integer;
+ }
+
+ });
+ return result.intValue();
+ } catch (PrivilegedActionException pae) {
+ Exception e = pae.getException();
+ if (e instanceof IOException) {
+ throw (IOException) e;
+ } else {
+ throw new RuntimeException(e.getMessage(), e);
+ }
+ }
+ } else {
+ return ib.read(b);
+ }
+ }
+
@Override
public int readLine(byte[] b, int off, int len) throws IOException {
Modified: tomcat/trunk/java/org/apache/catalina/connector/InputBuffer.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/connector/InputBuffer.java?rev=1759785&r1=1759784&r2=1759785&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/connector/InputBuffer.java (original)
+++ tomcat/trunk/java/org/apache/catalina/connector/InputBuffer.java Thu Sep 8 11:43:32 2016
@@ -18,6 +18,7 @@ package org.apache.catalina.connector;
import java.io.IOException;
import java.io.Reader;
+import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.security.AccessController;
import java.security.PrivilegedActionException;
@@ -337,6 +338,26 @@ public class InputBuffer extends Reader
}
+ /**
+ * Transfers bytes from the buffer to the specified ByteBuffer. After the
+ * operation the position of the ByteBuffer will be returned to the one
+ * before the operation, the limit will be the position incremented by
+ * the number of the transfered bytes.
+ *
+ * @param to the ByteBuffer into which bytes are to be written.
+ * @return an integer specifying the actual number of bytes read, or -1 if
+ * the end of the stream is reached
+ * @throws IOException if an input or output exception has occurred
+ */
+ public int read(ByteBuffer b) throws IOException {
+ if (closed) {
+ throw new IOException(sm.getString("inputBuffer.streamClosed"));
+ }
+
+ return bb.substract(b);
+ }
+
+
// ------------------------------------------------- Chars Handling Methods
Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java?rev=1759785&r1=1759784&r2=1759785&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java Thu Sep 8 11:43:32 2016
@@ -466,6 +466,30 @@ public final class ByteChunk implements
}
+ /**
+ * Transfers bytes from the buffer to the specified ByteBuffer. After the
+ * operation the position of the ByteBuffer will be returned to the one
+ * before the operation, the limit will be the position incremented by
+ * the number of the transfered bytes.
+ *
+ * @param to the ByteBuffer into which bytes are to be written.
+ * @return an integer specifying the actual number of bytes read, or -1 if
+ * the end of the stream is reached
+ * @throws IOException if an input or output exception has occurred
+ */
+ public int substract(ByteBuffer to) throws IOException {
+ if (checkEof()) {
+ return -1;
+ }
+ int n = Math.min(to.remaining(), getLength());
+ to.put(buff, start, n);
+ to.limit(to.position());
+ to.position(to.position() - n);
+ start += n;
+ return n;
+ }
+
+
private boolean checkEof() throws IOException {
if ((end - start) == 0) {
if (in == null) {
Added: tomcat/trunk/test/org/apache/catalina/connector/TestCoyoteInputStream.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/connector/TestCoyoteInputStream.java?rev=1759785&view=auto
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/connector/TestCoyoteInputStream.java (added)
+++ tomcat/trunk/test/org/apache/catalina/connector/TestCoyoteInputStream.java Thu Sep 8 11:43:32 2016
@@ -0,0 +1,72 @@
+/*
+ * 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.
+ */
+package org.apache.catalina.connector;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.catalina.Context;
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.tomcat.util.buf.ByteChunk;
+
+public class TestCoyoteInputStream extends TomcatBaseTest {
+
+ @Test
+ public void testReadWithByteBuffer() throws Exception {
+ Tomcat tomcat = getTomcatInstance();
+
+ Context root = tomcat.addContext("", TEMP_DIR);
+ Tomcat.addServlet(root, "testServlet", new TestServlet());
+ root.addServletMappingDecoded("/", "testServlet");
+
+ tomcat.start();
+
+ ByteChunk bc = new ByteChunk();
+ String requestBody = "HelloWorld";
+ int rc = postUrl(requestBody.getBytes(StandardCharsets.UTF_8),
+ "http://localhost:" + getPort() + "/", bc, null);
+ Assert.assertEquals(HttpServletResponse.SC_OK, rc);
+ Assert.assertTrue(requestBody.equals(bc.toString()));
+ }
+
+ private static final class TestServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
+ CoyoteInputStream is = (CoyoteInputStream) req.getInputStream();
+ ByteBuffer buffer = ByteBuffer.allocate(256);
+ is.read(buffer);
+ CoyoteOutputStream os = (CoyoteOutputStream) resp.getOutputStream();
+ os.write(buffer);
+ }
+
+ }
+
+}
Propchange: tomcat/trunk/test/org/apache/catalina/connector/TestCoyoteInputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org