1 // Copyright 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.google.android.downloader;
16 
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.io.Closeable;
19 import java.nio.channels.WritableByteChannel;
20 import java.util.List;
21 import java.util.Map;
22 
23 /**
24  * Interface representing a response to a URL request.
25  *
26  * <p>Note that this extends from {@link Closeable}. Callers that successfully obtain an instance of
27  * the response (via the future returned by {@link UrlRequest#send}) must take care to close the
28  * response once done using it.
29  */
30 public interface UrlResponse extends Closeable {
31   /**
32    * The HTTP status code returned by the server. Returns -1 if the response code can't be discerned
33    * or doesn't make sense for this response implementation, as mentioned in the javadocs for {@link
34    * java.net.HttpURLConnection#getResponseCode}
35    */
getResponseCode()36   int getResponseCode();
37 
38   /** The multi-valued HTTP response headers returned by the server. */
getResponseHeaders()39   Map<String, List<String>> getResponseHeaders();
40 
41   /**
42    * Writes the response body to the provided {@link WritableByteChannel}. The channel must be open
43    * prior to calling this method.
44    *
45    * <p>This method may only be called once for a given response! Attempting to call this method
46    * multiple times results in undefined behavior.
47    *
48    * @return future that resolves to the number of bytes written to the channel.
49    */
readResponseBody(WritableByteChannel destinationChannel)50   ListenableFuture<Long> readResponseBody(WritableByteChannel destinationChannel);
51 }
52