aspose file tools*
The moose likes JSP and the fly likes buffer overflow exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "buffer overflow exception" Watch "buffer overflow exception" New topic
Author

buffer overflow exception

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 357
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.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Vijay Dogra
Greenhorn

Joined: Sep 16, 2004
Posts: 22
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

Joined: May 18, 2005
Posts: 357
<%-- 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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 357
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.
 
 
subject: buffer overflow exception