??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲色大成网站www永久男同,亚洲国产精品成人久久蜜臀 ,亚洲天堂男人影院http://www.tkk7.com/sinpo/category/35269.htmlArchitect myself, implement it with Java.zh-cnThu, 30 Oct 2008 14:52:55 GMTThu, 30 Oct 2008 14:52:55 GMT60JDOM常用cMl及CZ代码http://www.tkk7.com/sinpo/archive/2008/10/25/236589.html徐辛?/dc:creator>徐辛?/author>Sat, 25 Oct 2008 13:02:00 GMThttp://www.tkk7.com/sinpo/archive/2008/10/25/236589.htmlhttp://www.tkk7.com/sinpo/comments/236589.htmlhttp://www.tkk7.com/sinpo/archive/2008/10/25/236589.html#Feedback0http://www.tkk7.com/sinpo/comments/commentRss/236589.htmlhttp://www.tkk7.com/sinpo/services/trackbacks/236589.html

JDOM因其z易用易懂的API而被q泛的用。JDOM常用的核心类及它们间的关pd下图所C:

Document代表了文对象,抽象cContent表示文中的内容元素Q各U内容组成了文对象。常用的内容元素有xml元素Element、xml注释Comment、文本Text。下面以如下片段来说明各cȝ含义?/font>

<?xml version="1.0" encoding="UTF-8"?>

<customers>

<customer>

<name>徐辛?lt;/name>

<occupation>developer</occupation>

<!-- comment:following is contact info -->

<contact>

<email>sinpo.xu@hotmail.com</email>

<mobile>15029357227</mobile>

<fix-phone>02985457683</fix-phone>

</contact>

</customer>

</customers>

上述文用Document来抽象;customers为文档的根元素(root element Q,Element即一个封闭v来的元素Qelement元素可以有子元素Q如<mobile>15029357227</mobile>是一个元素,?lt;contact>...</contact>也是一个元素,甚至<customers>...</customers>也是一个大元素Q?lt;!-- ... -->代表了xml中注释,注释在JDOM中用CommentcL抽象QText代表了xml中的文本|如元素属性的倹{元素的倹{注释的内容{,父元素的Text为子元素和值组成的Ԍ使用Textcd以方便的表示一些特D字W?如:

Element element = new Element("name");

Text text = new Text("AAA.<、BBB/>.<CCC>");

element.addContent(text);

值得一提的是Element的方法addContent(Content content),因参数是抽象父类ContentQ所以可以添加Text、Element和Comment{,如果d的是Text则自动作为element的文本|如果是Element则作为element的子元素Q如果是Comment则作为element的注释,使用十分方便。元素的值如<name>徐辛?lt;/name>中的“徐辛?#8221;也是一个和元素q的Content对象QText对象Q,当用Element的getDescendants()Ҏ时将q回一个该元素所有后代的q代器,q些后代包括Element、Comment、Text{,如元?lt;contact>的后代包括email、mobile、fix-phone三个元素以及q三个元素的Text?个后代,如果计算后代时有父子嵌套则应注意Q父元素作ؓ一个后代,其嵌套的子元素作为另一个后代?/font>

刚才提到核心c都包含在org.jdom包下Qjdomq包含了org.jdom.input和org.jdom.output两个包分别来处理xml内容的输入输出。当要读取xml资源时我们通常使用input包下的SAXBuildercM输入构建dom对象Q当资源加蝲后常用的做法是在内存中缓存,q样后箋的查找修改等操作非常快。文加载后内存的中各个元素是记录有各自的位|和关系的,即保持有上下文环境的。如果想要删除一D内容(Element Comment TextQ,只用调用该内容的detachҎ卛_Q这样元素即和文脱dpMQ再Ҏ档进行遍历或者持久化到磁盘上时游ȝ元素׃可见了。Jdom的输出类包括XMLOutputter、DOMOutputter、SAXOutputter。最常用的是XMLOutputterQ通过它可以将dom对象输出到指定的输出,q且可以指定所输出xml文g的格式,比如~进的样式等。DOMOutputter输出org.w3c.dom.Document对象Q用于JDOM对象同w3c dom对象转换QSAXOutputter可以注册回调函数来处理相应的sax事g?/font>


一下示例代码实C个常用的d配置文gq且允许更改后同步到盘的操作:
<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    package sinpo.usagedemo;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.util.List;

    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;

    /**
     * d配置文gQƈ且修改后及时同步到磁?/font>
     @author 徐辛?sinpo.xu@hotmail.com) 
     * Oct 23, 2008
     */
    public class Configuration {

        private Element root = null;

        private Document dom = null;

        private static final String resourceName = "/config.xml";

        private static Configuration _INSTANCE = null;

        public static synchronized Configuration getInstance() {
            if (_INSTANCE == null) {
                _INSTANCE = new Configuration();
            }

            return _INSTANCE;
        }

        private Configuration() {
            load();
        }

        public String getConfig(String configName) {
            String configValue = null;
            Element found = findRecursively(configName, root);
            if (found != null) {
                configValue = found.getText();
            }
            return configValue;
        }

        public void updateConfig(String configName, String newValue)
                throws IOException {
            Element found = findRecursively(configName, root);
            if (found != null) {
                found.setText(newValue);
            else {
                Element configNode = new Element(configName);
                configNode.addContent(newValue);
                // also: configNode.setText(newValue);
                root.addContent(configNode);
            }
            sync();
        }

        public void deleteConfig(String configNamethrows IOException {
            Element found = findRecursively(configName, root);
            if (found != null) {
                found.detach();
            }
            sync();
        }
        
        private void load() {
            SAXBuilder builder = new SAXBuilder();
            InputStream source = getClass().getResourceAsStream(resourceName);
            try {
                dom = builder.build(source);
                root = dom.getRootElement();
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 递归查找. 在指定的父节点下查找叶子元素
        private Element findRecursively(String name, Element parent) {
            Element found = null;
            List<Element> children = parent.getChildren();
            if (children != null) {
                for (int i = 0; i < children.size(); i++) {
                    Element element = children.get(i);
                    String tmpName = element.getName();
                    if ((name.equals(tmpName)) && (!hasChild(element))) {
                        return element;
                    }
                }

                for (int i = 0; i < children.size(); i++) {
                    Element element = children.get(i);
                    if (hasChild(element)) {
                        found = findRecursively(name, element);
                        if (found != null) {
                            return found;
                        }
                    }
                }
            }

            return found;
        }

        private boolean hasChild(Element element) {
            boolean hasChild = false;
            List children = element.getChildren();
            if ((children != null&& (children.size() 0)) {
                hasChild = true;
            }

            return hasChild;
        }

        private void sync() throws IOException {
            Format format = Format.getPrettyFormat();
            XMLOutputter outputter = new XMLOutputter(format);
            File file = null;
            URL url = getClass().getResource(resourceName);
            if (url == null) {
                file = new File(resourceName);
            else {
                file = new File(url.getPath());

                OutputStream out = null;
                try {
                    out = new FileOutputStream(file);
                    outputter.output(dom, out);
                    out.close();
                    out = null;
                catch (Exception e) {
                    e.printStackTrace();
                    if (out != null) {
                        out.close();
                    }
                }
            }
        }
    }
    վ֩ģ壺 Ļպ| þþþþAVר | һѻɫƬ| ޳avƬ߹ۿ| 91avѹۿ| þþþ޹AV鶹| þþþAVۺϲҰ| һëƬaaaaaaѿ| ޾Ʒ| ޹þþþƷ | ޹Ʒһ| ߹ۿվ| ޲߹ۿ| ղƷaëƬþ| Ļ߹ۿ| èwww˳վ| ޵av| ޾Ʒ| ѹۿ.WWW| aëƬѹۿƵ| | ƵС˵ͼƬ| Ů߹| ۲ӰԺѹۿ| ò߹ۿ| ĻƵ| Ƶ߹ۿ| һƬվ߹ۿ| 99reƵ| ɫҹƵ| պŷ޹ƷĻþþ| ޵һƷƷþ| ʵŹ| þҹҹ³³ƬӰ| fc2ѹƵվ| ޹Ƶþ| 91۲˽˳ӰԺ| Ļ| ?V˾Ʒպ| һƵ| ĻȫƵ |

    ]]>
    也谈U程的休眠与{待http://www.tkk7.com/sinpo/archive/2008/10/22/236061.html徐辛?/dc:creator>徐辛?/author>Wed, 22 Oct 2008 15:26:00 GMThttp://www.tkk7.com/sinpo/archive/2008/10/22/236061.htmlhttp://www.tkk7.com/sinpo/comments/236061.htmlhttp://www.tkk7.com/sinpo/archive/2008/10/22/236061.html#Feedback0http://www.tkk7.com/sinpo/comments/commentRss/236061.htmlhttp://www.tkk7.com/sinpo/services/trackbacks/236061.html 关于U程间的交互和共享数据通常有轮询和通知机制。一下D例说明:Thread1和Thread2׃n一块数据ShareDataQThread1使用数据QThread2更新数据。当Thread1使用数据时发现数据没有更新就可以先休眠(sleepQ)Q一D|间然后再d断是否更斎ͼ如此反复直到数据可用Q这是所q的轮询机制。可以看询机刉要不断的轮询数据状态,很耗费资源Q当采用通知机制时过E是q样的,Thread1发现数据不可用就在ShareData上等待(ShareData.wait()Q,当Thread2更新数据后就通知所有在ShareData上等待的U程QShareData.notifyAll())Q这样Thread1受到通知l箋q行?/font>

    关于{待和休眠还有另一个区别就是当U程{待Ӟ该线E锁定的资源是释放掉的,q时其它U程是可以锁定这些资源的Q当U程被唤醒或者等待时限到时线E重新获取资源才能l运行;而当U程休眠时线E锁定的资源是不被释攄?/font>

    q有一点就是要在对象lock上等待时是必d要获取lock的对象锁才能q行的,卛_要cM下面的逻辑 synchronizedQlock){ lock.wait()}

    以下Z个简单的CZQ?/font>

    package  sinpo.usagedemo;

    /**
      * 该例子说明线E休眠与{待以及注意事项?/font>
     
      @author  徐辛?sinpo.xu@hotmail.com) 
      * Oct 22, 2008
      */
    public class  PendingThreadDemo  {
         public  Console console =  new  Console () ;
         private  void  writeToConsole1 () {
             synchronized ( console ){
                 try  {
                     Thread.sleep ( 1000 ) ; //NOTE:sleep时ƈ未释放console别的U程是不能锁定console?/font>
                     //TODO do things
                 catch  ( InterruptedException e ) {
                     e.printStackTrace () ;
                 }
             }
         }
        
         private  void  writeToConsole2 () {
             synchronized ( console ){
                 try  {
                     console.wait ( 1 * 1000 ) ; //NOTE:wait时别的线E是可以锁定console?/font>
                     //TODO do things
                 catch  ( InterruptedException e ) {
                     e.printStackTrace () ;
                 }
             }
         }
    }
    //控制台类
    class  Console  {
         //TODO implements me
    }


    ]]>
    Java NIO学习-UDP的例?/title><link>http://www.tkk7.com/sinpo/archive/2008/10/20/235553.html</link><dc:creator>徐辛?/dc:creator><author>徐辛?/author><pubDate>Mon, 20 Oct 2008 14:38:00 GMT</pubDate><guid>http://www.tkk7.com/sinpo/archive/2008/10/20/235553.html</guid><wfw:comment>http://www.tkk7.com/sinpo/comments/235553.html</wfw:comment><comments>http://www.tkk7.com/sinpo/archive/2008/10/20/235553.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.tkk7.com/sinpo/comments/commentRss/235553.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/sinpo/services/trackbacks/235553.html</trackback:ping><description><![CDATA[<div id="kwaymgq" class="Section0" style="layout-grid:15.6000pt;"> <p class="p0" style="margin-bottom:0pt; margin-top:0pt; "> <span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; "> <font face="宋体">q几天需要实C个底层基于UDP的协议,该协议底层用UDP传输但是h拥塞控制、超旉发、数据确认等功能又比TCP?QRUDPQReliable UDPQ。在实现协议底层的UDP服务时准备用Java的NIOQ在|上查资料都是以TCPZ讲的Q于是自qI了一下基于UDP的NIO?/font> </span> <span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; "> <o:p> </o:p> </span> </p> <p class="p0" style="margin-bottom:0pt; margin-top:0pt; "> <span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; ">NIO<font face="宋体">的思\是基于多路选择的,即由原来的每个连接都׃个线E来{待消息Q改为每个连接都在选择器上注册Q由选择器来{待。当然NIO引入了很多新的概念,如ChannelQBuffer、Charset、Selector{,使得~程更简z、更面向对象化?/font></span> <span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; "> <o:p> </o:p> </span> </p> <p class="p0" style="margin-bottom:0pt; margin-top:0pt; "> <span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; "> <font face="宋体">下面贴出用NIO API攚w成UDPCZ代码Q注意其中用Charset来编码解码的q程Q当然Charsetq支持很多其他编码不仅局限于默认~码Q以及Buffer的用?/font> </span> <span style="mso-spacerun:'yes'; font-size:10.5000pt; font-family:'宋体'; "> <o:p> </o:p> </span> </p> </div> <!-- ======================================================== --> <!-- = Java Sourcecode to HTML automatically converted code = --> <!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = --> <!-- = Further information: http://www.java2html.de = --> <div align="left" class="java"> <table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff"> <tbody> <tr> <!-- start source code --> <td nowrap="nowrap" valign="top" align="left"> <code> <font color="#7f0055"> <strong>package </strong> </font> <font color="#000000">sinpo.usagedemo;</font> <br /> <font color="#ffffff"> </font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.net.DatagramSocket;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.net.InetSocketAddress;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.net.SocketAddress;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.nio.ByteBuffer;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.nio.CharBuffer;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.nio.channels.DatagramChannel;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.nio.channels.SelectionKey;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.nio.channels.Selector;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.nio.charset.Charset;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.util.Iterator;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.util.Set;</font> <br /> <font color="#ffffff"> </font> <br /> <font color="#3f5fbf">/**</font> <br /> <font color="#ffffff"> </font> <font color="#3f5fbf">* </font> <font color="#7f9fbf">@author </font> <font color="#3f5fbf">徐辛?sinpo.xu@hotmail.com) Oct 19, 2008</font> <br /> <font color="#ffffff"> </font> <font color="#3f5fbf">*/</font> <br /> <font color="#7f0055"> <strong>public class </strong> </font> <font color="#000000">UDPServer </font> <font color="#7f0055"> <strong>extends </strong> </font> <font color="#000000">Thread </font> <font color="#000000">{</font> <br /> <font color="#ffffff"> </font> <font color="#7f0055"> <strong>public </strong> </font> <font color="#7f0055"> <strong>void </strong> </font> <font color="#000000">run</font> <font color="#000000">() {</font> <br /> <font color="#ffffff"> </font> <font color="#000000">Selector selector = </font> <font color="#7f0055"> <strong>null</strong> </font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#7f0055"> <strong>try </strong> </font> <font color="#000000">{</font> <br /> <font color="#ffffff"> </font> <font color="#000000">DatagramChannel channel = DatagramChannel.open</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">DatagramSocket socket = channel.socket</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">channel.configureBlocking</font> <font color="#000000">(</font> <font color="#7f0055"> <strong>false</strong> </font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">socket.bind</font> <font color="#000000">(</font> <font color="#7f0055"> <strong>new </strong> </font> <font color="#000000">InetSocketAddress</font> <font color="#000000">(</font> <font color="#990000">5057</font> <font color="#000000">))</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <br /> <font color="#ffffff"> </font> <font color="#000000">selector = Selector.open</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">channel.register</font> <font color="#000000">(</font> <font color="#000000">selector, SelectionKey.OP_READ</font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">} </font> <font color="#7f0055"> <strong>catch </strong> </font> <font color="#000000">(</font> <font color="#000000">Exception e</font> <font color="#000000">) {</font> <br /> <font color="#ffffff"> </font> <font color="#000000">e.printStackTrace</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">}</font> <br /> <font color="#ffffff"> </font> <br /> <font color="#ffffff"> </font> <font color="#000000">ByteBuffer byteBuffer = ByteBuffer.allocate</font> <font color="#000000">(</font> <font color="#990000">65536</font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#7f0055"> <strong>while </strong> </font> <font color="#000000">(</font> <font color="#7f0055"> <strong>true</strong> </font> <font color="#000000">) {</font> <br /> <font color="#ffffff"> </font> <font color="#7f0055"> <strong>try </strong> </font> <font color="#000000">{</font> <br /> <font color="#ffffff"> </font> <font color="#7f0055"> <strong>int </strong> </font> <font color="#000000">eventsCount = selector.select</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#7f0055"> <strong>if </strong> </font> <font color="#000000">(</font> <font color="#000000">eventsCount > </font> <font color="#990000">0</font> <font color="#000000">) {</font> <br /> <font color="#ffffff"> </font> <font color="#000000">Set selectedKeys = selector.selectedKeys</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">Iterator iterator = selectedKeys.iterator</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#7f0055"> <strong>while </strong> </font> <font color="#000000">(</font> <font color="#000000">iterator.hasNext</font> <font color="#000000">()) {</font> <br /> <font color="#ffffff"> </font> <font color="#000000">SelectionKey sk = </font> <font color="#000000">(</font> <font color="#000000">SelectionKey</font> <font color="#000000">) </font> <font color="#000000">iterator.next</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">iterator.remove</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#7f0055"> <strong>if </strong> </font> <font color="#000000">(</font> <font color="#000000">sk.isReadable</font> <font color="#000000">()) {</font> <br /> <font color="#ffffff"> </font> <font color="#000000">DatagramChannel datagramChannel = </font> <font color="#000000">(</font> <font color="#000000">DatagramChannel</font> <font color="#000000">) </font> <font color="#000000">sk</font> <br /> <font color="#ffffff"> </font> <font color="#000000">.channel</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">SocketAddress sa = datagramChannel</font> <br /> <font color="#ffffff"> </font> <font color="#000000">.receive</font> <font color="#000000">(</font> <font color="#000000">byteBuffer</font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">byteBuffer.flip</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <br /> <font color="#ffffff"> </font> <font color="#3f7f5f">// 试Q通过收到的ByteBuffer首先通过~省的编码解码成CharBuffer 再输?/font> <br /> <font color="#ffffff"> </font> <font color="#000000">CharBuffer charBuffer = Charset.defaultCharset</font> <font color="#000000">()</font> <br /> <font color="#ffffff"> </font> <font color="#000000">.decode</font> <font color="#000000">(</font> <font color="#000000">byteBuffer</font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">System.out.println</font> <font color="#000000">(</font> <font color="#2a00ff">"receive message:"</font> <br /> <font color="#ffffff"> </font> <font color="#000000">+ charBuffer.toString</font> <font color="#000000">())</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">byteBuffer.clear</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <br /> <font color="#ffffff"> </font> <font color="#000000">String echo = </font> <font color="#2a00ff">"This is the reply message from 服务器?</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">ByteBuffer buffer = Charset.defaultCharset</font> <font color="#000000">()</font> <br /> <font color="#ffffff"> </font> <font color="#000000">.encode</font> <font color="#000000">(</font> <font color="#000000">echo</font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">datagramChannel.write</font> <font color="#000000">(</font> <font color="#000000">buffer</font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">}</font> <br /> <font color="#ffffff"> </font> <font color="#000000">}</font> <br /> <font color="#ffffff"> </font> <font color="#000000">}</font> <br /> <font color="#ffffff"> </font> <font color="#000000">} </font> <font color="#7f0055"> <strong>catch </strong> </font> <font color="#000000">(</font> <font color="#000000">Exception e</font> <font color="#000000">) {</font> <br /> <font color="#ffffff"> </font> <font color="#000000">e.printStackTrace</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">}</font> <br /> <font color="#ffffff"> </font> <font color="#000000">}</font> <br /> <font color="#ffffff"> </font> <br /> <font color="#ffffff"> </font> <font color="#000000">}</font> <br /> <font color="#ffffff"> </font> <br /> <font color="#ffffff"> </font> <font color="#7f0055"> <strong>public static </strong> </font> <font color="#7f0055"> <strong>void </strong> </font> <font color="#000000">main</font> <font color="#000000">(</font> <font color="#000000">String</font> <font color="#000000">[] </font> <font color="#000000">args</font> <font color="#000000">) {</font> <br /> <font color="#ffffff"> </font> <font color="#7f0055"> <strong>new </strong> </font> <font color="#000000">UDPServer</font> <font color="#000000">()</font> <font color="#000000">.start</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff"> </font> <font color="#000000">}</font> <br /> <font color="#000000">}</font> </code> </td> <!-- end source code --> </tr> </tbody> </table> </div> <!-- = END of automatically generated HTML code = --> <!-- ======================================================== --> Client <br /> <!-- ======================================================== --> <!-- = Java Sourcecode to HTML automatically converted code = --> <!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = --> <!-- = Further information: http://www.java2html.de = --> <div align="left" class="java"> <table border="0" cellpadding="3" cellspacing="0" bgcolor="#ffffff"> <tbody> <tr> <!-- start source code --> <td nowrap="nowrap" valign="top" align="left"> <code> <font color="#7f0055"> <strong>package </strong> </font> <font color="#000000">sinpo.usagedemo;</font> <br /> <font color="#ffffff"> </font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.net.InetSocketAddress;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.net.SocketAddress;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.nio.ByteBuffer;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.nio.channels.DatagramChannel;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.nio.channels.SelectionKey;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.nio.channels.Selector;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.nio.charset.Charset;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.util.Iterator;</font> <br /> <font color="#7f0055"> <strong>import </strong> </font> <font color="#000000">java.util.Set;</font> <br /> <font color="#ffffff"> </font> <br /> <font color="#3f5fbf">/**</font> <br /> <font color="#ffffff"> </font> <font color="#3f5fbf">* </font> <font color="#7f9fbf">@author </font> <font color="#3f5fbf">徐辛?sinpo.xu@hotmail.com)</font> <br /> <font color="#ffffff"> </font> <font color="#3f5fbf">* Oct 19, 2008</font> <br /> <font color="#ffffff"> </font> <font color="#3f5fbf">*/</font> <br /> <font color="#7f0055"> <strong>public class </strong> </font> <font color="#000000">UDPClient </font> <font color="#7f0055"> <strong>extends </strong> </font> <font color="#000000">Thread </font> <font color="#000000">{</font> <br /> <font color="#ffffff">    </font> <font color="#7f0055"> <strong>public </strong> </font> <font color="#7f0055"> <strong>void </strong> </font> <font color="#000000">run</font> <font color="#000000">() {</font> <br /> <font color="#ffffff">        </font> <font color="#000000">DatagramChannel channel = </font> <font color="#7f0055"> <strong>null</strong> </font> <font color="#000000">;</font> <br /> <font color="#ffffff">        </font> <font color="#000000">Selector selector = </font> <font color="#7f0055"> <strong>null</strong> </font> <font color="#000000">;</font> <br /> <font color="#ffffff">        </font> <font color="#7f0055"> <strong>try </strong> </font> <font color="#000000">{</font> <br /> <font color="#ffffff">            </font> <font color="#000000">channel = DatagramChannel.open</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">            </font> <font color="#000000">channel.configureBlocking</font> <font color="#000000">(</font> <font color="#7f0055"> <strong>false</strong> </font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff">            </font> <font color="#000000">SocketAddress sa = </font> <font color="#7f0055"> <strong>new </strong> </font> <font color="#000000">InetSocketAddress</font> <font color="#000000">(</font> <font color="#2a00ff">"localhost"</font> <font color="#000000">, </font> <font color="#990000">5057</font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff">            </font> <font color="#000000">channel.connect</font> <font color="#000000">(</font> <font color="#000000">sa</font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff">        </font> <font color="#000000">} </font> <font color="#7f0055"> <strong>catch </strong> </font> <font color="#000000">(</font> <font color="#000000">Exception e</font> <font color="#000000">) {</font> <br /> <font color="#ffffff">            </font> <font color="#000000">e.printStackTrace</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">        </font> <font color="#000000">}</font> <br /> <font color="#ffffff"> </font> <br /> <font color="#ffffff">        </font> <font color="#7f0055"> <strong>try </strong> </font> <font color="#000000">{</font> <br /> <font color="#ffffff">            </font> <font color="#000000">selector = Selector.open</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">            </font> <font color="#000000">channel.register</font> <font color="#000000">(</font> <font color="#000000">selector, SelectionKey.OP_READ</font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff">            </font> <font color="#000000">channel.write</font> <font color="#000000">(</font> <font color="#000000">Charset.defaultCharset</font> <font color="#000000">()</font> <font color="#000000">.encode</font> <font color="#000000">(</font> <font color="#2a00ff">"Tell me your time"</font> <font color="#000000">))</font> <font color="#000000">;</font> <br /> <font color="#ffffff">        </font> <font color="#000000">} </font> <font color="#7f0055"> <strong>catch </strong> </font> <font color="#000000">(</font> <font color="#000000">Exception e</font> <font color="#000000">) {</font> <br /> <font color="#ffffff">            </font> <font color="#000000">e.printStackTrace</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">        </font> <font color="#000000">}</font> <br /> <font color="#ffffff">        </font> <br /> <font color="#ffffff">        </font> <font color="#000000">ByteBuffer byteBuffer = ByteBuffer.allocate</font> <font color="#000000">(</font> <font color="#990000">100</font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff">        </font> <font color="#7f0055"> <strong>while </strong> </font> <font color="#000000">(</font> <font color="#7f0055"> <strong>true</strong> </font> <font color="#000000">) {</font> <br /> <font color="#ffffff">            </font> <font color="#7f0055"> <strong>try </strong> </font> <font color="#000000">{</font> <br /> <font color="#ffffff">                </font> <font color="#7f0055"> <strong>int </strong> </font> <font color="#000000">eventsCount = selector.select</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">                </font> <font color="#7f0055"> <strong>if </strong> </font> <font color="#000000">(</font> <font color="#000000">eventsCount > </font> <font color="#990000">0</font> <font color="#000000">) {</font> <br /> <font color="#ffffff">                    </font> <font color="#000000">Set selectedKeys = selector.selectedKeys</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">                    </font> <font color="#000000">Iterator iterator = selectedKeys.iterator</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">                    </font> <font color="#7f0055"> <strong>while </strong> </font> <font color="#000000">(</font> <font color="#000000">iterator.hasNext</font> <font color="#000000">()) {</font> <br /> <font color="#ffffff">                        </font> <font color="#000000">SelectionKey sk = </font> <font color="#000000">(</font> <font color="#000000">SelectionKey</font> <font color="#000000">) </font> <font color="#000000">iterator.next</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">                        </font> <font color="#000000">iterator.remove</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">                        </font> <font color="#7f0055"> <strong>if </strong> </font> <font color="#000000">(</font> <font color="#000000">sk.isReadable</font> <font color="#000000">()) {</font> <br /> <font color="#ffffff">                            </font> <font color="#000000">DatagramChannel datagramChannel = </font> <font color="#000000">(</font> <font color="#000000">DatagramChannel</font> <font color="#000000">) </font> <font color="#000000">sk</font> <br /> <font color="#ffffff">                                    </font> <font color="#000000">.channel</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">                            </font> <font color="#000000">datagramChannel.read</font> <font color="#000000">(</font> <font color="#000000">byteBuffer</font> <font color="#000000">)</font> <font color="#000000">;</font> <br /> <font color="#ffffff">                            </font> <font color="#000000">byteBuffer.flip</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">                            </font> <br /> <font color="#ffffff">                            </font> <font color="#3f7f5f">//TODO 报文{化ؓRUDP消息q调用RUDP协议处理器来处理</font> <br /> <font color="#ffffff">                            </font> <br /> <font color="#ffffff">                            </font> <font color="#000000">System.out.println</font> <font color="#000000">(</font> <font color="#000000">Charset.defaultCharset</font> <font color="#000000">()</font> <font color="#000000">.decode</font> <font color="#000000">(</font> <br /> <font color="#ffffff">                                    </font> <font color="#000000">byteBuffer</font> <font color="#000000">)</font> <font color="#000000">.toString</font> <font color="#000000">())</font> <font color="#000000">;</font> <br /> <font color="#ffffff">                            </font> <font color="#000000">byteBuffer.clear</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">                            </font> <font color="#000000">datagramChannel.write</font> <font color="#000000">(</font> <font color="#000000">Charset.defaultCharset</font> <font color="#000000">()</font> <br /> <font color="#ffffff">                                    </font> <font color="#000000">.encode</font> <font color="#000000">(</font> <font color="#2a00ff">"Tell me your time"</font> <font color="#000000">))</font> <font color="#000000">;</font> <br /> <font color="#ffffff">                        </font> <font color="#000000">}</font> <br /> <font color="#ffffff">                    </font> <font color="#000000">}</font> <br /> <font color="#ffffff">                </font> <font color="#000000">}</font> <br /> <font color="#ffffff">            </font> <font color="#000000">} </font> <font color="#7f0055"> <strong>catch </strong> </font> <font color="#000000">(</font> <font color="#000000">Exception e</font> <font color="#000000">) {</font> <br /> <font color="#ffffff">                </font> <font color="#000000">e.printStackTrace</font> <font color="#000000">()</font> <font color="#000000">;</font> <br /> <font color="#ffffff">            </font> <font color="#000000">}</font> <br /> <font color="#ffffff">        </font> <font color="#000000">}</font> <br /> <font color="#ffffff"> </font> <br /> <font color="#ffffff">    </font> <font color="#000000">}</font> <br /> <font color="#000000">}</font> </code> </td> <!-- end source code --> </tr> </tbody> </table> </div> <!-- = END of automatically generated HTML code = --> <!-- ======================================================== --> <img src ="http://www.tkk7.com/sinpo/aggbug/235553.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sinpo/" target="_blank">徐辛?/a> 2008-10-20 22:38 <a href="http://www.tkk7.com/sinpo/archive/2008/10/20/235553.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>模式学习-Z接口的模?/title><link>http://www.tkk7.com/sinpo/archive/2008/10/20/235541.html</link><dc:creator>徐辛?/dc:creator><author>徐辛?/author><pubDate>Mon, 20 Oct 2008 13:48:00 GMT</pubDate><guid>http://www.tkk7.com/sinpo/archive/2008/10/20/235541.html</guid><wfw:comment>http://www.tkk7.com/sinpo/comments/235541.html</wfw:comment><comments>http://www.tkk7.com/sinpo/archive/2008/10/20/235541.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/sinpo/comments/commentRss/235541.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/sinpo/services/trackbacks/235541.html</trackback:ping><description><![CDATA[     摘要:   <a href='http://www.tkk7.com/sinpo/archive/2008/10/20/235541.html'>阅读全文</a><img src ="http://www.tkk7.com/sinpo/aggbug/235541.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sinpo/" target="_blank">徐辛?/a> 2008-10-20 21:48 <a href="http://www.tkk7.com/sinpo/archive/2008/10/20/235541.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用ClassLoaderd资源文ghttp://www.tkk7.com/sinpo/archive/2008/10/19/235328.html徐辛?/dc:creator>徐辛?/author>Sun, 19 Oct 2008 12:36:00 GMThttp://www.tkk7.com/sinpo/archive/2008/10/19/235328.htmlhttp://www.tkk7.com/sinpo/comments/235328.htmlhttp://www.tkk7.com/sinpo/archive/2008/10/19/235328.html#Feedback0http://www.tkk7.com/sinpo/comments/commentRss/235328.htmlhttp://www.tkk7.com/sinpo/services/trackbacks/235328.htmlClasscȝgetResourceAsStream(String resourcePath);
    ClassLoadercȝgetResourceAsStream(String resourcePath)
    Classcȝ该方法最l还是委zClassLoader的getResourceAsStreamҎQ但是用中发现Class#getResourceAsStream()使用的是l对路径Q以/开_Q而ClassLoader#getResourceAsStream()使用的相对\径?
    propterty文gl常攑֜c\径的根\径下Q最层包的上层目录Q如classesQ,q样加蝲property文g时就可以先用Class#getResourceAsStreamҎ获取输入源,再从该输入源load各entry?/div> code piece:
    package sinpo.usagedemo;

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.Properties;

    import junit.framework.TestCase;

    /**
     @author 徐辛?sinpo.xu@hotmail.com)
     * Oct 19, 2008
     */
    public class LoadResource extends TestCase {
        public void test() throws Exception {
            //usage 1Q?#160;use absolute path (mostly used)
            InputStream in1 = this.getClass().getResourceAsStream("/sinpo/test2.properties");
            //usage 2: use relative path
            InputStream in2 = this.getClass().getClassLoader().getResourceAsStream("sinpo/test2.properties");
            //usage 3: use system class path
            InputStream in3 = ClassLoader.getSystemResourceAsStream("system.properties");
            
            //读取的资源作ؓProperties的输入源
            Properties props = new Properties();
            props.load(in1);
            String propValue = props.getProperty("propKey");
            System.out.println(propValue);
            
            //读取的资源作ؓ文本输出
            InputStreamReader reader = new InputStreamReader(in1);
            BufferedReader bReader = new BufferedReader(reader);
            String content = bReader.readLine();
            //输出W一行内?/font>
            System.out.println(content);
            
            //TODO close them
        }
    }


    ]]>