View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2001-2006 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.microemu.app.util;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.net.URL;
26  import java.net.URLConnection;
27  import java.util.Hashtable;
28  
29  public class ResURLConnection extends URLConnection {
30  	
31  	private static final String PREFIX = "res:";
32  
33  	private Hashtable entries;
34  	
35  	protected ResURLConnection(URL url, Hashtable entries) {
36  		super(url);
37  		
38  		this.entries = entries;
39  	}
40  
41  	public void connect() throws IOException {
42  	}
43  
44  	public InputStream getInputStream() throws IOException {
45  		String location = url.toString();
46  		int idx = location.indexOf(PREFIX);
47  		if (idx == -1) {
48  			throw new IOException();
49  		}
50  		location = location.substring(idx + PREFIX.length());
51  		byte[] data = (byte[]) entries.get(location);
52  		if (data == null) {
53  			throw new IOException();
54  		}
55  		return new ByteArrayInputStream(data);
56  	}
57  	
58  }