00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.common.sql.filters;
00026
00027 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 public class BlobEscapedFilter extends AbstractBlobFilter
00039 {
00040
00041
00042
00043
00044 public String encode(byte[] data)
00045 {
00046 return BlobEscapedFilter.toPGString(data);
00047 }
00048
00049
00050
00051
00052 public String encode(String data)
00053 {
00054 return data;
00055 }
00056
00057
00058
00059
00060 public byte[] decode(byte[] data)
00061 {
00062 return data;
00063 }
00064
00065
00066
00067
00068 public byte[] decode(String data)
00069 {
00070 return data.getBytes();
00071 }
00072
00073
00074
00075
00076 public String getXml()
00077 {
00078 return DatabasesXmlTags.VAL_escaped;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088 public static String toPGString(byte[] postgresBuf)
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
00104
00105
00106
00107
00108 if (l_int < 040 || l_int > 0176)
00109 {
00110
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
00120
00121
00122 stringBuffer.append("\\\\");
00123 }
00124 else
00125 {
00126
00127 stringBuffer.append((char) postgresBuf[i]);
00128 }
00129 }
00130 String x = stringBuffer.toString();
00131
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 }
00143
00144
00145
00146
00147
00148
00149
00150
00151 public static byte[] toBytes(byte[] s)
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
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 }
00191 }