Home | History | Annotate | Download | only in toolbox
      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 com.android.volley.Header;
     19 
     20 import java.io.InputStream;
     21 import java.util.Collections;
     22 import java.util.List;
     23 
     24 /** A response from an HTTP server. */
     25 public final class HttpResponse {
     26 
     27     private final int mStatusCode;
     28     private final List<Header> mHeaders;
     29     private final int mContentLength;
     30     private final InputStream mContent;
     31 
     32     /**
     33      * Construct a new HttpResponse for an empty response body.
     34      *
     35      * @param statusCode the HTTP status code of the response
     36      * @param headers the response headers
     37      */
     38     public HttpResponse(int statusCode, List<Header> headers) {
     39         this(statusCode, headers, -1 /* contentLength */, null /* content */);
     40     }
     41 
     42     /**
     43      * Construct a new HttpResponse.
     44      *
     45      * @param statusCode the HTTP status code of the response
     46      * @param headers the response headers
     47      * @param contentLength the length of the response content. Ignored if there is no content.
     48      * @param content an {@link InputStream} of the response content. May be null to indicate that
     49      *     the response has no content.
     50      */
     51     public HttpResponse(
     52             int statusCode, List<Header> headers, int contentLength, InputStream content) {
     53         mStatusCode = statusCode;
     54         mHeaders = headers;
     55         mContentLength = contentLength;
     56         mContent = content;
     57     }
     58 
     59     /** Returns the HTTP status code of the response. */
     60     public final int getStatusCode() {
     61         return mStatusCode;
     62     }
     63 
     64     /** Returns the response headers. Must not be mutated directly. */
     65     public final List<Header> getHeaders() {
     66         return Collections.unmodifiableList(mHeaders);
     67     }
     68 
     69     /** Returns the length of the content. Only valid if {@link #getContent} is non-null. */
     70     public final int getContentLength() {
     71         return mContentLength;
     72     }
     73 
     74     /**
     75      * Returns an {@link InputStream} of the response content. May be null to indicate that the
     76      * response has no content.
     77      */
     78     public final InputStream getContent() {
     79         return mContent;
     80     }
     81 }
     82