?? codegen.java
字號:
/*
* Created on 2005-03-10
*
* Copyright (c) 2005 nanoGames. All Rights Reserved.
*
*/
//package com.nano.KangooJumper;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.*;
/**
* @author plumkawka CodeGen
*/
public class CodeGen
{
public int nCodesCount;
private String[] sCode;
private String sName;
public CodeGen(String name, int codesnum)
{
sName = name;
nCodesCount = codesnum;
sCode = new String[nCodesCount];
if (!existsCodeList(name))
{
// generate code list
for (int i = 0; i < nCodesCount; i++)
{
boolean exists;
String newcode;
char[] tmp = new char[Config.CODES_MAX_CHARS];
do
{
// generate
for (int a = 0; a < Config.CODES_MAX_CHARS; a++)
{
char t = 0;
if (Math.Rand(0, 100) > 50)
{
t = (char) ('A' + (char) Math.Rand(0, ('Z' - 'A') + 1));
}
else
{
t = (char) ('1' + Math.Rand(0, ('9' - '1') + 1));
}
tmp[a] = t;
}
newcode = new String(tmp);
// check if already exists
exists = false;
for (int b = 0; b < (i - 1); b++)
{
if (newcode.equals(sCode[b]))
exists = true;
}
} while (exists);
//store new code
sCode[i] = newcode;
//Utils.Log(new String(tmp));
}
// save list to recordstore
saveCodeList();
}
else
{
loadCodeList();
}
}
public void removeCodeList()
{
if ((sName == null) || (sName.length() == 0))
{
Utils.Log("CodeGen: error removing code list");
return;
}
try
{
if (existsCodeList(sName) == true)
RecordStore.deleteRecordStore (sName);
}
catch (Exception e)
{
}
}
public void saveCodeList()
{
if ((sName == null) || (sName.length() == 0))
{
Utils.Log("CodeGen: error saving code list");
return;
}
try
{
ByteArrayOutputStream buf = new ByteArrayOutputStream ();
DataOutputStream stream = new DataOutputStream (buf);
for (int i=0; i<nCodesCount; i++)
{
stream.writeUTF(sCode[i]);
Utils.Log("storing: "+sCode[i]);
}
RecordStore rs = RecordStore.openRecordStore (sName, true);
if (rs.getNumRecords() == 0)
{
rs.addRecord (buf.toByteArray(), 0, buf.size());
}
else
{
rs.setRecord (1,
buf.toByteArray(),
0,
buf.size());
}
rs.closeRecordStore ();
}
catch (Exception e)
{
e.printStackTrace();
}
Utils.callGc();
}
public int checkCode(String code)
{
for (int i=0; i<nCodesCount; i++)
{
if (code.equals( sCode[i] ))
{
return i;
}
}
return -1;
}
public String getCode(int idx)
{
String s = "";
if ((idx >=0) && (idx <nCodesCount))
{
s = sCode[idx];
}
return s;
}
public void loadCodeList()
{
if ((sName == null) || (sName.length() == 0))
{
Utils.Log("CodeGen: error loading code list");
return;
}
try
{
RecordStore rs = RecordStore.openRecordStore (sName, true);
DataInputStream stream = new DataInputStream (
new ByteArrayInputStream (
rs.getRecord(Config.CODES_RECORDID)));
for (int i=0; i<nCodesCount; i++)
{
sCode[i] = stream.readUTF();
Utils.Log("loading: "+sCode[i]);
}
rs.closeRecordStore ();
}
catch (Exception e)
{
e.printStackTrace();
}
Utils.callGc();
}
public boolean existsCodeList(String name)
{
try
{
String[] records = RecordStore.listRecordStores();
// enumarate records
for (int lp = 0; lp < records.length; lp++)
{
if (records[lp].compareTo(name) == 0)
return true;
}
}
catch (Exception e)
{
}
return false;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -