Skip to content
Closed
Changes from 12 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 @@ -22,7 +22,6 @@
import org.jspecify.annotations.Nullable;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Represents a SockJS frame. Provides factory methods to create SockJS frames.
Expand All @@ -46,6 +45,7 @@ public class SockJsFrame {
private static final SockJsFrame CLOSE_ANOTHER_CONNECTION_OPEN_FRAME =
closeFrame(2010, "Another connection still open");

private static final String TRUNCATED_SUFFIX = "...(truncated)";

private final SockJsFrameType type;

Expand Down Expand Up @@ -83,7 +83,6 @@ else if (content.charAt(0) == 'c') {
}
}


/**
* Return the SockJS frame type.
*/
Expand Down Expand Up @@ -119,7 +118,6 @@ public byte[] getContentBytes() {
}
}


@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof SockJsFrame that &&
Expand All @@ -133,13 +131,28 @@ public int hashCode() {

@Override
public String toString() {
String result = this.content;
if (result.length() > 80) {
result = result.substring(0, 80) + "...(truncated)";
int maxLength = 80;
int contentLength = this.content.length();
int truncatedContentLength = Math.min(contentLength, maxLength);

int extra = (contentLength > maxLength ? TRUNCATED_SUFFIX.length() : 0);

StringBuilder sb = new StringBuilder(truncatedContentLength + extra);

for (int i = 0; i < truncatedContentLength; i++) {
char c = this.content.charAt(i);
switch (c) {
case '\n' -> sb.append("\\n");
case '\r' -> sb.append("\\r");
default -> sb.append(c);
}
}

if (contentLength > maxLength) {
sb.append(TRUNCATED_SUFFIX);
}
result = StringUtils.replace(result, "\n", "\\n");
result = StringUtils.replace(result, "\r", "\\r");
return "SockJsFrame content='" + result + "'";

return "SockJsFrame content='" + sb + "'";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the String Builder for this part as well? This PR is trying to optimize for memory allocation after all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review.

Regarding the other toString() methods, they also use StringBuilder, so I thought there’s no need for the unnecessary String objects to become GC targets in the JVM. Of course, I agree that the original code is more readable.

My perspective is that the readability of this part isn’t particularly high, and concatenating immutable Strings in this way is generally considered an anti-pattern, so I opted to use a mutable StringBuilder.

I will revise the return part as you suggested.

@bclozel

}


Expand Down