Use Long.parseLong(CharSequence,...) to avoid intermediate String creation

Where possible, switch to the Long.parseLong variant that accepts a
start and end index for the supplied CharSequence, thus avoiding making
unnecessary copies of the String input.

Closes gh-30710
This commit is contained in:
Patrick Strawderman
2023-06-20 20:55:35 -07:00
committed by Sam Brannen
parent af1c06917d
commit 01e90bbd0e
4 changed files with 12 additions and 11 deletions

View File

@@ -239,11 +239,11 @@ public class StompHeaderAccessor extends SimpMessageHeaderAccessor {
public long[] getHeartbeat() {
String rawValue = getFirstNativeHeader(STOMP_HEARTBEAT_HEADER);
String[] rawValues = StringUtils.split(rawValue, ",");
if (rawValues == null) {
int pos = rawValue != null ? rawValue.indexOf(',') : -1;
if (pos == -1) {
return Arrays.copyOf(DEFAULT_HEARTBEAT, 2);
}
return new long[] {Long.parseLong(rawValues[0]), Long.parseLong(rawValues[1])};
return new long[] {Long.parseLong(rawValue, 0, pos, 10), Long.parseLong(rawValue, pos + 1, rawValue.length(), 10)};
}
public void setAcceptVersion(String acceptVersion) {

View File

@@ -280,11 +280,11 @@ public class StompHeaders implements MultiValueMap<String, String>, Serializable
@Nullable
public long[] getHeartbeat() {
String rawValue = getFirst(HEARTBEAT);
String[] rawValues = StringUtils.split(rawValue, ",");
if (rawValues == null) {
int pos = rawValue != null ? rawValue.indexOf(',') : -1;
if (pos == -1) {
return null;
}
return new long[] {Long.parseLong(rawValues[0]), Long.parseLong(rawValues[1])};
return new long[] {Long.parseLong(rawValue, 0, pos, 10), Long.parseLong(rawValue, pos + 1, rawValue.length(), 10)};
}
/**