开发者

Bytes that should be written to ServletOutPutStream

I am wondering if there are specific rules or best practices when creating a byte[] whose content is written to the ServletOutputStream?

  byte[] buffer = new byte[1024];   
  int r = 0;   
  try {   
  in = new BufferedInputStream(new FileInputStream(new File("/path/to/the/some/file")));   
  sos = response.getOutputStream();   
  while ((r = in.read(buffer, 0, buffer.length)) != -1) {   
  sos.write(buffer, 0, r);   

In the code above, byte[] lenth is 1024. If I am running my servlet in tomcat, do I need to match the length of the buffer with that of the tomcat buffer? Or it doesn't really matter? Tomcat default buffer size maybe 4096 and my byte[] can be lets say 20000

Not sure if question makes sense but I am seeing that ever since I have increased the byte[] lenth in code, I am getting indexoutofboundsexception. Previously the byte[] length wa开发者_运维百科s 1Kb, I changed it to 64kb and I started getting indexoutofboundsexception


Can't you just forget about the buffer and error-prone loop and simply call:

IOUtils.copy(in, response.getOutputStream());

http://commons.apache.org/io/api-release/org/apache/commons/io/IOUtils.html

I know this is not exactly the answer to your question, but I prefer clean and bullet-proof code over questionable optimization.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜