Skip to content

Commit 7f03487

Browse files
committed
Polishing contribution
Closes gh-35510
1 parent e4a431e commit 7f03487

File tree

1 file changed

+10
-17
lines changed
  • spring-websocket/src/main/java/org/springframework/web/socket/sockjs/frame

1 file changed

+10
-17
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/frame/SockJsFrame.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ public class SockJsFrame {
4545
private static final SockJsFrame CLOSE_ANOTHER_CONNECTION_OPEN_FRAME =
4646
closeFrame(2010, "Another connection still open");
4747

48-
private static final String TRUNCATED_SUFFIX = "...(truncated)";
49-
50-
private static final String FRAME_PREFIX = "SockJsFrame content='";
51-
52-
private static final int MAX_CONTENT_PREVIEW_LENGTH = 80;
5348

5449
private final SockJsFrameType type;
5550

@@ -87,6 +82,7 @@ else if (content.charAt(0) == 'c') {
8782
}
8883
}
8984

85+
9086
/**
9187
* Return the SockJS frame type.
9288
*/
@@ -122,6 +118,7 @@ public byte[] getContentBytes() {
122118
}
123119
}
124120

121+
125122
@Override
126123
public boolean equals(@Nullable Object other) {
127124
return (this == other || (other instanceof SockJsFrame that &&
@@ -135,27 +132,23 @@ public int hashCode() {
135132

136133
@Override
137134
public String toString() {
138-
int contentLength = this.content.length();
139-
int truncatedLength = Math.min(contentLength, MAX_CONTENT_PREVIEW_LENGTH);
140-
boolean isTruncated = contentLength > MAX_CONTENT_PREVIEW_LENGTH;
141-
142-
int suffixLength = isTruncated ? TRUNCATED_SUFFIX.length() : 0;
143-
int initialCapacity = FRAME_PREFIX.length() + truncatedLength + suffixLength + 1;
144-
StringBuilder sb = new StringBuilder(initialCapacity);
135+
int maxLength = 80;
136+
int length = Math.min(this.content.length(), maxLength);
145137

146-
sb.append(FRAME_PREFIX);
138+
StringBuilder sb = new StringBuilder(length + 36);
139+
sb.append("SockJsFrame content='");
147140

148-
for (int i = 0; i < truncatedLength; i++) {
141+
for (int i = 0; i < length; i++) {
149142
char c = this.content.charAt(i);
150-
switch (c) {
143+
switch(c) {
151144
case '\n' -> sb.append("\\n");
152145
case '\r' -> sb.append("\\r");
153146
default -> sb.append(c);
154147
}
155148
}
156149

157-
if (isTruncated) {
158-
sb.append(TRUNCATED_SUFFIX);
150+
if (length < this.content.length()) {
151+
sb.append("...(truncated)");
159152
}
160153

161154
sb.append('\'');

0 commit comments

Comments
 (0)