@@ -181,10 +181,7 @@ public static List<Pair<String, List<String>>> getCanonicalHeaders(Map<String, L
181
181
* Each header-value pair is separated by a newline.
182
182
*/
183
183
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 );
188
185
canonicalHeaders .forEach (header -> {
189
186
result .append (header .left ());
190
187
result .append (":" );
@@ -249,42 +246,35 @@ private static String getCanonicalRequestString(String httpMethod, String canoni
249
246
* Matcher object as well.
250
247
*/
251
248
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 ;
274
258
}
259
+ result .append (' ' );
260
+ previousIsWhiteSpace = true ;
275
261
} 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 ;
282
265
}
283
266
}
284
267
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 ;
287
275
}
276
+
277
+ result .setLength (lastNonWhitespaceChar + 1 );
288
278
}
289
279
290
280
/**
@@ -375,17 +365,7 @@ private static String getCanonicalQueryString(SortedMap<String, List<String>> ca
375
365
}
376
366
377
367
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' ;
389
369
}
390
370
391
371
/**
0 commit comments