urlconnection.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 855 行 · 第 1/2 页

JAVA
855
字号
/* URLConnection.java -- Abstract superclass for reading from URL's
   Copyright (C) 1998, 2002 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the 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, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package java.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Permission;
import java.util.Collections;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;

/**
  * 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.
  *
  * @version 0.5
  *
  * @author Aaron M. Renn (arenn@urbanophile.com)
  */
public abstract class URLConnection {

	/*************************************************************************/

	/*
	 * Class Variables
	 */

	/**
	  * 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 = new MimeTypeMapper();

	/**
	  * 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 def_allow_user_inter;

	/**
	  * This is the default flag indicating whether or not to use caches to
	  * store the data returned from a server
	  */
	private static boolean def_use_caches;

	/**
	  * This is a Hashable for setting default request properties
	  */
	private static Hashtable def_req_props = new Hashtable();

	/*************************************************************************/

	/*
	 * Instance Variables
	 */

	/**
	  * 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;

	/**
	  * 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;

	/**
	  * The list of request properties for this connection
	  */
	private final Hashtable req_props;

	/*************************************************************************/

	/*
	 * Class Methods
	 */

	/**
	  * Set's the ContentHandlerFactory for an application.  This can be called
	  * once and only once.  If it is called again, then an Error is thrown.
	  * Unlike for other set factory methods, this one does not do a security
	  * check prior to setting the factory.
	  *
	  * @param factory The ContentHandlerFactory for this application
	  *
	  * @error Error If the factory is already set
	  */
	public static synchronized void setContentHandlerFactory(ContentHandlerFactory fac) {
		if (factory != null)
			throw new Error("The ContentHandlerFactory is already set");

		factory = fac;
	}

	/*************************************************************************/

	/**
	  * Returns the default flag for whether or not interaction with a user
	  * is allowed.  This will be used for all connections unless overidden
	  *
	  * @return true if user interaction is allowed, false otherwise
	  */
	public static boolean getDefaultAllowUserInteraction() {
		return (def_allow_user_inter);
	}

	/*************************************************************************/

	/**
	  * Sets the default flag for whether or not interaction with a user
	  * is allowed.  This will be used for all connections unless overridden
	  *
	  * @param allow true to allow user interaction, false otherwise
	  */
	public static synchronized void setDefaultAllowUserInteraction(boolean allow) {
		def_allow_user_inter = allow;
	}

	/*************************************************************************/

	/**
	  * Returns the default value of a request property.  This will be used
	  * for all connections unless the value of the property is manually
	  * overridden.
	  *
	  * @param key The request property to return the default value of
	  * 
	  * @return The default request property
	  */
	public static String getDefaultRequestProperty(String key) {
		return ((String) def_req_props.get(key.toLowerCase()));
	}

	/*************************************************************************/

	/**
	  * Sets the default value of a request property.  This will be used
	  * for all connections unless the value of the property is manually
	  * overridden.
	  *
	  * @param key The request property name the default is being set for
	  * @param value The value to set the default to
	  */
	public static synchronized void setDefaultRequestProperty(String key, String value) {
		def_req_props.put(key.toLowerCase(), value);
	}

	/*************************************************************************/

	/**
	  * Returns the MIME type of a file based on the name of the file.  This
	  * works by searching for the file's extension in a list of file extensions
	  * and returning the MIME type associated with it.  If no type is found,
	  * then a MIME type of "application/octet-stream" will be returned.
	  *
	  * @param filename The filename to determine the MIME type for
	  *
	  * @return The MIME type String
	  *
	  * @specnote public since jdk 1.4
	  */
	public static String guessContentTypeFromName(String filename) {
		return (fileNameMap.getContentTypeFor(filename.toLowerCase()));
	}

	/*************************************************************************/

	/**
	  * Returns the MIME type of a stream based on the first few characters
	  * at the beginning of the stream.  This routine can be used to determine
	  * the MIME type if a server is believed to be returning an incorrect
	  * MIME type.  This method returns "application/octet-stream" if it 
	  * cannot determine the MIME type.
	  * <p>
	  * NOTE: Overriding MIME types sent from the server can be obnoxious
	  * to user's.  See Internet Exploder 4 if you don't believe me.
	  *
	  * @param is The InputStream to determine the MIME type from
	  *
	  * @return The MIME type
	  *
	  * @exception IOException If an error occurs
	  */
	public static String guessContentTypeFromStream(InputStream is) throws IOException {
		return ("application/octet-stream");
	}

	/*************************************************************************/

	/**
	  * This method returns the <code>FileNameMap</code> object being used
	  * to decode MIME types by file extension.
	  *
	  * @return The <code>FileNameMap</code>.
	  */
	public static FileNameMap getFileNameMap() {
		return (fileNameMap);
	}

	/*************************************************************************/

	/**
	  * This method set the <code>FileNameMap</code> object being used
	  * to decode MIME types by file extension.
	  *
	  * @param fileNameMap The <code>FileNameMap</code>.
	  */
	public static void setFileNameMap(FileNameMap fileNameMap) {
		URLConnection.fileNameMap = fileNameMap;
	}

	/*************************************************************************/

	/*
	 * Constructors
	 */

	/**
	  * This constructs a URLConnection from a URL object
	  *
	  * @param url The URL for this connection
	  */
	protected URLConnection(URL url) {
		// Set up all our instance variables
		this.url = url;
		allowUserInteraction = def_allow_user_inter;
		useCaches = def_use_caches;

		req_props = new Hashtable(def_req_props);
	}

	/*************************************************************************/

	/*
	 * Instance Methods
	 */

	/**
	  * Returns the default value used to determine whether or not caching
	  * of documents will be done when possible.
	  *
	  * @return true if caches will be used, false otherwise
	  */
	public boolean getDefaultUseCaches() {
		return (def_use_caches);
	}

	/*************************************************************************/

	/**
	  * Sets the default value used to determine whether or not caching
	  * of documents will be done when possible.
	  *
	  * @param use true to use caches if possible by default, false otherwise
	  */
	public synchronized void setDefaultUseCaches(boolean use) {
		def_use_caches = use;
	}

	/*************************************************************************/

	/**
	  * Returns a boolean flag indicating whether or not user interaction is
	  * allowed for this connection.  (For example, in order to prompt for
	  * username and password info.
	  *
	  * @return true if user interaction is allowed, false otherwise
	  */
	public boolean getAllowUserInteraction() {
		return (allowUserInteraction);
	}

	/*************************************************************************/

	/**
	  * Sets a boolean flag indicating whether or not user interaction is
	  * allowed for this connection.  (For example, in order to prompt for
	  * username and password info.
	  *
	  * @param allow true if user interaction should be allowed, false otherwise
	  */
	public void setAllowUserInteraction(boolean allow) {
		allowUserInteraction = allow;
	}

	/*************************************************************************/

	/**
	  * Returns the value of a flag indicating whether or not input is going
	  * to be done for this connection.  This default to true unless the
	  * doOutput flag is set to false, in which case this defaults to false.
	  *
	  * @return true if input is to be done, false otherwise
	  */
	public boolean getDoInput() {
		return (doInput);
	}

	/*************************************************************************/

	/**
	  * Returns the value of a flag indicating whether or not input is going
	  * to be done for this connection.  This default to true unless the
	  * doOutput flag is set to false, in which case this defaults to false.
	  *
	  * @param input true if input is to be done, false otherwise
	  */
	public void setDoInput(boolean input) {
		doInput = input;
	}

	/*************************************************************************/

	/**
	  * Returns a boolean flag indicating whether or not output will be done
	  * on this connection.  This defaults to false.
	  *
	  * @return true if output is to be done, false otherwise
	  */
	public boolean getDoOutput() {
		return (doOutput);
	}

⌨️ 快捷键说明

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