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.servlet;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import org.eclipse.jetty.http.MimeTypes;
23  import org.eclipse.jetty.io.Buffer;
24  import org.eclipse.jetty.io.nio.DirectNIOBuffer;
25  import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
26  import org.eclipse.jetty.io.nio.NIOBuffer;
27  import org.eclipse.jetty.server.Connector;
28  import org.eclipse.jetty.server.HttpConnection;
29  import org.eclipse.jetty.server.ResourceCache;
30  import org.eclipse.jetty.server.nio.NIOConnector;
31  import org.eclipse.jetty.util.log.Log;
32  import org.eclipse.jetty.util.resource.Resource;
33  
34  class NIOResourceCache extends ResourceCache
35  {
36      boolean _useFileMappedBuffer;
37      
38      /* ------------------------------------------------------------ */
39      public NIOResourceCache(MimeTypes mimeTypes)
40      {
41          super(mimeTypes);
42      }
43  
44      /* ------------------------------------------------------------ */
45      protected void fill(Content content) throws IOException
46      {
47          Buffer buffer=null;
48          Resource resource=content.getResource();
49          long length=resource.length();
50  
51          if (_useFileMappedBuffer && resource.getFile()!=null) 
52          {            
53  	        buffer = new DirectNIOBuffer(resource.getFile());
54          } 
55          else 
56          {
57              InputStream is = resource.getInputStream();
58              try
59              {
60                  Connector connector = HttpConnection.getCurrentConnection().getConnector();
61  		 buffer = ((NIOConnector)connector).getUseDirectBuffers()?
62  			 (NIOBuffer)new DirectNIOBuffer((int)length):
63  			 (NIOBuffer)new IndirectNIOBuffer((int)length);
64              }
65              catch(OutOfMemoryError e)
66              {
67                  Log.warn(e.toString());
68                  Log.debug(e);
69  		buffer = new IndirectNIOBuffer((int) length);
70              }
71              buffer.readFrom(is,(int)length);
72              is.close();
73          }
74          content.setBuffer(buffer);
75      }
76  
77      public boolean isUseFileMappedBuffer()
78      {
79          return _useFileMappedBuffer;
80      }
81  
82      public void setUseFileMappedBuffer(boolean useFileMappedBuffer)
83      {
84          _useFileMappedBuffer = useFileMappedBuffer;
85      }
86  }