View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  /**
15   * 
16   */
17  package org.eclipse.jetty.server.nio;
18  
19  import org.eclipse.jetty.io.Buffer;
20  import org.eclipse.jetty.io.nio.DirectNIOBuffer;
21  import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
22  import org.eclipse.jetty.io.nio.NIOBuffer;
23  import org.eclipse.jetty.server.AbstractConnector;
24  
25  /* ------------------------------------------------------------ */
26  /**
27   * 
28   *
29   */
30  public abstract class AbstractNIOConnector extends AbstractConnector implements NIOConnector
31  {
32      private boolean _useDirectBuffers=true;
33   
34      /* ------------------------------------------------------------------------------- */
35      public boolean getUseDirectBuffers()
36      {
37          return _useDirectBuffers;
38      }
39  
40      /* ------------------------------------------------------------------------------- */
41      /**
42       * @param direct If True (the default), the connector can use NIO direct buffers.
43       * Some JVMs have memory management issues (bugs) with direct buffers.
44       */
45      public void setUseDirectBuffers(boolean direct)
46      {
47          _useDirectBuffers=direct;
48      }
49      
50  
51      // TODO
52      // Header buffers always byte array buffers (efficiency of random access)
53      // There are lots of things to consider here... DIRECT buffers are faster to
54      // send but more expensive to build and access! so we have choices to make...
55      // + headers are constructed bit by bit and parsed bit by bit, so INDiRECT looks
56      // good for them.   
57      // + but will a gather write of an INDIRECT header with a DIRECT body be any good?
58      // this needs to be benchmarked.
59      // + Will it be possible to get a DIRECT header buffer just for the gather writes of
60      // content from file mapped buffers?  
61      // + Are gather writes worth the effort?  Maybe they will work well with two INDIRECT
62      // buffers being copied into a single kernel buffer?
63  
64      /* ------------------------------------------------------------------------------- */
65      public Buffer newRequestBuffer(int size)
66      {
67          return _useDirectBuffers?new DirectNIOBuffer(size):new IndirectNIOBuffer(size);
68      }
69      
70      /* ------------------------------------------------------------------------------- */
71      public Buffer newRequestHeader(int size)
72      {
73          return new IndirectNIOBuffer(size);
74      }
75  
76      /* ------------------------------------------------------------------------------- */
77      public Buffer newResponseBuffer(int size)
78      {
79          return _useDirectBuffers?new DirectNIOBuffer(size):new IndirectNIOBuffer(size);
80      }
81      
82      /* ------------------------------------------------------------------------------- */
83      public Buffer newResponseHeader(int size)
84      {
85          // TODO maybe can be direct?
86          return new IndirectNIOBuffer(size);
87      }
88  
89  }