Skip to content

Commit a90aeb0

Browse files
committed
[#335] Getting package size fixed.
- conn.getContentLength() returns -1 when the file is sent in chunks, so it cannot be used; instead it is computed.
1 parent 8ec5bb8 commit a90aeb0

File tree

3 files changed

+45
-33
lines changed

3 files changed

+45
-33
lines changed

bundles/ilg.gnumcueclipse.packs.data/src/ilg/gnumcueclipse/packs/cmsis/PdscParserForContent.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111

1212
package ilg.gnumcueclipse.packs.cmsis;
1313

14-
import ilg.gnumcueclipse.core.Xml;
15-
import ilg.gnumcueclipse.packs.core.tree.Node;
16-
import ilg.gnumcueclipse.packs.core.tree.Property;
17-
import ilg.gnumcueclipse.packs.core.tree.Type;
18-
import ilg.gnumcueclipse.packs.data.Activator;
19-
import ilg.gnumcueclipse.packs.data.Utils;
20-
2114
import java.io.IOException;
2215
import java.net.URL;
2316
import java.util.List;
@@ -26,6 +19,13 @@
2619

2720
import com.github.zafarkhaja.semver.Version;
2821

22+
import ilg.gnumcueclipse.core.Xml;
23+
import ilg.gnumcueclipse.packs.core.tree.Node;
24+
import ilg.gnumcueclipse.packs.core.tree.Property;
25+
import ilg.gnumcueclipse.packs.core.tree.Type;
26+
import ilg.gnumcueclipse.packs.data.Activator;
27+
import ilg.gnumcueclipse.packs.data.Utils;
28+
2929
public class PdscParserForContent extends PdscParser {
3030

3131
public void parse(String pdscNname, String version, Node parent) {
@@ -131,7 +131,7 @@ public void parse(String pdscNname, String version, Node parent) {
131131
// Default as for unavailable packages
132132
String size = "0";
133133
try {
134-
int sz = Utils.getRemoteFileSize(new URL(archiveUrl));
134+
int sz = Utils.getRemoteFileSize(new URL(archiveUrl), fOut);
135135
if (sz > 0) {
136136
size = String.valueOf(sz);
137137
}

bundles/ilg.gnumcueclipse.packs.data/src/ilg/gnumcueclipse/packs/data/Utils.java

+34-22
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@
3333
import org.eclipse.ui.console.MessageConsoleStream;
3434

3535
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;
3936

4037
public class Utils {
4138

@@ -51,10 +48,11 @@ public static InputStream checkForUtf8BOM(InputStream inputStream) throws IOExce
5148
return pushbackInputStream;
5249
}
5350

54-
public static int getRemoteFileSize(URL url) throws IOException {
51+
public static int getRemoteFileSize(URL url, MessageConsoleStream out) throws IOException {
5552

5653
URLConnection connection;
5754
while (true) {
55+
out.println("Getting size of \"" + url + "\"...");
5856
connection = url.openConnection();
5957
if ("file".equals(url.getProtocol())) { //$NON-NLS-1$
6058
break;
@@ -79,6 +77,20 @@ public static int getRemoteFileSize(URL url) throws IOException {
7977
}
8078

8179
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+
}
8294

8395
if (connection instanceof HttpURLConnection) {
8496
((HttpURLConnection) connection).disconnect();
@@ -172,40 +184,40 @@ public static void copyFile(URL sourceUrl, File destinationFile, MessageConsoleS
172184
}
173185
}
174186

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-
193187
destinationFile.getParentFile().mkdirs();
194188

195189
InputStream input = connection.getInputStream();
196190
OutputStream output = new FileOutputStream(destinationFile);
197191

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+
198196
byte[] buf = new byte[1024];
199197
int bytesRead;
200198
while ((bytesRead = input.read(buf)) > 0) {
201199
output.write(buf, 0, bytesRead);
202200
if (monitor != null) {
203201
monitor.worked(bytesRead);
204202
}
203+
totalBytes += bytesRead;
205204
}
206205
output.close();
207206
input.close();
208207

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+
209221
if (connection instanceof HttpURLConnection) {
210222
((HttpURLConnection) connection).disconnect();
211223
// System.out.println("Disconnect "+connection);

bundles/ilg.gnumcueclipse.packs/src/ilg/gnumcueclipse/packs/jobs/InstallJob.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected IStatus run(IProgressMonitor monitor) {
114114

115115
int workUnits = 0;
116116
for (int i = 0; i < packsToInstall.size(); ++i) {
117-
workUnits += computeWorkUnits(packsToInstall.get(i));
117+
workUnits += computeWorkUnits(packsToInstall.get(i), fOut);
118118
}
119119

120120
workUnits++;
@@ -200,7 +200,7 @@ protected IStatus run(IProgressMonitor monitor) {
200200
return status;
201201
}
202202

203-
private int computeWorkUnits(Node versionNode) {
203+
private int computeWorkUnits(Node versionNode, MessageConsoleStream out) {
204204

205205
int workUnits = 0;
206206

@@ -215,7 +215,7 @@ private int computeWorkUnits(Node versionNode) {
215215
String pdscUrl = packNode.getProperty(Property.ARCHIVE_URL);
216216
if (pdscUrl.length() > 0) {
217217
try {
218-
int sz = Utils.getRemoteFileSize(new URL(pdscUrl));
218+
int sz = Utils.getRemoteFileSize(new URL(pdscUrl), out);
219219
if (sz > 0) {
220220
workUnits += sz;
221221
}

0 commit comments

Comments
 (0)