KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > sql > filters > BlobEscapedFilter


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Silvan Eugen Lincan
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.common.sql.filters;
26
27 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
28
29 /**
30  * This class defines a BlobEscapedFilter. It acts just the same as the
31  * <code>NoneBlobFilter</code> except that the content is escaped to prevent
32  * strange characters for disturbing the data. This has been adapted mainly for
33  * PostgreSQL bytea data, but it should be usable for any other database.
34  *
35  * @author <a HREF="mailto:s.lincan@moodmedia.ro">Silvan Eugen Lincan </a>
36  * @version 1.0
37  */

38 public class BlobEscapedFilter extends AbstractBlobFilter
39 {
40
41   /**
42    * @see org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter#encode(byte[])
43    */

44   public String JavaDoc encode(byte[] data)
45   {
46     return BlobEscapedFilter.toPGString(data);
47   }
48
49   /**
50    * @see org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter#encode(java.lang.String)
51    */

52   public String JavaDoc encode(String JavaDoc data)
53   {
54     return data;
55   }
56
57   /**
58    * @see org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter#decode(byte[])
59    */

60   public byte[] decode(byte[] data)
61   {
62     return data;
63   }
64
65   /**
66    * @see org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter#decode(java.lang.String)
67    */

68   public byte[] decode(String JavaDoc data)
69   {
70     return data.getBytes();
71   }
72
73   /**
74    * @see org.objectweb.cjdbc.common.sql.filters.AbstractBlobFilter#getXml()
75    */

76   public String JavaDoc getXml()
77   {
78     return DatabasesXmlTags.VAL_escaped;
79   }
80
81   /**
82    * Converts a java byte[] into a PG bytea string (i.e. the text representation
83    * of the bytea data type). Escape characters between 32 and 126.
84    *
85    * @param postgresBuf The byte array to be converted
86    * @return the string representation
87    */

88   public static String JavaDoc toPGString(byte[] postgresBuf)
89   {
90     if (postgresBuf == null)
91     {
92       return null;
93     }
94     StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc(2 * postgresBuf.length);
95     for (int i = 0; i < postgresBuf.length; i++)
96     {
97       int lInt = postgresBuf[i];
98       if (lInt < 0)
99       {
100         lInt = 256 + lInt;
101       }
102       /*
103        * Escape the same non-printable characters as the backend. Must escape
104        * all 8bit characters otherwise when convering from java unicode to the
105        * db character set we may end up with question marks if the character set
106        * is SQL_ASCII.
107        */

108       if (lInt < 040 || lInt > 0176)
109       {
110         /* escape charcter '\0000', but need two \\ because of the parser. */
111         stringBuffer.append("\\");
112         stringBuffer.append((char) (((lInt >> 6) & 0x3) + 48));
113         stringBuffer.append((char) (((lInt >> 3) & 0x7) + 48));
114         stringBuffer.append((char) ((lInt & 0x07) + 48));
115       }
116       else if (postgresBuf[i] == (byte) '\\')
117       {
118         /*
119          * escape the backslash character as \\, but need four \\\\ because of
120          * the parser.
121          */

122         stringBuffer.append("\\\\");
123       }
124       else
125       {
126         /* other characters are left alone */
127         stringBuffer.append((char) postgresBuf[i]);
128       }
129     }
130     String JavaDoc x = stringBuffer.toString();
131     /* Add 10% for escaping. */
132     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(2 + x.length() * 11 / 10);
133     for (int i = 0; i < x.length(); ++i)
134     {
135       char c = x.charAt(i);
136       if ((c == '\'') || (c == '\\'))
137         sbuf.append("\\");
138       sbuf.append(c);
139     }
140
141     return sbuf.toString();
142   }
143
144   /**
145    * Converts a PG bytea raw value (i.e. the raw binary representation of the
146    * bytea data type) into a java byte[].
147    *
148    * @param s The byte array to be converted.
149    * @return an java byte[]
150    */

151   public static byte[] toBytes(byte[] s)
152   {
153     if (s == null)
154     {
155       return null;
156     }
157     int slength = s.length;
158     byte[] buf = new byte[slength];
159     int bufpos = 0;
160     int thebyte;
161     byte nextbyte;
162     byte secondbyte;
163     for (int i = 0; i < slength; i++)
164     {
165       nextbyte = s[i];
166       if (nextbyte == (byte) '\\')
167       {
168         secondbyte = s[++i];
169         if (secondbyte == (byte) '\\')
170         {
171           /* escaped \ */
172           buf[bufpos++] = (byte) '\\';
173         }
174         else
175         {
176           thebyte = (secondbyte - 48) * 64 + (s[++i] - 48) * 8 + (s[++i] - 48);
177           if (thebyte > 127)
178             thebyte -= 256;
179           buf[bufpos++] = (byte) thebyte;
180         }
181       }
182       else
183       {
184         buf[bufpos++] = nextbyte;
185       }
186     }
187     byte[] resultReturn = new byte[bufpos];
188     System.arraycopy(buf, 0, resultReturn, 0, bufpos);
189     return resultReturn;
190   }
191 }
Popular Tags