37
37
38
38
public class Utils {
39
39
40
+ private final static int TIME_OUT = 60 * 000 ;
41
+
40
42
public static InputStream checkForUtf8BOM (InputStream inputStream ) throws IOException {
41
43
42
44
PushbackInputStream pushbackInputStream = new PushbackInputStream (new BufferedInputStream (inputStream ), 3 );
@@ -108,30 +110,32 @@ public static void copyFile(URL sourceUrl, File destinationFile, MessageConsoleS
108
110
URLConnection connection ;
109
111
while (true ) {
110
112
connection = url .openConnection ();
111
- if ("file" .equals (url .getProtocol ())) { //$NON-NLS-1$
112
- break ;
113
- }
114
113
if (connection instanceof HttpURLConnection ) {
115
- connection .setConnectTimeout (60 * 1000 );
116
- connection .setReadTimeout (60 * 1000 );
117
- int responseCode = ((HttpURLConnection ) connection ).getResponseCode ();
114
+ connection .setConnectTimeout (TIME_OUT );
115
+ connection .setReadTimeout (TIME_OUT );
116
+ HttpURLConnection httpURLConnection = (HttpURLConnection ) connection ;
117
+
118
+ int responseCode = httpURLConnection .getResponseCode ();
118
119
if (responseCode == HttpURLConnection .HTTP_OK ) {
119
120
break ;
121
+ } else if (responseCode == HttpURLConnection .HTTP_MOVED_TEMP
122
+ || responseCode == HttpURLConnection .HTTP_MOVED_PERM
123
+ || responseCode == HttpURLConnection .HTTP_SEE_OTHER ) {
124
+ String newUrl = connection .getHeaderField ("Location" );
125
+ url = new URL (newUrl );
126
+ continue ;
127
+ // System.out.println("Redirect to URL : " + newUrl);
128
+ } else if (responseCode == HttpURLConnection .HTTP_NOT_FOUND ) {
129
+ httpURLConnection .disconnect ();
130
+ throw new FileNotFoundException (
131
+ "File \" " + url + "\" not found (" + responseCode + "), pack not installed." );
120
132
} else {
121
- if (responseCode == HttpURLConnection .HTTP_MOVED_TEMP
122
- || responseCode == HttpURLConnection .HTTP_MOVED_PERM
123
- || responseCode == HttpURLConnection .HTTP_SEE_OTHER ) {
124
- String newUrl = connection .getHeaderField ("Location" );
125
- url = new URL (newUrl );
126
-
127
- // System.out.println("Redirect to URL : " + newUrl);
128
- } else if (responseCode == HttpURLConnection .HTTP_NOT_FOUND ) {
129
- throw new FileNotFoundException ("File \" " + url + "\" not found (" + responseCode + "), pack not installed." );
130
- } else {
131
- throw new IOException ("Failed to open connection, response code " + responseCode );
132
- }
133
+ httpURLConnection .disconnect ();
134
+ throw new IOException ("Failed to open connection, response code " + responseCode );
133
135
}
136
+
134
137
}
138
+ break ; // When non http protocol, for example.
135
139
}
136
140
137
141
destinationFile .getParentFile ().mkdirs ();
@@ -142,7 +146,7 @@ public static void copyFile(URL sourceUrl, File destinationFile, MessageConsoleS
142
146
// conn.getContentLength() returns -1 when the file is sent in
143
147
// chunks, so it cannot be used; instead it is computed.
144
148
int totalBytes = 0 ;
145
-
149
+
146
150
byte [] buf = new byte [1024 ];
147
151
int bytesRead ;
148
152
while ((bytesRead = input .read (buf )) > 0 ) {
0 commit comments