chris@1: /*
chris@1: * SONEWS News Server
chris@1: * see AUTHORS for the list of contributors
chris@1: *
chris@1: * This program is free software: you can redistribute it and/or modify
chris@1: * it under the terms of the GNU General Public License as published by
chris@1: * the Free Software Foundation, either version 3 of the License, or
chris@1: * (at your option) any later version.
chris@1: *
chris@1: * This program is distributed in the hope that it will be useful,
chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
chris@1: * GNU General Public License for more details.
chris@1: *
chris@1: * You should have received a copy of the GNU General Public License
chris@1: * along with this program. If not, see .
chris@1: */
chris@1:
chris@1: package org.sonews.daemon;
chris@1:
chris@1: import java.nio.ByteBuffer;
chris@1: import java.nio.channels.ClosedChannelException;
chris@1: import java.util.ArrayList;
chris@1: import java.util.List;
chris@1:
chris@1: /**
chris@1: * Class holding ByteBuffers for SocketChannels/NNTPConnection.
chris@1: * Due to the complex nature of AIO/NIO we must properly handle the line
chris@1: * buffers for the input and output of the SocketChannels.
chris@1: * @author Christian Lins
chris@1: * @since sonews/0.5.0
chris@1: */
cli@37: public class ChannelLineBuffers
chris@1: {
cli@25:
cli@37: /**
cli@37: * Size of one small buffer;
cli@37: * per default this is 512 bytes to fit one standard line.
cli@37: */
cli@37: public static final int BUFFER_SIZE = 512;
cli@37: private static int maxCachedBuffers = 2048; // Cached buffers maximum
cli@37: private static final List freeSmallBuffers = new ArrayList(maxCachedBuffers);
chris@1:
cli@37: /**
cli@37: * Allocates a predefined number of direct ByteBuffers (allocated via
cli@37: * ByteBuffer.allocateDirect()). This method is Thread-safe, but should only
cli@37: * called at startup.
cli@37: */
cli@37: public static void allocateDirect()
cli@37: {
cli@37: synchronized (freeSmallBuffers) {
cli@37: for (int n = 0; n < maxCachedBuffers; n++) {
cli@37: ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
cli@37: freeSmallBuffers.add(buffer);
cli@37: }
cli@37: }
cli@37: }
cli@37: private ByteBuffer inputBuffer = newLineBuffer();
cli@37: private List outputBuffers = new ArrayList();
chris@1:
cli@37: /**
cli@37: * Add the given ByteBuffer to the list of buffers to be send to the client.
cli@37: * This method is Thread-safe.
cli@37: * @param buffer
cli@37: * @throws java.nio.channels.ClosedChannelException If the client channel was
cli@37: * already closed.
cli@37: */
cli@37: public void addOutputBuffer(ByteBuffer buffer)
cli@37: throws ClosedChannelException
cli@37: {
cli@37: if (outputBuffers == null) {
cli@37: throw new ClosedChannelException();
cli@37: }
chris@1:
cli@37: synchronized (outputBuffers) {
cli@37: outputBuffers.add(buffer);
cli@37: }
cli@37: }
chris@1:
cli@37: /**
cli@37: * Currently a channel has only one input buffer. This *may* be a bottleneck
cli@37: * and should investigated in the future.
cli@37: * @param channel
cli@37: * @return The input buffer associated with given channel.
cli@37: */
cli@37: public ByteBuffer getInputBuffer()
cli@37: {
cli@37: return inputBuffer;
cli@37: }
chris@1:
cli@37: /**
cli@37: * Returns the current output buffer for writing(!) to SocketChannel.
cli@37: * @param channel
cli@37: * @return The next input buffer that contains unprocessed data or null
cli@37: * if the connection was closed or there are no more unprocessed buffers.
cli@37: */
cli@37: public ByteBuffer getOutputBuffer()
cli@37: {
cli@37: synchronized (outputBuffers) {
cli@37: if (outputBuffers == null || outputBuffers.isEmpty()) {
cli@37: return null;
cli@37: } else {
cli@37: ByteBuffer buffer = outputBuffers.get(0);
cli@37: if (buffer.remaining() == 0) {
cli@37: outputBuffers.remove(0);
cli@37: // Add old buffers to the list of free buffers
cli@37: recycleBuffer(buffer);
cli@37: buffer = getOutputBuffer();
cli@37: }
cli@37: return buffer;
cli@37: }
cli@37: }
cli@37: }
chris@1:
cli@37: /**
cli@37: * @return false if there are output buffers pending to be written to the
cli@37: * client.
cli@37: */
cli@37: boolean isOutputBufferEmpty()
cli@37: {
cli@37: synchronized (outputBuffers) {
cli@37: return outputBuffers.isEmpty();
cli@37: }
cli@37: }
chris@1:
cli@37: /**
cli@37: * Goes through the input buffer of the given channel and searches
cli@37: * for next line terminator. If a '\n' is found, the bytes up to the
cli@37: * line terminator are returned as array of bytes (the line terminator
cli@37: * is omitted). If none is found the method returns null.
cli@37: * @param channel
cli@37: * @return A ByteBuffer wrapping the line.
cli@37: */
cli@37: ByteBuffer nextInputLine()
cli@37: {
cli@37: if (inputBuffer == null) {
cli@37: return null;
cli@37: }
chris@1:
cli@37: synchronized (inputBuffer) {
cli@37: ByteBuffer buffer = inputBuffer;
cli@37:
cli@37: // Mark the current write position
cli@37: int mark = buffer.position();
cli@37:
cli@37: // Set position to 0 and limit to current position
cli@37: buffer.flip();
cli@37:
cli@37: ByteBuffer lineBuffer = newLineBuffer();
cli@37:
cli@37: while (buffer.position() < buffer.limit()) {
cli@37: byte b = buffer.get();
cli@37: if (b == 10) // '\n'
cli@37: {
cli@37: // The bytes between the buffer's current position and its limit,
cli@37: // if any, are copied to the beginning of the buffer. That is, the
cli@37: // byte at index p = position() is copied to index zero, the byte at
cli@37: // index p + 1 is copied to index one, and so forth until the byte
cli@37: // at index limit() - 1 is copied to index n = limit() - 1 - p.
cli@37: // The buffer's position is then set to n+1 and its limit is set to
cli@37: // its capacity.
cli@37: buffer.compact();
cli@37:
cli@37: lineBuffer.flip(); // limit to position, position to 0
cli@37: return lineBuffer;
cli@37: } else {
cli@37: lineBuffer.put(b);
cli@37: }
cli@37: }
cli@37:
cli@37: buffer.limit(BUFFER_SIZE);
cli@37: buffer.position(mark);
cli@37:
cli@37: if (buffer.hasRemaining()) {
cli@37: return null;
cli@37: } else {
cli@37: // In the first 512 was no newline found, so the input is not standard
cli@37: // compliant. We return the current buffer as new line and add a space
cli@37: // to the beginning of the next line which corrects some overlong header
cli@37: // lines.
cli@37: inputBuffer = newLineBuffer();
cli@37: inputBuffer.put((byte) ' ');
cli@37: buffer.flip();
cli@37: return buffer;
cli@37: }
cli@37: }
cli@37: }
cli@37:
cli@37: /**
cli@37: * Returns a at least 512 bytes long ByteBuffer ready for usage.
cli@37: * The method first try to reuse an already allocated (cached) buffer but
cli@37: * if that fails returns a newly allocated direct buffer.
cli@37: * Use recycleBuffer() method when you do not longer use the allocated buffer.
cli@37: */
cli@37: static ByteBuffer newLineBuffer()
cli@37: {
cli@37: ByteBuffer buf = null;
cli@37: synchronized (freeSmallBuffers) {
cli@37: if (!freeSmallBuffers.isEmpty()) {
cli@37: buf = freeSmallBuffers.remove(0);
cli@37: }
cli@37: }
cli@37:
cli@37: if (buf == null) {
cli@37: // Allocate a non-direct buffer
cli@37: buf = ByteBuffer.allocate(BUFFER_SIZE);
cli@37: }
cli@37:
cli@37: assert buf.position() == 0;
cli@37: assert buf.limit() >= BUFFER_SIZE;
cli@37:
cli@37: return buf;
cli@37: }
cli@37:
cli@37: /**
cli@37: * Adds the given buffer to the list of free buffers if it is a valuable
cli@37: * direct allocated buffer.
cli@37: * @param buffer
cli@37: */
cli@37: public static void recycleBuffer(ByteBuffer buffer)
cli@37: {
cli@37: assert buffer != null;
cli@37:
cli@37: if (buffer.isDirect()) {
cli@37: assert buffer.capacity() >= BUFFER_SIZE;
cli@37:
cli@37: // Add old buffers to the list of free buffers
cli@37: synchronized (freeSmallBuffers) {
cli@37: buffer.clear(); // Set position to 0 and limit to capacity
cli@37: freeSmallBuffers.add(buffer);
cli@37: }
cli@37: } // if(buffer.isDirect())
cli@37: }
cli@37:
cli@37: /**
cli@37: * Recycles all buffers of this ChannelLineBuffers object.
cli@37: */
cli@37: public void recycleBuffers()
cli@37: {
cli@37: synchronized (inputBuffer) {
cli@37: recycleBuffer(inputBuffer);
cli@37: this.inputBuffer = null;
cli@37: }
cli@37:
cli@37: synchronized (outputBuffers) {
cli@37: for (ByteBuffer buf : outputBuffers) {
cli@37: recycleBuffer(buf);
cli@37: }
cli@37: outputBuffers = null;
cli@37: }
cli@37: }
chris@1: }