1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.volley.toolbox; 17 18 import androidx.annotation.Nullable; 19 import com.android.volley.Header; 20 import java.io.ByteArrayInputStream; 21 import java.io.InputStream; 22 import java.util.Collections; 23 import java.util.List; 24 25 /** A response from an HTTP server. */ 26 public final class HttpResponse { 27 28 private final int mStatusCode; 29 private final List<Header> mHeaders; 30 private final int mContentLength; 31 @Nullable private final InputStream mContent; 32 @Nullable private final byte[] mContentBytes; 33 34 /** 35 * Construct a new HttpResponse for an empty response body. 36 * 37 * @param statusCode the HTTP status code of the response 38 * @param headers the response headers 39 */ HttpResponse(int statusCode, List<Header> headers)40 public HttpResponse(int statusCode, List<Header> headers) { 41 this(statusCode, headers, /* contentLength= */ -1, /* content= */ null); 42 } 43 44 /** 45 * Construct a new HttpResponse. 46 * 47 * @param statusCode the HTTP status code of the response 48 * @param headers the response headers 49 * @param contentLength the length of the response content. Ignored if there is no content. 50 * @param content an {@link InputStream} of the response content. May be null to indicate that 51 * the response has no content. 52 */ HttpResponse( int statusCode, List<Header> headers, int contentLength, InputStream content)53 public HttpResponse( 54 int statusCode, List<Header> headers, int contentLength, InputStream content) { 55 mStatusCode = statusCode; 56 mHeaders = headers; 57 mContentLength = contentLength; 58 mContent = content; 59 mContentBytes = null; 60 } 61 62 /** 63 * Construct a new HttpResponse. 64 * 65 * @param statusCode the HTTP status code of the response 66 * @param headers the response headers 67 * @param contentBytes a byte[] of the response content. This is an optimization for HTTP stacks 68 * that natively support returning a byte[]. 69 */ HttpResponse(int statusCode, List<Header> headers, byte[] contentBytes)70 public HttpResponse(int statusCode, List<Header> headers, byte[] contentBytes) { 71 mStatusCode = statusCode; 72 mHeaders = headers; 73 mContentLength = contentBytes.length; 74 mContentBytes = contentBytes; 75 mContent = null; 76 } 77 78 /** Returns the HTTP status code of the response. */ getStatusCode()79 public final int getStatusCode() { 80 return mStatusCode; 81 } 82 83 /** Returns the response headers. Must not be mutated directly. */ getHeaders()84 public final List<Header> getHeaders() { 85 return Collections.unmodifiableList(mHeaders); 86 } 87 88 /** Returns the length of the content. Only valid if {@link #getContent} is non-null. */ getContentLength()89 public final int getContentLength() { 90 return mContentLength; 91 } 92 93 /** 94 * If a byte[] was already provided by an HTTP stack that natively supports returning one, this 95 * method will return that byte[] as an optimization over copying the bytes from an input 96 * stream. It may return null, even if the response has content, as long as mContent is 97 * provided. 98 */ 99 @Nullable getContentBytes()100 public final byte[] getContentBytes() { 101 return mContentBytes; 102 } 103 104 /** 105 * Returns an {@link InputStream} of the response content. May be null to indicate that the 106 * response has no content. 107 */ 108 @Nullable getContent()109 public final InputStream getContent() { 110 if (mContent != null) { 111 return mContent; 112 } else if (mContentBytes != null) { 113 return new ByteArrayInputStream(mContentBytes); 114 } else { 115 return null; 116 } 117 } 118 } 119