Puts an UTF8 string into this byte vector. The byte vector is automatically enlarged if necessary. The string length is encoded in two bytes before the encoded characters, if there is space for that (i.e. if this.length - offset - 2 >= 0). @param stringValue the String to encode. @param offset t
(final String stringValue, final int offset, final int maxByteLength)
| 281 | * @return this byte vector. |
| 282 | */ |
| 283 | final ByteVector encodeUTF8(final String stringValue, final int offset, final int maxByteLength) { |
| 284 | int charLength = stringValue.length(); |
| 285 | int byteLength = offset; |
| 286 | for (int i = offset; i < charLength; ++i) { |
| 287 | char charValue = stringValue.charAt(i); |
| 288 | if (charValue >= '\u0001' && charValue <= '\u007F') { |
| 289 | byteLength++; |
| 290 | } else if (charValue <= '\u07FF') { |
| 291 | byteLength += 2; |
| 292 | } else { |
| 293 | byteLength += 3; |
| 294 | } |
| 295 | } |
| 296 | if (byteLength > maxByteLength) { |
| 297 | throw new IllegalArgumentException(); |
| 298 | } |
| 299 | // Compute where 'byteLength' must be stored in 'data', and store it at this location. |
| 300 | int byteLengthOffset = length - offset - 2; |
| 301 | if (byteLengthOffset >= 0) { |
| 302 | data[byteLengthOffset] = (byte) (byteLength >>> 8); |
| 303 | data[byteLengthOffset + 1] = (byte) byteLength; |
| 304 | } |
| 305 | if (length + byteLength - offset > data.length) { |
| 306 | enlarge(byteLength - offset); |
| 307 | } |
| 308 | int currentLength = length; |
| 309 | for (int i = offset; i < charLength; ++i) { |
| 310 | char charValue = stringValue.charAt(i); |
| 311 | if (charValue >= '\u0001' && charValue <= '\u007F') { |
| 312 | data[currentLength++] = (byte) charValue; |
| 313 | } else if (charValue <= '\u07FF') { |
| 314 | data[currentLength++] = (byte) (0xC0 | charValue >> 6 & 0x1F); |
| 315 | data[currentLength++] = (byte) (0x80 | charValue & 0x3F); |
| 316 | } else { |
| 317 | data[currentLength++] = (byte) (0xE0 | charValue >> 12 & 0xF); |
| 318 | data[currentLength++] = (byte) (0x80 | charValue >> 6 & 0x3F); |
| 319 | data[currentLength++] = (byte) (0x80 | charValue & 0x3F); |
| 320 | } |
| 321 | } |
| 322 | length = currentLength; |
| 323 | return this; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Puts an array of bytes into this byte vector. The byte vector is automatically enlarged if |
no test coverage detected