📄 resourcehandler.java
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3)
// Source File Name: ResourceHandler.java
package se.southend.drops.resource;
import java.io.*;
import java.util.Hashtable;
import java.util.Vector;
import se.southend.drops.cache.RecordStoreCache;
import se.southend.drops.cache.TimestampCacheInfo;
import se.southend.drops.screen.Log;
// Referenced classes of package se.southend.drops.resource:
// ResourceListener, HttpHandler
public class ResourceHandler
{
private static interface DownloadThread
extends Runnable
{
public abstract void abort();
}
private static class Request
{
String url;
byte post[];
int priority;
boolean shouldCache;
Request(String url, byte post[], int priority, boolean shouldCache)
{
this.url = url;
this.post = post;
this.priority = priority;
this.shouldCache = shouldCache;
}
}
public ResourceHandler()
{
requestQueue = new Vector(3);
localQueue = new Vector(3);
networkQueue = new Vector();
downloading = new Hashtable();
downloadThreads = new Vector(3);
shouldCache = true;
start(2);
}
public static ResourceHandler getInstance()
{
if(instance == null)
instance = new ResourceHandler();
return instance;
}
public void stop()
{
abortDownloads();
running = false;
}
public void start()
{
start(1);
}
public void start(int nbrNetworkThreads)
{
if(!running)
{
running = true;
createLocalThread();
while(--nbrNetworkThreads >= 0)
downloadThreads.addElement(createNetworkThread());
}
}
public boolean isRunning()
{
return running;
}
public void shouldCacheDownloads(boolean enabled)
{
shouldCache = enabled;
}
public long getBytesDownloaded()
{
return bytesDownloaded;
}
public void setBytesDownloaded(long nbrBytes)
{
bytesDownloaded = nbrBytes;
}
public String[] getNetworkQueueURLs()
{
String urls[] = new String[networkQueue.size()];
for(int i = 0; i < urls.length; i++)
urls[i] = ((Request)networkQueue.elementAt(i)).url;
return urls;
}
public String[] getLocalQueueURLs()
{
String urls[] = new String[localQueue.size()];
for(int i = 0; i < urls.length; i++)
urls[i] = ((Request)networkQueue.elementAt(i)).url;
return urls;
}
public synchronized void abortDownloads()
{
downloading.clear();
requestQueue.removeAllElements();
localQueue.removeAllElements();
networkQueue.removeAllElements();
for(int i = 0; i < downloadThreads.size(); i++)
((DownloadThread)downloadThreads.elementAt(i)).abort();
}
public void request(String url, ResourceListener listener)
{
request(url, 0, listener);
}
public void request(String url, int priority, ResourceListener listener)
{
request(url, null, priority, listener);
}
public void request(String url, byte post[], ResourceListener listener)
{
request(url, post, 0, listener);
}
public synchronized void request(String url, byte post[], int priority, ResourceListener listener)
{
for(int i = 0; i < requestQueue.size(); i++)
{
Request queued = (Request)requestQueue.elementAt(i);
if(url.equals(queued.url) && (post == queued.post || post != null && post.equals(queued.post)))
{
requestQueue.removeElementAt(i);
addToQueue(new Request(url, post, priority, shouldCache), requestQueue);
}
}
if(!downloading.containsKey(url))
{
Vector listeners = new Vector(1);
downloading.put(url, listeners);
addToQueue(new Request(url, post, priority, shouldCache), requestQueue);
}
addListener(listener, url);
unlock();
}
public InputStream requestStream(String url)
throws IOException
{
if(url.startsWith("http://"))
return HttpHandler.getInstance().requestStream(url);
else
return getClass().getResourceAsStream(url);
}
public void addListener(ResourceListener listener, String url)
{
if(listener != null && downloading.containsKey(url))
{
Vector listeners = (Vector)downloading.get(url);
listeners.addElement(listener);
}
}
private static void addToQueue(Request request, Vector queue)
{
int priority = request.priority;
int pos;
for(pos = queue.size(); --pos >= 0 && priority <= ((Request)queue.elementAt(pos)).priority;);
if(queue.size() == 0 || pos == queue.size() - 1)
queue.addElement(request);
else
queue.insertElementAt(request, pos + 1);
}
private synchronized Object getFromQueue(Vector queue)
{
if(!queue.isEmpty())
{
Object result = queue.elementAt(0);
queue.removeElementAt(0);
return result;
} else
{
return null;
}
}
private void notifyListeners(String url, byte data[], int errorCode, String MIMEtype)
{
if(errorCode == 0)
Log.println("Downloaded: " + url + " MIME: " + MIMEtype, Log.NETWORK);
else
Log.println("Error downloading: " + url + " Error code: " + errorCode, Log.NETWORK);
if(downloading.containsKey(url))
{
Vector listeners = (Vector)downloading.get(url);
downloading.remove(url);
for(int i = 0; i < listeners.size(); i++)
((ResourceListener)listeners.elementAt(i)).dataReceived(url, data, errorCode, MIMEtype);
}
}
private synchronized void unlock()
{
notifyAll();
lockable = false;
}
private synchronized void lock()
{
if(lockable)
try
{
wait();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
lockable = true;
}
private void createLocalThread()
{
Thread localThread = new Thread() {
public void run()
{
try
{
while(true)
{
if(!running)
break;
for(; requestQueue.isEmpty() && localQueue.isEmpty(); lock());
Request request = (Request)getFromQueue(requestQueue);
if(request != null)
{
if(RecordStoreCache.getInstance().contains(request.url) || !request.url.startsWith("http://"))
ResourceHandler.addToQueue(request, localQueue);
else
ResourceHandler.addToQueue(request, networkQueue);
unlock();
}
request = (Request)getFromQueue(localQueue);
if(request != null)
{
int errorCode = 0;
String url = request.url;
byte data[] = null;
try
{
Log.println("Request cache/local: " + url, Log.NETWORK);
if(data == null)
data = RecordStoreCache.getInstance().get(url);
if(data == null)
{
InputStream in = getClass().getResourceAsStream(url);
if(in != null)
{
data = new byte[in.available()];
in.read(data);
} else
{
errorCode = 1;
}
}
}
catch(Exception e)
{
System.out.println("Cache/local file error:");
e.printStackTrace();
errorCode = 1;
}
notifyListeners(url, data, errorCode, null);
}
}
}
catch(Exception e)
{
System.out.println("Cache/local thread error:");
e.printStackTrace();
}
}
};
localThread.start();
}
private DownloadThread createNetworkThread()
{
DownloadThread download = new DownloadThread() {
public void abort()
{
}
public void run()
{
http = new HttpHandler();
try
{
do
{
if(!running)
break;
for(; networkQueue.isEmpty(); lock());
Request request = (Request)getFromQueue(networkQueue);
if(request != null)
{
int errorCode = 0;
String url = request.url;
try
{
Log.println("Request HTTP: " + url, Log.NETWORK);
if(request.post == null)
http.requestGet(url);
else
http.requestPost(url, request.post);
bytesDownloaded = http.getLength();
if(request.shouldCache)
{
TimestampCacheInfo info = new TimestampCacheInfo();
info.setTimestamp(http.getLastModified());
RecordStoreCache.getInstance().put(url, info, http.getData());
}
}
catch(Exception e)
{
System.out.println("Network file error:");
e.printStackTrace();
errorCode = 2;
}
notifyListeners(url, http.getData(), errorCode, http.getType());
}
} while(true);
}
catch(Exception e)
{
System.out.println("Network thread error:");
e.printStackTrace();
}
}
private HttpHandler http;
};
(new Thread(download)).start();
return download;
}
private static final boolean USE_CACHE = true;
private static final String PREFIX_HTTP = "http://";
public static final int NO_ERROR = 0;
public static final int ERROR_OPENING_LOCAL_FILE = 1;
public static final int ERROR_OPENING_NETWORK_FILE = 2;
private static final int NBR_NETWORK_THREADS = 2;
private static ResourceHandler instance;
private Vector requestQueue;
private Vector localQueue;
private Vector networkQueue;
private Hashtable downloading;
private Vector downloadThreads;
private long bytesDownloaded;
private boolean shouldCache;
private boolean running;
private boolean lockable;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -