Class CharBufScratch

java.lang.Object
com.cedarsoftware.util.internal.CharBufScratch

public final class CharBufScratch extends Object

Internal API — not for external use.

Per-thread scratch char[] buffer for callers that walk a String as raw chars in a tight loop. getChars(String, int) bulk-copies the input via String.getChars(int, int, char[], int) — a HotSpot intrinsic that uses SIMD (SSE2/AVX2/NEON) to expand compact-string byte[] storage into chars in one pass. Subsequent buf[i] reads run as straight-line array access, avoiding the per-character charAt dispatch (LATIN1/UTF16 coder branch + bounds check + method dispatch). Typical wins are 20-50% on strings of 7+ characters.

This class is exposed to com.cedarsoftware:json-io via a qualified JPMS export (exports com.cedarsoftware.util.internal to com.cedarsoftware.io). Calling it from outside Cedar Software libraries is unsupported; signatures and semantics may change without notice across minor releases. Use String.getChars(int, int, char[], int) directly with your own buffer if you need this functionality from external code.

Re-entrancy contract

The returned array is a shared per-thread buffer. It is valid only until the next call to getChars(String, int) on the same thread. Consume the contents (or copy them out) before invoking any other getChars-based helper or calling methods that might do so transitively. Do NOT store the reference beyond the immediate scope. If you need two buffers simultaneously on the same thread (e.g., comparing two strings), copy one out or allocate a local new char[].
Author:
John DeRegnaucourt (jdereg@gmail.com)
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  • Method Details

    • getChars

      public static char[] getChars(String s, int len)
      Returns a thread-local char[] buffer populated with the first len characters of s via String.getChars(int, int, char[], int).

      The two-arg signature lets the caller pass a length it already computed (avoiding a redundant s.length() call). Passing len < s.length() copies only the first len chars (prefix extraction); passing len > s.length() raises StringIndexOutOfBoundsException via the underlying String.getChars call.

      See class-level Javadoc for the re-entrancy contract.

      Parameters:
      s - the string whose characters to extract (must not be null)
      len - the number of characters to copy, starting at index 0. Must satisfy 0 <= len <= s.length().
      Returns:
      a thread-local char[] whose first len entries hold the string's characters; indices beyond that are unspecified.
    • getChars

      public static char[] getChars(String s)
      Convenience overload that computes s.length() once and delegates to getChars(String, int). Prefer the two-arg form when the caller already has the length available.

      All thread-local semantics of getChars(String, int) apply here.

      Parameters:
      s - the string whose characters to extract (must not be null)
      Returns:
      a thread-local char[] whose first s.length() entries hold the string's characters; indices beyond that are unspecified.
      See Also: