Petter Reinholdtsen wrote:
Here is a patch to implement some magic numbers in java.net.URLConnection.guessContentTypeFromStream(). I picked a few mime types which seemed relevant from /etc/mime-magic and implemented tests for them. Is this approach good enough?Index: java/net/URLConnection.java =================================================================== RCS file: /sources/classpath/classpath/java/net/URLConnection.java,v retrieving revision 1.42 diff -u -3 -p -u -r1.42 URLConnection.java --- java/net/URLConnection.java 12 May 2006 20:59:30 -0000 1.42 +++ java/net/URLConnection.java 16 May 2006 22:08:55 -0000 @@ -971,10 +971,30 @@ public abstract class URLConnection * @exception IOException If an error occurs */ public static String guessContentTypeFromStream(InputStream is) - throws IOException, NotImplementedException + throws IOException {// See /etc/gnome-vfs-mime-magic or /etc/mime-magic for a reasonable// idea of how to handle this. + + is.mark(5); + int c0 = is.read(); + int c1 = is.read(); + int c2 = is.read(); + int c3 = is.read(); + is.reset(); + + if (c0 == 0xFF && c1 == 0xD8) + return "image/jpeg"; + + if (c0 == 'G' && c1 == 'I' && c2 == 'F' && c3 == '8') + return "image/gif"; + + if (c1 == 'P' && c2 == 'N' && c3 == 'G') + return "image/png"; + + if (c0 == 'P' && c1 == 'K' && c2 == 3 && c3 == 4) + return "application/zip"; + return "application/octet-stream"; }
Not if is.isMarkSupported() returns false... -- 犬 Chris Burdess "They that can give up essential liberty to obtain a little safety deserve neither liberty nor safety." - Benjamin Franklin
PGP.sig
Description: This is a digitally signed message part