📄 urlconnection.java
字号:
// URLConnection.java - Superclass of all communications links between// an application and a URL./* Copyright (C) 1999, 2000 Free Software Foundation This file is part of libgcj.This software is copyrighted work licensed under the terms of theLibgcj License. Please consult the file "LIBGCJ_LICENSE" fordetails. */package java.net;import java.io.*;import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Locale;import java.util.Hashtable;import java.util.Map;import java.util.StringTokenizer;import java.security.Permission;import java.security.AllPermission;import gnu.gcj.io.MimeTypes;/** * @author Warren Levy <warrenl@cygnus.com> * @date March 5, 1999. *//** * Written using on-line Java Platform 1.2 API Specification, as well * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). * Status: One guessContentTypeFrom... methods not implemented. * getContent method assumes content type from response; see comment there. */public abstract class URLConnection{ protected URL url; protected boolean doInput = true; protected boolean doOutput = false; protected boolean allowUserInteraction; protected boolean useCaches; protected long ifModifiedSince = 0L; protected boolean connected = false; private static boolean defaultAllowUserInteraction = false; private static boolean defaultUseCaches = true; private static FileNameMap fileNameMap; // Set by the URLConnection subclass. private static ContentHandlerFactory factory; private static ContentHandler contentHandler; private static Hashtable handlers = new Hashtable(); private static Locale locale; private static SimpleDateFormat dateFormat1, dateFormat2, dateFormat3; private static boolean dateformats_initialized = false; /** * Creates a URL connection to a given URL. A real connection is not made. * Use #connect to do this. * * @param url The Object to create the URL connection to * * @see URLConnection:connect */ protected URLConnection(URL url) { this.url = url; allowUserInteraction = defaultAllowUserInteraction; useCaches = defaultUseCaches; } /** * Creates a real connection to the object references by the URL given * to the constructor * * @exception IOException If an error occurs */ public abstract void connect() throws IOException; /** * Returns ths URL to the object. */ public URL getURL() { return url; } /** * Returns the value of the content-length header field */ public int getContentLength() { return getHeaderFieldInt("content-length", -1); } /** * Returns the value of the content-type header field */ public String getContentType() { return getHeaderField("content-type"); } /** * Returns the value of the content-encoding header field */ public String getContentEncoding() { return getHeaderField("content-encoding"); } /** * Returns the value of the expires header field */ public long getExpiration() { return getHeaderFieldDate("expiration", 0L); } /** * Returns the value of the date header field */ public long getDate() { return getHeaderFieldDate("date", 0L); } /** * Returns the value of the last-modified header field */ public long getLastModified() { return getHeaderFieldDate("last-modified", 0L); } /** * Returns the value of the n-th header field * * @param num The number of the header field */ public String getHeaderField(int num) { // Subclasses for specific protocols override this. return null; } /** * Returns the value of the header filed specified by name * * @param name The name of the header field */ public String getHeaderField(String name) { // Subclasses for specific protocols override this. return null; } /** * Returns a map of all sent header fields * * @since 1.4 */ public Map getHeaderFields() { // Subclasses for specific protocols override this. return null; } /** * Returns the value of the header filed name as int. * * @param name The name of the header field * @param val The default value * * @return Returns the value of the header filed or the default value * if the field is missing or malformed */ public int getHeaderFieldInt(String name, int val) { String str = getHeaderField(name); try { if (str != null) val = Integer.parseInt(str); } catch (NumberFormatException e) { ; // Do nothing; val is the default. } return val; } /** * Returns the value of a header field parsed as date. The result is then * number of milliseconds since January 1st, 1970 GMT. * * @param name The name of the header field * @param val The dafault date * * @return Returns the date value of the header filed or the default value * if the field is missing or malformed */ public long getHeaderFieldDate(String name, long val) { if (! dateformats_initialized) initializeDateFormats(); String str = getHeaderField(name); if (str != null) { Date date; if ((date = dateFormat1.parse(str, new ParsePosition(0))) != null) val = date.getTime(); else if ((date = dateFormat2.parse(str, new ParsePosition(0))) != null) val = date.getTime(); else if ((date = dateFormat3.parse(str, new ParsePosition(0))) != null) val = date.getTime(); } return val; } /** * Returns the key of the n-th header field * * @param num The number of the header field */ public String getHeaderFieldKey(int num) { // Subclasses for specific protocols override this. return null; } /** * Retrieves the content of this URLConnection * * @exception IOException If an error occurs * @exception UnknownServiceException If the protocol does not support the * content type */ public Object getContent() throws IOException { // FIXME: Doc indicates that other criteria should be applied as // heuristics to determine the true content type, e.g. see // guessContentTypeFromName() and guessContentTypeFromStream methods // as well as FileNameMap class & fileNameMap field & get/set methods. String cType = getContentType(); contentHandler = setContentHandler(cType); if (contentHandler == null) return getInputStream(); return contentHandler.getContent(this); } /** * Retrieves the content of this URLConnection * * @exception IOException If an error occurs * @exception UnknownServiceException If the protocol does not support the * content type */ public Object getContent(Class[] classes) throws IOException { // FIXME: implement this return getContent (); } /** * Returns a permission object representing the permission necessary to make * the connection represented by this object. This method returns null if no * permission is required to make the connection. * * @exception IOException If the computation of the permission requires * network or file I/O and an exception occurs while computing it */ public Permission getPermission() throws IOException { // Subclasses may override this. return new java.security.AllPermission(); } /** * Returns the input stream of the URL connection * * @exception IOException If an error occurs * @exception UnknownServiceException If the protocol does not support input */ public InputStream getInputStream() throws IOException { // Subclasses for specific protocols override this. throw new UnknownServiceException("Protocol " + url.getProtocol() + " does not support input."); } /** * Returns the output stream of the URL connection * * @exception IOException If an error occurs * @exception UnknownServiceException If the protocol does not support output */ public OutputStream getOutputStream() throws IOException { // Subclasses for specific protocols override this. throw new UnknownServiceException("Protocol " + url.getProtocol() + " does not support output."); } /** * Returns a string representation of the URL connection object */ public String toString() { return this.getClass().getName() + ":" + url.toString(); } /** * Sets tha value of the doInput field. * * @param doinput The new value of the doInput field * * @exception IllegalStateException If already connected */ public void setDoInput(boolean doinput) { if (connected) throw new IllegalStateException ("Already connected"); doInput = doinput; } /** * Returns the current value of the doInput field */ public boolean getDoInput() { return doInput; } /** * Sets the value of the doOutput field * * @param dooutput The new value of the doOutput field * * @exception IllegalStateException If already connected */ public void setDoOutput(boolean dooutput) { if (connected) throw new IllegalStateException ("Already connected"); doOutput = dooutput; } /** * Returns the current value of the doOutput field */ public boolean getDoOutput() { return doOutput; } /** * Sets a new value to the allowUserInteraction field * * @param allowed The new value * * @exception IllegalStateException If already connected */ public void setAllowUserInteraction(boolean allowed) { if (connected) throw new IllegalStateException ("Already connected"); allowUserInteraction = allowed; } /** * Returns the current value of the allowUserInteraction field */ public boolean getAllowUserInteraction() { return allowUserInteraction; } /** * Sets the default value if the allowUserInteraction field * * @param allowed The new default value */ public static void setDefaultAllowUserInteraction(boolean allowed)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -