473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

InputStream do not read properly

Hello,
Can you say me, where I make a mistake.
I am writing a proxy.When I read from InputStream in,
like:
try {
byte[] buf = new byte[4096];
int bytesIn = 0;
while (((bytesIn = in.read(buf)) >= 0))
{
String sss1 = new String(buf);
out.write(buf, 0, bytesIn);
}
} catch (Exception e) {
String errMsg = "Error getting HTTP body: " + e;
debugOut.println(errMsg);
}

So,when I look in sss1 contents,there have extra information like:

3




79
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3a
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
2d

The result is incorect page displaying.
Can you say me, where I make a mistake.
Or how I can remove this extra information, so that page can to display corect in the browser.
Jun 3 '07 #1
4 2249
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. byte[] buf = new byte[4096];
  2. int bytesIn = 0;
  3. while (((bytesIn = in.read(buf)) >= 0)) 
  4. {    
  5.    String sss1 = new String(buf);
  6.    out.write(buf, 0, bytesIn);
  7. }
So,when I look in sss1 contents,there have extra information like: <snip>
You're building a string out of the *entire* buffer 'buf'. Think what would happen
if you have read less than 4096 bytes. You do anticipate for a partially filled
buffer when you write the bytes to the 'out' stream. Do the same when you
create the string. (read the API docs for an applicable String constructor).

kind regards,

Jos
Jun 4 '07 #2
dmjpro
2,476 2GB
Update the line while
Expand|Select|Wrap|Line Numbers
  1. //while(((bytesIn = in.read(buf)) >= 0)) ==== Wrong
  2. while (((bytesIn = in.read(buf)) > 0)) //====Right
  3.  
Best of luck.

Kind regards,
Dmjpro.
Jun 5 '07 #3
I found the problem.
This happends when in the header is set "Transfer-Encoding: chunked".
So that extra info is from chunk.
Jun 6 '07 #4
JosAH
11,448 Expert 8TB
I found the problem.
This happends when in the header is set "Transfer-Encoding: chunked".
So that extra info is from chunk.
I think you found part of your problem; the other part is that you're still using
the entire buffer to build String sss1 out of it. The buffer might not have been
filled completly, so only the first part of it contains valid data.

kind regards,

Jos
Jun 6 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Charles.Deisler | last post by:
XmlTextReader requestdata = new XmlTextReader(Request.InputStream); XmlTextWriter xmltextwriter = new XmlTextWriter(somefile,someencoding); XmlTextWriter xmltextwriter2 = new...
0
by: Keith Harris | last post by:
Hi, I have an HtmlInputFile control which allows a user to upload a CSV file. I want to display the first few rows for preview and later, load the rows to a database. Displaying the preview...
0
by: bvasanth123 | last post by:
Hi, I would like to know how can I read an InputStream into Excel object. In my web app, user uploads MS-Excel file though asp.net html file control. I get UploadedFile.PostedFile.InputStream()...
0
by: bvasanth123 | last post by:
Hi, I would like to know how can I read an InputStream into Excel object. In my web app, user uploads MS-Excel file though asp.net html file control. I get UploadedFile.PostedFile.InputStream()...
1
by: O.B. | last post by:
I have the need to read a file (version.txt) that is embedded within an WAR file that is within an EAR file: foo.ear contains bar.war contains resources/version.txt I am able to get the bar.ear...
0
by: raheel javed | last post by:
i am using mobile for communication with live server when mobile send its information to server the input stream is encoded in utf-8 that is converted into string but it is still not...
4
by: macap.usenet | last post by:
Hello, I´ve a strange problem with my HttpPostedFile object. I coded a File Upload where the user can upload a zip file. My code looks like this: HttpFileCollection files =...
11
by: John Krukoff | last post by:
-----Original Message----- First off, ignore castironpi, it's a turing test failure. Second, I'm curious as to how Java manages this. I'd think their streams would have to be pretty magic to...
6
hsriat
by: hsriat | last post by:
public String receive() { InputStream is; try { is = socket.getInputStream(); } catch (IOException e) { e.printStackTrace(); return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.