Commit cf6a53ee authored by Christian Schudt's avatar Christian Schudt

Simplify/improve NIOConnection.deliver() by removing ByteBufferWriter.

For every packet being delivered the `ByteBufferWriter` creates a new String for every few chars written and puts it to the buffer.

According to my tests it is more efficient to create only one string and put it into the buffer once.
(10-20% better performance)
parent 21f57aa5
/**
* $Revision: $
* $Date: $
*
* Copyright (C) 2005-2008 Jive Software. All rights reserved.
*
* Licensed 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.jivesoftware.openfire.nio;
import org.apache.mina.core.buffer.IoBuffer;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.CharsetEncoder;
/**
* Wrapper on a MINA {@link IoBuffer} that extends the Writer class.
*
* @author Gaston Dombia
*/
public class ByteBufferWriter extends Writer {
private CharsetEncoder encoder;
private IoBuffer ioBuffer;
public ByteBufferWriter(IoBuffer byteBuffer, CharsetEncoder encoder) {
this.encoder = encoder;
this.ioBuffer = byteBuffer;
}
@Override
public void write(char cbuf[], int off, int len) throws IOException {
ioBuffer.putString(new String(cbuf, off, len), encoder);
}
@Override
public void flush() throws IOException {
// Ignore
}
@Override
public void close() throws IOException {
// Ignore
}
}
...@@ -46,7 +46,6 @@ import org.apache.mina.core.filterchain.IoFilterChain; ...@@ -46,7 +46,6 @@ import org.apache.mina.core.filterchain.IoFilterChain;
import org.apache.mina.core.session.IoSession; import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.compression.CompressionFilter; import org.apache.mina.filter.compression.CompressionFilter;
import org.apache.mina.filter.ssl.SslFilter; import org.apache.mina.filter.ssl.SslFilter;
import org.dom4j.io.OutputFormat;
import org.jivesoftware.openfire.Connection; import org.jivesoftware.openfire.Connection;
import org.jivesoftware.openfire.ConnectionCloseListener; import org.jivesoftware.openfire.ConnectionCloseListener;
import org.jivesoftware.openfire.PacketDeliverer; import org.jivesoftware.openfire.PacketDeliverer;
...@@ -61,7 +60,6 @@ import org.jivesoftware.openfire.session.ConnectionSettings; ...@@ -61,7 +60,6 @@ import org.jivesoftware.openfire.session.ConnectionSettings;
import org.jivesoftware.openfire.session.LocalSession; import org.jivesoftware.openfire.session.LocalSession;
import org.jivesoftware.openfire.session.Session; import org.jivesoftware.openfire.session.Session;
import org.jivesoftware.util.JiveGlobals; import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.XMLWriter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.xmpp.packet.Packet; import org.xmpp.packet.Packet;
...@@ -104,7 +102,7 @@ public class NIOConnection implements Connection { ...@@ -104,7 +102,7 @@ public class NIOConnection implements Connection {
* Compression policy currently in use for this connection. * Compression policy currently in use for this connection.
*/ */
private CompressionPolicy compressionPolicy = CompressionPolicy.disabled; private CompressionPolicy compressionPolicy = CompressionPolicy.disabled;
private static ThreadLocal<CharsetEncoder> encoder = new ThreadLocalEncoder(); private static final ThreadLocal<CharsetEncoder> encoder = new ThreadLocalEncoder();
/** /**
* Flag that specifies if the connection should be considered closed. Closing a NIO connection * Flag that specifies if the connection should be considered closed. Closing a NIO connection
...@@ -303,10 +301,7 @@ public class NIOConnection implements Connection { ...@@ -303,10 +301,7 @@ public class NIOConnection implements Connection {
if (!ioSession.isConnected()) { if (!ioSession.isConnected()) {
throw new IOException("Connection reset/closed by peer"); throw new IOException("Connection reset/closed by peer");
} }
XMLWriter xmlSerializer = buffer.putString(packet.getElement().asXML(), encoder.get());
new XMLWriter(new ByteBufferWriter(buffer, encoder.get()), new OutputFormat());
xmlSerializer.write(packet.getElement());
xmlSerializer.flush();
if (flashClient) { if (flashClient) {
buffer.put((byte) '\0'); buffer.put((byte) '\0');
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment