//---JAVA字节数组与字节数组流操作---------------------------------------------------
ByteArrayOutputStream bos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(bos);
byte records[]=null;
String r="";
try {
dos.writeInt(2); //记录个数
//dos.writeInt(2); //任务ID
dos.writeInt(0); //任务类型
dos.writeUTF("松鼠"); //名称
dos.writeInt(1); //对话次数/物品件数/杀怪数
//dos.writeInt(3); //任务ID
dos.writeInt(2); //任务类型
dos.writeUTF("王婆婆"); //名称
dos.writeInt(1); //对话次数/物品件数/杀怪数
records=bos.toByteArray();
r=bos.toString();
} catch (IOException ex) {
Logger.getLogger(TaskEvent.class.getName()).log(Level.SEVERE, null, ex);
}finally{
try {
dos.close();
bos.close();
} catch (IOException ex) {
Logger.getLogger(TaskEvent.class.getName()).log(Level.SEVERE, null, ex);
}
}
ByteArrayInputStream bis=new ByteArrayInputStream(records);
DataInputStream dis=new DataInputStream(bis);
try {
int size=dis.readInt();
for(int i=0;i<size;i++){
//int taskId=dis.readInt();
int taskType=dis.readInt();
String taskName=dis.readUTF();
int count=dis.readInt();
System.out.println("\tType:"+taskType+"\tname:"+taskName+"\tcount:"+count);
}
} catch (IOException ex) {
Logger.getLogger(TaskEvent.class.getName()).log(Level.SEVERE, null, ex);
}finally{
try {
dis.close();
bis.close();
} catch (IOException ex) {
Logger.getLogger(TaskEvent.class.getName()).log(Level.SEVERE, null, ex);
}
}
//---JAVA字节数组与对象流操作---------------------------------------------------
byte record[] = null;
ByteArrayOutputStream bos=new ByteArrayOutputStream();
ObjectOutputStream oos=null;
try{
oos=new ObjectOutputStream(bos);
Map map=new HashMap();
Map m1=new HashMap();
m1.put("type", Task.KILL_TYPE);
m1.put("name", "1_兔子");
m1.put("npc", "兔子");
m1.put("count", 1);
map.put(Task.KILL_TYPE+"1_兔子", m1);
oos.writeObject(map);
record=bos.toByteArray();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
oos.close();
bos.close();
} catch (IOException ex) {
Logger.getLogger(RoleEventHandle.class.getName()).log(Level.SEVERE, null, ex);
}
}
ByteArrayInputStream bis=new ByteArrayInputStream(record);
ObjectInputStream ois=null;
try{
ois=new ObjectInputStream(bis);
Map myMap=(Map) ois.readObject();
Map myMap1=(Map) myMap.get(Task.KILL_TYPE+"1_兔子");
String npc=(String) myMap1.get("npc");
System.out.println(npc);
}catch(Exception e){
e.printStackTrace();
}finally{
try {
ois.close();
bis.close();
} catch (IOException ex) {
Logger.getLogger(RoleEventHandle.class.getName()).log(Level.SEVERE, null, ex);
}
}
//---JAVA执行脚本--------------------------------------------------------
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
Boolean bool=false;
try{
Compilable compilable = (Compilable) engine; 编译器
CompiledScript compiled = compilable.compile("");
bool=(Boolean) compiled.eval();
System.out.println(bool);
}catch(Exception e){
e.printStackTrace();
}