View Javadoc

    1   //========================================================================
    2   //Copyright 2004-2008 Mort Bay Consulting Pty. Ltd.
    3   //------------------------------------------------------------------------
    4   //Licensed under the Apache License, Version 2.0 (the "License");
    5   //you may not use this file except in compliance with the License.
    6   //You may obtain a copy of the License at
    7   //http://www.apache.org/licenses/LICENSE-2.0
    8   //Unless required by applicable law or agreed to in writing, software
    9   //distributed under the License is distributed on an "AS IS" BASIS,
    10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  //See the License for the specific language governing permissions and
    12  //limitations under the License.
    13  //========================================================================
    14
    15  /**
    16   *
    17   */
    18  package org.mortbay.jetty.nio;
    19
    20  import org.mortbay.io.Buffer;
    21  import org.mortbay.io.nio.DirectNIOBuffer;
    22  import org.mortbay.io.nio.IndirectNIOBuffer;
    23  import org.mortbay.io.nio.NIOBuffer;
    24  import org.mortbay.jetty.AbstractConnector;
    25
    26  /* ------------------------------------------------------------ */
    27  /**
    28   * @author gregw
    29   *
    30   */
    31  public abstract class AbstractNIOConnector extends AbstractConnector implements NIOConnector
    32  {
    33      private boolean _useDirectBuffers=true;
    34
    35      /* ------------------------------------------------------------------------------- */
    36      public boolean getUseDirectBuffers()
    37      {
    38          return _useDirectBuffers;
    39      }
    40
    41      /* ------------------------------------------------------------------------------- */
    42      /**
    43       * @param direct If True (the default), the connector can use NIO direct buffers.
    44       * Some JVMs have memory management issues (bugs) with direct buffers.
    45       */
    46      public void setUseDirectBuffers(boolean direct)
    47      {
    48          _useDirectBuffers=direct;
    49      }
    50
    51      /* ------------------------------------------------------------------------------- */
    52      protected Buffer newBuffer(int size)
    53      {
    54          // TODO
    55          // Header buffers always byte array buffers (efficiency of random access)
    56          // There are lots of things to consider here... DIRECT buffers are faster to
    57          // send but more expensive to build and access! so we have choices to make...
    58          // + headers are constructed bit by bit and parsed bit by bit, so INDiRECT looks
    59          // good for them.
    60          // + but will a gather write of an INDIRECT header with a DIRECT body be any good?
    61          // this needs to be benchmarked.
    62          // + Will it be possible to get a DIRECT header buffer just for the gather writes of
    63          // content from file mapped buffers?
    64          // + Are gather writes worth the effort?  Maybe they will work well with two INDIRECT
    65          // buffers being copied into a single kernel buffer?
    66          //
    67          Buffer buf = null;
    68          if (size==getHeaderBufferSize())
    69              buf= new IndirectNIOBuffer(size);
    70          else
    71              buf = _useDirectBuffers
    72                  ?(NIOBuffer)new DirectNIOBuffer(size)
    73                  :(NIOBuffer)new IndirectNIOBuffer(size);
    74          return buf;
    75      }
    76
    77
    78  }