From 13a535d1c6f973e60d305ae58ecfac862e47bdae Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Tue, 15 Jul 2008 09:36:52 -0600 Subject: [PATCH] Added a getContentLength() method to URLConnection This is particularly important if you want to get the number of bytes of a resource loaded by the class loader: getClass().getResource("myFile").openConnection().getContentLength() --- classpath/java/net/URL.java | 6 ++++++ classpath/java/net/URLConnection.java | 4 ++++ src/builtin.cpp | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/classpath/java/net/URL.java b/classpath/java/net/URL.java index 4a1f577873..bf2174c447 100644 --- a/classpath/java/net/URL.java +++ b/classpath/java/net/URL.java @@ -100,6 +100,10 @@ public final class URL { super(url); } + public int getContentLength() { + return ResourceInputStream.getContentLength(url.getFile()); + } + public InputStream getInputStream() throws IOException { return new ResourceInputStream(url.getFile()); } @@ -116,6 +120,8 @@ public final class URL { } } + private static native int getContentLength(String path); + private static native long open(String path) throws IOException; private static native int read(long peer, int position) throws IOException; diff --git a/classpath/java/net/URLConnection.java b/classpath/java/net/URLConnection.java index b0c6e8a235..ab8f5619e4 100644 --- a/classpath/java/net/URLConnection.java +++ b/classpath/java/net/URLConnection.java @@ -25,6 +25,10 @@ public abstract class URLConnection { return getInputStream(); } + public int getContentLength() { + return -1; + } + public InputStream getInputStream() throws IOException { throw new UnknownServiceException(); } diff --git a/src/builtin.cpp b/src/builtin.cpp index df32700e42..396088dc56 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -741,6 +741,26 @@ Java_java_lang_Thread_enumerate(Thread* t, jclass, jobjectArray array) return count; } +extern "C" JNIEXPORT jint JNICALL +Java_java_net_URL_00024ResourceInputStream_getContentLength +(Thread* t, jclass, jstring path) +{ + ENTER(t, Thread::ActiveState); + + if (LIKELY(path)) { + char p[stringLength(t, *path) + 1]; + stringChars(t, *path, p); + + System::Region* r = t->m->finder->find(p); + if (r) { + jint rSize = r->length(); + r->dispose(); + return rSize; + } + } + return -1; +} + extern "C" JNIEXPORT jlong JNICALL Java_java_net_URL_00024ResourceInputStream_open (Thread* t, jclass, jstring path)