01: /*
02: * @(#)URLHelper.java 1.2 04/12/06
03: *
04: * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.io;
10:
11: import pnuts.lang.Context;
12: import pnuts.io.CharacterEncoding;
13: import java.net.*;
14: import java.io.*;
15:
16: public class URLHelper {
17:
18: public static Reader getReader(URL url, Context context)
19: throws IOException {
20: URLConnection con = url.openConnection();
21: String contentType = con.getContentType();
22: String encoding = null;
23: if (contentType != null) {
24: int semi = contentType.indexOf(';');
25: if (semi >= 0) {
26: int loc = contentType.indexOf("charset=", semi);
27: if (loc >= 0) {
28: encoding = contentType.substring(loc + 8).replace(
29: '"', ' ').trim();
30: }
31: }
32: }
33: return CharacterEncoding.getReader(con.getInputStream(),
34: encoding, context);
35: }
36: }
|