Incrementally try larger buffer sizes.
byte[] bbuf = new byte[1024];
byte[] bbuf = new byte[2048];
byte[] bbuf = new byte[4096];
byte[] bbuf = new byte[8192];
byte[] bbuf = new byte[16834];
Then narrow in on the size. I suspect that 8192 (or there abouts will
be best.
Also,
response.setContentLength( (int)file.length() );
May be expensive, see how long it takes.
Regards,
Dave
On Jun 23, 2008, at 5:19 PM, Mathias P.W Nilsson wrote:
Hi!
I have made a resource servlet to handle static content outside of
tomcat,
wicket. It looks like this
package se.edgesoft.hairless.web.resource;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.support.WebApplicationContextUtils
;
import se.edgesoft.hairless.application.HairlessApplicationSettings;
/**
* Resource servlet for getting images and flash movies
* @author Mathias Nilsson
*
*/
public class FileResourceServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static ServletConfig config;
File file;
protected Object getBean(String name) {
Object obj =
WebApplicationContextUtils
.getWebApplicationContext(config.getServletContext()).getBean(name);
return obj;
}
public void destroy() {
config = null;
}
public ServletConfig getServletConfig() {
return config;
}
public String getServletInfo() {
return "Resource servlet for Hairless";
}
public void init(ServletConfig servletConfig ) throws
ServletException {
config = servletConfig;
}
public HairlessApplicationSettings
getHairlessApplicationSettings(){
return (HairlessApplicationSettings)getBean(
"hairlessApplicationSettings" );
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse
response) throws ServletException, IOException {
try {
file = new File(
getHairlessApplicationSettings().getFileResourcePath() ,
request.getRequestURI().replace( request.getContextPath(), "" ) );
ServletContext context =
getServletConfig().getServletContext();
String mimetype =
context.getMimeType( file.getAbsolutePath()
);
response.setContentType( (mimetype != null) ? mimetype :
"application/octet-stream" );
response.setContentLength( (int)file.length() );
int length = 0;
ServletOutputStream op = response.getOutputStream();
byte[] bbuf = new byte[1024];
DataInputStream in = new DataInputStream(new
FileInputStream(file));
while ((in != null) && ((length = in.read(bbuf)) !=
-1))
{
op.write(bbuf,0,length);
}
in.close();
op.flush();
op.close();
} catch (Exception e) {
//e.printStackTrace();
}
}
}
Maybe I'm missing something but I think it is a little slow. Should
I take
something more into consideration?
--
View this message in context:
http://www.nabble.com/Faster-resource-servlet-tp18079759p18079759.html
Sent from the Tomcat - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]