33
33
import org .eclipse .ui .console .MessageConsoleStream ;
34
34
35
35
import ilg .gnumcueclipse .core .StringUtils ;
36
- import ilg .gnumcueclipse .packs .core .data .PacksStorage ;
37
- import ilg .gnumcueclipse .packs .core .tree .Node ;
38
- import ilg .gnumcueclipse .packs .xcdl .ContentSerialiser ;
39
36
40
37
public class Utils {
41
38
@@ -51,10 +48,11 @@ public static InputStream checkForUtf8BOM(InputStream inputStream) throws IOExce
51
48
return pushbackInputStream ;
52
49
}
53
50
54
- public static int getRemoteFileSize (URL url ) throws IOException {
51
+ public static int getRemoteFileSize (URL url , MessageConsoleStream out ) throws IOException {
55
52
56
53
URLConnection connection ;
57
54
while (true ) {
55
+ out .println ("Getting size of \" " + url + "\" ..." );
58
56
connection = url .openConnection ();
59
57
if ("file" .equals (url .getProtocol ())) { //$NON-NLS-1$
60
58
break ;
@@ -79,6 +77,20 @@ public static int getRemoteFileSize(URL url) throws IOException {
79
77
}
80
78
81
79
int length = connection .getContentLength ();
80
+ if (length < 0 ) {
81
+ // conn.getContentLength() returns -1 when the file is sent in
82
+ // chunks, so it cannot be used; instead it is computed.
83
+ InputStream input = connection .getInputStream ();
84
+ int totalBytes = 0 ;
85
+ byte [] buf = new byte [1024 ];
86
+ int bytesRead ;
87
+ while ((bytesRead = input .read (buf )) > 0 ) {
88
+ totalBytes += bytesRead ;
89
+ }
90
+ input .close ();
91
+
92
+ length = totalBytes ;
93
+ }
82
94
83
95
if (connection instanceof HttpURLConnection ) {
84
96
((HttpURLConnection ) connection ).disconnect ();
@@ -172,40 +184,40 @@ public static void copyFile(URL sourceUrl, File destinationFile, MessageConsoleS
172
184
}
173
185
}
174
186
175
- int size = connection .getContentLength ();
176
- if (size <= 0 ) {
177
- throw new IOException ("Illegal PDSC file size " + size );
178
- }
179
- String sizeString = StringUtils .convertSizeToString (size );
180
- if (out != null ) {
181
- String s = destinationFile .getPath ();
182
- if (s .endsWith (".download" )) {
183
- s = s .substring (0 , s .length () - ".download" .length ());
184
- }
185
- out .println ("Copy " + sizeString );
186
- out .println (" from \" " + url + "\" " );
187
- if (!url .equals (sourceUrl )) {
188
- out .println (" redirected from \" " + sourceUrl + "\" " );
189
- }
190
- out .println (" to \" " + s + "\" " );
191
- }
192
-
193
187
destinationFile .getParentFile ().mkdirs ();
194
188
195
189
InputStream input = connection .getInputStream ();
196
190
OutputStream output = new FileOutputStream (destinationFile );
197
191
192
+ // conn.getContentLength() returns -1 when the file is sent in
193
+ // chunks, so it cannot be used; instead it is computed.
194
+ int totalBytes = 0 ;
195
+
198
196
byte [] buf = new byte [1024 ];
199
197
int bytesRead ;
200
198
while ((bytesRead = input .read (buf )) > 0 ) {
201
199
output .write (buf , 0 , bytesRead );
202
200
if (monitor != null ) {
203
201
monitor .worked (bytesRead );
204
202
}
203
+ totalBytes += bytesRead ;
205
204
}
206
205
output .close ();
207
206
input .close ();
208
207
208
+ if (out != null ) {
209
+ String s = destinationFile .getPath ();
210
+ if (s .endsWith (".download" )) {
211
+ s = s .substring (0 , s .length () - ".download" .length ());
212
+ }
213
+ out .println ("Copied " + totalBytes + " bytes" );
214
+ out .println (" from \" " + url + "\" " );
215
+ if (!url .equals (sourceUrl )) {
216
+ out .println (" redirected from \" " + sourceUrl + "\" " );
217
+ }
218
+ out .println (" to \" " + s + "\" " );
219
+ }
220
+
209
221
if (connection instanceof HttpURLConnection ) {
210
222
((HttpURLConnection ) connection ).disconnect ();
211
223
// System.out.println("Disconnect "+connection);
0 commit comments