Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 20298 invoked from network); 31 Aug 2007 16:02:13 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 31 Aug 2007 16:02:13 -0000 Received: (qmail 89091 invoked by uid 500); 31 Aug 2007 16:02:08 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 89073 invoked by uid 500); 31 Aug 2007 16:02:08 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 89064 invoked by uid 99); 31 Aug 2007 16:02:08 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 31 Aug 2007 09:02:08 -0700 X-ASF-Spam-Status: No, hits=-100.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 31 Aug 2007 16:03:04 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 2B2921A983A; Fri, 31 Aug 2007 09:01:39 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r571503 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/net/URLConnection.java test/api/common/tests/api/java/net/URLConnectionTest.java Date: Fri, 31 Aug 2007 16:01:38 -0000 To: commits@harmony.apache.org From: tellison@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20070831160139.2B2921A983A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tellison Date: Fri Aug 31 09:01:38 2007 New Revision: 571503 URL: http://svn.apache.org/viewvc?rev=571503&view=rev Log: Enhancements to do a better guess at the content type of a stream. Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/net/URLConnectionTest.java Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java?rev=571503&r1=571502&r2=571503&view=diff ============================================================================== --- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java (original) +++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java Fri Aug 31 09:01:38 2007 @@ -722,26 +722,83 @@ * @throws IOException * If an IO error occurs */ + @SuppressWarnings("nls") public static String guessContentTypeFromStream(InputStream is) throws IOException { + if (!is.markSupported()) { return null; } - is.mark(4); - char[] chars = new char[4]; - for (int i = 0; i < chars.length; i++) { - chars[i] = (char) is.read(); - } + // Look ahead up to 64 bytes for the longest encoded header + is.mark(64); + byte[] bytes = new byte[64]; + int length = is.read(bytes); is.reset(); - if ((chars[0] == 'P') && (chars[1] == 'K')) { - return "application/zip"; //$NON-NLS-1$ + + // Check for Unicode BOM encoding indicators + String encoding = "ASCII"; + int start = 0; + if (length > 1) { + if ((bytes[0] == (byte) 0xFF) && (bytes[1] == (byte) 0xFE)) { + encoding = "UTF-16LE"; + start = 2; + length -= length & 1; + } + if ((bytes[0] == (byte) 0xFE) && (bytes[1] == (byte) 0xFF)) { + encoding = "UTF-16BE"; + start = 2; + length -= length & 1; + } + if (length > 2) { + if ((bytes[0] == (byte) 0xEF) && (bytes[1] == (byte) 0xBB) + && (bytes[2] == (byte) 0xBF)) { + encoding = "UTF-8"; + start = 3; + } + if (length > 3) { + if ((bytes[0] == (byte) 0x00) && (bytes[1] == (byte) 0x00) + && (bytes[2] == (byte) 0xFE) + && (bytes[3] == (byte) 0xFF)) { + encoding = "UTF-32BE"; + start = 4; + length -= length & 3; + } + if ((bytes[0] == (byte) 0xFF) && (bytes[1] == (byte) 0xFE) + && (bytes[2] == (byte) 0x00) + && (bytes[3] == (byte) 0x00)) { + encoding = "UTF-32LE"; + start = 4; + length -= length & 3; + } + } + } + } + + String header = new String(bytes, start, length - start, encoding); + + // Check binary types + if (header.startsWith("PK")) { + return "application/zip"; + } + if (header.startsWith("GI")) { + return "image/gif"; } - if ((chars[0] == 'G') && (chars[1] == 'I')) { - return "image/gif"; //$NON-NLS-1$ + + // Check text types + String textHeader = header.trim().toUpperCase(); + if (textHeader.startsWith("", + "", + "