Re: Problem with inserting bytea into postgresql
- From: Chris Smith <cdsmith@xxxxxxx>
- Date: Thu, 12 Jan 2006 09:54:23 -0700
...::WojT::.. wrote:
> I hav big problem with storing binary data into bytea (postgres) using plain
> text insert.
> I need to generate sql script (in fact plpgsql script) which inserts byte
> array into bytea column i my table.
> This byte array is serialized object.
> There is way i am doing it right now:
Just a few comments. I didn't look at your code in a lot of detail:
> {
> ....
> ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
> ObjectOutputStream stream= new ObjectOutputStream(outputStream);
> // serialize object
> stream.writeObject(model);
> // get bytes of object
> byte[] modelBytes = outputStream.toByteArray();
> ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
> DataOutputStream dataOutputStream = new DataOutputStream(arrayOutputStream);
> // convert to UTF8
> dataOutputStream.writeUTF(new String(modelBytes));
You should have stopped at "byte[] modelBytes = ..." instead of
continuing with the ensuing nonsense. What your code after that line
does is:
1. Convert your binary data into character data using the platform
default encoding. Since the platform default encoding is very possibly
unable to represent all of the Unicode code points below 256, the
process will convert many of your bytes into '?' characters, causing you
to become unable to recover the original data.
2. Convert the resulting mangled character stream back into a byte
array, except this time there's a 16-bit length count appended to the
beginning, and the characters are encoded via UTF-8.
> private String getEscapedByte(byte b) {
This seems to have lots of problems, such as doubling backslash
characters that shouldn't be, and so forth. I'm not going to sort
through your code... but here's the code from the latest PostgreSQL JDBC
driver to perform that task:
public static String toPGString(byte[] p_buf) throws SQLException
{
if (p_buf == null) return null;
StringBuffer l_strbuf = new StringBuffer(2 * p_buf.length);
for (int i = 0; i < p_buf.length; i++)
{
int l_int = (int)p_buf[i];
if (l_int < 0)
{
l_int = 256 + l_int;
}
if (l_int < 040 || l_int > 0176)
{
l_strbuf.append("\\");
l_strbuf.append((char)(((l_int >> 6) & 0x3) + 48));
l_strbuf.append((char)(((l_int >> 3) & 0x7) + 48));
l_strbuf.append((char)((l_int & 0x07) + 48));
}
else if (p_buf[i] == (byte)'\\')
{
l_strbuf.append("\\\\");
}
else
{
//other characters are left alone
l_strbuf.append((char)p_buf[i]);
}
}
return l_strbuf.toString();
}
If you have the JDBC drivers in your classpath, you need not copy the
code... just use
String str = PGbytea.toPGString(modelBytes);
PGbytea should be imported from org.postgresql.util.
If you fix those problems and still need more help, let us know.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
.
- References:
- Problem with inserting bytea into postgresql
- From: ..::WojT::..
- Problem with inserting bytea into postgresql
- Prev by Date: Need help with AxBridge !!
- Next by Date: Re: Slow Eclipse
- Previous by thread: Re: Problem with inserting bytea into postgresql
- Next by thread: Search in CSV files
- Index(es):
Relevant Pages
|