Unicode-16 for mySQL image storage
YEY!!!
BOO!!!
Well, I'm storing an image as a blob in a mysql database... great!
I can read out the bytes too... great!
What I'd like some help with is how to write to a file in unicode-16 format.
I'm trying to use OutputStreamWriter... But I dont know what I'm actually doing. I checked out the javadoc and I'm still unsure of what I'm doing. Some help using the OutputStreamWriter would be quite delicious.
Thanks for any help!
# 1
> I'm storing an image as a blob in a mysql database
> I can read the bytes
> I'd like some help with is how to write to a file in unicode-16 format.
> I'm trying to use OutputStreamWriter
Say what?
Why?
Why do you want to delve into character streams?
You have a perfectly good blob of bytes - use a byte stream (OutputStream) to write the bytes to a file.
# 2
Well that'd be nice...
But I HAVE TO use a unicode-16 setting to store a jpg (so it seems).
This is how I store the byte upon reading them out of a file...
byte cs[] = str.getBytes();
for ( int k = 0; k < str.length(); k++ )
{
String s = Integer.toString((int)((char)cs[k]), 16);
if ( s.length()==1 )
s = "0"+s;
if ( s.length()==3 )
s = "0"+s;
total += s + " ";
}
YES IT IS AWFULLY INEFFICIENT!!! but when I used a simple char array and tried storing it, I couldn't because the character " (quotation) appears as a byte and tricks mySQL to close the input field.
So why do I need unicode for writing?
Because in a base 16 system all of my data should be compressible to 2 characters. where 0 = 00 and 256 = ff. BUT, some of my sequences are being returned as 4 character sequences like fff8 for example. How do I store that?
Thanks again.
# 3
I'm lost! Why do you need a char array in the first place? Why do you need a String? If you are trying to hex encode the image then consider using the class Hex from Jakarta Commons Codec.
An image is best considered as bytes and should never be considered as characters.
Message was edited by:
sabre150
# 4
OH!
Well that should explain it then...
So a byte and char arent the same?
I thought the only difference was:
byte (-127 to 128)
char (0 to 255)
Thats what i thought... so i thought making a string of chars would be the same...
But wait, If I make an array of bytes how do I insert them into the database? Im using LONGTEXT as the mysql datatype. Doesn't that need to be a string?
Any other enlightenments.
Thanks... I love you guys =)
# 5
> OH!
> Well that should explain it then...
> So a byte and char arent the same?
Most definitely not!
> I thought the only difference was:
> byte (-127 to 128)
> char (0 to 255)
Definitely not! chars are UNICODE code points that are usually 16 bits but some characters are 32 bits.
> Thats what i thought... so i thought making a string
> of chars would be the same...
Definitely not!
> But wait, If I make an array of bytes how do I insert
> them into the database? Im using LONGTEXT as the
> mysql datatype. Doesn't that need to be a string?
Use a Binary Large Object (BLOB) column type - I do and it works great.