Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ public static DataSize parse(CharSequence text) {
public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit) {
Assert.notNull(text, "Text must not be null");
try {
Matcher matcher = DataSizeUtils.PATTERN.matcher(StringUtils.trimAllWhitespace(text));
CharSequence trimmedText = StringUtils.trimAllWhitespace(text);
Matcher matcher = DataSizeUtils.PATTERN.matcher(trimmedText);
Assert.state(matcher.matches(), "Does not match data size pattern");
DataUnit unit = DataSizeUtils.determineDataUnit(matcher.group(2), defaultUnit);
long amount = Long.parseLong(matcher.group(1));
long amount = Long.parseLong(trimmedText, matcher.start(1), matcher.end(1), 10);
return DataSize.of(amount, unit);
}
catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ public boolean isHeartbeat() {

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,11 @@ public void setHeartbeat(@Nullable long[] heartbeat) {
@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)};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,17 @@ private static HttpRange parseRange(String range) {
Assert.hasLength(range, "Range String must not be empty");
int dashIdx = range.indexOf('-');
if (dashIdx > 0) {
long firstPos = Long.parseLong(range.substring(0, dashIdx));
long firstPos = Long.parseLong(range, 0, dashIdx, 10);
if (dashIdx < range.length() - 1) {
Long lastPos = Long.parseLong(range.substring(dashIdx + 1));
Long lastPos = Long.parseLong(range, dashIdx + 1, range.length(), 10);
return new ByteRange(firstPos, lastPos);
}
else {
return new ByteRange(firstPos, null);
}
}
else if (dashIdx == 0) {
long suffixLength = Long.parseLong(range.substring(1));
long suffixLength = Long.parseLong(range, 1, range.length(), 10);
return new SuffixByteRange(suffixLength);
}
else {
Expand Down