📄 mediacollection.java
字号:
/*
* @(#)MediaCollection 1.0 04/07/01 @(#)
*
* Copyright 2004 NTT DoCoMo, Inc. All rights reserved.
*/
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import com.nttdocomo.io.HttpConnection;
import com.nttdocomo.ui.Dialog;
import com.nttdocomo.ui.Display;
import com.nttdocomo.ui.IApplication;
import com.nttdocomo.ui.Image;
import com.nttdocomo.ui.MediaImage;
import com.nttdocomo.ui.MediaManager;
import com.nttdocomo.ui.MediaSound;
/**
* MediaCollection<BR>
* The class which manages media data.
* <p>
* @version 1.0
* </p>
*/
public class MediaCollection {
/** The type of media : Image */
private static final String TYPE_IMAGE = "1";
/** The type of media : Sound */
private static final String TYPE_SOUND = "2";
/** URL of the download location */
private static final String[][] URL = {
{TYPE_IMAGE, "images/title.gif"},
{TYPE_IMAGE, "images/start.gif"},
{TYPE_IMAGE, "images/ranking.gif"},
{TYPE_IMAGE, "images/msg/clear.gif"},
{TYPE_IMAGE, "images/msg/gameover.gif"},
{TYPE_IMAGE, "images/msg/pause.gif"},
{TYPE_IMAGE, "images/msg/ready.gif"},
{TYPE_IMAGE, "images/item/1up.gif"},
{TYPE_IMAGE, "images/item/1up_C.gif"},
{TYPE_IMAGE, "images/item/bonus_min.gif"},
{TYPE_IMAGE, "images/item/bonus_big.gif"},
{TYPE_IMAGE, "images/item/fire.gif"},
{TYPE_IMAGE, "images/item/goal.gif"},
{TYPE_IMAGE, "images/item/obstacle.gif"},
{TYPE_IMAGE, "images/item/player.gif"},
{TYPE_SOUND, "sounds/bgm.mld"},
{TYPE_SOUND, "sounds/clear.mld"},
{TYPE_SOUND, "sounds/gameover.mld"},
{TYPE_SOUND, "sounds/getitem.mld"},
{TYPE_SOUND, "sounds/fire.mld"}
};
/*
* Image / Sound ID
*/
/** Image id :Game title */
public static final int IMG_TITLE = 0;
/** Image id :Start button */
public static final int IMG_START = 1;
/** Image id :Score ranking */
public static final int IMG_RANKING = 2;
/** Image id :Stage clear */
public static final int MSG_CLEAR = 3;
/** Image id :Game over */
public static final int MSG_GAME_OVER = 4;
/** Image id :Pause */
public static final int MSG_PAUSE = 5;
/** Image id :Ready */
public static final int MSG_READY = 6;
/** Image id :1up Before */
public static final int IMG_1UP = 7;
/** Image id :1up After */
public static final int IMG_1UP_C = 8;
/** Image id :Regular bonus */
public static final int IMG_BONUS_MIN = 9;
/** Image id :Big bonus */
public static final int IMG_BONUS_BIG = 10;
/** Image id :Flame */
public static final int IMG_FIRE = 11;
/** Image id :Goal */
public static final int IMG_GOAL = 12;
/** Image id :Obstacle */
public static final int IMG_OBSTACLE = 13;
/** Image id :Player car */
public static final int IMG_PLAYER = 14;
/** Sound id :BGM */
public static final int SND_BGM = 15;
/** Sound id :Clear */
public static final int SND_CLEAR = 16;
/** Sound id :Game over */
public static final int SND_GAME_OVER = 17;
/** Sound id :Get a item */
public static final int SND_GET_ITEM = 18;
/** Sound id :Flame */
public static final int SND_FIRE = 19;
/** Media data */
private static Object[] mediaData;
/** Start */
private static ProgressCanvas progressCanvas;
/**
* Reads media data.
* @return true / false
*/
public static boolean load() {
progressCanvas = new ProgressCanvas(URL.length);
Display.setCurrent(progressCanvas);
/* Downloading when not downloaded. */
if (false == isComplete()) {
progressCanvas.setMessage("Media data is downloading...");
if (false == download()) {
return false;
}
}
/* Loading image/sound. */
progressCanvas.reset(URL.length);
progressCanvas.setMessage("Media data is reading...");
if (false == loadResources()) {
dispose();
return false;
}
return true;
}
/**
* Confirms whether media data has been downloaded.
* <br>
* @return true:downloaded
*/
private static boolean isComplete() {
InputStream in = null;
int flag = 0;
try {
in = Connector.openDataInputStream("scratchpad:///1");
flag = in.read();
} catch (Exception e) {
Dialog dialog = new Dialog(Dialog.DIALOG_INFO, "Error");
dialog.setText("It failed to reading of a scratchpad.");
dialog.show();
IApplication.getCurrentApp().terminate();
} finally {
try {
if (null != in) {
in.close();
}
} catch (Exception e) {
// NOP
}
}
if (1 == flag) {
return true;
}
return false;
}
/**
* Downloads media data and saves on the scratchpad.
* @return true / false
*/
private static boolean download() {
byte[] data;
int offset = 1; /* The write-in position on the scratchpad */
for (int i = 0; i < URL.length; i++) {
/* download */
data = getServerData(URL[i][1]);
if (null == data) {
return false;
}
/* saving on the scratchpad. */
if (false == save(data, offset)) {
return false;
}
offset+=data.length+2;
/* Progress */
progressCanvas.add();
}
/* Complete */
if (false == complete()) {
return false;
}
return true;
}
/**
* Downloads the file specified by URL.
* @param url URL
* @return download data
* null:NG
*/
private static byte[] getServerData(String url) {
HttpConnection http = null;
InputStream in = null;
byte[] data = null;
if (!url.startsWith("http://")) {
url = IApplication.getCurrentApp().getSourceURL() + url;
}
try {
int contentLength = 0;
http = (HttpConnection)Connector.open(url, Connector.READ);
http.setRequestMethod(HttpConnection.GET);
http.connect();
contentLength = (int)http.getLength();
in = http.openInputStream();
data = new byte[contentLength];
in.read(data);
} catch (Exception e) {
return null;
} finally {
try {
if (null != in) {
in.close();
}
} catch (Exception e) {
// NOP
}
try {
if (null != http) {
http.close();
}
} catch (Exception e) {
// NOP
}
}
return data;
}
/**
* Saves data on the scratchpad.
* @param data data
* @param offset offset
* @return true / false
*/
private static boolean save(byte[] data, int offset) {
OutputStream out = null;
int length = data.length;
try {
out = Connector.openOutputStream("scratchpad:///1;pos=" + offset);
out.write((length >>> 8) & 0xff);
out.write(length & 0xff);
out.write(data);
} catch (Exception e) {
return false;
} finally {
try {
if (null != out) {
out.close();
}
} catch (Exception e) {
// NOP
}
}
return true;
}
/**
* Downloads media data.
*/
private static boolean complete() {
OutputStream out = null;
try {
out = Connector.openOutputStream("scratchpad:///1");
out.write(1 & 0xff);
} catch (Exception e) {
return false;
} finally {
try {
if (null != out) {
out.close();
}
} catch (Exception e) {
// NOP
}
}
return true;
}
/**
* Loadsimage/sound from the scratchpad.
* @return true / false
*/
private static boolean loadResources() {
mediaData = new Object[URL.length];
int size = 0;
int offset = 1;
for (int i = 0; i < URL.length; i++) {
/* Size */
size = getMediaSize(offset);
if (0 >= size) {
return false;
}
offset+=2;
/* Create */
mediaData[i] = createMediaData(offset, size, URL[i][0]);
if (null == mediaData[i]) {
return false;
}
offset+=size;
/* Progress */
progressCanvas.add();
}
return true;
}
/**
* Reads the size of an image / sound data from the scratchpad.
* @param offset Offset
* @return Size
*/
private static int getMediaSize(int offset) {
InputStream in = null;
int length = 0;
try {
in = Connector.openInputStream("scratchpad:///1;pos=" + offset);
length |= (in.read() << 8);
length |= in.read();
} catch (Exception e) {
Dialog dialog = new Dialog(Dialog.DIALOG_INFO, e.getClass().getName());
dialog.setText("It failed to get of media size.\n" + e.getMessage());
dialog.show();
length = 0;
} finally {
try {
if (null != in) {
in.close();
}
} catch (Exception e) {
// NOP
}
}
return length;
}
/**
* Reads media data from the scratchpad.
* @param offset Offset
* @param size Size
* @param type Type
* @return MediaData
*/
private static Object createMediaData(int offset,
int size,
String type) {
byte[] data = null;
InputStream in = null;
Object object = null;
try {
in = Connector.openInputStream("scratchpad:///1;pos=" + offset);
data = new byte[size];
in.read(data, 0, size);
if (TYPE_IMAGE.equals(type)) {
MediaImage mi = MediaManager.getImage(data);
mi.use();
object = mi;
} else if (TYPE_SOUND.equals(type)) {
MediaSound ms = MediaManager.getSound(data);
ms.use();
object = ms;
} else {
object = null;
}
} catch (Exception e) {
Dialog dialog = new Dialog(Dialog.DIALOG_INFO, e.getClass().getName());
dialog.setText("It failed to creation of media data.\n" + e.getMessage());
dialog.show();
object = null;
} finally {
try {
if (null != in) {
in.close();
}
} catch (Exception e) {
// NOP
}
}
return object;
}
/**
* Acquires image object.
* @param imageID Image id
* @return Image object
*/
public static Image getImage(int imageID) {
if ((imageID > mediaData.length) ||
(!(TYPE_IMAGE.equals(URL[imageID][0])))) {
return null;
}
return ((MediaImage)mediaData[imageID]).getImage();
}
/**
* Acquires sound object.
* @param soundID Sound id
* @return Sound object
*/
public static MediaSound getSound(int soundID) {
if ((soundID > mediaData.length) ||
(!(TYPE_SOUND.equals(URL[soundID][0])))) {
return null;
}
return (MediaSound)mediaData[soundID];
}
/**
* Cancels media image / sound.
*/
public static void dispose() {
if (null == mediaData) {
return;
}
for (int i = 0; i < mediaData.length; i++) {
if (null != mediaData[i]) {
if (mediaData[i] instanceof MediaImage) {
((MediaImage)mediaData[i]).dispose();
} else if (mediaData[i] instanceof MediaSound) {
((MediaSound)mediaData[i]).dispose();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -