1 24 25 package org.objectweb.cjdbc.common.sql.filters; 26 27 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 28 29 38 public class BlobEscapedFilter extends AbstractBlobFilter 39 { 40 41 44 public String encode(byte[] data) 45 { 46 return BlobEscapedFilter.toPGString(data); 47 } 48 49 52 public String encode(String data) 53 { 54 return data; 55 } 56 57 60 public byte[] decode(byte[] data) 61 { 62 return data; 63 } 64 65 68 public byte[] decode(String data) 69 { 70 return data.getBytes(); 71 } 72 73 76 public String getXml() 77 { 78 return DatabasesXmlTags.VAL_escaped; 79 } 80 81 88 public static String toPGString(byte[] postgresBuf) 89 { 90 if (postgresBuf == null) 91 { 92 return null; 93 } 94 StringBuffer stringBuffer = new StringBuffer (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 108 if (lInt < 040 || lInt > 0176) 109 { 110 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 122 stringBuffer.append("\\\\"); 123 } 124 else 125 { 126 127 stringBuffer.append((char) postgresBuf[i]); 128 } 129 } 130 String x = stringBuffer.toString(); 131 132 StringBuffer sbuf = new StringBuffer (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 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 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 |