Skip to content

Commit

Permalink
Get content as String for ContentCachingRequestWrapper
Browse files Browse the repository at this point in the history
This commits adds a getContentAsString method to
ContentCachingRequestWrapper that uses the configured charset without
copying the underlying byte array.

See spring-projectsgh-30709
  • Loading branch information
kilink authored and snicoll committed Aug 22, 2023
1 parent d529eee commit d9b8826
Showing 1 changed file with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
Expand Down Expand Up @@ -196,6 +197,14 @@ public byte[] getContentAsByteArray() {
return this.cachedContent.toByteArray();
}

/**
* Return the cached request content as a String. The Charset used to decode
* the cached content is the same as returned by getCharacterEncoding.
*/
public String getContentAsString() {
return this.cachedContent.toString(Charset.forName(getCharacterEncoding()));
}

/**
* Template method for handling a content overflow: specifically, a request
* body being read that exceeds the specified content cache limit.
Expand Down

0 comments on commit d9b8826

Please sign in to comment.