diff options
Diffstat (limited to 'src/main/java/com/android/volley/toolbox/HttpResponse.java')
-rw-r--r-- | src/main/java/com/android/volley/toolbox/HttpResponse.java | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/src/main/java/com/android/volley/toolbox/HttpResponse.java b/src/main/java/com/android/volley/toolbox/HttpResponse.java index 9a9294f..595f926 100644 --- a/src/main/java/com/android/volley/toolbox/HttpResponse.java +++ b/src/main/java/com/android/volley/toolbox/HttpResponse.java @@ -15,7 +15,9 @@ */ package com.android.volley.toolbox; +import androidx.annotation.Nullable; import com.android.volley.Header; +import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.Collections; import java.util.List; @@ -26,7 +28,8 @@ public final class HttpResponse { private final int mStatusCode; private final List<Header> mHeaders; private final int mContentLength; - private final InputStream mContent; + @Nullable private final InputStream mContent; + @Nullable private final byte[] mContentBytes; /** * Construct a new HttpResponse for an empty response body. @@ -53,6 +56,23 @@ public final class HttpResponse { mHeaders = headers; mContentLength = contentLength; mContent = content; + mContentBytes = null; + } + + /** + * Construct a new HttpResponse. + * + * @param statusCode the HTTP status code of the response + * @param headers the response headers + * @param contentBytes a byte[] of the response content. This is an optimization for HTTP stacks + * that natively support returning a byte[]. + */ + public HttpResponse(int statusCode, List<Header> headers, byte[] contentBytes) { + mStatusCode = statusCode; + mHeaders = headers; + mContentLength = contentBytes.length; + mContentBytes = contentBytes; + mContent = null; } /** Returns the HTTP status code of the response. */ @@ -71,10 +91,28 @@ public final class HttpResponse { } /** + * If a byte[] was already provided by an HTTP stack that natively supports returning one, this + * method will return that byte[] as an optimization over copying the bytes from an input + * stream. It may return null, even if the response has content, as long as mContent is + * provided. + */ + @Nullable + public final byte[] getContentBytes() { + return mContentBytes; + } + + /** * Returns an {@link InputStream} of the response content. May be null to indicate that the * response has no content. */ + @Nullable public final InputStream getContent() { - return mContent; + if (mContent != null) { + return mContent; + } else if (mContentBytes != null) { + return new ByteArrayInputStream(mContentBytes); + } else { + return null; + } } } |