Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

buffer overflow exception

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i have the following code.

<%-- The size of file parul.txt is <200kb. Then why the exception --%>

<%@ page buffer="200kb"
autoFlush="false"
import="java.io.*"
errorPage="errorPage.jsp"
%>

<%

try{

FileReader dis = new FileReader("C:\\Tomcat5\\webapps\\scwcd\\HFSJ\\chap08\\parul.txt");
int i=dis.read();

while(i!=-1){
out.print(i);
i=dis.read();
}
}catch(Exception e){
out.println("ex:"+e.toString());
}


%>

Here the size of parul.txt is less than 200kb(buffer size). I am getting java.io.IOException: Error: JSP Buffer overflow.

Can some pls explain the reason of exception.
 
Vijay Dogra
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

You are setting the page buffer size to 200kb and also setting the autoflush to false. Also when you are reading from file you are reading in bytes and converting that to int. Now this int is larger in size than that of a byte that you are converting. As you are reading one byte and converting to int the out variable gets filled more quickly than you expect (as size of int is larger than that of a byte). Try to print the bytes directly to ouput.

Hope this helps.

Vijay
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%-- The size of file parul.txt is <200kb. Then why the exception --%>

<%@ page buffer="500kb"
autoFlush="false"
import="java.io.*"
errorPage="errorPage.jsp"
%>

<%

try{

FileReader dis = new FileReader("C:\\Tomcat5\\webapps\\scwcd\\HFSJ\\chap08\\parul.txt");
char i= (char)dis.read();

while(i!=-1){
out.print(i);
i=(char)dis.read();
}
}catch(Exception e){
out.println("ex:"+e.toString());
}


%>



I have increased the buffer size (500kb) which is more than 3 times the size of file(135kb) . Also now i am reading characters and writing them to output steam. Still i am getting the same exception : java.io.IOException: Error: JSP Buffer overflow
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by N Chaurasia:



I have increased the buffer size (500kb) which is more than 3 times the size of file(135kb) .


A Java int is 4 bytes.
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben i am typecasting int to char, which is half the size of int. More over if i change the code to use byte instead of char, still i am having the same problem of overflow.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic