Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

org.objectweb.cjdbc.common.sql.filters.BlobEscapedFilter Class Reference

Inheritance diagram for org.objectweb.cjdbc.common.sql.filters.BlobEscapedFilter:

Inheritance graph
[legend]
Collaboration diagram for org.objectweb.cjdbc.common.sql.filters.BlobEscapedFilter:

Collaboration graph
[legend]
List of all members.

Public Member Functions

String encode (byte[] data)
String encode (String data)
byte[] decode (byte[] data)
byte[] decode (String data)
String getXml ()

Static Public Member Functions

String toPGString (byte[] postgresBuf)
byte[] toBytes (byte[] s)

Detailed Description

This class defines a BlobEscapedFilter. It acts just the same as the NoneBlobFilter except that the content is escaped to prevent strange characters for disturbing the data. This has been adapted mainly for PostgreSQL bytea data, but it should be usable for any other database.

Author:
Silvan Eugen Lincan
Version:
1.0

Definition at line 38 of file BlobEscapedFilter.java.


Member Function Documentation

byte [] org.objectweb.cjdbc.common.sql.filters.BlobEscapedFilter.decode String  data  )  [virtual]
 

See also:
org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter.decode(java.lang.String)

Implements org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter.

Definition at line 68 of file BlobEscapedFilter.java.

00069   {
00070     return data.getBytes();
00071   }

byte [] org.objectweb.cjdbc.common.sql.filters.BlobEscapedFilter.decode byte[]  data  )  [virtual]
 

See also:
org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter.decode(byte[])

Implements org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter.

Definition at line 60 of file BlobEscapedFilter.java.

00061   {
00062     return data;
00063   }

String org.objectweb.cjdbc.common.sql.filters.BlobEscapedFilter.encode String  data  )  [virtual]
 

See also:
org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter.encode(java.lang.String)

Implements org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter.

Definition at line 52 of file BlobEscapedFilter.java.

00053   {
00054     return data;
00055   }

String org.objectweb.cjdbc.common.sql.filters.BlobEscapedFilter.encode byte[]  data  )  [virtual]
 

See also:
org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter.encode(byte[])

Implements org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter.

Definition at line 44 of file BlobEscapedFilter.java.

00045   {
00046     return BlobEscapedFilter.toPGString(data);
00047   }

String org.objectweb.cjdbc.common.sql.filters.BlobEscapedFilter.getXml  )  [virtual]
 

See also:
org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter.getXml()

Implements org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter.

Definition at line 76 of file BlobEscapedFilter.java.

00077   {
00078     return DatabasesXmlTags.VAL_escaped;
00079   }

byte [] org.objectweb.cjdbc.common.sql.filters.BlobEscapedFilter.toBytes byte[]  s  )  [static]
 

Converts a PG bytea raw value (i.e. the raw binary representation of the bytea data type) into a java byte[].

Parameters:
s The byte array to be converted.
Returns:
an java byte[]

Definition at line 151 of file BlobEscapedFilter.java.

00152   {
00153     if (s == null)
00154     {
00155       return null;
00156     }
00157     int slength = s.length;
00158     byte[] buf = new byte[slength];
00159     int bufpos = 0;
00160     int thebyte;
00161     byte nextbyte;
00162     byte secondbyte;
00163     for (int i = 0; i < slength; i++)
00164     {
00165       nextbyte = s[i];
00166       if (nextbyte == (byte) '\\')
00167       {
00168         secondbyte = s[++i];
00169         if (secondbyte == (byte) '\\')
00170         {
00171           /* escaped \ */
00172           buf[bufpos++] = (byte) '\\';
00173         }
00174         else
00175         {
00176           thebyte = (secondbyte - 48) * 64 + (s[++i] - 48) * 8 + (s[++i] - 48);
00177           if (thebyte > 127)
00178             thebyte -= 256;
00179           buf[bufpos++] = (byte) thebyte;
00180         }
00181       }
00182       else
00183       {
00184         buf[bufpos++] = nextbyte;
00185       }
00186     }
00187     byte[] resultReturn = new byte[bufpos];
00188     System.arraycopy(buf, 0, resultReturn, 0, bufpos);
00189     return resultReturn;
00190   }

String org.objectweb.cjdbc.common.sql.filters.BlobEscapedFilter.toPGString byte[]  postgresBuf  )  [static]
 

Converts a java byte[] into a PG bytea string (i.e. the text representation of the bytea data type). Escape characters between 32 and 126.

Parameters:
postgresBuf The byte array to be converted
Returns:
the string representation

Definition at line 88 of file BlobEscapedFilter.java.

00089   {
00090     if (postgresBuf == null)
00091     {
00092       return null;
00093     }
00094     StringBuffer stringBuffer = new StringBuffer(2 * postgresBuf.length);
00095     for (int i = 0; i < postgresBuf.length; i++)
00096     {
00097       int l_int = (int) postgresBuf[i];
00098       if (l_int < 0)
00099       {
00100         l_int = 256 + l_int;
00101       }
00102       /*
00103        * Escape the same non-printable characters as the backend. Must escape
00104        * all 8bit characters otherwise when convering from java unicode to the
00105        * db character set we may end up with question marks if the character set
00106        * is SQL_ASCII.
00107        */
00108       if (l_int < 040 || l_int > 0176)
00109       {
00110         /* escape charcter '\0000', but need two \\ because of the parser. */
00111         stringBuffer.append("\\");
00112         stringBuffer.append((char) (((l_int >> 6) & 0x3) + 48));
00113         stringBuffer.append((char) (((l_int >> 3) & 0x7) + 48));
00114         stringBuffer.append((char) ((l_int & 0x07) + 48));
00115       }
00116       else if (postgresBuf[i] == (byte) '\\')
00117       {
00118         /*
00119          * escape the backslash character as \\, but need four \\\\ because of
00120          * the parser.
00121          */
00122         stringBuffer.append("\\\\");
00123       }
00124       else
00125       {
00126         /* other characters are left alone */
00127         stringBuffer.append((char) postgresBuf[i]);
00128       }
00129     }
00130     String x = stringBuffer.toString();
00131     /* Add 10% for escaping. */
00132     StringBuffer sbuf = new StringBuffer(2 + x.length() * 11 / 10);
00133     for (int i = 0; i < x.length(); ++i)
00134     {
00135       char c = x.charAt(i);
00136       if ((c == '\'') || (c == '\\'))
00137         sbuf.append("\\");
00138       sbuf.append(c);
00139     }
00140 
00141     return sbuf.toString();
00142   }


The documentation for this class was generated from the following file:
Generated on Mon Apr 11 22:02:12 2005 for C-JDBC by  doxygen 1.3.9.1