2222import org .jspecify .annotations .Nullable ;
2323
2424import org .springframework .util .Assert ;
25- import org .springframework .util .StringUtils ;
2625
2726/**
2827 * Represents a SockJS frame. Provides factory methods to create SockJS frames.
@@ -46,6 +45,11 @@ public class SockJsFrame {
4645 private static final SockJsFrame CLOSE_ANOTHER_CONNECTION_OPEN_FRAME =
4746 closeFrame (2010 , "Another connection still open" );
4847
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 ;
4953
5054 private final SockJsFrameType type ;
5155
@@ -83,7 +87,6 @@ else if (content.charAt(0) == 'c') {
8387 }
8488 }
8589
86-
8790 /**
8891 * Return the SockJS frame type.
8992 */
@@ -119,7 +122,6 @@ public byte[] getContentBytes() {
119122 }
120123 }
121124
122-
123125 @ Override
124126 public boolean equals (@ Nullable Object other ) {
125127 return (this == other || (other instanceof SockJsFrame that &&
@@ -133,13 +135,32 @@ public int hashCode() {
133135
134136 @ Override
135137 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+ }
139155 }
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 ();
143164 }
144165
145166
0 commit comments