private void tlsWrite(ByteBuffer buf) throws IOException {
        ByteBuffer tlsBuffer;
        ByteBuffer tlsOutput;
        do {
            // TODO Consider optimizing by not creating new instances each time
            tlsBuffer = ByteBuffer.allocate(Math.min(buf.remaining(), wrapper.getAppBuffSize()));
            tlsOutput = ByteBuffer.allocate(wrapper.getNetBuffSize());

            while (tlsBuffer.hasRemaining() && buf.hasRemaining()) {
                tlsBuffer.put(buf.get());
            }

            tlsBuffer.flip();
            wrapper.wrap(tlsBuffer, tlsOutput);

            tlsOutput.flip();
            writeToSocket(tlsOutput);

            tlsOutput.clear();
        } while (buf.hasRemaining());
    }
<code>TLSWrapper</code> is a TLS wrapper for connections requiring TLS protocol.
xmppserver/src/main/java/org/jivesoftware/openfire/net/TLSStreamWriter.java