Home arrow static arrow Java Programming [Archive] - converting binarystream into string
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - converting binarystream into string
This topic has 16 replies on 2 pages.    1 | 2 | Next »

Posts:102
Registered: 2/27/04
converting binarystream into string  
Jun 14, 2004 12:16 AM



 
hi All,

I am using J2SDK 1.4.2. For one of my java class, I need to convert a binary stream into String. How can I do that?

Please help.

-bh_t_76.
 

Posts:4,000
Registered: 24.02.01
Re: converting binarystream into string  
Jun 14, 2004 12:19 AM (reply 1 of 16)



 
Continuouslly read the stream into a string (buffer/builder).
 

Posts:102
Registered: 2/27/04
Re: converting binarystream into string  
Jun 14, 2004 12:38 AM (reply 2 of 16)



 
can u pls give me the code example?

Thanks in adv.
 

Posts:835
Registered: 2/12/01
Re: converting binarystream into string  
Jun 14, 2004 12:54 AM (reply 3 of 16)



 
string has a constructor
    public String(byte bytes[]);

I'm doing the following way
    public static String readString(final InputStream in) throws IOException {        final int len = Protocol.readInt(in); // get length        if (len == 0)            return "";        final byte[] name = new byte[len]; // create buf        Protocol.readBuf(in, name); // fill buf        return new String(name); // convert    }

Consider DataInputStream.
 

Posts:102
Registered: 2/27/04
Re: converting binarystream into string  
Jun 14, 2004 1:49 AM (reply 4 of 16)



 
may I know from which class I have to import Protocol. Or it refers to some constants? Pls help.
 

Posts:835
Registered: 2/12/01
Re: converting binarystream into string  
Jun 14, 2004 2:51 AM (reply 5 of 16)



 
You should not import anything, I give you code for copy&paste. it is my custom class I share between client and server. Did I show you how to convert byte array into a string? Here are the others functions I use.
    public static void readBuf(final InputStream in, final byte[] buf) throws IOException { readBuf(in, buf, buf.length); }     public static void readBuf(final InputStream in, final byte[] buf, final int size) throws IOException {        int off = 0;        int read = 0;        while (read != size)            if ((read = in.read(buf, off, size - read)) == -1)                throw new IOException("unexpected end of stream reading integer.");    }     /**reads an integer from byte stream*/    public static int readInt(final InputStream in) throws IOException {        final byte[] arr = new byte[4];        readBuf(in, arr);        int res = (arr[0] & 0xFF) + ((arr[1] & 0xFF) << 8) + ((arr[2] & 0xFF) << 16) + ((arr[3] & 0xFF) << 24);//        System.out.println("receiving an integer, bytes = " + arr[0] + "." + arr[1] + "." + arr[2] + "." + arr[3] + ", result = " + res);        return res;    }

You should understand first what you want to do.
 

Posts:102
Registered: 2/27/04
Re: converting binarystream into string  
Jun 14, 2004 3:29 AM (reply 6 of 16)



 
thanks a lot.
 

Posts:2,909
Registered: 13.8.2003
Re: converting binarystream into string  
Jun 14, 2004 3:39 AM (reply 7 of 16)



 
Your class doesn't take in consideration different encodings. Also you do some unnecessary work, since you can create a Reader from an InputStream by using InputStreamReader class (and wrapping it in a BufferedReader for example).
 

Posts:835
Registered: 2/12/01
-Kayaman-   
Jun 14, 2004 4:42 AM (reply 8 of 16)



 
-Kayaman-

1. You mean that client and server are in risk to run into different default system encodings? So that specifying a concrete one I can hardcode the encoding?

2. I'll thank you if you tell me how can I read both text and row byte array from a single input stream. I mean the conventional Java solution. For ex, header of HTTP message is textual but content is a byte stream. This issue is intriguing me for many years.
 

Posts:2,909
Registered: 13.8.2003
Re: -Kayaman-   
Jun 14, 2004 5:35 AM (reply 9 of 16)



 
1. You mean that client and server are in risk to run
into different default system encodings? So that
specifying a concrete one I can hardcode the encoding?

Yes, that is a risk. On 2 windows machines you probably don't have any problems (at first in any case) but what if the server is on a linux machine and it's sending UTF-8 characters, while your client is happily reading them as ISO-8859-1 characters. There's a String constructor that takes encoding as a parameter too (new String(byte[] bytes, String encoding) unless I'm mistaken, so you can hardocode the encoding to be (for example) UTF-8 in all cases.

2. I'll thank you if you tell me how can I read both
text and row byte array from a single input stream. I
mean the conventional Java solution. For ex, header of
HTTP message is textual but content is a byte stream.
This issue is intriguing me for many years.

I have a http server under development at the moment (GET works and I'm starting to get bored with it) but I can't seem to remember how I dealt with that (that is if I dealt with that).
 

Posts:835
Registered: 2/12/01
Re: converting binarystream into string  
Jun 14, 2004 7:26 PM (reply 10 of 16)



 
I've discovered a serious error in readBuf. Should be thomething like

    public static void readBuf(final InputStream in, final byte[] buf, final int size) throws IOException {        int off = 0;        int read = 0;        while (off != size) {            if ((read = in.read(buf, off, size - off)) == -1)                throw new IOException("unexpected end of stream.");            off += read;        }    }
 

Posts:102
Registered: 2/27/04
Re: converting binarystream into string  
Jun 14, 2004 9:58 PM (reply 11 of 16)



 
Like the above codes, I tried to convert the Binary Stream into the String like the below:

java.io.InputStream in = rs1.getBinaryStream("map_content");
StringBuffer map= new StringBuffer();
byte buffer[]= new byte[1024];
while (in.read(buffer) != -1)
map.append(new String(buffer).trim());
in.close();

String map1 = new String(map);
System.out.println("map1 "+map1);
map1 = map1.replaceFirst("UTF-8","ISO-8859-1");
System.out.println("map1 "+map1);
DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
ByteArrayInputStream bais =new ByteArrayInputStream(map1.getBytes());
document = builder.parse(bais);

But, the string prints only the binary values, which I wanted to be as the Character values. From one of my oracle database, I am retrieving this content. Is there any way, to convert this binary value into character value.

Please help.

Thanks in advance.

-bh_t_76.
 

Posts:6,147
Registered: 11/9/00
Re: converting binarystream into string  
Jun 14, 2004 10:17 PM (reply 12 of 16)



 
If you're parsing XML you don't need or want to covert bytes to characters first. The XML parser does that for you and, indeed, it should be allowed to do that because the character encoding is specified in the first header line of an XML document.

just do
document = builder.parse(in);
 

Posts:102
Registered: 2/27/04
Re: converting binarystream into string  
Jun 14, 2004 10:28 PM (reply 13 of 16)



 
i gave the same way you suggested:

document = builder.parse(in);

But it throws the exception:

'Premature end of file'.

What could be the reason? Pls help.
 

Posts:102
Registered: 2/27/04
Re: converting binarystream into string  
Jun 14, 2004 10:47 PM (reply 14 of 16)



 
Should I add something like 'UTF-8' to the document?
 
This topic has 16 replies on 2 pages.    1 | 2 | Next »