2011-02-08 109 views
0

我想知道是否有創建一個字節[]的內容寫入ServletOutputStream的特定規則或最佳做法?應寫入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); 

在上面的代碼,字節[] lenth是1024。如果我跑我的tomcat中的servlet,我需要的緩衝區的長度與Tomcat的緩衝區的匹配嗎?或者它並不重要? Tomcat的默認緩衝區大小可能是4096,而我的字節[]可以說是20000

不知道問題是否有意義,但自從我在代碼中增加了byte []長度後,我得到了indexoutofboundsexception。此前字節[]長度爲1KB,我改成了64KB和我開始IndexOutOfBoundsException異常

回答