1 25 package org.jresearch.gossip.util; 26 27 33 public class MySQLCodec { 34 35 public static char[] defSpecSymbols = new char[] { '"', '\'', '\\' }; 36 37 public static final char ESCAPE = '\\'; 38 39 50 public static String encode(final String text) { 51 return encode(text, defSpecSymbols); 52 } 53 54 69 public static String encode(final String text, final String specSymbols) { 70 final char[] cSpecSymbols; 71 72 if ((specSymbols == null) || (specSymbols.length() == 0)) { 73 cSpecSymbols = defSpecSymbols; 74 } else { 75 cSpecSymbols = specSymbols.toCharArray(); 76 } 77 78 return encode(text, cSpecSymbols); 79 } 80 81 97 public static String encode(final String text, char[] specSymbols) { 98 if (text == null) { 99 return null; 100 } 101 102 if ((specSymbols == null) || (specSymbols.length == 0)) { 103 specSymbols = defSpecSymbols; 104 } 105 106 StringBuffer buffer = new StringBuffer (text.length()); 107 char[] cText = text.toCharArray(); 108 109 for (int i = 0; i < cText.length; i++) { 110 char cTextChar = cText[i]; boolean isSpecial = false; 112 113 for (int ss = 0; ss < specSymbols.length; ss++) { 114 if (specSymbols[ss] == cTextChar) { 115 isSpecial = true; 116 buffer.append(ESCAPE); 117 buffer.append(cTextChar); 118 119 break; } 121 } 122 123 if (!isSpecial) { 124 buffer.append(cTextChar); 125 } 126 } 127 128 return buffer.toString(); 129 } 130 } 131 | Popular Tags |