Class CharBufScratch
Internal API — not for external use.
Per-thread scratchchar[] 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 togetChars(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 Summary
Modifier and TypeMethodDescriptionstatic char[]Convenience overload that computess.length()once and delegates togetChars(String, int).static char[]Returns a thread-localchar[]buffer populated with the firstlencharacters ofsviaString.getChars(int, int, char[], int).
-
Method Details
-
getChars
Returns a thread-localchar[]buffer populated with the firstlencharacters ofsviaString.getChars(int, int, char[], int).The two-arg signature lets the caller pass a length it already computed (avoiding a redundant
s.length()call). Passinglen < s.length()copies only the firstlenchars (prefix extraction); passinglen > s.length()raisesStringIndexOutOfBoundsExceptionvia the underlyingString.getCharscall.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 satisfy0 <= len <= s.length().- Returns:
- a thread-local char[] whose first
lenentries hold the string's characters; indices beyond that are unspecified.
-
getChars
Convenience overload that computess.length()once and delegates togetChars(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:
-