Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

converting byte array to character, by reading BufferedInputStream from socket

 
sudh sasi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to socket programming. When trying to read a big response from socket, get the byte array, convert it to string and append to a string buffer. My issue is if the response is big I am receving data with some unexpected/unwanted character along with reponse . I am reading response in chunk of 1024 and converting to string. The unwanted character will come at the begining and during frequent chunks of data. See the piece of code below.

in = New BufferedInputStream(socket.getInputStream)
byte [] buf = new byte[1024]

while (i=in.read(buf) > -1)
{
String s = new String (buf,0,i)
strinbuf.append(s)
}

Reading work good with small response. When reading big response I am seeing number 8000 at the begining of response and this 8000 is repeating at frequent intervals of data chunk. Is there anything I can try to see what is this unexpected number , coming in as part of response.

Any response highly appreciable.............Thanks in advance
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a Reader:
The Reader will take care of the actual byte-to-char conversion. If needed you can even specify the charset to use for the conversion.
 
sudh sasi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Use a Reader:
The Reader will take care of the actual byte-to-char conversion. If needed you can even specify the charset to use for the conversion.


I cannot append byte[] to string buffer directly without converting to char. How can I do that ? type casting binary array to String ???
 
sudh sasi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sudh sasi wrote:
Rob Prime wrote:Use a Reader:
The Reader will take care of the actual byte-to-char conversion. If needed you can even specify the charset to use for the conversion.


Reader can't read byte array directly.I cannot append byte[] to string buffer directly without converting to char. How can I do that ? type casting binary array to String ???
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you even read the code? It uses a char[], not a byte[]. Reader (and Writer) work with characters, not bytes.
 
sudh sasi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still not able to getrid of unwanted characters in string.
Expected message after reading is <soapenv:Envelope xmlns: XXXXXXXXX ></soapenv:Envelope>
But after reading stream data from socket I am seeing

7a92<soapenv:Envelope xmlns: XXXXXXXXX ></soapenv:Envelope>0


Not sure why I am getting "7a92" at begining and "0" at end. If the message I am trying to read from socket is very large , I am seeing "7a92" is getting repeated within message and spoiling the SOAP XML. Because of these junk characters I am not able to parse the response.

Reply highly appreciable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!