DEADSOFTWARE

Initial commit.
[LongFlight.git] / src / code / kalter / longflight / Loader.java
1 package code.kalter.longflight;
3 import code.kalter.longflight.crypto.CIStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.Vector;
7 import javax.microedition.lcdui.Image;
8 import javax.microedition.media.Manager;
9 import javax.microedition.media.MediaException;
10 import javax.microedition.media.Player;
12 /**
13 * Дабы избежать повторных загрузок из разных мест программы, а так же
14 * обеспечить загрузку с расшифкой создан это класс
15 *
16 * @author KalterFive
17 */
18 public class Loader {
20 private static final int KEY = 5; // шифровальный XOR ключ
22 private static Loader instance;
24 public static Loader getInstance() {
25 if (instance == null) {
26 instance = new Loader();
27 }
28 return instance;
29 }
31 private final Vector dump; // все успешно загруженные ресурсы
33 private Loader() {
34 dump = new Vector();
35 }
37 public Object getObject(String path) {
38 Object result = null;
39 for (int i = 0; i < dump.size(); i++) {
40 ObjectItem item = (ObjectItem) dump.elementAt(i);
41 if (item.get(path) != null) {
42 result = item.get(path);
43 break;
44 }
45 }
46 return result;
47 }
49 public Image getImage(String path) throws IOException {
50 Image result = (Image) getObject(path);
51 if (result == null) {
52 final InputStream istream = getClass().getResourceAsStream(path);
53 final InputStream cistream = new CIStream(istream, KEY);
54 result = Image.createImage(cistream);
55 cistream.close();
56 dump.addElement(new ObjectItem(result, path));
57 }
58 return result;
59 }
61 public Player getPlayer(String path, String type) throws
62 IOException, MediaException {
63 Player result = (Player) getObject(path);
64 if (result == null) {
65 final InputStream iStream = getClass().getResourceAsStream(path);
66 result = Manager.createPlayer(iStream, type);
67 dump.addElement(new ObjectItem(result, path));
68 }
69 return result;
70 }
71 }