/** * Returns an {@link AsciiString} containing the given string and retains/caches the input * string for later use in {@link #toString()}. * Used for the constants (which already stored in the JVM's string table) and in cases * where the guaranteed use of the {@link #toString()} method. */ public static AsciiString cached(String string) { AsciiString asciiString = new AsciiString(string); asciiString.string = string; return asciiString; }
private static AsciiString[] toAsciiStringArray(String[] jdkResult) { AsciiString[] res = new AsciiString[jdkResult.length]; for (int i = 0; i < jdkResult.length; i++) { res[i] = new AsciiString(jdkResult[i]); } return res; }
/** * Create an optimized {@link CharSequence} which can be used as header name or value. * This should be used if you expect to use it multiple times liked for example adding the same header name or value * for multiple responses or requests. */ public static CharSequence createOptimized(String value) { return new AsciiString(value); }
private static AsciiString[] toAsciiStringArray(String[] jdkResult) { AsciiString[] res = new AsciiString[jdkResult.length]; for (int i = 0; i < jdkResult.length; i++) { res[i] = new AsciiString(jdkResult[i]); } return res; }
/** * Returns an {@link AsciiString} containing the given string and retains/caches the input * string for later use in {@link #toString()}. * Used for the constants (which already stored in the JVM's string table) and in cases * where the guaranteed use of the {@link #toString()} method. */ public static AsciiString cached(String string) { AsciiString asciiString = new AsciiString(string); asciiString.string = string; return asciiString; }
/** * Returns an {@link AsciiString} containing the given character sequence. If the given string is already a * {@link AsciiString}, just returns the same instance. */ public static AsciiString of(CharSequence string) { return string.getClass() == AsciiString.class ? (AsciiString) string : new AsciiString(string); }
private static AsciiString[] toAsciiStringArray(String[] jdkResult) { AsciiString[] res = new AsciiString[jdkResult.length]; for (int i = 0; i < jdkResult.length; i++) { res[i] = new AsciiString(jdkResult[i]); } return res; }
/** * Returns an {@link AsciiString} containing the given string and retains/caches the input * string for later use in {@link #toString()}. * Used for the constants (which already stored in the JVM's string table) and in cases * where the guaranteed use of the {@link #toString()} method. */ public static AsciiString cached(String string) { AsciiString asciiString = new AsciiString(string); asciiString.string = string; return asciiString; }
/** * Returns an {@link AsciiString} containing the given character sequence. If the given string is already a * {@link AsciiString}, just returns the same instance. */ public static AsciiString of(CharSequence string) { return string.getClass() == AsciiString.class ? (AsciiString) string : new AsciiString(string); }
@Override public CharSequence getCharSequence(int index, int length, Charset charset) { if (CharsetUtil.US_ASCII.equals(charset) || CharsetUtil.ISO_8859_1.equals(charset)) { // ByteBufUtil.getBytes(...) will return a new copy which the AsciiString uses directly return new AsciiString(ByteBufUtil.getBytes(this, index, length, true), false); } return toString(index, length, charset); }
@Override public CharSequence getCharSequence(int index, int length, Charset charset) { if (CharsetUtil.US_ASCII.equals(charset) || CharsetUtil.ISO_8859_1.equals(charset)) { // ByteBufUtil.getBytes(...) will return a new copy which the AsciiString uses directly return new AsciiString(ByteBufUtil.getBytes(this, index, length, true), false); } return toString(index, length, charset); }
private CharSequence readStringLiteral(ByteBuf in, int length, boolean huffmanEncoded) throws Http2Exception { if (huffmanEncoded) { return hpackHuffmanDecoder.decode(in, length); } byte[] buf = new byte[length]; in.readBytes(buf); return new AsciiString(buf, false); }
System.arraycopy(value, arrayOffset(), newValue, 0, thisLen); System.arraycopy(that.value, that.arrayOffset(), newValue, thisLen, thatLen); return new AsciiString(newValue, false); return new AsciiString(string); return new AsciiString(newValue, false);
/** * Duplicates this string removing white space characters from the beginning and end of the * string, without copying. * * @return a new string with characters {@code <= \\u0020} removed from the beginning and the end. */ public AsciiString trim() { int start = arrayOffset(), last = arrayOffset() + length() - 1; int end = last; while (start <= end && value[start] <= ' ') { start++; } while (end >= start && value[end] <= ' ') { end--; } if (start == 0 && end == last) { return this; } return new AsciiString(value, start, end - start + 1, false); }
/** * Either copy or share a subset of underlying sub-sequence of bytes. * @param start the offset of the first character (inclusive). * @param end The index to stop at (exclusive). * @param copy If {@code true} then a copy of the underlying storage will be made. * If {@code false} then the underlying storage will be shared. * @return a new string containing the characters from start to the end of the string. * @throws IndexOutOfBoundsException if {@code start < 0} or {@code start > length()}. */ public AsciiString subSequence(int start, int end, boolean copy) { if (isOutOfBounds(start, end - start, length())) { throw new IndexOutOfBoundsException("expected: 0 <= start(" + start + ") <= end (" + end + ") <= length(" + length() + ')'); } if (start == 0 && end == length()) { return this; } if (end == start) { return EMPTY_STRING; } return new AsciiString(value, start + offset, end - start, copy); }
/** * Copies this string replacing occurrences of the specified character with another character. * * @param oldChar the character to replace. * @param newChar the replacement character. * @return a new string with occurrences of oldChar replaced by newChar. */ public AsciiString replace(char oldChar, char newChar) { if (oldChar > MAX_CHAR_VALUE) { return this; } final byte oldCharAsByte = c2b0(oldChar); final byte newCharAsByte = c2b(newChar); final int len = offset + length; for (int i = offset; i < len; ++i) { if (value[i] == oldCharAsByte) { byte[] buffer = PlatformDependent.allocateUninitializedArray(length()); System.arraycopy(value, offset, buffer, 0, i - offset); buffer[i - offset] = newCharAsByte; ++i; for (; i < len; ++i) { byte oldValue = value[i]; buffer[i - offset] = oldValue != oldCharAsByte ? oldValue : newCharAsByte; } return new AsciiString(buffer, false); } } return this; }
@Test public void testToString() { assertEquals("the_string", ConversionHelper.toObject(new AsciiString("the_string"))); }
/** * Converts the characters in this string to uppercase, using the default Locale. * * @return a new string containing the uppercase characters equivalent to the characters in this string. */ public AsciiString toUpperCase() { boolean uppercased = true; int i, j; final int len = length() + arrayOffset(); for (i = arrayOffset(); i < len; ++i) { byte b = value[i]; if (b >= 'a' && b <= 'z') { uppercased = false; break; } } // Check if this string does not contain any lowercase characters. if (uppercased) { return this; } final byte[] newValue = PlatformDependent.allocateUninitializedArray(length()); for (i = 0, j = arrayOffset(); i < newValue.length; ++i, ++j) { newValue[i] = toUpperCase(value[j]); } return new AsciiString(newValue, false); }
/** * Converts the characters in this string to lowercase, using the default Locale. * * @return a new string containing the lowercase characters equivalent to the characters in this string. */ public AsciiString toLowerCase() { boolean lowercased = true; int i, j; final int len = length() + arrayOffset(); for (i = arrayOffset(); i < len; ++i) { byte b = value[i]; if (b >= 'A' && b <= 'Z') { lowercased = false; break; } } // Check if this string does not contain any uppercase characters. if (lowercased) { return this; } final byte[] newValue = PlatformDependent.allocateUninitializedArray(length()); for (i = 0, j = arrayOffset(); i < newValue.length; ++i, ++j) { newValue[i] = toLowerCase(value[j]); } return new AsciiString(newValue, false); }
@Test public void testToJsonObject() { Map<String, Object> map = new HashMap<>(); map.put("string", "the_string"); map.put("integer", 4); map.put("boolean", true); map.put("charsequence", new AsciiString("the_charsequence")); map.put("biginteger", new BigInteger("1234567")); map.put("binary", Buffer.buffer("hello")); map.put("object", Collections.singletonMap("nested", 4)); map.put("array", Arrays.asList(1, 2, 3)); JsonObject json = (JsonObject) ConversionHelper.toObject(map); assertEquals(8, json.size()); assertEquals("the_string", json.getString("string")); assertEquals(4, (int)json.getInteger("integer")); assertEquals(true, json.getBoolean("boolean")); assertEquals("the_charsequence", json.getString("charsequence")); assertEquals(1234567, (int)json.getInteger("biginteger")); assertEquals("hello", new String(json.getBinary("binary"))); assertEquals(new JsonObject().put("nested", 4), json.getJsonObject("object")); assertEquals(new JsonArray().add(1).add(2).add(3), json.getJsonArray("array")); }