Solved

Output stream seems to be getting cut off

Posted on 2001-08-09
11
332 Views
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();
..
res.setContentType("text/plain");

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

fileoutput.close();
0
Comment
Question by:Mero
11 Comments
 
LVL 2

Expert Comment

by:Prasanna_Hebbar
Comment Utility
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.
0
 

Author Comment

by:Mero
Comment Utility
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.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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.
0
 

Expert Comment

by:raghu_k_n
Comment Utility
Make sure that ur request is if type "post" and not "get"

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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."
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 92

Expert Comment

by:objects
Comment Utility
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;

}

   
0
 
LVL 4

Expert Comment

by:omry_y
Comment Utility
isn`t PDF a binary format?
why are you sending it as text/plain ?
try application/octet-stream or application/binary
0
 

Author Comment

by:Mero
Comment Utility
objects,
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.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
Comment Utility
> 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.
0
 

Author Comment

by:Mero
Comment Utility
objects,

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

Expert Comment

by:objects
Comment Utility
Thanks for the points :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 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

11 Experts available now in Live!

Get 1:1 Help Now