⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 urlconnection.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* URLConnection.java -- Abstract superclass for reading from URL's   Copyright (C) 1998, 2002, 2003, 2004 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.net;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.security.AllPermission;import java.security.Permission;import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.Collections;import java.util.Date;import java.util.Hashtable;import java.util.Locale;import java.util.Map;import java.util.StringTokenizer;import gnu.gcj.io.MimeTypes;/** * 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. *//** * This class models a connection that retrieves the information pointed * to by a URL object.  This is typically a connection to a remote node * on the network, but could be a simple disk read. * <p> * A URLConnection object is normally created by calling the openConnection() * method of a URL object.  This method is somewhat misnamed because it does * not actually open the connection.  Instead, it return an unconnected * instance of this object.  The caller then has the opportunity to set * various connection options prior to calling the actual connect() method. * <p> * After the connection has been opened, there are a number of methods in * this class that access various attributes of the data, typically * represented by headers sent in advance of the actual data itself. * <p> * Also of note are the getInputStream and getContent() methods which allow * the caller to retrieve the actual data from the connection.  Note that * for some types of connections, writing is also allowed.  The setDoOutput() * method must be called prior to connecing in order to enable this, then * the getOutputStream method called after the connection in order to * obtain a stream to write the output to. * <p> * The getContent() method is of particular note.  This method returns an * Object that encapsulates the data returned.  There is no way do determine * the type of object that will be returned in advance.  This is determined * by the actual content handlers as described in the description of that * method. * * @author Aaron M. Renn (arenn@urbanophile.com) * @author Warren Levy (warrenl@cygnus.com) */public abstract class URLConnection{  /**   * This is an object that maps filenames to MIME types.  The interface   * to do this is implemented by this class, so just create an empty   * instance and store it here.   */  private static FileNameMap fileNameMap;  /**   * This is the ContentHandlerFactory set by the caller, if any   */  private static ContentHandlerFactory factory;  /**   * This is the default value that will be used to determine whether or   * not user interaction should be allowed.   */  private static boolean defaultAllowUserInteraction;  /**   * This is the default flag indicating whether or not to use caches to   * store the data returned from a server   */  private static boolean defaultUseCaches = true;  private static ContentHandlerFactory defaultFactory    = new gnu.java.net.DefaultContentHandlerFactory();  /**   * This variable determines whether or not interaction is allowed with   * the user.  For example, to prompt for a username and password.   */  protected boolean allowUserInteraction;  /**   * Indicates whether or not a connection has been established to the   * destination specified in the URL   */  protected boolean connected;  /**   * Indicates whether or not input can be read from this URL   */  protected boolean doInput = true;  /**   * Indicates whether or not output can be sent to this URL   */  protected boolean doOutput;  /**   * If this flag is set, the protocol is allowed to cache data whenever   * it can (caching is not guaranteed). If it is not set, the protocol   * must a get a fresh copy of the data.   * <p>   * This field is set by the setUseCaches method and returned by the   * getUseCaches method.   *   * Its default value is that determined by the last invocation of   * setDefaultUseCaches   */  protected boolean useCaches;  /**   * If this value is non-zero, then the connection will only attempt to   * fetch the document pointed to by the URL if the document has been   * modified more recently than the date set in this variable.  That date   * should be specified as the number of seconds since 1/1/1970 GMT.   */  protected long ifModifiedSince;  /**   * This is the URL associated with this connection   */  protected URL url;  private static Hashtable handlers = new Hashtable();  private static SimpleDateFormat[] dateFormats;  private static boolean dateformats_initialized;  /* Cached ParsePosition, used when parsing dates. */  private ParsePosition position;  /**   * 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)  {    // Set up all our instance variables    this.url = url;    allowUserInteraction = defaultAllowUserInteraction;    useCaches = defaultUseCaches;  }  /**   * Establishes the actual connection to the URL associated with this   * connection object   *   * @exception IOException if an error occurs   */  public abstract void connect() throws IOException;  /**   * Returns the URL object associated with this connection   *   * @return The URL for this connection.   */  public URL getURL()  {    return url;  }  /**   * Returns the value of the content-length header field or -1 if the value   * is not known or not present.   *   * @return The content-length field   */  public int getContentLength()  {    return getHeaderFieldInt("content-length", -1);  }  /**   * Returns the the content-type of the data pointed to by the URL.  This   * method first tries looking for a content-type header.  If that is not   * present, it attempts to use the file name to determine the content's   * MIME type.  If that is unsuccessful, the method returns null.  The caller   * may then still attempt to determine the MIME type by a call to   * guessContentTypeFromStream()   *   * @return The content MIME type   */  public String getContentType()  {    return getHeaderField("content-type");  }  /**   * Returns the value of the content-encoding field or null if it is not   * known or not present.   *   * @return The content-encoding field   */  public String getContentEncoding()  {    return getHeaderField("content-encoding");  }  /**   * Returns the value of the expires header or 0 if not known or present.   * If populated, the return value is number of seconds since midnight   * on 1/1/1970 GMT.   *   * @return The expiration time.   */  public long getExpiration()  {    return getHeaderFieldDate("expires", 0L);  }  /**   * Returns the date of the document pointed to by the URL as reported in   * the date field of the header or 0 if the value is not present or not   * known. If populated, the return value is number of seconds since   * midnight on 1/1/1970 GMT.   *   * @return The document date   */  public long getDate()  {    return getHeaderFieldDate("date", 0L);  }  /**   * Returns the value of the last-modified header field or 0 if not known known   * or not present.  If populated, the return value is the number of seconds   * since midnight on 1/1/1970.   *   * @return The last modified time   */  public long getLastModified()  {    return getHeaderFieldDate("last-modified", 0L);  }  /**   * Return a String representing the header value at the specified index.   * This allows the caller to walk the list of header fields.  The analogous   * getHeaderFieldKey(int) method allows access to the corresponding key   * for this header field   *   * @param index The index into the header field list to retrieve the value for   *   * @return The header value or null if index is past the end of the headers   */  public String getHeaderField(int index)  {    // Subclasses for specific protocols override this.    return null;  }  /**   * Returns a String representing the value of the header field having   * the named key.  Returns null if the header field does not exist.   *   * @param name The key of the header field   *   * @return The value of the header field as a String   */  public String getHeaderField(String name)  {    // Subclasses for specific protocols override this.    return null;  }  /**   * Returns a map of all sent header fields   *   * @return all header fields   *   * @since 1.4   */  public Map getHeaderFields()  {    // Subclasses for specific protocols override this.    return Collections.EMPTY_MAP;  }  /**   * Returns the value of the named header field as an int.  If the field   * is not present or cannot be parsed as an integer, the default value   * will be returned.   *   * @param name The header field key to lookup   * @param defaultValue The defaule value if the header field is not found   * or can't be parsed.   *   * @return The value of the header field or the default value if the field   * is missing or malformed   */  public int getHeaderFieldInt(String name, int defaultValue)  {    String value = getHeaderField(name);    if (value == null)      return defaultValue;    try      {	return Integer.parseInt(value);      }    catch (NumberFormatException e)      {	return defaultValue;      }  }  /**   * Returns the value of the named header field as a date.  This date will   * be the number of seconds since midnight 1/1/1970 GMT or the default   * value if the field is not present or cannot be converted to a date.   *   * @param name The name of the header field

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -