hc-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rajat Sharma" <rsha...@airvana.com>
Subject RE: Hi ... httpclient for PDF files ???
Date Tue, 28 Mar 2006 21:22:31 GMT
Good website Ortwin......keep it up....thanks

-----Original Message-----
From: Ortwin Gl├╝ck [mailto:odi@odi.ch]
Sent: Tuesday, March 28, 2006 11:12 AM
To: HttpClient Project
Subject: Re: Hi ... httpclient for PDF files ???


Vijay,

It's wrong to use a DataInputStream this way. DataInputStream is for 
serialized Java objects! A typical beginner's mistake I should put in my 
list of anti-patterns: http://www.odi.ch/prog/design/newbies.php

The correct way to copy a byte stream to disk is:

InputStream in = new BufferedInputStream(responseBodyStream);
FileOutputStream fos = new FileOutputStream("rwservlet.pdf");
byte[] buffer = new byte[4098];
int len = 0;
while ((len = in.read(buffer) > 0) {
     fos.write(buffer, 0, len);
}

Cheers

Ortwin

Vijay Gomatam wrote:
> DataInputStream data = new DataInputStream(new
> BufferedInputStream(responseBodyStream));
 > FileOutputStream fos = new FileOutputStream("rwservlet.pdf");
 >
 > *while* ((c =data.read(b)) != -1){
 >
 > *byte*[] inter=*new* *byte*[c];
 >
 > *for*(*int* i=0;i<c;i++)  inter[i]=b[i];
 >
 > fos.write(inter);
 >
 > }


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-dev-help@jakarta.apache.org


Mime
View raw message