Index: src/main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java =================================================================== --- src/main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java (revision 531380) +++ src/main/java/org/apache/harmony/luni/internal/net/www/protocol/jar/JarURLConnection.java (working copy) @@ -354,40 +354,60 @@ } /** - * Answers the content type of the resource. Test cases reveal that only if - * the URL is refering to a Jar file, that this method answers a non-null - * value - x-java/jar. + * Answers the content type of the resource. + * For jar file itself "x-java/jar" should be returned, + * for jar entries the content type of the entry should be returned. + * Returns non-null results ("content/unknown" for unknown types). * * @return the content type */ @Override public String getContentType() { - // it could also return "x-java/jar" which jdk returns but here, we get - // it from the URLConnection - try { - if (url.getFile().endsWith("!/")) { //$NON-NLS-1$ - return getJarFileURL().openConnection().getContentType(); + if (url.getFile().endsWith("!/")) { //$NON-NLS-1$ + // the type for jar file itself is always "x-java/jar" + return "x-java/jar"; //$NON-NLS-1$ + } else { + String cType = null; + String entryName = getEntryName(); + + if (entryName != null) { + // if there is an Jar Entry, get the content type from the name + cType = guessContentTypeFromName(entryName); + } else { + try { + cType = getJarFileURL().openConnection().getContentType(); + } catch (IOException ioe) { + // Ignore + } } - } catch (IOException ioe) { - // Ignore + + if (cType == null) { + cType = "content/unknown"; //$NON-NLS-1$ + } + return cType; } - // if there is an Jar Entry, get the content type from the name - return guessContentTypeFromName(url.getFile()); } /** * Answers the content length of the resource. Test cases reveal that if the * URL is refering to a Jar file, this method answers a content-length - * returned by URLConnection. If not, it will return -1. + * returned by URLConnection. For jar entry it should return it's size. + * Otherwise, it will return -1. * * @return the content length */ @Override public int getContentLength() { try { - if (url.getFile().endsWith("!/")) { //$NON-NLS-1$ - return getJarFileURL().openConnection().getContentLength(); + if (!connected) { + connect(); } + + if (jarEntry == null) { + return jarFileURLConnection.getContentLength(); + } else { + return (int) getJarEntry().getSize(); + } } catch (IOException e) { //Ignored } Index: src/test/java/tests/api/java/net/JarURLConnectionTest.java =================================================================== --- src/test/java/tests/api/java/net/JarURLConnectionTest.java (revision 531380) +++ src/test/java/tests/api/java/net/JarURLConnectionTest.java (working copy) @@ -272,6 +272,48 @@ assertNull(juc.getCertificates()); } + /** + * @tests java.net.JarURLConnection#getContentLength() + * Regression test for HARMONY-3665 + */ + public void test_getContentLength() throws Exception { + // check length for jar file itself + URL u = new URL("jar:" + + BASE.toString()+"/lf.jar!/"); + assertEquals("Returned incorrect size for jar file", 33095, + u.openConnection().getContentLength()); + + // check length for jar entry + u = new URL("jar:" + + BASE.toString()+"/lf.jar!/plus.bmp"); + assertEquals("Returned incorrect size for the entry", 190, + u.openConnection().getContentLength()); + } + + /** + * @tests java.net.JarURLConnection#getContentType() + * Regression test for HARMONY-3665 + */ + public void test_getContentType() throws Exception { + // check type for jar file itself + URL u = new URL("jar:" + + BASE.toString()+"/lf.jar!/"); + assertEquals("Returned incorrect type for jar file", "x-java/jar", + u.openConnection().getContentType()); + + // check type for jar entry with known type + u = new URL("jar:" + + BASE.toString()+"/lf.jar!/plus.bmp"); + assertEquals("Returned incorrect type for the entry with known type", + "image/bmp", u.openConnection().getContentType()); + + // check type for jar entry with unknown type + u = new URL("jar:" + + BASE.toString()+"/lf.jar!/Manifest.mf"); + assertEquals("Returned incorrect type for the entry with known type", + "content/unknown", u.openConnection().getContentType()); + } + protected void setUp() { }