urlretriever.java

来自「world wind java sdk 源码」· Java 代码 · 共 526 行 · 第 1/2 页

JAVA
526
字号
/*Copyright (C) 2001, 2006 United States Governmentas represented by the Administrator of theNational Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.retrieve;import gov.nasa.worldwind.*;import gov.nasa.worldwind.avlist.AVKey;import gov.nasa.worldwind.util.*;import java.io.*;import java.net.*;import java.nio.ByteBuffer;import java.nio.channels.*;import java.util.concurrent.atomic.AtomicInteger;import java.util.logging.Level;import java.util.zip.*;/** * @author Tom Gaskins * @version $Id: URLRetriever.java 9306 2009-03-11 20:05:32Z tgaskins $ */public abstract class URLRetriever extends WWObjectImpl implements Retriever{    private volatile String state = RETRIEVER_STATE_NOT_STARTED;    private volatile int contentLength = 0;    private AtomicInteger contentLengthRead = new AtomicInteger(0);    private volatile String contentType;    private volatile ByteBuffer byteBuffer;    private volatile URLConnection connection;    private final URL url;    private final RetrievalPostProcessor postProcessor;    private int connectTimeout = Configuration.getIntegerValue(AVKey.URL_CONNECT_TIMEOUT, 8000);    private int readTimeout = Configuration.getIntegerValue(AVKey.URL_READ_TIMEOUT, 5000);    private int staleRequestLimit = -1;    private long submitTime;    private long beginTime;    private long endTime;    /**     * Create the appropriate retriever for a URL's protocol.     *     * @param url           the url that will be the source of the retrieval.     * @param postProcessor the retriever's post-processor.     *     * @return a retriever for the protocol specified in the url, or null if no retriever exists for the protocol.     *     * @throws IllegalArgumentException if the url is null.     */    public static URLRetriever createRetriever(URL url, RetrievalPostProcessor postProcessor)    {        if (url == null)        {            String message = Logging.getMessage("nullValue.URLIsNull");            Logging.logger().severe(message);            throw new IllegalArgumentException(message);        }        if (url.getProtocol().equalsIgnoreCase("http"))            return new HTTPRetriever(url, postProcessor);        else            return null;    }    /**     * @param url           the URL of the resource to retrieve.     * @param postProcessor the retrieval post-processor to invoke when the resource is retrieved.     *     * @throws IllegalArgumentException if <code>url</code> or <code>postProcessor</code> is null.     */    public URLRetriever(URL url, RetrievalPostProcessor postProcessor)    {        if (url == null)        {            String message = Logging.getMessage("nullValue.URLIsNull");            Logging.logger().severe(message);            throw new IllegalArgumentException(message);        }        this.url = url;        this.postProcessor = postProcessor;    }    public final URL getUrl()    {        return url;    }    public final int getContentLength()    {        return this.contentLength;    }    protected void setContentLengthRead(int length)    {        this.contentLengthRead.set(length);    }    public final int getContentLengthRead()    {        return this.contentLengthRead.get();    }    public final String getContentType()    {        return this.contentType;    }    public final ByteBuffer getBuffer()    {        return this.byteBuffer;    }    public final String getName()    {        return this.url.toString();    }    public final String getState()    {        return this.state;    }    protected final URLConnection getConnection()    {        return this.connection;    }    public final RetrievalPostProcessor getPostProcessor()    {        return postProcessor;    }    public final int getConnectTimeout()    {        return connectTimeout;    }    public int getReadTimeout()    {        return readTimeout;    }    public void setReadTimeout(int readTimeout)    {        this.readTimeout = readTimeout;    }    public int getStaleRequestLimit()    {        return staleRequestLimit;    }    public void setStaleRequestLimit(int staleRequestLimit)    {        this.staleRequestLimit = staleRequestLimit;    }    public final void setConnectTimeout(int connectTimeout)    {        this.connectTimeout = connectTimeout;    }    public long getSubmitTime()    {        return submitTime;    }    public void setSubmitTime(long submitTime)    {        this.submitTime = submitTime;    }    public long getBeginTime()    {        return beginTime;    }    public void setBeginTime(long beginTime)    {        this.beginTime = beginTime;    }    public long getEndTime()    {        return endTime;    }    public void setEndTime(long endTime)    {        this.endTime = endTime;    }    public final Retriever call() throws Exception    {        if (this.interrupted())            return this;        try        {            this.setState(RETRIEVER_STATE_STARTED);            if (!this.interrupted())            {                this.setState(RETRIEVER_STATE_CONNECTING);                this.connection = this.openConnection();            }            if (!this.interrupted())            {                this.setState(RETRIEVER_STATE_READING);                this.byteBuffer = this.read();            }            if (!this.interrupted())                this.setState(RETRIEVER_STATE_SUCCESSFUL);            WorldWind.getNetworkStatus().logAvailableHost(this.url);        }        catch (UnknownHostException e)        {            this.setState(RETRIEVER_STATE_ERROR);            WorldWind.getNetworkStatus().logUnavailableHost(this.url);            throw e;        }        catch (SocketException e)        {            this.setState(RETRIEVER_STATE_ERROR);            WorldWind.getNetworkStatus().logUnavailableHost(this.url);            throw e;        }        catch (ClosedByInterruptException e)        {            this.interrupted();        }        catch (Exception e)        {            this.setState(RETRIEVER_STATE_ERROR);            if (!(e instanceof java.net.SocketTimeoutException))            {                Logging.logger().log(Level.SEVERE,                    Logging.getMessage("URLRetriever.ErrorAttemptingToRetrieve", this.url.toString()), e);            }            throw e;        }        finally        {            this.end();        }        return this;    }    private void setState(String state)    {        String oldState = this.state;        this.state = state;        this.firePropertyChange(AVKey.RETRIEVER_STATE, oldState, this.state);    }    private boolean interrupted()

⌨️ 快捷键说明

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