Puts a String in UTF format into this byte vector. The byte vector is automatically enlarged if necessary. @param s a String. @return this byte vector.
(final String s)
| 211 | */ |
| 212 | |
| 213 | public ByteVector putUTF (final String s) { |
| 214 | int charLength = s.length(); |
| 215 | int byteLength = 0; |
| 216 | for (int i = 0; i < charLength; ++i) { |
| 217 | char c = s.charAt(i); |
| 218 | if (c >= '\001' && c <= '\177') { |
| 219 | byteLength++; |
| 220 | } else if (c > '\u07FF') { |
| 221 | byteLength += 3; |
| 222 | } else { |
| 223 | byteLength += 2; |
| 224 | } |
| 225 | } |
| 226 | if (byteLength > 65535) { |
| 227 | throw new IllegalArgumentException(); |
| 228 | } |
| 229 | int length = this.length; |
| 230 | if (length + 2 + byteLength > data.length) { |
| 231 | enlarge(2 + byteLength); |
| 232 | } |
| 233 | byte[] data = this.data; |
| 234 | data[length++] = (byte)(byteLength >>> 8); |
| 235 | data[length++] = (byte)(byteLength); |
| 236 | for (int i = 0; i < charLength; ++i) { |
| 237 | char c = s.charAt(i); |
| 238 | if (c >= '\001' && c <= '\177') { |
| 239 | data[length++] = (byte)c; |
| 240 | } else if (c > '\u07FF') { |
| 241 | data[length++] = (byte)(0xE0 | c >> 12 & 0xF); |
| 242 | data[length++] = (byte)(0x80 | c >> 6 & 0x3F); |
| 243 | data[length++] = (byte)(0x80 | c & 0x3F); |
| 244 | } else { |
| 245 | data[length++] = (byte)(0xC0 | c >> 6 & 0x1F); |
| 246 | data[length++] = (byte)(0x80 | c & 0x3F); |
| 247 | } |
| 248 | } |
| 249 | this.length = length; |
| 250 | return this; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Puts an array of bytes into this byte vector. The byte vector is |