Output stream seems to be getting cut off

Posted on 2001-08-09
Last Modified: 2008-03-17
I have this Java code below where I'm trying to output fdf(Adobe form format) data to the browser.  But when I see it in the browser, I don't seem to get the whole stream.  I don't have data in there as far as I know that tells the browser "end of file".  Should I be using getContentLength in my loop?  If not, what?  Thanks.

ServletOutputStream out = res.getOutputStream();

byte data[] = new byte[req.getContentLength()];
for (int i = 0; i<req.getContentLength(); i++){
   out.print((char) data[i]);//writes the stream to the browser

Question by:Mero

Expert Comment

ID: 6370071
I could not understand this program. Basically you are trying to read the data from the input stream and write the same data back to the output stream. In case the browser did not send any data to the servlet's input stream, then nothing will be written back to the browser.

Author Comment

ID: 6370160
In short, the program is looking at a pdf form that has fdf data.  There is a submit button in the form that calls the servlet above.   The browser is sending data to the input stream because I did see that but I cannot see all of the data that is supposed to come from the stream.
LVL 92

Expert Comment

ID: 6370428
> req.getInputStream().read(data);              

read() does not necessarilty read all the bytes. It reads as many as are available at the time and returns the number of bytes read.
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Expert Comment

ID: 6371692
Make sure that ur request is if type "post" and not "get"

LVL 92

Expert Comment

ID: 6371739
From the read() javadoc:

"Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer."
LVL 92

Expert Comment

ID: 6371749
You need to put your read in a loop something like this:

int n = buf.length;
while (n>0)
   int read = read(buf, buf.length - n, n);
   n -= read;



Expert Comment

ID: 6372540
isn`t PDF a binary format?
why are you sending it as text/plain ?
try application/octet-stream or application/binary

Author Comment

ID: 6372930
I am trying your example but I get an ArrayIndexOutofBounds Exception error because at one point buf.length-n will be greater than n and thus, buf won't get all of the data.  I'm trying to modify your code someway to take care of this, but your or anybody's input would be appreciated.  I am not using a buffer because last time I used one in my program,  it returned no data at all from the stream.  buf is "data" which is my byte array in my program.

omry, I'll get to your question later.
LVL 92

Accepted Solution

objects earned 100 total points
ID: 6374426
> I am trying your example but I get an
> ArrayIndexOutofBounds Exception error because at one
> point buf.length-n will be greater than n and thus, buf
> won't get all of the data.

Shouldn't be. As bytes are read n is decreased.
Have used the same code 100's of times without problem.

Can you post your code.

Author Comment

ID: 6376054

Never mind, it was somewhere else where I was getting an arrayoutofbounds error.  Your code worked.  Thanks.
LVL 92

Expert Comment

ID: 6376515
Thanks for the points :)

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 54
netstat -ano | find "8000" and taskkill /f /pid 2984 3 55
Tomcat: Unable to run tomcat service. 2 18
Java string replace 11 43
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now