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.NIOBuffer;
22  import org.mortbay.jetty.AbstractConnector;
23  
24  /* ------------------------------------------------------------ */
25  /**
26   * @author gregw
27   *
28   */
29  public abstract class AbstractNIOConnector extends AbstractConnector implements NIOConnector
30  {
31      private boolean _useDirectBuffers=true;
32   
33      /* ------------------------------------------------------------------------------- */
34      public boolean getUseDirectBuffers()
35      {
36          return _useDirectBuffers;
37      }
38  
39      /* ------------------------------------------------------------------------------- */
40      /**
41       * @param direct If True (the default), the connector can use NIO direct buffers.
42       * Some JVMs have memory management issues (bugs) with direct buffers.
43       */
44      public void setUseDirectBuffers(boolean direct)
45      {
46          _useDirectBuffers=direct;
47      }
48  
49      /* ------------------------------------------------------------------------------- */
50      protected Buffer newBuffer(int size)
51      {
52          // TODO
53          // Header buffers always byte array buffers (efficiency of random access)
54          // There are lots of things to consider here... DIRECT buffers are faster to
55          // send but more expensive to build and access! so we have choices to make...
56          // + headers are constructed bit by bit and parsed bit by bit, so INDiRECT looks
57          // good for them.   
58          // + but will a gather write of an INDIRECT header with a DIRECT body be any good?
59          // this needs to be benchmarked.
60          // + Will it be possible to get a DIRECT header buffer just for the gather writes of
61          // content from file mapped buffers?  
62          // + Are gather writes worth the effort?  Maybe they will work well with two INDIRECT
63          // buffers being copied into a single kernel buffer?
64          // 
65          Buffer buf = null;
66          if (size==getHeaderBufferSize())
67              buf= new NIOBuffer(size, NIOBuffer.INDIRECT);
68          else
69              buf = new NIOBuffer(size, _useDirectBuffers?NIOBuffer.DIRECT:NIOBuffer.INDIRECT);
70          return buf;
71      }
72      
73  
74  }