@@ -181,10 +181,7 @@ public static List<Pair<String, List<String>>> getCanonicalHeaders(Map<String, L
181181 * Each header-value pair is separated by a newline.
182182 */
183183 public static String getCanonicalHeadersString (List <Pair <String , List <String >>> canonicalHeaders ) {
184- // 2048 chosen experimentally to avoid always needing to resize the string builder's internal byte array.
185- // The minimal DynamoDB get-item request at the time of testing used ~1100 bytes. 2048 was chosen as the
186- // next-highest power-of-two.
187- StringBuilder result = new StringBuilder (2048 );
184+ StringBuilder result = new StringBuilder (512 );
188185 canonicalHeaders .forEach (header -> {
189186 result .append (header .left ());
190187 result .append (":" );
@@ -249,42 +246,35 @@ private static String getCanonicalRequestString(String httpMethod, String canoni
249246 * Matcher object as well.
250247 */
251248 private static void addAndTrim (StringBuilder result , String value ) {
252- int start = 0 ;
253- int valueLength = value .length ();
254-
255- // Find first non-whitespace
256- while (isWhiteSpace (value .charAt (start ))) {
257- ++start ;
258- if (start > valueLength ) {
259- return ;
260- }
261- }
262-
263- // Add things word-by-word
264- int lastWordStart = start ;
265- boolean lastWasWhitespace = false ;
266- for (int i = start ; i < valueLength ; i ++) {
267- char c = value .charAt (i );
268-
269- if (isWhiteSpace (c )) {
270- if (!lastWasWhitespace ) {
271- // End of word, add word
272- result .append (value , lastWordStart , i );
273- lastWasWhitespace = true ;
249+ int lengthBefore = result .length ();
250+ boolean isStart = true ;
251+ boolean previousIsWhiteSpace = false ;
252+
253+ for (int i = 0 ; i < value .length (); i ++) {
254+ char ch = value .charAt (i );
255+ if (isWhiteSpace (ch )) {
256+ if (previousIsWhiteSpace || isStart ) {
257+ continue ;
274258 }
259+ result .append (' ' );
260+ previousIsWhiteSpace = true ;
275261 } else {
276- if (lastWasWhitespace ) {
277- // Start of new word, add space
278- result .append (' ' );
279- lastWordStart = i ;
280- lastWasWhitespace = false ;
281- }
262+ result .append (ch );
263+ isStart = false ;
264+ previousIsWhiteSpace = false ;
282265 }
283266 }
284267
285- if (!lastWasWhitespace ) {
286- result .append (value , lastWordStart , valueLength );
268+ if (lengthBefore == result .length ()) {
269+ return ;
270+ }
271+
272+ int lastNonWhitespaceChar = result .length () - 1 ;
273+ while (isWhiteSpace (result .charAt (lastNonWhitespaceChar ))) {
274+ --lastNonWhitespaceChar ;
287275 }
276+
277+ result .setLength (lastNonWhitespaceChar + 1 );
288278 }
289279
290280 /**
@@ -375,17 +365,7 @@ private static String getCanonicalQueryString(SortedMap<String, List<String>> ca
375365 }
376366
377367 private static boolean isWhiteSpace (char ch ) {
378- switch (ch ) {
379- case ' ' :
380- case '\t' :
381- case '\n' :
382- case '\u000b' :
383- case '\r' :
384- case '\f' :
385- return true ;
386- default :
387- return false ;
388- }
368+ return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\u000b' || ch == '\r' || ch == '\f' ;
389369 }
390370
391371 /**
0 commit comments