summaryrefslogtreecommitdiffstats
path: root/luni
diff options
context:
space:
mode:
authorNeil Fuller <nfuller@google.com>2014-02-14 17:24:32 +0000
committerNeil Fuller <nfuller@google.com>2014-02-19 18:25:53 +0000
commit2587ef91ba693d73e97704e8163c050b286e7330 (patch)
treea89282a25ccd09ac8d7ad1e7c8770b993fe4a9f4 /luni
parent5a95445260e22dcca730b582c881cc67b8de640e (diff)
downloadlibcore-2587ef91ba693d73e97704e8163c050b286e7330.zip
libcore-2587ef91ba693d73e97704e8163c050b286e7330.tar.gz
libcore-2587ef91ba693d73e97704e8163c050b286e7330.tar.bz2
Addition of 1.7 methods to URLConnection.
URLConnection.getHeaderLong() and URLConnection.getContentLengthLong(). These methods are required by okhttp. Changed JarURLConnectionImpl.getContentLength() to return -1 when the size of an entry cannot be represented as an int. Previously it would have returned a corrupted, possibly negative, value due to a cast. Changed FileURLConnection.getContentLength() to return -1 when the size of the file cannot be represented as an int. Previously it would have returned a corrupted, possibly negative, value due to a cast. Change-Id: Ib43e68a2536c2602b4c7ee0cda68fa1f90045f57
Diffstat (limited to 'luni')
-rw-r--r--luni/src/main/java/java/net/URLConnection.java39
-rw-r--r--luni/src/main/java/libcore/net/url/FileURLConnection.java22
-rw-r--r--luni/src/main/java/libcore/net/url/JarURLConnectionImpl.java27
-rw-r--r--luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java74
4 files changed, 136 insertions, 26 deletions
diff --git a/luni/src/main/java/java/net/URLConnection.java b/luni/src/main/java/java/net/URLConnection.java
index 74c15ce..cc7de90 100644
--- a/luni/src/main/java/java/net/URLConnection.java
+++ b/luni/src/main/java/java/net/URLConnection.java
@@ -308,15 +308,25 @@ public abstract class URLConnection {
/**
* Returns the content length in bytes specified by the response header field
- * {@code content-length} or {@code -1} if this field is not set.
- *
- * @return the value of the response header field {@code content-length}.
+ * {@code content-length} or {@code -1} if this field is not set or cannot be represented as an
+ * {@code int}.
*/
public int getContentLength() {
return getHeaderFieldInt("Content-Length", -1);
}
/**
+ * Returns the content length in bytes specified by the response header field
+ * {@code content-length} or {@code -1} if this field is not set.
+ *
+ * @since 1.7
+ * @hide Until ready for a public API change
+ */
+ public long getContentLengthLong() {
+ return getHeaderFieldLong("Content-Length", -1);
+ }
+
+ /**
* Returns the MIME-type of the content specified by the response header field
* {@code content-type} or {@code null} if type is unknown.
*
@@ -531,7 +541,7 @@ public abstract class URLConnection {
/**
* Returns the specified header value as a number. Returns the {@code
* defaultValue} if no such header field could be found or the value could
- * not be parsed as an {@code Integer}.
+ * not be parsed as an {@code int}.
*
* @param field
* the header field name whose value is needed.
@@ -548,6 +558,27 @@ public abstract class URLConnection {
}
/**
+ * Returns the specified header value as a number. Returns the {@code
+ * defaultValue} if no such header field could be found or the value could
+ * not be parsed as a {@code long}.
+ *
+ * @param field
+ * the header field name whose value is needed.
+ * @param defaultValue
+ * the default value if no field has been found.
+ * @return the value of the specified header field as a number.
+ * @since 1.7
+ * @hide Until ready for a public API change
+ */
+ public long getHeaderFieldLong(String field, long defaultValue) {
+ try {
+ return Long.parseLong(getHeaderField(field));
+ } catch (NumberFormatException e) {
+ return defaultValue;
+ }
+ }
+
+ /**
* Returns the name of the header field at the given position {@code posn} or
* {@code null} if there are fewer than {@code posn} fields. The base
* implementation of this method returns always {@code null}.
diff --git a/luni/src/main/java/libcore/net/url/FileURLConnection.java b/luni/src/main/java/libcore/net/url/FileURLConnection.java
index b4654cd..f8d7926 100644
--- a/luni/src/main/java/libcore/net/url/FileURLConnection.java
+++ b/luni/src/main/java/libcore/net/url/FileURLConnection.java
@@ -42,7 +42,7 @@ public class FileURLConnection extends URLConnection {
private InputStream is;
- private int length = -1;
+ private long length = -1;
private boolean isDir;
@@ -80,21 +80,31 @@ public class FileURLConnection extends URLConnection {
// use -1 for the contentLength
} else {
is = new BufferedInputStream(new FileInputStream(f));
- long lengthAsLong = f.length();
- length = lengthAsLong <= Integer.MAX_VALUE ? (int) lengthAsLong : Integer.MAX_VALUE;
+ length = f.length();
}
connected = true;
}
/**
+ * Returns the length of the file in bytes, or {@code -1} if the length cannot be
+ * represented as an {@code int}. See {@link #getContentLengthLong()} for a method that can
+ * handle larger files.
+ */
+ @Override
+ public int getContentLength() {
+ long length = getContentLengthLong();
+ return length <= Integer.MAX_VALUE ? (int) length : -1;
+ }
+
+ /**
* Returns the length of the file in bytes.
*
* @return the length of the file
- *
- * @see #getContentType()
+ * @since 1.7
+ * @hide Until ready for a public API change
*/
@Override
- public int getContentLength() {
+ public long getContentLengthLong() {
try {
if (!connected) {
connect();
diff --git a/luni/src/main/java/libcore/net/url/JarURLConnectionImpl.java b/luni/src/main/java/libcore/net/url/JarURLConnectionImpl.java
index 762f0e2..e00bcab 100644
--- a/luni/src/main/java/libcore/net/url/JarURLConnectionImpl.java
+++ b/luni/src/main/java/libcore/net/url/JarURLConnectionImpl.java
@@ -258,21 +258,32 @@ public class JarURLConnectionImpl extends JarURLConnection {
}
/**
- * Returns the content length of the resource. Test cases reveal that if the
- * URL is referring to a Jar file, this method answers a content-length
- * returned by URLConnection. For jar entry it should return it's size.
- * Otherwise, it will return -1.
- *
- * @return the content length
+ * Returns the content length of the resource. Test cases reveal that if the URL is referring to
+ * a Jar file, this method answers a content-length returned by URLConnection. For a jar entry
+ * it returns the entry's size if it can be represented as an {@code int}. Otherwise, it will
+ * return -1.
*/
@Override
public int getContentLength() {
+ long length = getContentLengthLong();
+ return length > Integer.MAX_VALUE ? -1 : (int) length;
+ }
+
+ /**
+ * Returns the content length of the resource. Test cases reveal that if the URL is referring to
+ * a Jar file, this method answers a content-length returned by URLConnection. For a jar entry
+ * it should return the entry's size. Otherwise, it will return -1.
+ *
+ * @hide Until ready for a public API change
+ */
+ @Override
+ public long getContentLengthLong() {
try {
connect();
if (jarEntry == null) {
- return jarFileURLConnection.getContentLength();
+ return jarFileURLConnection.getContentLengthLong();
}
- return (int) getJarEntry().getSize();
+ return getJarEntry().getSize();
} catch (IOException e) {
// Ignored
}
diff --git a/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java b/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
index d1674f3..e2a3a47 100644
--- a/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
+++ b/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
@@ -209,10 +209,14 @@ public class URLConnectionTest extends TestCase {
URL url2;
+ URL url3;
+
URLConnection uc;
URLConnection uc2;
+ URLConnection uc3;
+
Support_TestWebServer server;
@Override
@@ -225,6 +229,8 @@ public class URLConnectionTest extends TestCase {
uc = url.openConnection();
url2 = new URL("http://localhost:" + port + "/test2");
uc2 = url2.openConnection();
+ url3 = new URL("http://localhost:" + port + "/test3");
+ uc3 = url3.openConnection();
fileURL = createTempHelloWorldFile();
fileURLCon = fileURL.openConnection();
@@ -239,6 +245,7 @@ public class URLConnectionTest extends TestCase {
server.close();
((HttpURLConnection) uc).disconnect();
((HttpURLConnection) uc2).disconnect();
+ ((HttpURLConnection) uc3).disconnect();
}
/**
@@ -472,8 +479,22 @@ public class URLConnectionTest extends TestCase {
assertEquals(Support_TestWebData.test1.length, uc.getContentLength());
assertEquals(Support_TestWebData.test2.length, uc2.getContentLength());
- assertNotNull(jarURLCon.getContentLength());
- assertNotNull(gifURLCon.getContentLength());
+ assertTrue(jarURLCon.getContentLength() > 0);
+ assertTrue(gifURLCon.getContentLength() > 0);
+
+ fileURLCon.getInputStream().close();
+ }
+
+ /**
+ * {@link java.net.URLConnection#getContentLengthLong()}
+ */
+ public void test_getContentLengthLong() throws Exception {
+ assertEquals(testString.getBytes().length, fileURLCon.getContentLengthLong());
+ assertEquals(Support_TestWebData.test1.length, uc.getContentLengthLong());
+ assertEquals(Support_TestWebData.test2.length, uc2.getContentLengthLong());
+
+ assertTrue(jarURLCon.getContentLength() > 0);
+ assertTrue(gifURLCon.getContentLength() > 0);
fileURLCon.getInputStream().close();
}
@@ -719,17 +740,17 @@ public class URLConnectionTest extends TestCase {
}
/**
- * @throws IOException
* {@link java.net.URLConnection#getHeaderFieldInt(String, int)}
*/
public void test_getHeaderFieldInt() throws IOException, ParseException {
- Support_TestWebData params = Support_TestWebData.testParams[1];
+ // Test getHeaderFieldInt() can read an int value.
+ Support_TestWebData params1 = Support_TestWebData.testParams[1];
+ int hf = uc2.getHeaderFieldInt("Content-Length", Integer.MIN_VALUE);
+ assertEquals(params1.testLength, hf);
- int hf = 0;
+ // The remaining fields should be invalid or missing. Confirm the default is returned.
hf = uc2.getHeaderFieldInt("Content-Encoding", Integer.MIN_VALUE);
assertEquals(Integer.MIN_VALUE, hf);
- hf = uc2.getHeaderFieldInt("Content-Length", Integer.MIN_VALUE);
- assertEquals(params.testLength, hf);
hf = uc2.getHeaderFieldInt("Content-Type", Integer.MIN_VALUE);
assertEquals(Integer.MIN_VALUE, hf);
hf = uc2.getHeaderFieldInt("Date", Integer.MIN_VALUE);
@@ -745,6 +766,43 @@ public class URLConnectionTest extends TestCase {
hf = uc2.getHeaderFieldInt("DoesNotExist", Integer.MIN_VALUE);
assertEquals(Integer.MIN_VALUE, hf);
+ // Test getHeaderFieldInt() for a value outside of the range of int.
+ Support_TestWebData params2 = Support_TestWebData.testParams[2];
+ hf = uc3.getHeaderFieldInt("Content-Length", Integer.MIN_VALUE);
+ assertEquals(Integer.MIN_VALUE, hf);
+ }
+
+ /**
+ * {@link java.net.URLConnection#getHeaderFieldLong(String, long)}
+ */
+ public void test_getHeaderFieldLong() throws IOException, ParseException {
+ // Test getHeaderFieldLong() can read an int value.
+ Support_TestWebData params0 = Support_TestWebData.testParams[0];
+ long hf = uc.getHeaderFieldLong("Content-Length", Long.MIN_VALUE);
+ assertEquals(params0.testLength, hf);
+
+ // Test getHeaderFieldLong() for a value outside of the range of int.
+ Support_TestWebData params2 = Support_TestWebData.testParams[2];
+ hf = uc3.getHeaderFieldLong("Content-Length", Long.MIN_VALUE);
+ assertEquals(params2.testLength, hf);
+
+ // The remaining fields should be invalid or missing. Confirm the default is returned.
+ hf = uc3.getHeaderFieldLong("Content-Encoding", Long.MIN_VALUE);
+ assertEquals(Long.MIN_VALUE, hf);
+ hf = uc3.getHeaderFieldInt("Content-Type", Integer.MIN_VALUE);
+ assertEquals(Integer.MIN_VALUE, hf);
+ hf = uc3.getHeaderFieldInt("Date", Integer.MIN_VALUE);
+ assertEquals(Integer.MIN_VALUE, hf);
+ hf = uc3.getHeaderFieldInt("Expires", Integer.MIN_VALUE);
+ assertEquals(Integer.MIN_VALUE, hf);
+ hf = uc3.getHeaderFieldInt("SERVER", Integer.MIN_VALUE);
+ assertEquals(Integer.MIN_VALUE, hf);
+ hf = uc3.getHeaderFieldInt("Last-Modified", Integer.MIN_VALUE);
+ assertEquals(Integer.MIN_VALUE, hf);
+ hf = uc3.getHeaderFieldInt("accept-ranges", Integer.MIN_VALUE);
+ assertEquals(Integer.MIN_VALUE, hf);
+ hf = uc3.getHeaderFieldInt("DoesNotExist", Integer.MIN_VALUE);
+ assertEquals(Integer.MIN_VALUE, hf);
}
/**
@@ -1253,7 +1311,7 @@ public class URLConnectionTest extends TestCase {
String cts = System.getProperty("java.io.tmpdir");
File tmpDir = new File(cts);
Support_Resources.copyFile(tmpDir, null, "Harmony.GIF");
- URL fUrl1 = new URL("file:/" + tmpDir.getPath()
+ URL fUrl1 = new URL("file://" + tmpDir.getPath()
+ "/Harmony.GIF");
URLConnection con1 = fUrl1.openConnection();
return con1;