(String str)
| 108 | return new StringBuffer(new String(toUTFArray(str.toString()))); |
| 109 | } |
| 110 | public static byte[] toUTFArray(String str) { |
| 111 | int srcLen = str.length(); |
| 112 | // srcLen * 2 because russian utf8 character is 2 bytes length and we are russian. |
| 113 | ByteArrayOutputStream out = new ByteArrayOutputStream(srcLen * 2); |
| 114 | for(int i=0; i < srcLen; ++i) { |
| 115 | int c = (int)str.charAt(i); |
| 116 | |
| 117 | //TODO: эскейпить коды <0x20 |
| 118 | if ((c >= 0) && (c <= 0x7f)) { |
| 119 | out.write(c); |
| 120 | |
| 121 | } else if (((c >= 0x80) && (c <= 0x7ff))) { |
| 122 | out.write((char)(0xc0 | (0x1f & (c >> 6)))); |
| 123 | out.write((char)(0x80 | (0x3f & c))); |
| 124 | |
| 125 | } else if ((c >= 0x800) && (c <= 0xffff)) { |
| 126 | out.write(((char)(0xe0 | (0x0f & (c >> 12))))); |
| 127 | out.write((char)(0x80 | (0x3f & (c >> 6)))); |
| 128 | out.write(((char)(0x80 | (0x3f & c)))); |
| 129 | } |
| 130 | } |
| 131 | return out.toByteArray(); |
| 132 | } |
| 133 | |
| 134 | public static byte[] fromBase64(String s) { |
| 135 | return baosFromBase64(s).toByteArray(); |
no test coverage detected