You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2015/04/10 16:08:57 UTC
svn commit: r1672671 - in /tomcat/trunk/java/org/apache/tomcat/util:
buf/ByteBufferUtils.java net/SecureNioChannel.java
Author: markt
Date: Fri Apr 10 14:08:57 2015
New Revision: 1672671
URL: http://svn.apache.org/r1672671
Log:
Reduce code duplication
Added:
tomcat/trunk/java/org/apache/tomcat/util/buf/ByteBufferUtils.java (with props)
Modified:
tomcat/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java
Added: tomcat/trunk/java/org/apache/tomcat/util/buf/ByteBufferUtils.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/ByteBufferUtils.java?rev=1672671&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/buf/ByteBufferUtils.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/util/buf/ByteBufferUtils.java Fri Apr 10 14:08:57 2015
@@ -0,0 +1,71 @@
+/*
+ * 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.tomcat.util.buf;
+
+import java.nio.ByteBuffer;
+
+public class ByteBufferUtils {
+
+ private ByteBufferUtils() {
+ // Hide the default constructor since this is a utility class.
+ }
+
+
+ /**
+ * Default byte buffer expansion. Doubles current size. Buffers are assumed
+ * to be in 'write to' mode since there would be no need to expand a buffer
+ * while it was in 'read from' mode.
+ *
+ * @param in Buffer to expand
+ * @return The expanded buffer with any data from the input buffer copied
+ * in to it
+ */
+ public static ByteBuffer expand(ByteBuffer in) {
+ return expand(in, in.capacity() * 2);
+ }
+
+
+ /**
+ * Expands buffer to the given size unless it is already as big or bigger.
+ * Buffers are assumed to be in 'write to' mode since there would be no need
+ * to expand a buffer while it was in 'read from' mode.
+ *
+ * @param in Buffer to expand
+ * @param newSize The size t which the buffer should be expanded
+ * @return The expanded buffer with any data from the input buffer
+ * copied in to it or the original buffer if there was no
+ * need for expansion
+ */
+ public static ByteBuffer expand(ByteBuffer in, int newSize) {
+ if (in.capacity() >= newSize) {
+ return in;
+ }
+
+ ByteBuffer out;
+ if (in.isDirect()) {
+ out = ByteBuffer.allocateDirect(newSize);
+ } else {
+ out = ByteBuffer.allocate(newSize);
+ }
+
+ // Copy data
+ in.flip();
+ out.put(in);
+
+ return out;
+ }
+}
Propchange: tomcat/trunk/java/org/apache/tomcat/util/buf/ByteBufferUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: tomcat/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java?rev=1672671&r1=1672670&r2=1672671&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/SecureNioChannel.java Fri Apr 10 14:08:57 2015
@@ -31,6 +31,7 @@ import javax.net.ssl.SSLEngineResult.Sta
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
+import org.apache.tomcat.util.buf.ByteBufferUtils;
import org.apache.tomcat.util.net.SNIExtractor.SNIResult;
import org.apache.tomcat.util.res.StringManager;
@@ -227,8 +228,10 @@ public class SecureNioChannel extends Ni
if (netInBuffer == null) {
if (sp.getDirectSslBuffer()) {
netInBuffer = ByteBuffer.allocateDirect(DEFAULT_NET_BUFFER_SIZE);
+ netOutBuffer = ByteBuffer.allocateDirect(DEFAULT_NET_BUFFER_SIZE);
} else {
netInBuffer = ByteBuffer.allocate(DEFAULT_NET_BUFFER_SIZE);
+ netOutBuffer = ByteBuffer.allocateDirect(DEFAULT_NET_BUFFER_SIZE);
}
}
@@ -238,15 +241,10 @@ public class SecureNioChannel extends Ni
while (extractor.getResult() == SNIResult.UNDERFLOW) {
// extractor needed more data to process but netInBuffer was full so
// double the size of the buffer and read some more data.
- ByteBuffer newNetInBuffer;
- if (sp.getDirectSslBuffer()) {
- newNetInBuffer = ByteBuffer.allocateDirect(netInBuffer.capacity() * 2);
- } else {
- newNetInBuffer = ByteBuffer.allocate(netInBuffer.capacity() * 2);
- }
- netInBuffer.flip();
- newNetInBuffer.put(netInBuffer);
- netInBuffer = newNetInBuffer;
+ log.info(sm.getString("channel.nio.ssl.expandNetInBuffer",
+ Integer.toString(netInBuffer.capacity() * 2)));
+
+ netInBuffer = ByteBufferUtils.expand(netInBuffer);
sc.read(netInBuffer);
extractor = new SNIExtractor(netInBuffer);
}
@@ -276,7 +274,13 @@ public class SecureNioChannel extends Ni
// Ensure the application buffers (which have to be created earlier) are
// big enough.
bufHandler.expand(sslEngine.getSession().getApplicationBufferSize());
- expandNetBuffers(sslEngine.getSession().getPacketBufferSize(), sp.getDirectSslBuffer());
+ if (netOutBuffer.capacity() < sslEngine.getSession().getApplicationBufferSize()) {
+ // Info for now as we may need to increase DEFAULT_NET_BUFFER_SIZE
+ log.info(sm.getString("channel.nio.ssl.expandNetOutBuffer",
+ Integer.toString(sslEngine.getSession().getApplicationBufferSize())));
+ }
+ netInBuffer = ByteBufferUtils.expand(netInBuffer, sslEngine.getSession().getPacketBufferSize());
+ netOutBuffer = ByteBufferUtils.expand(netOutBuffer, sslEngine.getSession().getPacketBufferSize());
// Set limit and position to expected values
netOutBuffer.position(0);
@@ -290,48 +294,6 @@ public class SecureNioChannel extends Ni
}
- private void expandNetBuffers(int newSize, boolean direct) {
-
- // The input buffer will always have been created by the time this
- // method is called.
- if (netInBuffer.capacity() < newSize) {
- // Info as we may need to increase DEFAULT_NET_BUFFER_SIZE
- log.info(sm.getString("channel.nio.ssl.expandNetInBuffer", Integer.toString(newSize)));
- ByteBuffer newInBuffer;
- if (direct) {
- newInBuffer = ByteBuffer.allocateDirect(newSize);
- } else {
- newInBuffer = ByteBuffer.allocate(newSize);
- }
- // Need to expand the buffers, making sure no data is lost.
- netInBuffer.flip();
- newInBuffer.put(netInBuffer);
- netInBuffer = newInBuffer;
- }
-
- if (netOutBuffer == null) {
- if (direct) {
- netOutBuffer = ByteBuffer.allocateDirect(newSize);
- } else {
- netOutBuffer = ByteBuffer.allocate(newSize);
- }
- } else if (netOutBuffer.capacity() < newSize) {
- // Info as we may need to increase DEFAULT_NET_BUFFER_SIZE
- log.info(sm.getString("channel.nio.ssl.expandNetOutBuffer", Integer.toString(newSize)));
- // Need to expand the buffers, making sure no data is lost.
- ByteBuffer newOutBuffer;
- if (direct) {
- newOutBuffer = ByteBuffer.allocateDirect(newSize);
- } else {
- newOutBuffer = ByteBuffer.allocate(newSize);
- }
- netOutBuffer.flip();
- newOutBuffer.put(netOutBuffer);
- netOutBuffer = newOutBuffer;
- }
- }
-
-
/**
* Force a blocking handshake to take place for this key.
* This requires that both network and application buffers have been emptied out prior to this call taking place, or a
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org