22
22
import org .jspecify .annotations .Nullable ;
23
23
24
24
import org .springframework .util .Assert ;
25
- import org .springframework .util .StringUtils ;
26
25
27
26
/**
28
27
* Represents a SockJS frame. Provides factory methods to create SockJS frames.
@@ -46,6 +45,11 @@ public class SockJsFrame {
46
45
private static final SockJsFrame CLOSE_ANOTHER_CONNECTION_OPEN_FRAME =
47
46
closeFrame (2010 , "Another connection still open" );
48
47
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 ;
49
53
50
54
private final SockJsFrameType type ;
51
55
@@ -83,7 +87,6 @@ else if (content.charAt(0) == 'c') {
83
87
}
84
88
}
85
89
86
-
87
90
/**
88
91
* Return the SockJS frame type.
89
92
*/
@@ -119,7 +122,6 @@ public byte[] getContentBytes() {
119
122
}
120
123
}
121
124
122
-
123
125
@ Override
124
126
public boolean equals (@ Nullable Object other ) {
125
127
return (this == other || (other instanceof SockJsFrame that &&
@@ -133,13 +135,32 @@ public int hashCode() {
133
135
134
136
@ Override
135
137
public String toString () {
136
- String result = this .content ;
137
- if (result .length () > 80 ) {
138
- result = result .substring (0 , 80 ) + "...(truncated)" ;
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 );
145
+
146
+ sb .append (FRAME_PREFIX );
147
+
148
+ for (int i = 0 ; i < truncatedLength ; i ++) {
149
+ char c = this .content .charAt (i );
150
+ switch (c ) {
151
+ case '\n' -> sb .append ("\\ n" );
152
+ case '\r' -> sb .append ("\\ r" );
153
+ default -> sb .append (c );
154
+ }
139
155
}
140
- result = StringUtils .replace (result , "\n " , "\\ n" );
141
- result = StringUtils .replace (result , "\r " , "\\ r" );
142
- return "SockJsFrame content='" + result + "'" ;
156
+
157
+ if (isTruncated ) {
158
+ sb .append (TRUNCATED_SUFFIX );
159
+ }
160
+
161
+ sb .append ('\'' );
162
+
163
+ return sb .toString ();
143
164
}
144
165
145
166
0 commit comments