Make JarURLConnection return entry's length from getContentLengthLong()

Closes gh-9484
This commit is contained in:
Andy Wilkinson
2017-06-12 13:46:59 +01:00
parent 436eb17610
commit 2d3bcae4e1
2 changed files with 24 additions and 1 deletions

View File

@@ -187,6 +187,15 @@ final class JarURLConnection extends java.net.JarURLConnection {
@Override
public int getContentLength() {
long longContentLength = getContentLengthLong();
if (longContentLength > Integer.MAX_VALUE) {
return -1;
}
return (int) longContentLength;
}
@Override
public long getContentLengthLong() {
if (this.jarFile == null) {
return -1;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -136,6 +136,20 @@ public class JarURLConnectionTests {
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
}
@Test
public void getContentLengthReturnsLengthOfUnderlyingEntry() throws Exception {
URL url = new URL(new URL("jar", null, -1,
"file:" + getAbsolutePath() + "!/nested.jar!/", new Handler()), "/3.dat");
assertThat(url.openConnection().getContentLength()).isEqualTo(1);
}
@Test
public void getContentLengthLongReturnsLengthOfUnderlyingEntry() throws Exception {
URL url = new URL(new URL("jar", null, -1,
"file:" + getAbsolutePath() + "!/nested.jar!/", new Handler()), "/3.dat");
assertThat(url.openConnection().getContentLengthLong()).isEqualTo(1);
}
private String getAbsolutePath() {
return this.rootJarFile.getAbsolutePath().replace('\\', '/');
}