-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
FileDownloadUrlConnection.java
209 lines (175 loc) · 6.66 KB
/
FileDownloadUrlConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (c) 2015 LingoChamp Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.liulishuo.filedownloader.connection;
import com.liulishuo.filedownloader.util.FileDownloadHelper;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
/**
* The FileDownloadConnection implemented using {@link URLConnection}.
*/
public class FileDownloadUrlConnection implements FileDownloadConnection {
protected URLConnection mConnection;
public FileDownloadUrlConnection(String originUrl, Configuration configuration)
throws IOException {
this(new URL(originUrl), configuration);
}
public FileDownloadUrlConnection(URL url, Configuration configuration) throws IOException {
if (configuration != null && configuration.proxy != null) {
mConnection = url.openConnection(configuration.proxy);
} else {
mConnection = url.openConnection();
}
if (mConnection instanceof HttpURLConnection) {
((HttpURLConnection) mConnection).setInstanceFollowRedirects(false);
}
if (configuration != null) {
if (configuration.readTimeout != null) {
mConnection.setReadTimeout(configuration.readTimeout);
}
if (configuration.connectTimeout != null) {
mConnection.setConnectTimeout(configuration.connectTimeout);
}
}
}
public FileDownloadUrlConnection(String originUrl) throws IOException {
this(originUrl, null);
}
@Override
public void addHeader(String name, String value) {
mConnection.addRequestProperty(name, value);
}
@Override
public boolean dispatchAddResumeOffset(String etag, long offset) {
return false;
}
@Override
public InputStream getInputStream() throws IOException {
return mConnection.getInputStream();
}
@Override
public Map<String, List<String>> getRequestHeaderFields() {
return mConnection.getRequestProperties();
}
@Override
public Map<String, List<String>> getResponseHeaderFields() {
return mConnection.getHeaderFields();
}
@Override
public String getResponseHeaderField(String name) {
return mConnection.getHeaderField(name);
}
@Override public boolean setRequestMethod(String method) throws ProtocolException {
if (mConnection instanceof HttpURLConnection) {
((HttpURLConnection) mConnection).setRequestMethod(method);
return true;
}
return false;
}
@Override
public void execute() throws IOException {
mConnection.connect();
}
@Override
public int getResponseCode() throws IOException {
if (mConnection instanceof HttpURLConnection) {
return ((HttpURLConnection) mConnection).getResponseCode();
}
return FileDownloadConnection.NO_RESPONSE_CODE;
}
@Override
public void ending() {
try {
mConnection.getInputStream().close();
} catch (IOException ignored) {
}
}
public static class Creator implements FileDownloadHelper.ConnectionCreator {
private final Configuration mConfiguration;
public Creator() {
this(null);
}
public Creator(Configuration configuration) {
this.mConfiguration = configuration;
}
FileDownloadConnection create(URL url) throws IOException {
return new FileDownloadUrlConnection(url, mConfiguration);
}
@Override
public FileDownloadConnection create(String originUrl) throws IOException {
return new FileDownloadUrlConnection(originUrl, mConfiguration);
}
}
/**
* The sample configuration for the {@link FileDownloadUrlConnection}
*/
public static class Configuration {
private Proxy proxy;
private Integer readTimeout;
private Integer connectTimeout;
/**
* The connection will be made through the specified proxy.
* <p>
* This {@code proxy} will be used when invoke {@link URL#openConnection(Proxy)}
*
* @param proxy the proxy will be applied to the {@link FileDownloadUrlConnection}
*/
public Configuration proxy(Proxy proxy) {
this.proxy = proxy;
return this;
}
/**
* Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies
* the timeout when reading from Input stream when a connection is established to a resource
* <p>
* If the timeout expires before there is data available for read, a
* java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an
* infinite timeout.
* <p>
* This {@code readTimeout} will be applied through
* {@link URLConnection#setReadTimeout(int)}
*
* @param readTimeout an <code>int</code> that specifies the timeout value to be used in
* milliseconds
*/
public Configuration readTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}
/**
* Sets a specified timeout value, in milliseconds, to be used when opening a communications
* link to the resource referenced by this URLConnection. If the timeout expires before the
* connection can be established, a java.net.SocketTimeoutException is raised. A timeout of
* zero is interpreted as an infinite timeout.
* <p>
* This {@code connectionTimeout} will be applied through
* {@link URLConnection#setConnectTimeout(int)}
*
* @param connectTimeout an <code>int</code> that specifies the connect timeout value in
* milliseconds
*/
public Configuration connectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}
}
}