📄 protocol.java
字号:
/*wiseremote - a client/server remote control system for wireless devicesCopyright (C) 2005 Martin ProfittlichThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import javax.microedition.io.Connector;import javax.microedition.io.StreamConnection;import javax.microedition.lcdui.*;class Protocol{ DataInputStream iStream; DataOutputStream oStream; public Protocol (String url) { WiseremoteMIDlet.instance.display.setCurrent (new Splash ("Connecting...")); try { StreamConnection conn = (StreamConnection) Connector.open (url); DataInputStream is = conn.openDataInputStream (); DataOutputStream os = conn.openDataOutputStream (); iStream = is; oStream = os; exec ("Login"); } catch (IOException e) { WiseremoteMIDlet.instance.display.setCurrent (new Splash ("Error connecting..." + e.toString ())); } } public Protocol (DataInputStream i, DataOutputStream o) { iStream = i; oStream = o; } public void exec (String cmd) { sendCommand (cmd); updateState (); } void sendCommand (String cmd) { if (iStream != null) { try{ if (iStream.available () > 0) { //byte[] buf = new byte [256]; //iStream.readFully (buf, 0, iStream.available ()); } } catch (Exception e) { } } if (oStream != null) { try { oStream.write (cmd.getBytes (), 0, cmd.length ()); oStream.flush (); } catch (IOException e) { } } } void updateState () { int num = getNumber (); String res = getString (num); if (res == "OK") { return; } if (res.equals ("MENU")) { getMenu (); } if (res.equals ("KEYINPUT")) { keyScreen (); } } void keyScreen () { KeyInputScreen sc = new KeyInputScreen (); String title = getString (getNumber ()); sc.setTitle (title); int len = getNumber (); if (len > 0) { byte[] buf = getRawData (len); try { Image img = Image.createImage (buf, 0, len); sc.setImage (img); WiseremoteMIDlet.instance.display.setCurrent (sc); } catch (Exception e) { } } } void getMenu () { String title = getString (getNumber ()); Menu main = new Menu (null, title); String[] mainItems = getMenuItems (); int i; for (i = 0; i < mainItems.length; i++) { CommandEntry e = new CommandEntry (mainItems[i], mainItems[i]); main.addEntry (e); } WiseremoteMIDlet.instance.display.setCurrent (main); } protected String[] getMenuItems () { waitForData (); int num = getNumber (); String[] mainItems = new String[num]; int i; for (i = 0; i < num; i++) { waitForData (); int len = getNumber (); mainItems[i] = getString (len); } return mainItems; } public void waitForData () { try { while (iStream.available () == 0) { } } catch (Exception e) { } } public int getNumber () { try { byte[] buf = new byte[4]; iStream.readFully (buf); int x0 = buf[0] & 0xff; int x1 = buf[1] & 0xff; int x2 = buf[2] & 0xff; int x3 = buf[3] & 0xff; return x3 + x2 * 256 + x1 * 256 * 256 + x0 * 256 * 256 * 256; } catch (Exception e) { return 0; } } public String getString (int len) { byte [] buf = new byte [len]; try { iStream.readFully (buf); } catch (Exception e) { return new String (""); } return new String (buf); } public byte[] getRawData (int len) { byte[] buf = new byte [len]; try { iStream.readFully (buf); } catch (Exception e) { } return buf; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -