001: package org.bouncycastle.util;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.util.Vector;
005:
006: public final class Strings {
007: public static String fromUTF8ByteArray(byte[] bytes) {
008: int i = 0;
009: int length = 0;
010:
011: while (i < bytes.length) {
012: length++;
013: if ((bytes[i] & 0xf0) == 0xf0) {
014: // surrogate pair
015: length++;
016: i += 4;
017: } else if ((bytes[i] & 0xe0) == 0xe0) {
018: i += 3;
019: } else if ((bytes[i] & 0xc0) == 0xc0) {
020: i += 2;
021: } else {
022: i += 1;
023: }
024: }
025:
026: char[] cs = new char[length];
027:
028: i = 0;
029: length = 0;
030:
031: while (i < bytes.length) {
032: char ch;
033:
034: if ((bytes[i] & 0xf0) == 0xf0) {
035: int codePoint = ((bytes[i] & 0x03) << 18)
036: | ((bytes[i + 1] & 0x3F) << 12)
037: | ((bytes[i + 2] & 0x3F) << 6)
038: | (bytes[i + 3] & 0x3F);
039: int U = codePoint - 0x10000;
040: char W1 = (char) (0xD800 | (U >> 10));
041: char W2 = (char) (0xDC00 | (U & 0x3FF));
042: cs[length++] = W1;
043: ch = W2;
044: i += 4;
045: } else if ((bytes[i] & 0xe0) == 0xe0) {
046: ch = (char) (((bytes[i] & 0x0f) << 12)
047: | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f));
048: i += 3;
049: } else if ((bytes[i] & 0xd0) == 0xd0) {
050: ch = (char) (((bytes[i] & 0x1f) << 6) | (bytes[i + 1] & 0x3f));
051: i += 2;
052: } else if ((bytes[i] & 0xc0) == 0xc0) {
053: ch = (char) (((bytes[i] & 0x1f) << 6) | (bytes[i + 1] & 0x3f));
054: i += 2;
055: } else {
056: ch = (char) (bytes[i] & 0xff);
057: i += 1;
058: }
059:
060: cs[length++] = ch;
061: }
062:
063: return new String(cs);
064: }
065:
066: public static byte[] toUTF8ByteArray(String string) {
067: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
068: char[] c = string.toCharArray();
069: int i = 0;
070:
071: while (i < c.length) {
072: char ch = c[i];
073:
074: if (ch < 0x0080) {
075: bOut.write(ch);
076: } else if (ch < 0x0800) {
077: bOut.write(0xc0 | (ch >> 6));
078: bOut.write(0x80 | (ch & 0x3f));
079: }
080: // surrogate pair
081: else if (ch >= 0xD800 && ch <= 0xDFFF) {
082: // in error - can only happen, if the Java String class has a
083: // bug.
084: if (i + 1 >= c.length) {
085: throw new IllegalStateException(
086: "invalid UTF-16 codepoint");
087: }
088: char W1 = ch;
089: ch = c[++i];
090: char W2 = ch;
091: // in error - can only happen, if the Java String class has a
092: // bug.
093: if (W1 > 0xDBFF) {
094: throw new IllegalStateException(
095: "invalid UTF-16 codepoint");
096: }
097: int codePoint = (((W1 & 0x03FF) << 10) | (W2 & 0x03FF)) + 0x10000;
098: bOut.write(0xf0 | (codePoint >> 18));
099: bOut.write(0x80 | ((codePoint >> 12) & 0x3F));
100: bOut.write(0x80 | ((codePoint >> 6) & 0x3F));
101: bOut.write(0x80 | (codePoint & 0x3F));
102: } else {
103: bOut.write(0xe0 | (ch >> 12));
104: bOut.write(0x80 | ((ch >> 6) & 0x3F));
105: bOut.write(0x80 | (ch & 0x3F));
106: }
107:
108: i++;
109: }
110:
111: return bOut.toByteArray();
112: }
113:
114: /**
115: * A locale independent version of toUpperCase.
116: *
117: * @param string input to be converted
118: * @return a US Ascii uppercase version
119: */
120: public static String toUpperCase(String string) {
121: boolean changed = false;
122: char[] chars = string.toCharArray();
123:
124: for (int i = 0; i != chars.length; i++) {
125: char ch = chars[i];
126: if ('a' <= ch && 'z' >= ch) {
127: changed = true;
128: chars[i] = (char) (ch - 'a' + 'A');
129: }
130: }
131:
132: if (changed) {
133: return new String(chars);
134: }
135:
136: return string;
137: }
138:
139: /**
140: * A locale independent version of toLowerCase.
141: *
142: * @param string input to be converted
143: * @return a US ASCII lowercase version
144: */
145: public static String toLowerCase(String string) {
146: boolean changed = false;
147: char[] chars = string.toCharArray();
148:
149: for (int i = 0; i != chars.length; i++) {
150: char ch = chars[i];
151: if ('A' <= ch && 'Z' >= ch) {
152: changed = true;
153: chars[i] = (char) (ch - 'A' + 'a');
154: }
155: }
156:
157: if (changed) {
158: return new String(chars);
159: }
160:
161: return string;
162: }
163:
164: public static byte[] toByteArray(String string) {
165: byte[] bytes = new byte[string.length()];
166:
167: for (int i = 0; i != bytes.length; i++) {
168: char ch = string.charAt(i);
169:
170: bytes[i] = (byte) ch;
171: }
172:
173: return bytes;
174: }
175:
176: public static String[] split(String input, char delimiter) {
177: Vector v = new Vector();
178: boolean moreTokens = true;
179: String subString;
180:
181: while (moreTokens) {
182: int tokenLocation = input.indexOf(delimiter);
183: if (tokenLocation > 0) {
184: subString = input.substring(0, tokenLocation);
185: v.addElement(subString);
186: input = input.substring(tokenLocation + 1);
187: } else {
188: moreTokens = false;
189: v.addElement(input);
190: }
191: }
192:
193: String[] res = new String[v.size()];
194:
195: for (int i = 0; i != res.length; i++) {
196: res[i] = (String) v.elementAt(i);
197: }
198: return res;
199: }
200: }
|