com.google.api.client.http.HttpMediaType

Here are the examples of the java api class com.google.api.client.http.HttpMediaType taken from open source projects.

1. MockLowLevelHttpRequest#getContentAsString()

Project: google-http-java-client
File: MockLowLevelHttpRequest.java
/**
   * Returns HTTP content as a string, taking care of any encodings of the content if necessary.
   *
   * <p>
   * Returns an empty string if there is no HTTP content.
   * </p>
   *
   * @since 1.12
   */
public String getContentAsString() throws IOException {
    if (getStreamingContent() == null) {
        return "";
    }
    // write content to a byte[]
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    getStreamingContent().writeTo(out);
    // determine gzip encoding
    String contentEncoding = getContentEncoding();
    if (contentEncoding != null && contentEncoding.contains("gzip")) {
        InputStream contentInputStream = new GZIPInputStream(new ByteArrayInputStream(out.toByteArray()));
        out = new ByteArrayOutputStream();
        IOUtils.copy(contentInputStream, out);
    }
    // determine charset parameter from content type
    String contentType = getContentType();
    HttpMediaType mediaType = contentType != null ? new HttpMediaType(contentType) : null;
    Charset charset = mediaType == null || mediaType.getCharsetParameter() == null ? Charsets.ISO_8859_1 : mediaType.getCharsetParameter();
    return out.toString(charset.name());
}