﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-逛奔的蜗牛</title><link>http://www.cppblog.com/biao/</link><description> 我不聪明，但我会很努力</description><language>zh-cn</language><lastBuildDate>Wed, 22 Apr 2026 17:31:00 GMT</lastBuildDate><pubDate>Wed, 22 Apr 2026 17:31:00 GMT</pubDate><ttl>60</ttl><item><title>Java：static final 变量的初始化</title><link>http://www.cppblog.com/biao/archive/2014/08/18/208047.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Mon, 18 Aug 2014 00:48:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2014/08/18/208047.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/208047.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2014/08/18/208047.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/208047.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/208047.html</trackback:ping><description><![CDATA[<div>
<div style="background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all;"><span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">class</span>&nbsp;Avatar&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">final</span>&nbsp;String&nbsp;NAME;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">final</span>&nbsp;String&nbsp;INFO&nbsp;=&nbsp;"avatar";<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NAME&nbsp;=&nbsp;"Biao";&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;可以从文件读取</span><span style="color: #008000; "><br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;main(String[]&nbsp;args)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Name:&nbsp;"&nbsp;+&nbsp;Avatar.NAME&nbsp;+&nbsp;",&nbsp;info:&nbsp;"&nbsp;+&nbsp;Avatar.INFO);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
</div>
</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/208047.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2014-08-18 08:48 <a href="http://www.cppblog.com/biao/archive/2014/08/18/208047.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java：Sublimetext格式化插件</title><link>http://www.cppblog.com/biao/archive/2014/08/16/208037.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sat, 16 Aug 2014 10:36:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2014/08/16/208037.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/208037.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2014/08/16/208037.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/208037.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/208037.html</trackback:ping><description><![CDATA[<ol>
    <li>格式化 HTML：Tag</li>
    <li>格式化 JavaScript, CSS, PHP: CodeFormatter，快捷键 Ctrl + Alt + F</li>
</ol>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div>CodeFormatter也能格式化HTML，但是效果没有Tag的好</div><img src ="http://www.cppblog.com/biao/aggbug/208037.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2014-08-16 18:36 <a href="http://www.cppblog.com/biao/archive/2014/08/16/208037.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Log4J 配置</title><link>http://www.cppblog.com/biao/archive/2014/07/16/207660.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Tue, 15 Jul 2014 16:03:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2014/07/16/207660.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/207660.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2014/07/16/207660.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/207660.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/207660.html</trackback:ping><description><![CDATA[<!--?xml version="1.0" encoding="UTF-8" standalone="no"?-->
<div style="font-family: Monaco; font-size: medium; line-height: normal;"><font face="Andale Mono"># 7 levels: all&lt;debug&lt;info&lt;warn&lt;error&lt;fatal&lt;off</font>
<div><font face="Andale Mono"><br />
</font></div>
<div><font face="Andale Mono"># Root logger option</font></div>
<div><font face="Andale Mono">log4j.rootLogger=INFO, stdout, file, error</font></div>
<div><font face="Andale Mono"><br />
</font></div>
<div><font face="Andale Mono"># Redirect log messages to console</font></div>
<div><font face="Andale Mono">log4j.appender.stdout=org.apache.log4j.ConsoleAppender</font></div>
<div><font face="Andale Mono">log4j.appender.stdout.layout=org.apache.log4j.PatternLayout</font></div>
<div><font face="Andale Mono">log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] [%-5p] [%C.%M():%L] - %m%n</font></div>
<div><font face="Andale Mono"><br />
</font></div>
<div><font face="Andale Mono"># Redirect log messages to a log file, support file rolling.</font></div>
<div><font face="Andale Mono">log4j.appender.file=org.apache.log4j.DailyRollingFileAppender</font></div>
<div><font face="Andale Mono">log4j.appender.file.File=logs/app.log</font></div>
<div><font face="Andale Mono">log4j.appender.file.DatePattern='_'yyyyMMdd'.log'</font></div>
<div><font face="Andale Mono">log4j.appender.file.layout=org.apache.log4j.PatternLayout</font></div>
<div><font face="Andale Mono">log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] [%-5p] [%C.%M():%L] - %m%n</font></div>
<div><font face="Andale Mono"><br />
</font></div>
<div><font face="Andale Mono"># Redirect log messages to a log file, support file rolling.</font></div>
<div><font face="Andale Mono">log4j.appender.error=org.apache.log4j.DailyRollingFileAppender</font></div>
<div><font face="Andale Mono">log4j.appender.error.File=logs/error.log</font></div>
<div><font face="Andale Mono">log4j.appender.error.DatePattern='_'yyyyMMdd'.log'</font></div>
<div><font face="Andale Mono">log4j.appender.error.Threshold=ERROR</font></div>
<div><font face="Andale Mono">log4j.appender.error.layout=org.apache.log4j.PatternLayout</font></div>
<div><font face="Andale Mono">log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] [%-5p] [%C.%M():%L] - %m%n</font></div>
<div><br />
</div>
</div>
<div style="font-family: Monaco; font-size: medium; line-height: normal;"><hr />
<br />
</div>
<div style="font-family: Monaco; font-size: medium; line-height: normal;"><span style="font-family: 'Andale Mono';">log4j.appender.error.Threshold=ERROR&nbsp;</span><font color="#C03AFF" style="font-family: 'Andale Mono';"># 这个 Appender 输出 &gt;= ERROR 级别的信息</font><br />
</div>
<div style="font-family: Monaco; font-size: medium; line-height: normal;"><font face="Andale Mono"><br />
</font></div>
<div style="font-family: Monaco; font-size: medium; line-height: normal;"><font face="Andale Mono">Refer to:&nbsp;</font></div>
<div style="font-family: Monaco; font-size: medium; line-height: normal;"><a href="http://www.cnblogs.com/likwo/archive/2010/09/01/1814994.html">http://www.cnblogs.com/likwo/archive/2010/09/01/1814994.html</a><font face="Andale Mono"><br />
</font></div>
<div style="font-family: Monaco; font-size: medium; line-height: normal;"><a href="http://www.open-open.com/bbs/view/1318596826374">http://www.open-open.com/bbs/view/1318596826374</a></div>
<div style="font-family: Monaco; font-size: medium; line-height: normal;"><a href="http://zhw2527.iteye.com/blog/1006302">http://zhw2527.iteye.com/blog/1006302</a><br />
</div>
<div style="font-family: Monaco; font-size: medium; line-height: normal;"><br />
</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/207660.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2014-07-16 00:03 <a href="http://www.cppblog.com/biao/archive/2014/07/16/207660.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>硬币抛起来的瞬间，你就已经知道了答案</title><link>http://www.cppblog.com/biao/archive/2014/07/09/207589.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Wed, 09 Jul 2014 05:40:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2014/07/09/207589.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/207589.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2014/07/09/207589.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/207589.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/207589.html</trackback:ping><description><![CDATA[<div>硬币抛起来的瞬间，你就已经知道了答案。<br /><br />问题写到一半，其实你已经知道自己的选择。<br /><br />你呀，只是需要别人站在你旁边，为你的选择加上筹码而已。<br /><br />所以，就算我们没有站在你的那一边，你也不要改变决定，因为这种怎么选都不会错的问题，你一旦违背了自己的初心，那个选项就会成为你的执念。</div><br />From: <a href="http://www.zhihu.com/question/24413345#answer-6422725">http://www.zhihu.com/question/24413345#answer-6422725</a><img src ="http://www.cppblog.com/biao/aggbug/207589.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2014-07-09 13:40 <a href="http://www.cppblog.com/biao/archive/2014/07/09/207589.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java：FastJson 主要API与类型</title><link>http://www.cppblog.com/biao/archive/2014/03/06/206062.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 06 Mar 2014 00:01:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2014/03/06/206062.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/206062.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2014/03/06/206062.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/206062.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/206062.html</trackback:ping><description><![CDATA[<div style="margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;"><strong style="color: red;">主要的使用入口</strong></div>
<div style="margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">Fastjson API入口类是com.alibaba.fastjson.JSON，常用的序列化操作都可以在JSON类上的静态方法直接完成。</div>
<div style="margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">
<div style="padding: 4px; margin: 0px; border-left-color: #cccccc; font-size: 10pt; width: 705px; color: #000000; word-break: break-all; line-height: 16px; font-family: verdana, 宋体; background-color: #eeeeee;"><font color="#0000ff" style="padding: 0px; margin: 0px;">public</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">static</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">final</font>&nbsp;Object parse(String text);&nbsp;<font color="#008000" style="padding: 0px; margin: 0px;">// 把JSON文本parse为JSONObject或者JSONArray&nbsp;</font><br style="padding: 0px; margin: 0px;" />
<font color="#0000ff" style="padding: 0px; margin: 0px;">public</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">static</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">final</font>&nbsp;JSONObject parseObject(String text)；&nbsp;<font color="#008000" style="padding: 0px; margin: 0px;">// 把JSON文本parse成JSONObject&nbsp;&nbsp;&nbsp;&nbsp;</font><br style="padding: 0px; margin: 0px;" />
<font color="#0000ff" style="padding: 0px; margin: 0px;">public</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">static</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">final</font>&nbsp;&lt;T&gt; T parseObject(String text, Class&lt;T&gt; clazz);&nbsp;<font color="#008000" style="padding: 0px; margin: 0px;">// 把JSON文本parse为JavaBean&nbsp;</font><br style="padding: 0px; margin: 0px;" />
<font color="#0000ff" style="padding: 0px; margin: 0px;">public</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">static</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">final</font>&nbsp;JSONArray parseArray(String text);&nbsp;<font color="#008000" style="padding: 0px; margin: 0px;">// 把JSON文本parse成JSONArray&nbsp;</font><br style="padding: 0px; margin: 0px;" />
<font color="#0000ff" style="padding: 0px; margin: 0px;">public</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">static</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">final</font>&nbsp;&lt;T&gt; List&lt;T&gt; parseArray(String text, Class&lt;T&gt; clazz);&nbsp;<font color="#008000" style="padding: 0px; margin: 0px;">//把JSON文本parse成JavaBean集合&nbsp;</font><br style="padding: 0px; margin: 0px;" />
<font color="#0000ff" style="padding: 0px; margin: 0px;">public</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">static</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">final</font>&nbsp;String toJSONString(Object object);&nbsp;<font color="#008000" style="padding: 0px; margin: 0px;">// 将JavaBean序列化为JSON文本&nbsp;</font><br style="padding: 0px; margin: 0px;" />
<font color="#0000ff" style="padding: 0px; margin: 0px;">public</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">static</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">final</font>&nbsp;String toJSONString(Object object,&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">boolean</font>&nbsp;prettyFormat);&nbsp;<font color="#008000" style="padding: 0px; margin: 0px;">// 将JavaBean序列化为带格式的JSON文本&nbsp;</font><br style="padding: 0px; margin: 0px;" />
<font color="#0000ff" style="padding: 0px; margin: 0px;">public</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">static</font>&nbsp;<font color="#0000ff" style="padding: 0px; margin: 0px;">final</font>&nbsp;Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。</div>
</div>
<div style="margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp;</div>
<div style="margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;"><strong style="color: red;">有关类库的一些说明</strong></div>
<div style="margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">SerializeWriter：相当于StringBuffer</div>
<div style="margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">JSONArray：相当于List&lt;Object&gt;</div>
<div style="margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">JSONObject：相当于Map&lt;String, Object&gt;</div>
<div style="margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">&nbsp;</div>
<div style="margin: 0px; color: #555555; font-family: 宋体, 'Arial Narrow', arial, serif; line-height: 28px; background-color: #ffffff;">JSON反序列化没有真正数组，本质类型都是List&lt;Object&gt;</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/206062.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2014-03-06 08:01 <a href="http://www.cppblog.com/biao/archive/2014/03/06/206062.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java：用 Java 7 运行 IDEA 13</title><link>http://www.cppblog.com/biao/archive/2014/02/23/205916.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sun, 23 Feb 2014 15:19:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2014/02/23/205916.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/205916.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2014/02/23/205916.html#Feedback</comments><slash:comments>20</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/205916.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/205916.html</trackback:ping><description><![CDATA[<div><br />
</div>
系统: MacOS X 10.9
<div>IDEA版本: 13</div>
<div>Java版本: 7</div>
<div>运行IDEA是提示需要安装Java 6.</div>
<div><img src="http://www.cppblog.com/images/cppblog_com/biao/Need-Install-Java6.png" border="0" alt="" width="480" height="190" /><br />
</div>
<div><br />
</div>
<div>难道必须安装Java 6 才能运行IDEA? 似乎不太合理。</div>
<div>其实不必安装Java 6也能够运行IDEA.</div>
<div>打开IDEA的Info.plist (<span style="background-color: #000000; color: #f5f5f5; font-family: Monaco; font-size: 12px; line-height: normal;">/Applications/IntelliJ\ IDEA\ 13\ CE.app/Contents/Info.plist</span>)，找到</div>
<div>&nbsp;&nbsp; &nbsp; &nbsp;&lt;key&gt;JVMVersion&lt;/key&gt;</div>
<div>&nbsp; &nbsp; &nbsp; &lt;string&gt;1.6*&lt;/string&gt;</div>
<div>修改为</div>
<div>
<div>&nbsp; &nbsp; &nbsp; &lt;key&gt;JVMVersion&lt;/key&gt;</div>
<div>&nbsp; &nbsp; &nbsp; &lt;string&gt;1.7*&lt;/string&gt;</div>
</div>
<div><br />
</div>
<div>然后启动IDEA，这时就不会提示安装Java 6就能正常运行IDEA了。</div><img src ="http://www.cppblog.com/biao/aggbug/205916.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2014-02-23 23:19 <a href="http://www.cppblog.com/biao/archive/2014/02/23/205916.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java：Mavericks里安装Java 6</title><link>http://www.cppblog.com/biao/archive/2013/10/23/203892.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Wed, 23 Oct 2013 15:56:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/10/23/203892.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/203892.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/10/23/203892.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/203892.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/203892.html</trackback:ping><description><![CDATA[<a href="http://medievalcode.com/blog/2013/08/29/osx-mavericks-you-need-a-java-6-runtime-to-run-x-error-dialog-fix/">http://medievalcode.com/blog/2013/08/29/osx-mavericks-you-need-a-java-6-runtime-to-run-x-error-dialog-fix/</a>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/203892.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-10-23 23:56 <a href="http://www.cppblog.com/biao/archive/2013/10/23/203892.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt：编码范围</title><link>http://www.cppblog.com/biao/archive/2013/10/15/203755.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Tue, 15 Oct 2013 12:45:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/10/15/203755.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/203755.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/10/15/203755.html#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/203755.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/203755.html</trackback:ping><description><![CDATA[<div>编码范围</div>
<div>1. GBK (GB2312/GB18030)</div>
<div>x00-xff GBK双字节编码范围</div>
<div>x20-x7f ASCII</div>
<div>xa1-xff 中文</div>
<div>x80-xff 中文</div>
<div><br />
</div>
<div>2. UTF-8 (Unicode)</div>
<div>u4e00-u9fa5 (中文)</div>
<div>x3130-x318F (韩文)</div>
<div>xAC00-xD7A3 (韩文)</div>
<div>u0800-u4e00 (日文)</div>
<div>ps: 韩文是大于[u9fa5]的字符</div>
<div><br />
</div>
<div>有时候我们也会用到全角英文、特殊符号等</div>
<div>全角英文的UTF8是: uff21 &#8211; uff5a ，是从大写A开始到小写的z。</div>
<div>utf8中的全角数字是 uff10 &#8211; uff19 ，对应关系自然是　０ &#8211; ９ 。</div>
<div>utf8中的 uff01 到 uff09 是我们美式键盘上shift + 从1到9键上的特殊符号。</div>
<div><br />
</div>
<div>utf8中的 uff20是@, 要注意的是因为@是 uff20，所以 uff02 是双引号，</div>
<div>6的&#8230;&#8230;是两个符号的组合，</div>
<div>也就是说ff06是＆， ff09是 ）。</div>
<div><br />
</div>
<div>正则例子（使用PHP）:</div>
<div>preg_replace(&#8220;/([x80-xff])/&#8221;,&#8221;",$str); //GBK中匹配</div>
<div>preg_replace(&#8220;/([u4e00-u9fa5])/&#8221;,&#8221;",$str); //UTF8中匹配</div>
<div><br />
</div>
<div>转载地址：http://blog.csdn.net/sjw890821sjw/article/details/7004474</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/203755.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-10-15 20:45 <a href="http://www.cppblog.com/biao/archive/2013/10/15/203755.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Mac：怎么在 Mac 下把百度网盘映射到另一个文件夹</title><link>http://www.cppblog.com/biao/archive/2013/09/09/203123.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Mon, 09 Sep 2013 14:54:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/09/09/203123.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/203123.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/09/09/203123.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/203123.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/203123.html</trackback:ping><description><![CDATA[<span style="background-color: #ffffff; font-size: 14pt; color: #ff0000; ">背景：</span>@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div>例如百度云存储的路径是&nbsp;<span style="color: #000000; background-color: #ccffcc; ">/</span><span style="color: #000000; background-color: #ccffcc; ">U</span><span style="color: #000000; background-color: #ccffcc; ">sers/Biao/Baidu/百度云同步盘</span>，路径中有中文字符。</div>
<div>有一个 Qt 工程叫 QtTest 存储在&nbsp;<span style="color: #000000; background-color: #ccffcc; ">百度云同步盘</span>&nbsp;里面:&nbsp;<span style="background-color: #ccffcc; ">/Users/Biao/Baidu/百度云同步盘/QtTest</span></div>
<div>因为 Qt 工程 QtTest 的路径中有中文字符，所以 QtCreator 是不能编译这个 Qt 工程的。</div>
<div><br />
</div>
<div>于是我们就想把这个<span style="background-color: #ccffcc; ">含有中文的路径映射到一个没有中文的路径</span>，例如 <span style="color: #000000; background-color: #ccffcc; ">/Users/Biao/BaiduYun</span>，</div>
<div>修改这两个目录中的任何一个目录里的数据，另一个目录会自动同步，但是路径却不一样。</div>
<div><br />
</div>
<div>很多不支持路径中有中文的软件也可以使用此方法解决。</div>
<div><br />
</div>
<div>如果在 Linux 上则很好解决，使用 mount --bind source_directory destination_directory 则就完成了。</div>
<div>但是在 Mac 上却不支持这个命令，不过我们可以用下面两个小程序来创建和删除文件夹的硬链接。</div>
<div><br />
</div>
<div><span style="font-size: 14pt; color: #ff0000; ">一、创建文件夹的硬链接</span></div>
<div>
<div style="background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all; "><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->#include&nbsp;&lt;unistd.h&gt;<br />
#include&nbsp;&lt;stdio.h&gt;<br />
<br />
<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;Build:&nbsp;gcc&nbsp;-o&nbsp;hlink&nbsp;hlink.c&nbsp;-Wall</span><span style="color: #008000; "><br />
</span><br />
<span style="color: #0000FF; ">int</span>&nbsp;main(<span style="color: #0000FF; ">int</span>&nbsp;argc,&nbsp;<span style="color: #0000FF; ">char</span>&nbsp;*argv[])&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;printf("Usage:&nbsp;assume&nbsp;the&nbsp;program&nbsp;name&nbsp;is&nbsp;hlink\n");<br />
&nbsp;&nbsp;&nbsp;&nbsp;printf("sudo&nbsp;./hlink&nbsp;source_directory&nbsp;destination_directory\n");<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(argc&nbsp;!=&nbsp;3)&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;1;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;ret&nbsp;=&nbsp;link(argv[1],&nbsp;argv[2]);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(ret&nbsp;==&nbsp;0)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("******&nbsp;Create&nbsp;hard&nbsp;link&nbsp;successful&nbsp;******\n");<br />
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("link");<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;ret;<br />
}<br />
</div>
</div>
<div><br />
</div>
<div>1. 在终端里使用命令编译程序，生成可执行文件 hlink：</div>
<div>
<div style="background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all; "><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->gcc&nbsp;-o&nbsp;hlink&nbsp;hlink.c&nbsp;-Wall</div>
</div>
<div><br />
</div>
<div>2. 创建&nbsp;<span style="background-color: #ccffcc; ">/</span><span style="background-color: #ccffcc; ">U</span><span style="background-color: #ccffcc; ">sers/Biao/Baidu/百度云同步盘</span>&nbsp;的硬链接&nbsp;<span style="background-color: #ccffcc; ">/Users/Biao/BaiduYun</span>&nbsp;</div>
<div>(首先BaiduYun这个目录是不存在的，是在执行下面创建硬链接的命令后自动生成的)</div>
<div>&nbsp;<span style="background-color: #eeeeee; font-size: 13px; ">.</span><span style="background-color: #eeeeee; font-size: 13px; ">/</span><span style="background-color: #eeeeee; font-size: 13px; ">hlink&nbsp;</span><span style="background-color: #eeeeee; font-size: 13px; ">/</span><span style="background-color: #eeeeee; font-size: 13px; ">Users</span><span style="background-color: #eeeeee; font-size: 13px; ">/</span><span style="background-color: #eeeeee; font-size: 13px; ">Biao</span><span style="background-color: #eeeeee; font-size: 13px; ">/</span><span style="background-color: #eeeeee; font-size: 13px; ">Baidu</span><span style="background-color: #eeeeee; font-size: 13px; ">/</span><span style="background-color: #eeeeee; font-size: 13px; ">百度云同步盘&nbsp;</span><span style="background-color: #eeeeee; font-size: 13px; ">/</span><span style="background-color: #eeeeee; font-size: 13px; ">Users</span><span style="background-color: #eeeeee; font-size: 13px; ">/</span><span style="background-color: #eeeeee; font-size: 13px; ">Biao</span><span style="background-color: #eeeeee; font-size: 13px; ">/</span><span style="background-color: #eeeeee; font-size: 13px; ">BaiduYun</span></div>
<div><br />
</div>
<div><span style="font-size: 14pt; color: #ff0000; ">二、删除文件夹的硬链接</span></div>
<div>
<div style="background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all; "><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->#include&nbsp;&lt;stdio.h&gt;<br />
<br />
<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;Build:&nbsp;gcc&nbsp;-o&nbsp;hunlink&nbsp;hunlink.c</span><span style="color: #008000; "><br />
</span><br />
<span style="color: #0000FF; ">int</span>&nbsp;main(<span style="color: #0000FF; ">int</span>&nbsp;argc,&nbsp;<span style="color: #0000FF; ">char</span>&nbsp;*argv[])&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;printf("Usage:&nbsp;assume&nbsp;the&nbsp;program&nbsp;name&nbsp;is&nbsp;hunlink\n");<br />
&nbsp;&nbsp;&nbsp;&nbsp;printf("sudo&nbsp;./hunlink&nbsp;destination_directory\n");<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(argc&nbsp;!=&nbsp;2)&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;1;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;ret&nbsp;=&nbsp;unlink(argv[1]);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">if</span>&nbsp;(ret&nbsp;==&nbsp;0)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("\n");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("******&nbsp;Delete&nbsp;hard&nbsp;link&nbsp;successful&nbsp;******\n");<br />
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span style="color: #0000FF; ">else</span>&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror("unlink");<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">return</span>&nbsp;ret;<br />
}<br />
</div>
</div>
<div><br />
</div>
<div>1. 在终端里使用命令编译程序，生成可执行文件 hunlink：</div>
<div>
<div style="background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all; "><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
-->gcc&nbsp;-o&nbsp;hunlink&nbsp;hunlink.c</div>
</div>
<div><br />
</div>
<div>
<div>2. 删除&nbsp;<span style="background-color: #ccffcc; ">/</span><span style="background-color: #ccffcc; ">U</span><span style="background-color: #ccffcc; ">sers/Biao/Baidu/百度云同步盘</span>&nbsp;的硬链&nbsp;<span style="background-color: #ccffcc; ">/Users/Biao/BaiduYun</span>&nbsp;</div>
<div>（注意，这里的参数是/Users/Biao/BaiduYun，而不是源文件夹&nbsp;<span style="background-color: #ffffff; ">/</span><span style="background-color: #ffffff; ">U</span><span style="background-color: #ffffff; ">sers/Biao/Baidu/百度云同步盘</span>）</div>
<div>&nbsp;<span style="background-color: #eeeeee; font-size: 13px; ">.</span><span style="background-color: #eeeeee; font-size: 13px; ">/</span><span style="background-color: #eeeeee; font-size: 13px; ">hunlink&nbsp;</span><span style="background-color: #eeeeee; font-size: 13px; ">/</span><span style="background-color: #eeeeee; font-size: 13px; ">Users</span><span style="background-color: #eeeeee; font-size: 13px; ">/</span><span style="background-color: #eeeeee; font-size: 13px; ">Biao</span><span style="background-color: #eeeeee; font-size: 13px; ">/</span><span style="background-color: #eeeeee; font-size: 13px; ">BaiduYun</span></div>
</div><img src ="http://www.cppblog.com/biao/aggbug/203123.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-09-09 22:54 <a href="http://www.cppblog.com/biao/archive/2013/09/09/203123.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>What is the Unix command to create a hardlink to a directory in OS X?</title><link>http://www.cppblog.com/biao/archive/2013/09/09/203120.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Mon, 09 Sep 2013 13:39:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/09/09/203120.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/203120.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/09/09/203120.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/203120.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/203120.html</trackback:ping><description><![CDATA[<!--?xml version="1.0" encoding="UTF-8" standalone="no"?-->
<span  style="border-collapse: separate; font-family: Arial; line-height: normal; border-spacing: 0px; font-size: medium; ">
<div>1. 给文件夹 "你好"&nbsp;创建硬链接 "B":</div>
<div>sudo ./hlink /Users/Biao/Desktop/你好&nbsp;/Users/Biao/B</div>
<div><br />
</div>
<div>2. 删除文件夹 "你好" 的硬链接:</div>
<div>注册，删除的是目标文件夹 "B"，而不是源文件夹&nbsp;"你好"</div>
<div>sudo ./hunlink /Users/Biao/B</div>
<div>&nbsp;</div>
<div><hr />
</div>
<div>You can't do it directly in BASH then. However... I found an article here that discusses how to do it indirectly:<a href="http://www.mactech.com/articles/mactech/Vol.23/23.11/ExploringLeopardwithDTrace/index.html" rel="nofollow" style="padding: 0px; border-width: 0px; font-size: 14px; vertical-align: baseline; color: #4a6b82; text-decoration: none; cursor: pointer; ">http://www.mactech.com/articles/mactech/Vol.23/23.11/ExploringLeopardwithDTrace/index.html</a>&nbsp;by compiling a simple little C program:</div>
<div><br />
</div>
<pre style="padding: 5px; border-width: 0px; font-size: 14px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow: auto; max-height: 600px; line-height: 18px; text-align: left; "><code style="padding: 0px; border-width: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: gray; ">#include &lt;unistd.h&gt;</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: gray; ">#include &lt;stdio.h&gt;</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
int main</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">(</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">int argc</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">,</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> char </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">*</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">argv</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">[])</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">{</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
&nbsp;&nbsp; </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #00008b; ">if</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">(</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">argc </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">!=</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #800000; ">3</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">)</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #00008b; ">return</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #800000; ">1</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">;</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
&nbsp;&nbsp; int ret </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">=</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> link</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">(</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">argv</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">[</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #800000; ">1</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">],</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> argv</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">[</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #800000; ">2</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">]);</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
&nbsp;&nbsp; </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #00008b; ">if</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">(</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">ret </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">!=</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #800000; ">0</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">)</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> perror</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">(</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #800000; ">"link"</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">);</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
&nbsp;&nbsp; </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #00008b; ">return</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> ret</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">;</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">}</span></code></pre>
<p style="padding: 0px; border-width: 0px; font-size: 14px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 18px; text-align: left; ">...and build in Terminal.app with:</p>
<span  style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 18px; white-space: pre; "><code style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">$ gcc </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">-</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">o hlink hlink</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">.</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">c </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">-</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af; ">Wall</span></code></span>
<div><span  style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 18px; white-space: pre; "><code style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af; "><br />
</span></code></span></div>
<div><span  style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 18px; white-space: pre; "><code style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af; "><hr />
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">I agree that hard-linking folders/directories can cause problems if not careful, but they have a very definite advantage - Time Machine is a perfect example. Without them it simply would not be practical as the duplication of redundant versions of files would very quickly consume even the largest of disks.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">Snow Leopard can create hard links to directories as long as you follow Amit Singh's six rules:</p>
<ol style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; list-style-position: initial; list-style-image: initial; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">
    <li style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; word-wrap: break-word; ">The file system must be journaled HFS+.</li>
    <li style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; word-wrap: break-word; ">The parent directories of the source and destination must be different.</li>
    <li style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; word-wrap: break-word; ">The source&#8217;s parent must not be the root directory.</li>
    <li style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; word-wrap: break-word; ">The destination must not be in the root directory.</li>
    <li style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; word-wrap: break-word; ">The destination must not be a descendent of the source.</li>
    <li style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; word-wrap: break-word; ">The destination must not have any ancestor that&#8217;s a directory hard link.</li>
</ol>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">So it's not correct at all that Snow Leopard has lost the ability to create hard links to folders.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">I just verified that link/unlink do work on Snow Leopard - as long as you follow the six rules. I just tried it and it works fine on my Snow Leopard 10.6.6 system - tried it on the boot volume and on a separate USB external volume and it worked fine in both cases.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">Here is the "hunlink.c" program:</p>
<pre style="padding: 5px; border-width: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow: auto; max-height: 600px; color: #000000; text-align: left; "><code style="padding: 0px; border-width: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: gray; ">#include &lt;stdio.h&gt;</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
int
main</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">(</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">int argc</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">,</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> char </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">*</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">argv</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">[])</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">{</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
&nbsp;&nbsp; </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #00008b; ">if</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">(</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">argc </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">!=</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #800000; ">2</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">)</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #00008b; ">return</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #800000; ">1</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">;</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
&nbsp;&nbsp; int ret </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">=</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> unlink</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">(</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">argv</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">[</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #800000; ">1</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">]);</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
&nbsp;&nbsp; </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #00008b; ">if</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">(</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">ret </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">!=</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #800000; ">0</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">)</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">(</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #800000; ">"unlink"</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">);</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
&nbsp;&nbsp; </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #00008b; ">return</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; "> ret</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">;</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">}</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">
gcc </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">-</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">o hunlink hunlink</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">.</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">c</span></code></pre>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">So, be careful if you try it - remember to follow the rules and use hlink to create these hard links and use hunlink to remove the hard link afterwards. And don't forget to document what you've done for later on or for someone else who might need to know this.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">One other "gotcha" that I just learned about these "hard links" to folders. When you create them there is really a lot that happens "behind the curtain" of Mac OS X. One really important issue is that the folder you create the link to is really moved to a super-magical super-hidden folder called /.HFS+ Private Directory Data%000d/dir_xxx where xxx is the inode number of the "source_folder" - remember the format of the command is</p>
<pre style="padding: 5px; border-width: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow: auto; max-height: 600px; color: #000000; text-align: left; "><code style="padding: 0px; border-width: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">hlink source_folder target_folder</span></code></pre>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">So because of this, you have to be careful of not having any files open in the "source_folder" because if you do, they just got moved to the super-magical folder and you will likely have a problem if you try and save any changes to those files that were open in the "source_folder". This happened to me a couple of times until it dawned on me what was happening and the solution is pretty simple. I noticed that you couldn't do a "ls -la" command any longer without getting funny errors for all the folders/directories that were in the original "source_folder" but you could do a "ls" command and all looked well.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">If you run "Verify disk" in the "Disk Utility" program, you will notice that it probably complains and gives a "Volume bitmap needs minor repair for orphaned blocks" which is what just happened with the creation of the super-magical folder and the movement of the "source_folder" to it.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">If you do find yourself in this situation with "orphaned blocks", first save the changed files to some other temporary location not in the volume containing the "source_folder" tree, then use "Disk Utility" to unmount and remount the volume that contains the "source_folder" or just restart the computer. Then copy the files you saved to the temporary locations back to their original locations and you should be back in business. This is what worked for me, so can't guarantee this will work for you too. So it might be a good idea to try this out on a volume you have a good backup of just in case.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">It seems so very weird that all this overhead occurs just for the simple task of creating a hard link to a folder. Does anyone have any idea why Mac OS X goes to all this effort for this hard link creation to folders? Does it have something to do with the fact that this is a "journaled" file system?</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">I discovered the info about the super-magical, super-hidden location by reading Amit Singh's explanation of his "hfsdebug" utility. If you want more details see his web site at&nbsp;<a href="http://osxbook.com/blog/2008/11/09/hfsdebug-40-and-new-hfs-features/" style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #4a6b82; text-decoration: none; cursor: pointer; ">Amit Singh's hfsdebug utility</a>. It's a very interesting piece of software and will tell you lots of details about HFS+ file systems. It's free and I encourage you to download it and try it out. It's no longer supported but it still works on both Snow Leopard and Leopard - basically any HFS+ supported system. You can't really do any harm with it as it's a "read-only" tool - so it's great to use to look at some details of the filesystem.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">One more issue about these "hard links to folders" - once you create one and the super-magical super-secret-hidden folder gets created, it's there for good. Even if you unlink the folder that caused it to be created in the first place, this magic folder stays around. Not sure why, but it definitely does. You can use "hfsdebug" to find this out if you wish to try it out. You can also use "hfsdebug" to find out how many of these "hard links to folders" exist on a drive. For these details refer to Amit's article on the "hfsdebug" utility.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">He also has another newer utility that's supported but costs. It's called fileXray and costs $79 for one person on any number of computers in the same household for a personal non-business type license. It has an extensive 173-page User Guide that you can download to see what it can do before you purchase. Unfortunately there is no trial version, so read the manual and check out the web site for more details to see if it can help you out of a jam. Learn all the details about it at their web site - see<a href="http://filexray.com/" style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #4a6b82; text-decoration: none; cursor: pointer; ">fileXray web site</a>&nbsp;for more info.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">There are a couple of issues you should be aware of when using these hard links to folders. If the volume that they are created on is mounted to a remote client, there can be significant problems, depending on how they are mounted. If you use AFP to mount the volume to a remote client, there are big problems as any folder that currently has a hard link to it or has ever had one but later removed, will be unable to be used as all the lower level folders (but not files) will be inaccessible from either the Finder or a Terminal window. If you try to do a simple "ls -lR" command, it will fail and give you "ls: xxx: No such file or directory" error messages for all lower level folders. If you use a Finder window to traverse the directory tree of the remote volume, the folders that are in the folder that had or has a hard link to it will simply disappear without any error when you first click on the folder name.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">These problems don't appear to occur (except for the error message) if you use NFS to mount the remote client (and assuming you had a NFS server on the system that has the volume as a local HFS+ filesystem). Details on how to use NFS to mount volumes are not provided here. I used a nice program from Dr. Marcel Bresink called "NFS Manager" to help with the NFS mounts on the server and client. You can get it from his web site - just search for "Bresink NFS Manager" in your favorite search engine, but he has a free trial version so you can try before you buy. It's not that big a deal if you want to learn how to do the NFS mounts, but the "NFS Manager" makes it pretty easy to set things up and to tweak all the different settings to help optimize it. He has several other neat Mac OS X utilities too that are very reasonably priced - one called "Hardware Monitor" that lets you monitor and graph all kinds of things like power usage, temperature of CPU, speed of fans and many many other variables for both the local and remote Mac systems over extended periods of time (from minutes to days). Definitely worth checking out if you are into handy utilities.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">One thing I did notice is that NFS file transfers were about 20% slower than doing them via AFP, but your "mileage may vary", so no guarantees one way or the other, but I would rather have something that works even if I have to pay a 20% performance hit as compared to having nothing work at all.</p>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">Apple is aware of the problems with hard links and remote AFP filesystems, and they refer to it as an "implentation limitation" of the AFP client - I prefer to call it what it really appears to me to be - A BUG!!! I can only hope the next release of Mac OS X fixes the problem, as I really like having the ability to use hard links to folders when it makes sense.</p>
</span></code></span><span  style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 18px; white-space: pre; "><code style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af; ">
<div><br />
</div>
<div>These notes are my own personal opinion and I don't make any warranty about their correctness so use them at your own risk. Have a good backup before you play around with these "hard links to folders" just in case something unforeseen happens. But I hope you have fun if you do decide to look a bit more into this interesting aspect of Mac OS X.</div>
<div><br />
</div>
<div>
<p style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #ffffff; clear: both; word-wrap: break-word; color: #000000; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; text-align: left; white-space: normal; ">Another solution is to use bindfs&nbsp;<a href="https://code.google.com/p/bindfs/" rel="nofollow" style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #4a6b82; text-decoration: none; cursor: pointer; ">https://code.google.com/p/bindfs/</a>&nbsp;which is installable via port:</p>
<pre style="padding: 5px; border-width: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow: auto; max-height: 600px; color: #000000; text-align: left; "><code style="padding: 0px; border-width: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">sudo port install bindfs
</span></code></pre>
</div>
</span></code></span><span  style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 18px; white-space: pre; "><code style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af; ">
<div style="display: inline !important; ">
<pre style="padding: 5px; border-width: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow: auto; max-height: 600px; color: #000000; text-align: left; display: inline !important; "><code style="padding: 0px; border-width: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">sudo bindfs </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">~/</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">source_dir </span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">~/</span><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; ">target_dir</span></code></pre>
</div>
</span></code></span><span  style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 18px; white-space: pre; "><code style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af; ">
<div><br />
</div>
<div><br />
</div>
<div>From: </div>
</span></code></span><span  style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; font-size: 14px; line-height: 18px; white-space: pre; "><code style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span style="padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af; ">
<div style="display: inline !important; "><a href="http://stackoverflow.com/questions/80875/what-is-the-unix-command-to-create-a-hardlink-to-a-directory-in-os-x">http://stackoverflow.com/questions/80875/what-is-the-unix-command-to-create-a-hardlink-to-a-directory-in-os-x</a></div>
</span></code></span></div>
</span>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/203120.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-09-09 21:39 <a href="http://www.cppblog.com/biao/archive/2013/09/09/203120.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java：内存映射</title><link>http://www.cppblog.com/biao/archive/2013/09/03/202983.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Tue, 03 Sep 2013 03:23:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/09/03/202983.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202983.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/09/03/202983.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202983.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202983.html</trackback:ping><description><![CDATA[<div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000; ">From: <a href="http://bbs.csdn.net/topics/390577067">http://bbs.csdn.net/topics/390577067</a><br />/**</span><span style="color: #008000; "><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;内存映射<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;</span><span style="color: #808080; ">@throws</span><span style="color: #008000; ">&nbsp;IOException<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">*/</span><span style="color: #000000; "><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">static</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;mapChannel()&nbsp;</span><span style="color: #0000FF; ">throws</span><span style="color: #000000; ">&nbsp;IOException&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">long</span><span style="color: #000000; ">&nbsp;t1&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;System.currentTimeMillis();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileInputStream&nbsp;in&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;FileInputStream(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">d:/1.txt</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">long</span><span style="color: #000000; ">&nbsp;size&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;in.available();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RandomAccessFile&nbsp;out&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;RandomAccessFile(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">d:/2.txt</span><span style="color: #000000; ">"</span><span style="color: #000000; ">,&nbsp;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">rw</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileChannel&nbsp;inc&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;in.getChannel();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MappedByteBuffer&nbsp;bf&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;inc.map(FileChannel.MapMode.READ_ONLY,&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">,&nbsp;size);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileChannel&nbsp;outc&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;out.getChannel();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MappedByteBuffer&nbsp;outbf&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;outc.map(FileChannel.MapMode.READ_WRITE,&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">,&nbsp;size);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outbf.put(bf);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inc.close();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outc.close();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;in.close();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.close();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">long</span><span style="color: #000000; ">&nbsp;t2&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;System.currentTimeMillis();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(t2&nbsp;</span><span style="color: #000000; ">-</span><span style="color: #000000; ">&nbsp;t1);<br />&nbsp;&nbsp;&nbsp;&nbsp;}</span></div><img src ="http://www.cppblog.com/biao/aggbug/202983.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-09-03 11:23 <a href="http://www.cppblog.com/biao/archive/2013/09/03/202983.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt：Windows Qt 去掉选中的虚线框</title><link>http://www.cppblog.com/biao/archive/2013/09/03/202977.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Mon, 02 Sep 2013 19:19:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/09/03/202977.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202977.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/09/03/202977.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202977.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202977.html</trackback:ping><description><![CDATA[<span  style="border-collapse: separate; font-family: Helvetica; line-height: normal; border-spacing: 0px; font-size: medium; ">class myStyle: public QProxyStyle<br />
{<br />
public:<br />
&nbsp; &nbsp;myStyle(QStyle *baseStyle)<br />
&nbsp; &nbsp; &nbsp; &nbsp;: QProxyStyle(baseStyle)<br />
&nbsp; &nbsp;{<br />
&nbsp; &nbsp;}<br />
<br />
&nbsp; &nbsp;void drawPrimitive(PrimitiveElement element,<br />
&nbsp; &nbsp; &nbsp; &nbsp;const QStyleOption *option,<br />
&nbsp; &nbsp; &nbsp; &nbsp;QPainter *painter,<br />
&nbsp; &nbsp; &nbsp; &nbsp;const QWidget *widget = 0) const<br />
&nbsp; &nbsp;{<br />
&nbsp; &nbsp; &nbsp; &nbsp;if (element == QStyle::PE_FrameFocusRect)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;<br />
&nbsp; &nbsp; &nbsp; &nbsp;QProxyStyle::drawPrimitive(element, option, painter, widget);<br />
&nbsp; &nbsp;}<br />
};&nbsp;</span>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/202977.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-09-03 03:19 <a href="http://www.cppblog.com/biao/archive/2013/09/03/202977.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt：定制qDebug等输出的格式</title><link>http://www.cppblog.com/biao/archive/2013/09/02/202956.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Mon, 02 Sep 2013 09:48:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/09/02/202956.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202956.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/09/02/202956.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202956.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202956.html</trackback:ping><description><![CDATA[<br />
<pre style="margin-top: 0px; margin-bottom: 0px; "><hr />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style="color: #808000; font-size: 12pt; ">void</span><span style="color: #c0c0c0; "> </span><span style="font-size: 12pt; ">testMessagePattern()</span><span style="color: #c0c0c0; "> </span><span style="font-size: 12pt; ">{</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span><span style="color: #800080; font-size: 12pt; ">QString</span><span style=" color:#c0c0c0;"> </span><span style="font-size: 12pt; ">QT_MESSAGE_PATTERN=</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">            </span><span style="color: #008000; font-size: 12pt; ">"[%{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}]"</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">            </span><span style="color: #008000; font-size: 12pt; ">":</span><span style=" color:#c0c0c0;"> </span><span style="color: #008000; font-size: 12pt; ">%{file}:%{line}</span><span style=" color:#c0c0c0;"> </span><span style="color: #008000; font-size: 12pt; ">-</span><span style=" color:#c0c0c0;"> </span><span style="color: #008000; font-size: 12pt; ">%{message}"</span><span style="font-size: 12pt; ">;</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span><span style="font-size: 12pt; ">qSetMessagePattern(QT_MESSAGE_PATTERN);</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span><span style="color: #000080; font-size: 12pt; ">qDebug</span><span style="font-size: 12pt; ">()</span><span style=" color:#c0c0c0;"> </span><span style="font-size: 12pt; ">&lt;&lt;</span><span style=" color:#c0c0c0;"> </span><span style="color: #008000; font-size: 12pt; ">"Hello</span><span style=" color:#c0c0c0;"> </span><span style="color: #008000; font-size: 12pt; ">Pattern"</span><span style="font-size: 12pt; ">;</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span><span style="color: #000080; font-size: 12pt; ">qDebug</span><span style="font-size: 12pt; ">()</span><span style=" color:#c0c0c0;"> </span><span style="font-size: 12pt; ">&lt;&lt;</span><span style=" color:#c0c0c0;"> </span><span style="color: #800080; font-size: 12pt; ">QString</span><span style="font-size: 12pt; ">(</span><span style="color: #008000; font-size: 12pt; ">"黄彪"</span><span style="font-size: 12pt; ">).toUtf8().toHex();</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style="font-size: 12pt; ">}</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><hr />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; ">
<p style="margin: 0px; "><span style="font-family: Monaco; font-size: 10pt; color: #aa0000; ">[D]: /Users/Biao/Dropbox/workspace/Qt/Test/main.cpp:26 - Hello Pattern </span></p>
<p style="margin: 0px; "><span style="font-family: Monaco; font-size: 10pt; color: #aa0000; ">[D]: /Users/Biao/Dropbox/workspace/Qt/Test/main.cpp:28 - "e9bb84e5bdaa" </span></p>
<hr />
<h3 class="fn" style="font-size: 14px; line-height: 16px; font-family: Arial; margin: 45px 0px 0px; border: 1px solid #e6e6e6; border-top-left-radius: 7px; border-top-right-radius: 7px; border-bottom-right-radius: 7px; border-bottom-left-radius: 7px; background-color: #f6f6f6; word-spacing: 3px; padding: 5px; max-width: 75%; color: #313131; text-align: justify; white-space: normal; "><span class="type" style="color: #808080;">void</span>&nbsp;<span class="name" style="max-width: 75%; font-weight: 100; color: #1a1a1a;">qSetMessagePattern</span>(const&nbsp;<span class="type" style="color: #808080;"><a href="qthelp://org.qt-project.qtcore.511/qtcore/qstring.html" style="color: #007330; text-decoration: none; text-align: left;">QString</a></span>&nbsp;&amp;<em>&nbsp;pattern</em>)</h3>
<p style="line-height: 20px; color: #313131; font-family: Arial, Helvetica; text-align: justify; white-space: normal; ">Changes the output of the default message handler.</p>
<p style="line-height: 20px; color: #313131; font-family: Arial, Helvetica; text-align: justify; white-space: normal; ">Allows to tweak the output of&nbsp;<a href="qthelp://org.qt-project.qtcore.511/qtcore/qtglobal.html#qDebug" style="color: #007330; text-decoration: none; text-align: left;">qDebug</a>(),&nbsp;<a href="qthelp://org.qt-project.qtcore.511/qtcore/qtglobal.html#qWarning" style="color: #007330; text-decoration: none; text-align: left;">qWarning</a>(),&nbsp;<a href="qthelp://org.qt-project.qtcore.511/qtcore/qtglobal.html#qCritical" style="color: #007330; text-decoration: none; text-align: left;">qCritical</a>() and&nbsp;<a href="qthelp://org.qt-project.qtcore.511/qtcore/qtglobal.html#qFatal" style="color: #007330; text-decoration: none; text-align: left;">qFatal</a>().</p>
<p style="line-height: 20px; color: #313131; font-family: Arial, Helvetica; text-align: justify; white-space: normal; ">Following placeholders are supported:</p>
<table class="generic" style="border-top-left-radius: 7px; border-top-right-radius: 7px; border-bottom-right-radius: 7px; border-bottom-left-radius: 7px; background-color: #f6f6f6; margin-bottom: 25px; margin-left: 15px; font-size: 12px; line-height: 1.2; color: #313131; font-family: Arial, Helvetica; text-align: justify; ">
    <thead style="text-align: left; padding-left: 20px; background-color: #e1e0e0; border-left-style: none; border-right-style: none;">
        <tr class="qt-style">
            <th style="padding: 5px 10px; ">Placeholder</th><th style="padding: 5px 10px; ">Description</th>
        </tr>
    </thead>
    <tbody>
        <tr valign="top" class="odd" style="color: #66666e; ">
            <td style="padding: 3px 15px 3px 20px; "><tt style="text-align: left;">%{appname}</tt></td>
            <td style="padding: 3px 15px 3px 20px; "><a href="qthelp://org.qt-project.qtcore.511/qtcore/qcoreapplication.html#applicationName-prop" style="color: #007330; text-decoration: none; text-align: left;">QCoreApplication::applicationName</a>()</td>
        </tr>
        <tr valign="top" class="even" style="background-color: white; color: #66666e;">
            <td style="padding: 3px 15px 3px 20px; "><tt style="text-align: left;">%{file}</tt></td>
            <td style="padding: 3px 15px 3px 20px; ">Path to source file</td>
        </tr>
        <tr valign="top" class="odd" style="color: #66666e; ">
            <td style="padding: 3px 15px 3px 20px; "><tt style="text-align: left;">%{function}</tt></td>
            <td style="padding: 3px 15px 3px 20px; ">Function</td>
        </tr>
        <tr valign="top" class="even" style="background-color: white; color: #66666e;">
            <td style="padding: 3px 15px 3px 20px; "><tt style="text-align: left;">%{line}</tt></td>
            <td style="padding: 3px 15px 3px 20px; ">Line in source file</td>
        </tr>
        <tr valign="top" class="odd" style="color: #66666e; ">
            <td style="padding: 3px 15px 3px 20px; "><tt style="text-align: left;">%{message}</tt></td>
            <td style="padding: 3px 15px 3px 20px; ">The actual message</td>
        </tr>
        <tr valign="top" class="even" style="background-color: white; color: #66666e;">
            <td style="padding: 3px 15px 3px 20px; "><tt style="text-align: left;">%{pid}</tt></td>
            <td style="padding: 3px 15px 3px 20px; "><a href="qthelp://org.qt-project.qtcore.511/qtcore/qcoreapplication.html#applicationPid" style="color: #007330; text-decoration: none; text-align: left;">QCoreApplication::applicationPid</a>()</td>
        </tr>
        <tr valign="top" class="odd" style="color: #66666e; ">
            <td style="padding: 3px 15px 3px 20px; "><tt style="text-align: left;">%{threadid}</tt></td>
            <td style="padding: 3px 15px 3px 20px; ">ID of current thread</td>
        </tr>
        <tr valign="top" class="even" style="background-color: white; color: #66666e;">
            <td style="padding: 3px 15px 3px 20px; "><tt style="text-align: left;">%{type}</tt></td>
            <td style="padding: 3px 15px 3px 20px; ">"debug", "warning", "critical" or "fatal"</td>
        </tr>
    </tbody>
</table>
<p style="line-height: 20px; color: #313131; font-family: Arial, Helvetica; text-align: justify; white-space: normal; ">You can also use conditionals on the type of the message using&nbsp;<tt style="text-align: left;">%{if-debug}</tt>,&nbsp;<tt style="text-align: left;">%{if-warning}</tt>,&nbsp;<tt style="text-align: left;">%{if-critical}</tt>&nbsp;or&nbsp;<tt style="text-align: left;">%{if-fatal}</tt>&nbsp;followed by an&nbsp;<tt style="text-align: left;">%{endif}</tt>. What is inside the<tt style="text-align: left;">%{if-*}</tt>&nbsp;and&nbsp;<tt style="text-align: left;">%{endif}</tt>&nbsp;will only be printed if the type matches.</p>
<p style="line-height: 20px; color: #313131; font-family: Arial, Helvetica; text-align: justify; white-space: normal; ">Example:</p>
<pre class="cpp" style="border-top-left-radius: 7px; border-top-right-radius: 7px; border-bottom-right-radius: 7px; border-bottom-left-radius: 7px; background-color: #f6f6f6; border: 1px solid #e6e6e6; border-collapse: separate; margin: 10px; font-size: 12px; line-height: 1.2; overflow: auto; padding: 20px; color: #313131; text-align: justify; ">QT_MESSAGE_PATTERN<span class="operator">=</span><span class="string">"[%{if-debug}D%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{file}:%{line} - %{message}"</span></pre>
<p style="line-height: 20px; color: #313131; font-family: Arial, Helvetica; text-align: justify; white-space: normal; ">The default&nbsp;<em>pattern</em>&nbsp;is "%{message}".</p>
<p style="line-height: 20px; color: #313131; font-family: Arial, Helvetica; text-align: justify; white-space: normal; ">The&nbsp;<em>pattern</em>&nbsp;can also be changed at runtime by setting the QT_MESSAGE_PATTERN environment variable; if both qSetMessagePattern() is called and QT_MESSAGE_PATTERN is set, the environment variable takes precedence.</p>
<p style="line-height: 20px; color: #313131; font-family: Arial, Helvetica; text-align: justify; white-space: normal; ">qSetMessagePattern() has no effect if a custom message handler is installed.</p>
<p style="line-height: 20px; color: #313131; font-family: Arial, Helvetica; text-align: justify; white-space: normal; ">This function was introduced in QtCore 5.0.</p>
<p style="line-height: 20px; color: #313131; font-family: Arial, Helvetica; text-align: justify; white-space: normal; "><strong>See also&nbsp;</strong><a href="qthelp://org.qt-project.qtcore.511/qtcore/qtglobal.html#qInstallMessageHandler" style="color: #007330; text-decoration: none; text-align: left;">qInstallMessageHandler</a>() and Debugging Techniques.</p>
<br />
</pre><img src ="http://www.cppblog.com/biao/aggbug/202956.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-09-02 17:48 <a href="http://www.cppblog.com/biao/archive/2013/09/02/202956.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt：给QWidget添加shadow效果</title><link>http://www.cppblog.com/biao/archive/2013/09/02/202949.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Mon, 02 Sep 2013 07:19:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/09/02/202949.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202949.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/09/02/202949.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202949.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202949.html</trackback:ping><description><![CDATA[@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div><hr />
</div>
<img src="http://www.cppblog.com/images/cppblog_com/biao/QGraphicsDropShadowEffect.png" alt="" />
<div><hr />
<pre style="margin-top: 0px; margin-bottom: 0px; "><!--StartFragment--><span style=" color:#800080;">Widget</span>::Widget(<span style=" color:#800080;">QWidget</span><span style=" color:#c0c0c0;"> </span>*parent)<span style=" color:#c0c0c0;"> </span>:<span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QWidget</span>(parent),</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span><span style=" color:#800000;">ui</span>(<span style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">Ui</span>::<span style=" color:#800080;">Widget</span>),<span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">data</span>(<span style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">WidgetPrivate</span>)<span style=" color:#c0c0c0;"> </span>{</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span><span style=" color:#800000;">ui</span>-&gt;setupUi(<span style=" color:#808000;">this</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span><span style=" color:#800080;">QGraphicsBlurEffect</span><span style=" color:#c0c0c0;"> </span>*effect<span style=" color:#c0c0c0;"> </span>=<span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QGraphicsBlurEffect</span>(<span style=" color:#808000;">this</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span>effect-&gt;setBlurRadius(<span style=" color:#000080;">1.5</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span><span style=" color:#800080;">QGraphicsDropShadowEffect</span><span style=" color:#c0c0c0;"> </span>*shadowEffect<span style=" color:#c0c0c0;"> </span>=<span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">new</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QGraphicsDropShadowEffect</span>(<span style=" color:#808000;">this</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span>shadowEffect-&gt;setBlurRadius(<span style=" color:#000080;">5</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span>shadowEffect-&gt;setXOffset(<span style=" color:#000080;">2</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span>shadowEffect-&gt;setYOffset(<span style=" color:#000080;">2</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span><span style=" color:#800000;">ui</span>-&gt;<span style=" color:#800000;">checkBox</span>-&gt;setGraphicsEffect(shadowEffect);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; ">}<!--EndFragment--></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><hr />
</pre>
</div><img src ="http://www.cppblog.com/biao/aggbug/202949.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-09-02 15:19 <a href="http://www.cppblog.com/biao/archive/2013/09/02/202949.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt：Q_DECLARE_METATYPE和qRegisterMetaType</title><link>http://www.cppblog.com/biao/archive/2013/09/02/202948.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Mon, 02 Sep 2013 07:16:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/09/02/202948.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202948.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/09/02/202948.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202948.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202948.html</trackback:ping><description><![CDATA[@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#008000;">Adding</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">a</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">Q_DECLARE_METATYPE()</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">makes</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">the</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">type</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">known</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">to</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">all</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">template</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">based</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">functions,</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">including</span><span style=" color:#c0c0c0;"> </span><span style="color: red; ">QVariant.</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#008000;">Note</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">that</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">if</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">you</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">intend</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">to</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">use</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">the</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">type</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">in</span><span style=" color:#c0c0c0;"> </span><span style="color: red; ">queued</span><span style=" color:#c0c0c0;"> </span><span style="color: red; ">signal</span><span style=" color:#c0c0c0;"> </span><span style="color: red; ">and</span><span style=" color:#c0c0c0;"> </span><span style="color: red; ">slot</span><span style=" color:#c0c0c0;"> </span><span style="color: red; ">connections</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">or</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">in</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">QObject's</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">property</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">system,</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#008000;">you</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">also</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">have</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">to</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">call</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">qRegisterMetaType()</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">since</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">the</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">names</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">are</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">resolved</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">at</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">runtime.</span><!--EndFragment--></pre>
</div>
<div><hr />
</div>
<div>
<pre style="margin-top: 0px; margin-bottom: 0px; "><!--StartFragment--><span style=" color:#808000;">class</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">BH</span><span style=" color:#c0c0c0;"> </span>{</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#808000;">public</span>:</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span><span style=" color:#800080;">BH</span>()<span style=" color:#c0c0c0;"> </span>{</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">        </span><span style=" color:#800000;">value</span><span style=" color:#c0c0c0;"> </span>=<span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">10</span>;</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span>}</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#c0c0c0;">    </span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span><span style=" color:#800000;">value</span>;</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; ">};</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#000080;">Q_DECLARE_METATYPE</span>(BH)<!--EndFragment--></pre>
</div>
<div></div>
<div><hr />
</div>
<div><span style="color: #800080; ">BH</span><span style="color: #c0c0c0; "> </span>bh;</div>
<div>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#800080;">QVariant</span><span style=" color:#c0c0c0;"> </span>var<span style=" color:#c0c0c0;"> </span>=<span style=" color:#c0c0c0;"> </span><span style=" color:#800080;">QVariant</span>::fromValue(bh);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#000080;">qDebug</span>()<span style=" color:#c0c0c0;"> </span>&lt;&lt;<span style=" color:#c0c0c0;"> </span>var.canConvert&lt;<span style=" color:#800080;">BH</span>&gt;();</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#800080;">BH</span><span style=" color:#c0c0c0;"> </span>bh2<span style=" color:#c0c0c0;"> </span>=<span style=" color:#c0c0c0;"> </span>var.value&lt;<span style=" color:#800080;">BH</span>&gt;();</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><span style=" color:#000080;">qDebug</span>()<span style=" color:#c0c0c0;"> </span>&lt;&lt;<span style=" color:#c0c0c0;"> </span>bh2.<span style=" color:#800000;">value</span>;<!--EndFragment--></pre>
<pre style="margin-top: 0px; margin-bottom: 0px; "><br />
</pre>
</div><img src ="http://www.cppblog.com/biao/aggbug/202948.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-09-02 15:16 <a href="http://www.cppblog.com/biao/archive/2013/09/02/202948.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java：Java NIO之Buffer基础</title><link>http://www.cppblog.com/biao/archive/2013/08/07/202395.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Wed, 07 Aug 2013 14:04:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/08/07/202395.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202395.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/08/07/202395.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202395.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202395.html</trackback:ping><description><![CDATA[<div><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">From:&nbsp;<a href="http://hhyyllgg.iteye.com/blog/1921386">http://hhyyllgg.iteye.com/blog/1921386</a></span></div>
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">Java NIO 由以下几个核心部分组成：&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<ul style="margin: 0px 0px 1.5em; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">
    <li style="margin: 0px 0px 0.25em 30px; padding: 0px; ">Channels</li>
    <li style="margin: 0px 0px 0.25em 30px; padding: 0px; ">Buffers</li>
    <li style="margin: 0px 0px 0.25em 30px; padding: 0px; ">Selectors</li>
</ul>
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">虽然Java NIO 中除此之外还有很多类和组件，但在我看来，Channel，Buffer 和 Selector 构成了核心的API。&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">Buffers 缓冲区，以及缓冲区如何工作，是所有 I/O 的基础。所谓&#8220;输入／输出&#8221;讲的无非就是把数据移 进或移出缓冲区。&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<strong style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><span style="font-size: large; ">概念</span></strong><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">：实际上缓冲区就是一个包含在对象里的基本数据元素数组&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; font-size: large; "><strong>属性</strong></span><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">：所有的缓冲区都具有四个属性来提供关于其所包含的数据元素的信息。它们是：&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<ul style="margin: 0px 0px 1.5em; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">
    <li style="margin: 0px 0px 0.25em 30px; padding: 0px; "><strong>容量（Capacity）</strong>&nbsp;缓冲区能够容纳的数据元素的最大数量。这一容量在缓冲区创建时被设定，并且永远不能被改变。</li>
    <li style="margin: 0px 0px 0.25em 30px; padding: 0px; "><strong>上界（Limit）</strong>&nbsp;缓冲区的第一个不能被读或写的元素。或者说，缓冲区中现存元素的计数。</li>
    <li style="margin: 0px 0px 0.25em 30px; padding: 0px; "><strong>位置（Position）</strong>&nbsp;下一个要被读或写的元素的索引。位置会自动由相应的get( )和put( )函数更新。</li>
    <li style="margin: 0px 0px 0.25em 30px; padding: 0px; "><strong>标记（Mark）</strong>&nbsp;一个备忘位置。调用mark( )来设定mark = postion。调用reset( )设定position = mark。标记在设定前是未定义的(undefined)。</li>
</ul>
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">看下图 新创建一个ByteBuffer&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<img src="http://dl2.iteye.com/upload/attachment/0087/9399/c806cb41-3fea-3bdd-8cf9-19316f11029b.bmp" style="border: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "  alt="" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">我们通过 ByteBuffer.allocate(11) 方法创建一个 10 个 byte 的数组缓冲区，初始状态如上图所示，position 的位置为 0，capacity 和 limit 默认都是数组长度。当我们写入 5 个字节时位置变化如下图所示：&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<img src="http://dl2.iteye.com/upload/attachment/0087/9401/6a9dfbcb-7ecb-338b-aec0-1a66d46c1fdb.bmp" style="border: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "  alt="" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">这时如果我们想在不丢失位置的情况下进行一些修改，比如说将hello更改为MelloW&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">可以这样实现&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">buffer.put(0,(byte)'M').put((byte)'w');&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">修改后的Buffer图像如下图&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<img src="http://dl2.iteye.com/upload/attachment/0087/9403/0d043329-2d17-3add-9746-75f68fec8c53.bmp" style="border: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "  alt="" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">如果我们想将缓冲区的 5 个字节数据写入 Channel 通信信道，我们需要调用 byteBuffer.flip() 方法，数组的状态又发生如下变化&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<img src="http://dl2.iteye.com/upload/attachment/0087/9405/e9fc5046-ac81-3683-a830-7c6cd0952091.bmp" style="border: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "  alt="" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">还有一个方法 rewind ()，会重置position的位置为0 但不改变limit的位置，可以使用rewind()后退，重读已经被翻转的缓冲区中的数据。&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<ul style="margin: 0px 0px 1.5em; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">
    <li style="margin: 0px 0px 0.25em 30px; padding: 0px; ">一旦读完Buffer中的数据，需要让Buffer准备好再次被写入。可以通过clear()或compact()方法来完成。</li>
    <li style="margin: 0px 0px 0.25em 30px; padding: 0px; ">如果调用的是clear()方法，position将被设回0，limit被设置成 capacity的值。换句话说，Buffer 被清空了。但这个时候Buffer中的数据并未清除，只是这些标记告诉我们可以从哪里开始往Buffer里写数据。</li>
</ul>
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">看下面代码	</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<div class="dp-highlighter" id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #ffffff; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 25px; text-align: left; ">
<div class="bar">
<div class="tools" style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; color: black; font-weight: bold; ">Java代码&nbsp;<embed wmode="transparent" src="http://hhyyllgg.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=public%20static%20void%20main(String%5B%5D%20argv)%20throws%20Exception%20%7B%0A%09%09CharBuffer%20buffer%20%3D%20CharBuffer.allocate(20)%3B%0A%09%09System.out.println(%22%E5%88%9D%E5%A7%8B%E5%8C%96%E6%97%B6%E5%80%99%EF%BC%9Aposition%3D%22%2Bbuffer.position()%2B%22%20limit%3D%22%2Bbuffer.limit())%3B%0A%09%09fillBuffer(buffer%2C%22hello%22)%3B%0A%09%09System.out.println(%22%E5%A1%AB%E5%85%A5hello%E4%BB%A5%E5%90%8E%EF%BC%9Aposition%3D%22%2Bbuffer.position()%2B%22%20limit%3D%22%2Bbuffer.limit())%3B%0A%09%09buffer.put(0%2C'M').put('w')%3B%0A%09%09System.out.println(%22%E4%BF%AE%E6%94%B9%E6%88%90Mellow%E5%90%8E%EF%BC%9Aposition%3D%22%2Bbuffer.position()%2B%22%20limit%3D%22%2Bbuffer.limit())%3B%0A%09%09buffer.flip()%3B%0A%09%09System.out.println(%22%E7%BF%BB%E8%BD%AC%E5%90%8E%EF%BC%9Aposition%3D%22%2Bbuffer.position()%2B%22%20limit%3D%22%2Bbuffer.limit())%3B%0A%09%09buffer.clear()%3B%0A%09%09System.out.println(%22clear%E5%90%8E%EF%BC%9Aposition%3D%22%2Bbuffer.position()%2B%22%20limit%3D%22%2Bbuffer.limit())%3B%0A%09%09buffer.position(0)%3B%0A%09%09while%20(buffer.hasRemaining())%20%7B%0A%09%09%09System.out.println(buffer.get())%3B%0A%09%09%7D%0A%09%7Dprivate%20static%20void%20fillBuffer(CharBuffer%20buffer)%20%7B%0A%09%09String%20string%20%3D%20%22MY%20TEST%20BUFFER%22%3B%0A%09%09for%20(int%20i%20%3D%200%3B%20i%20%3C%20string.length()%3B%20i%2B%2B)%20%7B%0A%09%09%09buffer.put(string.charAt(i))%3B%0A%09%09%7D%0A%09%7D%0A%0A%E7%BB%93%E6%9E%9C%EF%BC%9A%0A%E5%88%9D%E5%A7%8B%E5%8C%96%E6%97%B6%E5%80%99%EF%BC%9Aposition%3D0%20limit%3D20%0A%E5%A1%AB%E5%85%A5hello%E4%BB%A5%E5%90%8E%EF%BC%9Aposition%3D5%20limit%3D20%0A%E4%BF%AE%E6%94%B9%E6%88%90Mellow%E5%90%8E%EF%BC%9Aposition%3D6%20limit%3D20%0A%E7%BF%BB%E8%BD%AC%E5%90%8E%EF%BC%9Aposition%3D0%20limit%3D6%0Aclear%E5%90%8E%EF%BC%9Aposition%3D0%20limit%3D20%0AM%0Ae%0Al%0Al%0Ao%0Aw" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="http://hhyyllgg.iteye.com/blog/1921386" title="收藏这段代码" style="color: #108ac6; text-decoration: underline; "><img class="star" src="http://hhyyllgg.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px; " /></a></div>
</div>
<ol start="1" class="dp-j" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af; ">
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; "><span class="keyword" style="color: #7f0055; ">public</span>&nbsp;<span class="keyword" style="color: #7f0055; ">static</span>&nbsp;<span class="keyword" style="color: #7f0055; ">void</span>&nbsp;main(String[]&nbsp;argv)&nbsp;<span class="keyword" style="color: #7f0055; ">throws</span>&nbsp;Exception&nbsp;{&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CharBuffer&nbsp;buffer&nbsp;=&nbsp;CharBuffer.allocate(<span class="number" style="color: #c00000; ">20</span>);&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"初始化时候：position="</span>+buffer.position()+<span class="string">"&nbsp;limit="</span>+buffer.limit());&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fillBuffer(buffer,<span class="string">"hello"</span>);&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"填入hello以后：position="</span>+buffer.position()+<span class="string">"&nbsp;limit="</span>+buffer.limit());&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.put(<span class="number" style="color: #c00000; ">0</span>,<span class="string">'M'</span>).put(<span class="string">'w'</span>);&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"修改成Mellow后：position="</span>+buffer.position()+<span class="string">"&nbsp;limit="</span>+buffer.limit());&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.flip();&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"翻转后：position="</span>+buffer.position()+<span class="string">"&nbsp;limit="</span>+buffer.limit());&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.clear();&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string">"clear后：position="</span>+buffer.position()+<span class="string">"&nbsp;limit="</span>+buffer.limit());&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.position(<span class="number" style="color: #c00000; ">0</span>);&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #7f0055; ">while</span>&nbsp;(buffer.hasRemaining())&nbsp;{&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(buffer.get());&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;}<span class="keyword" style="color: #7f0055; ">private</span>&nbsp;<span class="keyword" style="color: #7f0055; ">static</span>&nbsp;<span class="keyword" style="color: #7f0055; ">void</span>&nbsp;fillBuffer(CharBuffer&nbsp;buffer)&nbsp;{&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;string&nbsp;=&nbsp;<span class="string">"MY&nbsp;TEST&nbsp;BUFFER"</span>;&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #7f0055; ">for</span>&nbsp;(<span class="keyword" style="color: #7f0055; ">int</span>&nbsp;i&nbsp;=&nbsp;<span class="number" style="color: #c00000; ">0</span>;&nbsp;i&nbsp;&lt;&nbsp;string.length();&nbsp;i++)&nbsp;{&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.put(string.charAt(i));&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">结果：&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">初始化时候：position=<span class="number" style="color: #c00000; ">0</span>&nbsp;limit=<span class="number" style="color: #c00000; ">20</span>&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">填入hello以后：position=<span class="number" style="color: #c00000; ">5</span>&nbsp;limit=<span class="number" style="color: #c00000; ">20</span>&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">修改成Mellow后：position=<span class="number" style="color: #c00000; ">6</span>&nbsp;limit=<span class="number" style="color: #c00000; ">20</span>&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">翻转后：position=<span class="number" style="color: #c00000; ">0</span>&nbsp;limit=<span class="number" style="color: #c00000; ">6</span>&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">clear后：position=<span class="number" style="color: #c00000; ">0</span>&nbsp;limit=<span class="number" style="color: #c00000; ">20</span>&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">M&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">e&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">l&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">l&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">o&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">w&nbsp;&nbsp;</li>
</ol>
</div>
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">从上面例子可以看出clear后实际上buffer中数据还是存在的&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">如果Buffer中有一些未读的数据，调用clear()方法，数据将&#8220;被遗忘&#8221;，意味着不再有任何标记会告诉你哪些数据被读过，哪些还没有。&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">如果Buffer中仍有未读的数据，且后续还需要这些数据，但是此时想要先先写些数据，那么使用compact()方法。&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">看下面代码：&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<div class="dp-highlighter" id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; background-color: #ffffff; width: 679px; margin-left: 9px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; line-height: 25px; text-align: left; ">
<div class="bar">
<div class="tools" style="padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin: 0px; color: black; font-weight: bold; ">Java代码&nbsp;<embed wmode="transparent" src="http://hhyyllgg.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=public%20static%20void%20compactTest()%7B%0A%09%09CharBuffer%20buffer%20%3D%20CharBuffer.allocate(11)%3B%0A%09%09fillBuffer(buffer%2C%22hello%20world%22)%3B%0A%09%09buffer.position(6)%3B%0A%09%09buffer.compact()%3B%0A%09%09buffer.put(%22j%22)%3B%0A%09%09buffer.put(%22a%22)%3B%0A%09%09buffer.put(%22v%22)%3B%0A%09%09buffer.put(%22a%22)%3B%0A%09%09buffer.flip()%3B%0A%09%09while%20(buffer.hasRemaining())%20%7B%0A%09%09%09System.out.print(buffer.get())%3B%0A%09%09%7D%0A%09%7D%0A%E6%89%93%E5%8D%B0%E7%BB%93%E6%9E%9C%E4%B8%BAworldjava" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="http://hhyyllgg.iteye.com/blog/1921386" title="收藏这段代码" style="color: #108ac6; text-decoration: underline; "><img class="star" src="http://hhyyllgg.iteye.com/images/icon_star.png" alt="收藏代码" style="border: 0px; " /></a></div>
</div>
<ol start="1" class="dp-j" style="font-size: 1em; line-height: 1.4em; margin-left: 0px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af; ">
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; "><span class="keyword" style="color: #7f0055; ">public</span>&nbsp;<span class="keyword" style="color: #7f0055; ">static</span>&nbsp;<span class="keyword" style="color: #7f0055; ">void</span>&nbsp;compactTest(){&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CharBuffer&nbsp;buffer&nbsp;=&nbsp;CharBuffer.allocate(<span class="number" style="color: #c00000; ">11</span>);&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fillBuffer(buffer,<span class="string">"hello&nbsp;world"</span>);&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.position(<span class="number" style="color: #c00000; ">6</span>);&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.compact();&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.put(<span class="string">"j"</span>);&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.put(<span class="string">"a"</span>);&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.put(<span class="string">"v"</span>);&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.put(<span class="string">"a"</span>);&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer.flip();&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #7f0055; ">while</span>&nbsp;(buffer.hasRemaining())&nbsp;{&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print(buffer.get());&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li>
    <li style="font-size: 1em; margin-left: 38px; padding-right: 0px; border-left-width: 1px; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; ">打印结果为worldjava&nbsp;&nbsp;</li>
</ol>
</div>
<br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">compact()方法将所有未读的数据拷贝到Buffer起始处。然后将position设到最后一个未读元素正后面。limit属性依然像clear()方法一样，设置成capacity。现在Buffer准备好写数据了，但是不会覆盖未读的数据。&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">其它还有mark()与reset() equals()等方法 就不一一介绍了。&nbsp;</span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; " />
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/202395.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-08-07 22:04 <a href="http://www.cppblog.com/biao/archive/2013/08/07/202395.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java：URLConnection访问网页</title><link>http://www.cppblog.com/biao/archive/2013/08/07/202393.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Wed, 07 Aug 2013 12:54:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/08/07/202393.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202393.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/08/07/202393.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202393.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202393.html</trackback:ping><description><![CDATA[参考：http://lamoop.diandian.com/post/2012-08-07/40032367973@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div><br />
</div>
<div>示例:</div>
<div>
<div style="background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all; "><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000FF; ">package</span>&nbsp;com.tur.demo;<br />
<br />
<span style="color: #0000FF; ">import</span>&nbsp;java.io.InputStream;<br />
<span style="color: #0000FF; ">import</span>&nbsp;java.net.URL;<br />
<span style="color: #0000FF; ">import</span>&nbsp;java.net.URLConnection;<br />
<span style="color: #0000FF; ">import</span>&nbsp;java.net.URLEncoder;<br />
<span style="color: #0000FF; ">import</span>&nbsp;java.util.Scanner;<br />
<br />
<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">class</span>&nbsp;URLConnectionDemo&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;有道词典的在线查询功能.</span><span style="color: #008000; "><br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;query1()&nbsp;<span style="color: #0000FF; ">throws</span>&nbsp;Exception&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;如果URL有中文，则会出现乱码，这种情况需要用方法query2中的URLEncoder处理.</span><span style="color: #008000; "><br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URLConnection&nbsp;connection&nbsp;=&nbsp;<span style="color: #0000FF; ">new</span>&nbsp;URL(<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"http://fanyi.youdao.com/openapi.do?keyfrom=N3verL4nd&amp;key=208118276&amp;type=data&amp;doctype=xml&amp;version=1.1&amp;q=investigate")<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.openConnection();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStream&nbsp;response&nbsp;=&nbsp;connection.getInputStream();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Scanner&nbsp;scanner&nbsp;=&nbsp;<span style="color: #0000FF; ">new</span>&nbsp;Scanner(response);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(scanner.hasNextLine())&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(scanner.nextLine());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;query2()&nbsp;<span style="color: #0000FF; ">throws</span>&nbsp;Exception&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;url&nbsp;=&nbsp;"http://fanyi.youdao.com/openapi.do";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;charset&nbsp;=&nbsp;"UTF-8";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;keyFrom&nbsp;=&nbsp;"N3verL4nd";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;key&nbsp;=&nbsp;"208118276";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;type&nbsp;=&nbsp;"data";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;docType&nbsp;=&nbsp;"xml";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;version&nbsp;=&nbsp;"1.1";<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;q&nbsp;=&nbsp;"中国";<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;query&nbsp;=&nbsp;String.format("keyfrom=%s&amp;key=%s&amp;type=%s&amp;doctype=%s&amp;version=%s&amp;q=%s",<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URLEncoder.encode(keyFrom,&nbsp;charset),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URLEncoder.encode(key,&nbsp;charset),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URLEncoder.encode(type,&nbsp;charset),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URLEncoder.encode(docType,&nbsp;charset),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URLEncoder.encode(version,&nbsp;charset),<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URLEncoder.encode(q,&nbsp;charset));<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;URLConnection&nbsp;connection&nbsp;=&nbsp;<span style="color: #0000FF; ">new</span>&nbsp;URL(url&nbsp;+&nbsp;"?"&nbsp;+&nbsp;query).openConnection();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStream&nbsp;response&nbsp;=&nbsp;connection.getInputStream();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Scanner&nbsp;scanner&nbsp;=&nbsp;<span style="color: #0000FF; ">new</span>&nbsp;Scanner(response);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">while</span>&nbsp;(scanner.hasNextLine())&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(scanner.nextLine());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">public</span>&nbsp;<span style="color: #0000FF; ">static</span>&nbsp;<span style="color: #0000FF; ">void</span>&nbsp;main(String[]&nbsp;args)&nbsp;<span style="color: #0000FF; ">throws</span>&nbsp;Exception&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;query1();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;query2();<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
</div>
</div><img src ="http://www.cppblog.com/biao/aggbug/202393.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-08-07 20:54 <a href="http://www.cppblog.com/biao/archive/2013/08/07/202393.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt：绘制带高光的按钮</title><link>http://www.cppblog.com/biao/archive/2013/08/07/202387.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Tue, 06 Aug 2013 23:32:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/08/07/202387.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202387.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/08/07/202387.html#Feedback</comments><slash:comments>8</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202387.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202387.html</trackback:ping><description><![CDATA[@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<img src="http://www.cppblog.com/images/cppblog_com/biao/Button.png" width="403" height="383" alt="" />&nbsp;
<div><br />
<div>
<div style="background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; padding: 4px 5px 4px 4px; width: 98%; word-break: break-all; "><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #0000FF; ">void</span>&nbsp;Widget::paintEvent(QPaintEvent&nbsp;*)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;QPainter&nbsp;painter(<span style="color: #0000FF; ">this</span>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;painter.translate(width()&nbsp;/&nbsp;2,&nbsp;height()&nbsp;/&nbsp;2);<br />
&nbsp;&nbsp;&nbsp;&nbsp;painter.setRenderHint(QPainter::Antialiasing);<br />
&nbsp;&nbsp;&nbsp;&nbsp;painter.setPen(Qt::transparent);<br />
&nbsp;&nbsp;&nbsp;&nbsp;painter.fillRect(-width(),&nbsp;-height(),&nbsp;width()&nbsp;*&nbsp;2,&nbsp;height()&nbsp;*&nbsp;2,&nbsp;Qt::gray);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000FF; ">int</span>&nbsp;radius&nbsp;=&nbsp;100;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;外边框</span><span style="color: #008000; "><br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;QLinearGradient&nbsp;lg1(0,&nbsp;-radius,&nbsp;0,&nbsp;radius);<br />
&nbsp;&nbsp;&nbsp;&nbsp;lg1.setColorAt(0,&nbsp;QColor(255,&nbsp;255,&nbsp;255));<br />
&nbsp;&nbsp;&nbsp;&nbsp;lg1.setColorAt(1,&nbsp;QColor(166,&nbsp;166,&nbsp;166));<br />
&nbsp;&nbsp;&nbsp;&nbsp;painter.setBrush(lg1);<br />
&nbsp;&nbsp;&nbsp;&nbsp;painter.drawEllipse(-radius,&nbsp;-radius,&nbsp;radius&nbsp;&lt;&lt;&nbsp;1,&nbsp;radius&nbsp;&lt;&lt;&nbsp;1);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;内边框</span><span style="color: #008000; "><br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;radius&nbsp;-=&nbsp;13;<br />
&nbsp;&nbsp;&nbsp;&nbsp;QLinearGradient&nbsp;lg2(0,&nbsp;-radius,&nbsp;0,&nbsp;radius);<br />
&nbsp;&nbsp;&nbsp;&nbsp;lg2.setColorAt(0,&nbsp;QColor(155,&nbsp;155,&nbsp;155));<br />
&nbsp;&nbsp;&nbsp;&nbsp;lg2.setColorAt(1,&nbsp;QColor(255,&nbsp;255,&nbsp;255));<br />
&nbsp;&nbsp;&nbsp;&nbsp;painter.setBrush(lg2);<br />
&nbsp;&nbsp;&nbsp;&nbsp;painter.drawEllipse(-radius,&nbsp;-radius,&nbsp;radius&nbsp;&lt;&lt;&nbsp;1,&nbsp;radius&nbsp;&lt;&lt;&nbsp;1);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;内部的圆</span><span style="color: #008000; "><br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;radius&nbsp;-=&nbsp;4;<br />
&nbsp;&nbsp;&nbsp;&nbsp;QRadialGradient&nbsp;rg(0,&nbsp;0,&nbsp;radius);<br />
&nbsp;&nbsp;&nbsp;&nbsp;rg.setColorAt(0,&nbsp;QColor(245,&nbsp;0,&nbsp;0));<br />
&nbsp;&nbsp;&nbsp;&nbsp;rg.setColorAt(0.6,&nbsp;QColor(210,&nbsp;0,&nbsp;0));<br />
&nbsp;&nbsp;&nbsp;&nbsp;rg.setColorAt(1,&nbsp;QColor(140,&nbsp;0,&nbsp;0));<br />
&nbsp;&nbsp;&nbsp;&nbsp;painter.setBrush(rg);<br />
&nbsp;&nbsp;&nbsp;&nbsp;painter.drawEllipse(-radius,&nbsp;-radius,&nbsp;radius&nbsp;&lt;&lt;&nbsp;1,&nbsp;radius&nbsp;&lt;&lt;&nbsp;1);<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;高光</span><span style="color: #008000; "><br />
</span>&nbsp;&nbsp;&nbsp;&nbsp;radius&nbsp;-=&nbsp;3;<br />
&nbsp;&nbsp;&nbsp;&nbsp;QPainterPath&nbsp;path;<br />
&nbsp;&nbsp;&nbsp;&nbsp;path.addEllipse(-radius,&nbsp;-radius&nbsp;-&nbsp;2,&nbsp;radius&nbsp;&lt;&lt;&nbsp;1,&nbsp;radius&nbsp;&lt;&lt;&nbsp;1);<br />
&nbsp;&nbsp;&nbsp;&nbsp;QPainterPath&nbsp;bigEllipse;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;radius&nbsp;*=&nbsp;2;<br />
&nbsp;&nbsp;&nbsp;&nbsp;bigEllipse.addEllipse(-radius,&nbsp;-radius&nbsp;+&nbsp;140,&nbsp;radius&nbsp;&lt;&lt;&nbsp;1,&nbsp;radius&nbsp;&lt;&lt;&nbsp;1);<br />
&nbsp;&nbsp;&nbsp;&nbsp;path&nbsp;-=&nbsp;bigEllipse;<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;QLinearGradient&nbsp;lg3(0,&nbsp;-radius&nbsp;/&nbsp;2,&nbsp;0,&nbsp;0);<br />
&nbsp;&nbsp;&nbsp;&nbsp;lg3.setColorAt(0,&nbsp;QColor(255,&nbsp;255,&nbsp;255,&nbsp;220));<br />
&nbsp;&nbsp;&nbsp;&nbsp;lg3.setColorAt(1,&nbsp;QColor(255,&nbsp;255,&nbsp;255,&nbsp;30));<br />
&nbsp;&nbsp;&nbsp;&nbsp;painter.setBrush(lg3);<br />
&nbsp;&nbsp;&nbsp;&nbsp;painter.drawPath(path);<br />
}</div>
</div>
</div><img src ="http://www.cppblog.com/biao/aggbug/202387.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-08-07 07:32 <a href="http://www.cppblog.com/biao/archive/2013/08/07/202387.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt：FontMetrics</title><link>http://www.cppblog.com/biao/archive/2013/08/03/202322.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sat, 03 Aug 2013 10:27:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/08/03/202322.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202322.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/08/03/202322.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202322.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202322.html</trackback:ping><description><![CDATA[<div>在一个Rect的正中间绘制文本: rect的中点与文本的中点重合.</div>
<div>
<div>int cx = rect.x + rect.width / 2;</div>
<div>int fx = cx - fm.width(text);</div>
</div>
<div><br />
</div>
<div>int cy = rect.y + rect.height / 2;</div>
<div>int fy = cy + fm.xHeight&nbsp;/ 2;</div>
<div><br />
</div>
<div><br />
</div>
<div><br />
</div>
<img src="http://www.cppblog.com/images/cppblog_com/biao/FontMetrics.png" width="506" height="274" alt="" />
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/202322.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-08-03 18:27 <a href="http://www.cppblog.com/biao/archive/2013/08/03/202322.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Mac：Mac下安装PHP, MySql开发环境</title><link>http://www.cppblog.com/biao/archive/2013/08/03/202319.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sat, 03 Aug 2013 02:24:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/08/03/202319.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202319.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/08/03/202319.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202319.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202319.html</trackback:ping><description><![CDATA[最简单的两个选择: XAMPP, MAMP
<div>都集成了&nbsp;<span style="color: red; ">MySql + PhpMyAdmin&nbsp;</span>+ PHP 开发环境，建立开发环境简单（都只需要安装一个程序）。</div>
<div>如果单独的安装MySql，PhpMyAdmin，PHP开发环境，会麻烦很多。</div>
<div><br />
<div><span style="color: red; ">XAMPP</span>: 提供多平台版本:&nbsp;Mac,&nbsp;Windows，Linux, Solaris，但是几乎不更新了，相对MAMP来说易用性就差那么一点点。</div>
<div>下载地址：<a href="http://www.apachefriends.org/zh_cn/xampp.html">http://www.apachefriends.org/zh_cn/xampp.html</a></div>
<div><span style="color: red; "><br />
</span></div>
<div><span style="color: red; ">MAMP</span>: 只能运行于Mac，经常更新，帮助文档丰富，易用性更好。</div>
<div>下载地址：<a href="http://www.mamp.info/en/index.html">http://www.mamp.info/en/index.html</a></div>
<div><img src="http://www.cppblog.com/images/cppblog_com/biao/MAMP.png" width="537" height="458" alt="" /><br />
</div>
<div><img src="http://www.cppblog.com/images/cppblog_com/biao/MAMP-Preferences.png" width="537" height="458" alt="" /><br />
</div>
<div><br />
</div>
</div><img src ="http://www.cppblog.com/biao/aggbug/202319.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-08-03 10:24 <a href="http://www.cppblog.com/biao/archive/2013/08/03/202319.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C：函数指针数组的定义</title><link>http://www.cppblog.com/biao/archive/2013/07/26/202124.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 25 Jul 2013 23:56:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/07/26/202124.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202124.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/07/26/202124.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202124.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202124.html</trackback:ping><description><![CDATA[<pre style="margin-top: 0px; margin-bottom: 0px;"><font color="#808000"><br />
</font></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><font color="#808000">// [[1]] 使用typedef定义函数指针为一个类型，更喜欢这种方式</font></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><!--StartFragment--><span style=" color:#808000;">typedef</span><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span>(*<span style=" color:#800080;">MYFUN</span>)(<span style=" color:#808000;">int</span>,<span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#800080;">MYFUN</span><span style=" color:#c0c0c0;"> </span>funcs[<span style=" color:#000080;">10</span>];</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><font color="#808000">// [[2]] 使用typedef定义函数指针数组为一个类型，不是很直观</font></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#808000;">typedef</span><span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span>(*<span style=" color:#800080;">MYFUN2</span>[<span style=" color:#000080;">10</span>])(<span style=" color:#808000;">int</span>,<span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">int</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#800080;">MYFUN2</span><span style=" color:#c0c0c0;"> </span>funcs2;</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span>main(<span style=" color:#808000;">int</span><span style=" color:#c0c0c0;"> </span>argc,<span style=" color:#c0c0c0;"> </span><span style=" color:#808000;">char</span><span style=" color:#c0c0c0;"> </span>*argv[])<span style=" color:#c0c0c0;"> </span>{</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#c0c0c0;">    </span>funcs[<span style=" color:#000080;">0</span>]<span style=" color:#c0c0c0;"> </span>=<span style=" color:#c0c0c0;"> </span>add;</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#c0c0c0;">    </span><span style=" color:#000080;">qDebug</span>()<span style=" color:#c0c0c0;"> </span>&lt;&lt;<span style=" color:#c0c0c0;"> </span>(*funcs[<span style=" color:#000080;">0</span>])(<span style=" color:#000080;">2</span>,<span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">3</span>); <span style="color: #808000; font-family: verdana, 'courier new';">// 也可以用 funcs[0](2, 3).</span></pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#c0c0c0;">    </span>funcs2[<span style=" color:#000080;">0</span>]<span style=" color:#c0c0c0;"> </span>=<span style=" color:#c0c0c0;"> </span>add;</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#c0c0c0;">    </span><span style=" color:#000080;">qDebug</span>()<span style=" color:#c0c0c0;"> </span>&lt;&lt;<span style=" color:#c0c0c0;"> </span>(*funcs2[<span style=" color:#000080;">0</span>])(<span style=" color:#000080;">2</span>,<span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">3</span>);</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><br />
</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#c0c0c0;">    </span><span style=" color:#808000;">return</span><span style=" color:#c0c0c0;"> </span><span style=" color:#000080;">0</span>;</pre>
<pre style="margin-top: 0px; margin-bottom: 0px;">}<!--EndFragment--></pre>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/202124.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-07-26 07:56 <a href="http://www.cppblog.com/biao/archive/2013/07/26/202124.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt：无标题栏无边框程序的拖动和改变大小</title><link>http://www.cppblog.com/biao/archive/2013/07/11/201704.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 11 Jul 2013 11:45:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/07/11/201704.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/201704.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/07/11/201704.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/201704.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/201704.html</trackback:ping><description><![CDATA[<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><span style="white-space: pre; ">From: <a href="http://blog.csdn.net/kfbyj/article/details/9284923">http://blog.csdn.net/kfbyj/article/details/9284923</a></span></p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><span style="white-space: pre; "><br />
</span></p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><span style="white-space: pre; ">最近做项目遇到的问题，总结下。</span></p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><span style="white-space: pre; "></span>有时候我们觉得系统的标题栏和按钮太丑太呆板，想做自己的标题栏以及最大化、最小化、关闭，菜单按钮，我们就需要&nbsp;</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "></p>
<div class="dp-highlighter bg_cpp" style="width: 722px; text-align: left; line-height: 26px; ">
<div class="bar">
<div class="tools" style="border-left-color: #999999; "><strong>[cpp]</strong>&nbsp;<a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="ViewSource" title="view plain">view plain</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="CopyToClipboard" title="copy">copy</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="PrintSource" title="print">print</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="About" title="?">?</a>
<div style="position: absolute; left: 393px; top: 524px; width: 21px; height: 11px; z-index: 99; "><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="21" height="11" name="ZeroClipboardMovie_1" align="center" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&amp;width=21&amp;height=11" wmode="transparent"></div>
</div>
</div>
<ol start="1" class="dp-cpp">
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">setWindowFlags(Qt::FramelessWindowHint);&nbsp;&nbsp;</li>
</ol>
</div>
<span style="font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">但是这样过后，我们就不能拖动窗口改变位置，以及拖动边缘改变窗口大小了。</span>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "></p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><span style="white-space: pre; "></span>有两种方案处理这种情况：</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">1.自己对mouseMoveEvent,mousePressEvent,mouseReleaseEvent 等事件进行处理。</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">2.Qt可以处理windows的消息。大家重新实现bool winEvent(MSG *message, long *result);(在此又一次感觉Qt的NB)</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />
</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><span style="white-space: pre; "></span>我刚开始使用第一种方法去实现的。移动窗口很容易做，大家可以去看看这个大大写的，比网上其他版本问题少些。</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><a href="http://blog.csdn.net/aqtata/article/details/8902889" style="color: #ca0000; text-decoration: none; "><span style="white-space: pre; "></span>http://blog.csdn.net/aqtata/article/details/8902889</a><br />
</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><span style="white-space: pre; "></span>在窗口边缘按下鼠标拖动改变窗口大小就比较麻烦了。</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><span style="white-space: pre; "></span>我是这样做的：</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">在mousePressEvent 按下设置m_bPressed为真。</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">在mouseMoveEvent中m_bPressed为真且event-&gt;x() 在窗口边缘 及处理算出鼠标移动的增量 然后不断resize窗口。<br />
</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">至于如何为边缘的断定，就自己设定一个 差值 假如 在窗口边缘 &#177;4个px 就算在在该边缘就处理该resize。</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">这样做缺点很多，1.拖快了不行，很容易超过该差值 ， 2.窗口抖动的厉害，一直在resize，3.要处理太多情况</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />
</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">鉴于上诉缺点于是乎就到处问人百度google。有了第二种方法：</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><span style="white-space: pre; "></span>第二种方法很好用，效果和有标题边框程序一样~~~</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">Qt居然可以处理windows消息。。</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">这里我们要重新实现winEvent ( MSG * message, long * result )&nbsp;</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">该虚函数在QWidget和QWizard以及QSizeGrip以及他们的子类中都可以实现。</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><span style="white-space: pre; "></span>如果你想停止Qt处理消息就返回true，并且设置result到你想要保存的值返回给window处理。否者的话返回false。</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">这里我们主要想处理WM_NCHITTEST消息。</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "></p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">The&nbsp;<strong>WM_NCHITTEST</strong>&nbsp;message is sent to a window in order to determine what part of the window corresponds to a particular screen coordinate. This can happen, for example, when the cursor moves, when a mouse button is pressed or released, or in response to a call to a function such as&nbsp;<a class="mtps-internal-link" href="http://127.0.0.1:47873/help/1-6004/ms.help?method=page&amp;id=VS%7CWINUI%7C%7E%5CWINUI%5CWINDOWSUSERINTERFACE%5CWINDOWING%5CWINDOWS%5CWINDOWREFERENCE%5CWINDOWFUNCTIONS%5CWINDOWFROMPOINT.HTM&amp;product=VS&amp;productVersion=100&amp;topicVersion=85&amp;locale=ZH-CN&amp;topicLocale=EN-US" target="" style="color: #ca0000; text-decoration: none; "><strong>WindowFromPoint</strong></a>. If the mouse is not captured, the message is sent to the window beneath the cursor. Otherwise, the message is sent to the window that has captured the mouse.</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">A window receives this message through its&nbsp;<a class="mtps-internal-link" href="http://127.0.0.1:47873/help/1-6004/ms.help?method=page&amp;id=VS%7CWINUI%7C%7E%5CWINUI%5CWINDOWSUSERINTERFACE%5CWINDOWING%5CWINDOWPROCEDURES%5CWINDOWPROCEDUREREFERENCE%5CWINDOWPROCEDUREFUNCTIONS%5CWINDOWPROC.HTM&amp;product=VS&amp;productVersion=100&amp;topicVersion=85&amp;locale=ZH-CN&amp;topicLocale=EN-US" target="" style="color: #ca0000; text-decoration: none; "><strong>WindowProc</strong></a>&nbsp;function.&nbsp;</p>
<span style="font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">WM_NCHITTEST的消息响应函数会根据鼠标当前的坐标来判断鼠标命中了窗口的哪个部位，消息响应函数的返回值指出了部位，例如它可能会返回HTCAPTION，或者HTCLIENT等。（其返回值有很多，请查阅MSDN）。</span><br style="font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><span style="white-space: pre; "></span>知道这个就好了，我们还是要判断下鼠标的位置，然后通过该位置保存到result给window处理。</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">其实就是我们的程序没有边框不能发送这些消息，我们把它告诉windows，然后windows帮我们处理拖动，改变大小等效果。所以效果和有边框有标题程序效果一样的。</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">头文件申明：</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "></p>
<div class="dp-highlighter bg_cpp" style="width: 722px; text-align: left; line-height: 26px; ">
<div class="bar">
<div class="tools" style="border-left-color: #999999; "><strong>[cpp]</strong>&nbsp;<a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="ViewSource" title="view plain">view plain</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="CopyToClipboard" title="copy">copy</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="PrintSource" title="print">print</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="About" title="?">?</a>
<div style="position: absolute; left: 393px; top: 1462px; width: 21px; height: 11px; z-index: 99; "><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="21" height="11" name="ZeroClipboardMovie_2" align="center" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&amp;width=21&amp;height=11" wmode="transparent"></div>
</div>
</div>
<ol start="1" class="dp-cpp">
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; "><span class="keyword" style="color: blue; ">class</span>&nbsp;MainWindow&nbsp;:&nbsp;<span class="keyword" style="color: blue; ">public</span>&nbsp;QMainWindow&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">{&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;Q_OBJECT&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; "><span class="keyword" style="color: blue; ">public</span>:&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;MainWindow(QWidget&nbsp;*parent&nbsp;=&nbsp;0);&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;~MainWindow();&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; "><span class="keyword" style="color: blue; ">protected</span>:&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="datatypes" style="color: #2e8b57; font-weight: bold; ">bool</span>&nbsp;winEvent(MSG&nbsp;*message,&nbsp;<span class="datatypes" style="color: #2e8b57; font-weight: bold; ">long</span>&nbsp;*result);&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">};&nbsp;&nbsp;</li>
</ol>
</div>
<span style="font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">CPP实现</span><br style="font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "></p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "></p>
<div class="dp-highlighter bg_cpp" style="width: 722px; text-align: left; line-height: 26px; ">
<div class="bar">
<div class="tools" style="border-left-color: #999999; "><strong>[cpp]</strong>&nbsp;<a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="ViewSource" title="view plain">view plain</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="CopyToClipboard" title="copy">copy</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="PrintSource" title="print">print</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="About" title="?">?</a>
<div style="position: absolute; left: 393px; top: 1730px; width: 21px; height: 11px; z-index: 99; "><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="21" height="11" name="ZeroClipboardMovie_3" align="center" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&amp;width=21&amp;height=11" wmode="transparent"></div>
</div>
</div>
<ol start="1" class="dp-cpp">
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; "><span class="datatypes" style="color: #2e8b57; font-weight: bold; ">bool</span>&nbsp;MainWindow::winEvent(MSG&nbsp;*message,&nbsp;<span class="datatypes" style="color: #2e8b57; font-weight: bold; ">long</span>&nbsp;*result)&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">{&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">switch</span>(message-&gt;message)&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">case</span>&nbsp;WM_NCHITTEST:&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="datatypes" style="color: #2e8b57; font-weight: bold; ">int</span>&nbsp;xPos&nbsp;=&nbsp;GET_X_LPARAM(message-&gt;lParam)&nbsp;-&nbsp;<span class="keyword" style="color: blue; ">this</span>-&gt;frameGeometry().x();&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="datatypes" style="color: #2e8b57; font-weight: bold; ">int</span>&nbsp;yPos&nbsp;=&nbsp;GET_Y_LPARAM(message-&gt;lParam)&nbsp;-&nbsp;<span class="keyword" style="color: blue; ">this</span>-&gt;frameGeometry().y();&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">if</span>(<span class="keyword" style="color: blue; ">this</span>-&gt;childAt(xPos,yPos)&nbsp;==&nbsp;0)&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*result&nbsp;=&nbsp;HTCAPTION;&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<span class="keyword" style="color: blue; ">else</span>{&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">return</span>&nbsp;<span class="keyword" style="color: blue; ">false</span>;&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">if</span>(xPos&nbsp;&gt;&nbsp;18&nbsp;&amp;&amp;&nbsp;xPos&nbsp;&lt;&nbsp;22)&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*result&nbsp;=&nbsp;HTLEFT;&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">if</span>(xPos&nbsp;&gt;&nbsp;(<span class="keyword" style="color: blue; ">this</span>-&gt;width()&nbsp;-&nbsp;22)&nbsp;&amp;&amp;&nbsp;xPos&nbsp;&lt;&nbsp;(<span class="keyword" style="color: blue; ">this</span>-&gt;width()&nbsp;-&nbsp;18))&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*result&nbsp;=&nbsp;HTRIGHT;&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">if</span>(yPos&nbsp;&gt;&nbsp;18&nbsp;&amp;&amp;&nbsp;yPos&nbsp;&lt;&nbsp;22)&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*result&nbsp;=&nbsp;HTTOP;&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">if</span>(yPos&nbsp;&gt;&nbsp;(<span class="keyword" style="color: blue; ">this</span>-&gt;height()&nbsp;-&nbsp;22)&nbsp;&amp;&amp;&nbsp;yPos&nbsp;&lt;&nbsp;(<span class="keyword" style="color: blue; ">this</span>-&gt;height()&nbsp;-&nbsp;18))&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*result&nbsp;=&nbsp;HTBOTTOM;&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">if</span>(xPos&nbsp;&gt;&nbsp;18&nbsp;&amp;&amp;&nbsp;xPos&nbsp;&lt;&nbsp;22&nbsp;&amp;&amp;&nbsp;yPos&nbsp;&gt;&nbsp;18&nbsp;&amp;&amp;&nbsp;yPos&nbsp;&lt;&nbsp;22)&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*result&nbsp;=&nbsp;HTTOPLEFT;&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">if</span>(xPos&nbsp;&gt;&nbsp;(<span class="keyword" style="color: blue; ">this</span>-&gt;width()&nbsp;-&nbsp;22)&nbsp;&amp;&amp;&nbsp;xPos&nbsp;&lt;&nbsp;(<span class="keyword" style="color: blue; ">this</span>-&gt;width()&nbsp;-&nbsp;18)&nbsp;&amp;&amp;&nbsp;yPos&nbsp;&gt;&nbsp;18&nbsp;&amp;&amp;&nbsp;yPos&nbsp;&lt;&nbsp;22)&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*result&nbsp;=&nbsp;HTTOPRIGHT;&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">if</span>(xPos&nbsp;&gt;&nbsp;18&nbsp;&amp;&amp;&nbsp;xPos&nbsp;&lt;&nbsp;22&nbsp;&amp;&amp;&nbsp;yPos&nbsp;&gt;&nbsp;(<span class="keyword" style="color: blue; ">this</span>-&gt;height()&nbsp;-&nbsp;22)&nbsp;&amp;&amp;&nbsp;yPos&nbsp;&lt;&nbsp;(<span class="keyword" style="color: blue; ">this</span>-&gt;height()&nbsp;-&nbsp;18))&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*result&nbsp;=&nbsp;HTBOTTOMLEFT;&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">if</span>(xPos&nbsp;&gt;&nbsp;(<span class="keyword" style="color: blue; ">this</span>-&gt;width()&nbsp;-&nbsp;22)&nbsp;&amp;&amp;&nbsp;xPos&nbsp;&lt;&nbsp;(<span class="keyword" style="color: blue; ">this</span>-&gt;width()&nbsp;-&nbsp;18)&nbsp;&amp;&amp;&nbsp;yPos&nbsp;&gt;&nbsp;(<span class="keyword" style="color: blue; ">this</span>-&gt;height()&nbsp;-&nbsp;22)&nbsp;&amp;&amp;&nbsp;yPos&nbsp;&lt;&nbsp;(<span class="keyword" style="color: blue; ">this</span>-&gt;height()&nbsp;-&nbsp;18))&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*result&nbsp;=&nbsp;HTBOTTOMRIGHT;&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">return</span>&nbsp;<span class="keyword" style="color: blue; ">true</span>;&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</li>
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: blue; ">return</span>&nbsp;<span class="keyword" style="color: blue; ">false</span>;&nbsp;&nbsp;</li>
    <li class="" style="border-left-color: #999999; background-color: #f5fae2; color: #555555; line-height: 18px; ">}&nbsp;&nbsp;</li>
</ol>
</div>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "></p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">把各种边界情况保存到result给windows处理，我们就省去很多事情，我想windows肯定比我们自己实现的效果要好多了。</p>
<span style="font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">以上的18 以及 22 是我对程序的边缘进行判断的范围。</span>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">因为</p>
<p style="margin: 0px; padding: 0px; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">我做了边框阴影。阴影边框设定为20px所以在</p>
<div class="dp-highlighter bg_cpp" style="width: 722px; text-align: left; line-height: 26px; ">
<div class="bar">
<div class="tools" style="border-left-color: #999999; "><strong>[cpp]</strong>&nbsp;<a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="ViewSource" title="view plain">view plain</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="CopyToClipboard" title="copy">copy</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="PrintSource" title="print">print</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="About" title="?">?</a>
<div style="position: absolute; left: 393px; top: 2588px; width: 21px; height: 11px; z-index: 99; "><embed id="ZeroClipboardMovie_4" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="21" height="11" name="ZeroClipboardMovie_4" align="center" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&amp;width=21&amp;height=11" wmode="transparent"></div>
</div>
</div>
<ol start="1" class="dp-cpp">
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">xPos&nbsp;&gt;&nbsp;18&nbsp;&amp;&amp;&nbsp;xPos&nbsp;&lt;&nbsp;22&nbsp;其实就是我们假定的边框了。&nbsp;&nbsp;</li>
</ol>
</div>
<div class="dp-highlighter bg_cpp" style="width: 722px; text-align: left; line-height: 26px; ">
<div class="bar">
<div class="tools" style="border-left-color: #999999; "><strong>[cpp]</strong>&nbsp;<a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="ViewSource" title="view plain">view plain</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="CopyToClipboard" title="copy">copy</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="PrintSource" title="print">print</a><a href="http://blog.csdn.net/kfbyj/article/details/9284923#" class="About" title="?">?</a>
<div style="position: absolute; left: 393px; top: 2650px; width: 21px; height: 11px; z-index: 99; "><embed id="ZeroClipboardMovie_5" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="21" height="11" name="ZeroClipboardMovie_5" align="center" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&amp;width=21&amp;height=11" wmode="transparent"></div>
</div>
</div>
<ol start="1" class="dp-cpp">
    <li class="alt" style="border-left-color: #999999; background-color: #f5fae2; line-height: 18px; ">&nbsp;&nbsp;</li>
    <li></li>
</ol>
</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/201704.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-07-11 19:45 <a href="http://www.cppblog.com/biao/archive/2013/07/11/201704.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JDK里的设计模式</title><link>http://www.cppblog.com/biao/archive/2013/07/04/201504.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Wed, 03 Jul 2013 20:56:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/07/04/201504.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/201504.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/07/04/201504.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/201504.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/201504.html</trackback:ping><description><![CDATA[<div class="post" id="post-3320" style="margin: 0px 0px 20px; padding-bottom: 5px !important; box-shadow: #cccccc 0px 0px 5px 2px; color: #555555; font-family: Arial, Verdana, 'BitStream vera Sans', Tahoma, Helvetica, sans-serif; line-height: normal; ">
<div class="content" style="margin: 0px; padding: 8px; overflow: hidden; border-left-color: #eeeeee; background-color: white; line-height: 22px; ">
<p style="margin: 0px 0px 10px; padding: 0px; ">下面是JDK中有关23个经典设计模式的示例，在stakeoverflow也有相应的讨论：<br />
<a href="http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns" target="_blank" style="color: #2970a6; text-decoration: none; ">http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns</a></p>
<h4 style="margin: 0px 0px 10px; padding: 0px; letter-spacing: -0.05em; font-family: 'Microsoft YaHei', Arial; font-size: 17px; color: #008800; "><strong><span style="text-decoration: underline; ">Structural（结构模式）</span></strong></h4>
<div style="margin: 0px; "><strong>Adapter:</strong><br />
把一个接口或是类变成另外一种。
<p style="margin: 0px 0px 10px; padding: 0px; "></p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Arrays#asList()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.swing.JTable(TableModel)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.io.InputStreamReader(InputStream)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.io.OutputStreamWriter(OutputStream)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.xml.bind.annotation.adapters.XmlAdapter#marshal()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal()</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Bridge:</strong><br />
把抽象和实现解藕，于是接口和实现可在完全独立开来。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">AWT (提供了抽象层映射于实际的操作系统)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">JDBC</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Composite:</strong><br />
让使用者把单独的对象和组合对象混用。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.swing.JComponent#add(Component)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.awt.Container#add(Component)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Map#putAll(Map)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.List#addAll(Collection)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Set#addAll(Collection)</li>
</ul>
</div>
<p style="margin: 0px 0px 10px; padding: 0px; "><span id="more-3320"></span></p>
<div style="margin: 0px; ">
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Decorator:</strong><br />
为一个对象动态的加上一系列的动作，而不需要因为这些动作的不同而产生大量的继承类。这个模式在JDK中几乎无处不在，所以，下面的列表只是一些典型的。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.io.BufferedInputStream(InputStream)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.io.DataInputStream(InputStream)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.io.BufferedOutputStream(OutputStream)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.zip.ZipOutputStream(OutputStream)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Collections#checked[List|Map|Set|SortedSet|SortedMap]()</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Facade:</strong><br />
用一个简单的接口包状一组组件，接口，抽象或是子系统。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Class</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.faces.webapp.FacesServlet</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Flyweight:</strong><br />
有效率地存储大量的小的对象。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Integer#valueOf(int)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Boolean#valueOf(boolean)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Byte#valueOf(byte)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Character#valueOf(char)</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Proxy:</strong><br />
用一个简单的对象来代替一个复杂的对象。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.reflect.Proxy</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">RMI</li>
</ul>
</div>
<div style="margin: 0px; ">
<h4 style="margin: 0px 0px 10px; padding: 0px; letter-spacing: -0.05em; font-family: 'Microsoft YaHei', Arial; font-size: 17px; color: #008800; "><strong><span style="text-decoration: underline; ">Creational（创建模式）</span></strong></h4>
</div>
<div style="margin: 0px; "><strong></strong><strong>Abstract factory:</strong><br />
创建一组有关联的对象实例。这个模式在JDK中也是相当的常见，还有很多的framework例如Spring。我们很容易找到这样的实例。
<p style="margin: 0px 0px 10px; padding: 0px; "></p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Calendar#getInstance()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Arrays#asList()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.ResourceBundle#getBundle()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.sql.DriverManager#getConnection()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.sql.Connection#createStatement()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.sql.Statement#executeQuery()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.text.NumberFormat#getInstance()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.xml.transform.TransformerFactory#newInstance()</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Builder:</strong><br />
主要用来简化一个复杂的对象的创建。这个模式也可以用来实现一个&nbsp;<a href="http://en.wikipedia.org/wiki/Fluent_interface" target="_blank" style="color: #2970a6; text-decoration: none; ">Fluent Interface</a>。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.StringBuilder#append()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.StringBuffer#append()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.sql.PreparedStatement</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.swing.GroupLayout.Group#addComponent()</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Factory:</strong><br />
简单来说，按照需求返回一个类型的实例。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Proxy#newProxyInstance()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Object#toString()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Class#newInstance()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.reflect.Array#newInstance()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.reflect.Constructor#newInstance()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Boolean#valueOf(String)</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Class#forName()</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Prototype:</strong><br />
使用自己的实例创建另一个实例。有时候，创建一个实例然后再把已有实例的值拷贝过去，是一个很复杂的动作。所以，使用这个模式可以避免这样的复杂性。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Object#clone()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Cloneable</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Singleton:</strong><br />
只允许一个实例。在 Effective Java中建议使用Emun.</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Runtime#getRuntime()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.awt.Toolkit#getDefaultToolkit()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.awt.GraphicsEnvironment#getLocalGraphicsEnvironment()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.awt.Desktop#getDesktop()</li>
</ul>
<h4 style="margin: 0px 0px 10px; padding: 0px; letter-spacing: -0.05em; font-family: 'Microsoft YaHei', Arial; font-size: 17px; color: #008800; "><strong><span style="text-decoration: underline; ">Behavioral(行为模式)</span></strong></h4>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Chain of responsibility:</strong><br />
把一个对象在一个链接传递直到被处理。在这个链上的所有的对象有相同的接口（抽象类）但却有不同的实现。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.logging.Logger#log()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.servlet.Filter#doFilter()</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Command:</strong><br />
把一个或一些命令封装到一个对象中。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.Runnable</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.swing.Action</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Interpreter:</strong><br />
一个语法解释器的模式。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Pattern</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.text.Normalizer</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.text.Format</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Iterator:</strong><br />
提供一种一致的方法来顺序遍历一个容器中的所有元素。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Iterator</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Enumeration</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Mediator:</strong><br />
用来减少对象单的直接通讯的依赖关系。使用一个中间类来管理消息的方向。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Timer</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.concurrent.Executor#execute()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.concurrent.ExecutorService#submit()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.lang.reflect.Method#invoke()</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Memento:</strong><br />
给一个对象的状态做一个快照。Date类在内部使用了一个long型来做这个快照。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Date</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.io.Serializable</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Null Object:</strong><br />
这个模式用来解决如果一个Collection中没有元素的情况。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Collections#emptyList()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Collections#emptyMap()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Collections#emptySet()</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Observer:</strong><br />
允许一个对象向所有的侦听的对象广播自己的消息或事件。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.EventListener</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.servlet.http.HttpSessionBindingListener</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.servlet.http.HttpSessionAttributeListener</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.faces.event.PhaseListener</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>State:</strong><br />
这个模式允许你可以在运行时很容易地根据自身内部的状态改变对象的行为。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Iterator</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.faces.lifecycle.LifeCycle#execute()</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Strategy:</strong><br />
定义一组算法，并把其封装到一个对象中。然后在运行时，可以灵活的使用其中的一个算法。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Comparator#compare()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.servlet.http.HttpServlet</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.servlet.Filter#doFilter()</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Template method:</strong><br />
允许子类重载部分父类而不需要完全重写。</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.Collections#sort()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.io.InputStream#skip()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.io.InputStream#read()</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">java.util.AbstractList#indexOf()</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; "><strong>Visitor:</strong></p>
<p style="margin: 0px 0px 10px; padding: 0px; ">作用于某个对象群中各个对象的操作. 它可以使你在不改变这些对象本身的情况下,定义作用于这些对象的新操作.</p>
<ul style="margin: 0px; padding: 0px 0px 10px; ">
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.lang.model.element.Element 和javax.lang.model.element.ElementVisitor</li>
    <li style="margin: 0px; padding: 0px 0px 0px 20px; list-style-position: inside; ">javax.lang.model.type.TypeMirror 和javax.lang.model.type.TypeVisitor</li>
</ul>
<p style="margin: 0px 0px 10px; padding: 0px; ">（全文完）</p>
</div>
<div style="margin: 15px 0px 0px; font-size: 11px; color: #cc0000; ">
<p align="center" style="margin: 0px 0px 10px; padding: 0px; "><strong>（转载本站文章请注明作者和出处&nbsp;<a href="http://coolshell.cn/" style="color: #2970a6; text-decoration: none; ">酷壳 &#8211; CoolShell.cn</a>&nbsp;，请勿用于任何商业用途）</strong></p>
</div>
<div style="margin: 0px 0px 50px; text-align: center; "><span style="background-color: #e3e4e6; font-family: 'Microsoft YaHei', Verdana, 'BitStream vera Sans', Arial, Helvetica, sans-serif; font-size: 12px; letter-spacing: 0em; line-height: normal; ">相关文章</span></div>
</div>
</div>
<div id="related_posts" style="margin: 10px 0px 0px; padding-bottom: 10px; box-shadow: #cccccc 0px 0px 5px 2px; color: #555555; font-family: Arial, Verdana, 'BitStream vera Sans', Tahoma, Helvetica, sans-serif; line-height: normal; ">
<div class="wp_rp_wrap  wp_rp_plain" id="wp_rp_first" style="margin: 0px; ">
<div class="wp_rp_content" style="margin: 0px; ">
<ul class="related_post wp_rp" style="margin: 0px; padding: 3px 12px 8px; background-image: url(http://coolshell.cn/wp-content/themes/inove/img/box.gif); background-color: #f4f5f7; visibility: visible; background-position: 100% 100%; background-repeat: no-repeat no-repeat; ">
    <li data-position="0" data-poid="in-6950" data-post-type="none" style="margin: 0px; padding: 3px 5px; list-style: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #e3e4e6; line-height: 18px; ">2012年03月26日 --&nbsp;<a href="http://coolshell.cn/articles/6950.html" class="wp_rp_title" style="color: #2970a6; text-decoration: none; ">需求变化与IoC</a></li>
    <li data-position="1" data-poid="in-7236" data-post-type="none" style="margin: 0px; padding: 3px 5px; list-style: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #e3e4e6; line-height: 18px; ">2012年05月03日 --&nbsp;<a href="http://coolshell.cn/articles/7236.html" class="wp_rp_title" style="color: #2970a6; text-decoration: none; ">用Unix的设计思想来应对多变的需求</a></li>
    <li data-position="2" data-poid="in-7992" data-post-type="none" style="margin: 0px; padding: 3px 5px; list-style: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #e3e4e6; line-height: 18px; ">2012年08月06日 --&nbsp;<a href="http://coolshell.cn/articles/7992.html" class="wp_rp_title" style="color: #2970a6; text-decoration: none; ">C++的坑真的多吗？</a></li>
    <li data-position="3" data-poid="in-8088" data-post-type="none" style="margin: 0px; padding: 3px 5px; list-style: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #e3e4e6; line-height: 18px; ">2012年08月16日 --&nbsp;<a href="http://coolshell.cn/articles/8088.html" class="wp_rp_title" style="color: #2970a6; text-decoration: none; ">对技术的态度</a></li>
    <li data-position="4" data-poid="in-1954" data-post-type="none" style="margin: 0px; padding: 3px 5px; list-style: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #e3e4e6; line-height: 18px; ">2009年12月15日 --&nbsp;<a href="http://coolshell.cn/articles/1954.html" class="wp_rp_title" style="color: #2970a6; text-decoration: none; ">Java读写Excel</a></li>
    <li data-position="5" data-poid="in-2483" data-post-type="none" style="margin: 0px; padding: 3px 5px; list-style: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #e3e4e6; line-height: 18px; ">2010年06月02日 --&nbsp;<a href="http://coolshell.cn/articles/2483.html" class="wp_rp_title" style="color: #2970a6; text-decoration: none; ">四个流行的Java连接池</a></li>
    <li data-position="6" data-poid="in-4844" data-post-type="none" style="margin: 0px; padding: 3px 5px; list-style: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #e3e4e6; line-height: 18px; ">2011年06月16日 --&nbsp;<a href="http://coolshell.cn/articles/4844.html" class="wp_rp_title" style="color: #2970a6; text-decoration: none; ">&#8220;另类&#8221; 设计模式</a></li>
    <li data-position="7" data-poid="in-8961" data-post-type="none" style="margin: 0px; padding: 3px 5px; list-style: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #e3e4e6; line-height: 18px; ">2013年02月01日 --&nbsp;<a href="http://coolshell.cn/articles/8961.html" class="wp_rp_title" style="color: #2970a6; text-decoration: none; ">从面向对象的设计模式看软件设计</a></li>
</ul>
</div>
</div>
</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/201504.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-07-04 04:56 <a href="http://www.cppblog.com/biao/archive/2013/07/04/201504.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>其他：序列号</title><link>http://www.cppblog.com/biao/archive/2013/07/02/201451.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Tue, 02 Jul 2013 01:52:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/07/02/201451.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/201451.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/07/02/201451.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/201451.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/201451.html</trackback:ping><description><![CDATA[@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&amp;file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div><span style="font-size: 14px;"><br />
<br />
1.&nbsp;<br />
QQ: 1819215588<br />
</span>机器码: YAIF-YAAJ-QAYO-PNIH<br />
序列号:&nbsp;ZNPQ-ZNNI-VNTL-OAPM</div>
<div><br />
</div>
<div>2.</div>
<div>QQ:&nbsp;<span style="font-family: Verdana; font-size: small; line-height: normal; ">463427077</span></div>
<div>机器码:&nbsp;<span style="font-family: Verdana; font-size: small; line-height: normal; ">YAII-FAGF-QNEO-FAIA</span><br />
序列号:&nbsp;ZNPX-WNVQ-VABL-WNPN</div><img src ="http://www.cppblog.com/biao/aggbug/201451.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-07-02 09:52 <a href="http://www.cppblog.com/biao/archive/2013/07/02/201451.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>设计模式文章收集</title><link>http://www.cppblog.com/biao/archive/2013/06/30/201403.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sun, 30 Jun 2013 04:35:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/06/30/201403.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/201403.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/06/30/201403.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/201403.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/201403.html</trackback:ping><description><![CDATA[@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&amp;file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&amp;file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&amp;file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&amp;file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);&nbsp;
<div><br />
</div>
<div>1. 命令模式
<div>&nbsp; &nbsp; (屌丝程序员版): 小左啊，这要加xxx功能:&nbsp;<a href="http://blog.csdn.net/zuoxiaolong8810/article/details/9153511">http://blog.csdn.net/zuoxiaolong8810/article/details/9153511</a>
<div><br />
</div>
<font color="#333333">2. Build模式</font></div>
</div>
<div><font color="#333333">&nbsp; &nbsp; (民工版): 要造房子，找几个民工(Builder)，一个设计师(Director)，开始吧:&nbsp;<a href="http://www.iteye.com/topic/71175">http://www.iteye.com/topic/71175</a></font></div>
<div>
<div><font color="#333333"><br />
</font></div>
</div><img src ="http://www.cppblog.com/biao/aggbug/201403.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-06-30 12:35 <a href="http://www.cppblog.com/biao/archive/2013/06/30/201403.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt：Use Third Party Libraries</title><link>http://www.cppblog.com/biao/archive/2013/06/28/201358.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 27 Jun 2013 23:31:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/06/28/201358.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/201358.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/06/28/201358.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/201358.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/201358.html</trackback:ping><description><![CDATA[<div class="descr" style="margin-top: 35px; margin-left: 5px; text-align: justify; vertical-align: top; color: #313131; font-family: Arial, Helvetica; line-height: 16px; ">
<p style="line-height: 20px;">Using a third-party library with Qt is a simple process. Suppose you know of a cross-platform library that accepts audio samples of a cat's meows and translates them into English words. This library is named&nbsp;<tt style="text-align: left;">CatWhisperer</tt>, and has several files that it provides as part of its library. Your project,&nbsp;<tt style="text-align: left;">MyQtApp</tt>, stores these files in a folder named&nbsp;<tt style="text-align: left;">3rdparty</tt>:</p>
<ul style="padding-bottom: 2px;">
    <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">MyQtApp/
    <ul style="padding-bottom: 2px;">
        <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">MyQtApp.pro</li>
        <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">src/
        <ul style="padding-bottom: 2px;">
            <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">main.cpp</li>
        </ul>
        </li>
        <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">3rdparty/
        <ul style="padding-bottom: 2px;">
            <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">CatWhisperer
            <ul style="padding-bottom: 2px;">
                <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">include/
                <ul style="padding-bottom: 2px;">
                    <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">CatWhisperer.h</li>
                </ul>
                </li>
                <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">lib/
                <ul style="padding-bottom: 2px;">
                    <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">libCatWhisperer.so</li>
                    <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">CatWhisperer.lib</li>
                </ul>
                </li>
                <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">bin/
                <ul style="padding-bottom: 2px;">
                    <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">CatWhisperer.dll</li>
                </ul>
                </li>
            </ul>
            </li>
        </ul>
        </li>
    </ul>
    </li>
</ul>
<p style="line-height: 20px;">To use the&nbsp;<tt style="text-align: left;">CatWhisperer</tt>&nbsp;library in&nbsp;<tt style="text-align: left;">MyQtApp</tt>,&nbsp;<tt style="text-align: left;">qmake</tt>&nbsp;requires the location and names of the&nbsp;<tt style="text-align: left;">CatWhisperer</tt>&nbsp;libraries. Optionally, you can also:</p>
<ul style="padding-bottom: 2px;">
    <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">Provide the location of the&nbsp;<tt>CatWhisperer</tt>&nbsp;source code so that you don't have to type out the full path to each file when you include them in your own code.</li>
    <li style="margin-bottom: 10px; padding-left: 8px; list-style: square outside; text-align: left;">Choose the destination in which the&nbsp;<tt>MyQtApp</tt>&nbsp;executable will be created.</li>
</ul>
<p style="line-height: 20px;">The information above is provided in the&nbsp;<tt style="text-align: left;">.pro</tt>&nbsp;file, so that&nbsp;<tt style="text-align: left;">qmake</tt>&nbsp;can parse it and produce makefiles. Makefiles contain all the information needed by your compiler and linker to produce output, whether it is an executable, another library file, etc. The next sections explain the syntax with which&nbsp;<tt style="text-align: left;">qmake</tt>expects you to provide this information.</p>
<a name="source-code" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a>
<h2 style="font-size: 16px; line-height: 1.2; font-family: Arial; font-weight: 100; background-color: #f2f3f4; padding: 4px; margin-bottom: 30px; margin-top: 30px; border-top-color: #e0e0de; border-top-width: 1px; border-top-style: solid; border-bottom-color: #e0e0de; border-bottom-width: 1px; border-bottom-style: solid; max-width: 99%; overflow: hidden; ">Source code</h2>
<p style="line-height: 20px;">To be able to write</p>
<pre class="cpp" style="border-top-left-radius: 7px; border-top-right-radius: 7px; border-bottom-right-radius: 7px; border-bottom-left-radius: 7px; background-color: #f6f6f6; border: 1px solid #e6e6e6; border-collapse: separate; margin: 10px; font-size: 12px; line-height: 1.2; overflow: auto; padding: 20px; "><span class="preprocessor">#include &lt;CatWhisperer.h&gt;</span></pre>
<p style="line-height: 20px;">instead of</p>
<pre class="cpp" style="border-top-left-radius: 7px; border-top-right-radius: 7px; border-bottom-right-radius: 7px; border-bottom-left-radius: 7px; background-color: #f6f6f6; border: 1px solid #e6e6e6; border-collapse: separate; margin: 10px; font-size: 12px; line-height: 1.2; overflow: auto; padding: 20px; "><span class="preprocessor">#include &lt;3rdparty/CatWhisperer/include/CatWhisperer.h&gt;</span></pre>
<p style="line-height: 20px;">you can provide the path to the&nbsp;<tt style="text-align: left;">CatWhisperer</tt>&nbsp;<tt style="text-align: left;">include</tt>&nbsp;directory, using the INCLUDEPATH variable:</p>
<pre class="cpp" style="border-top-left-radius: 7px; border-top-right-radius: 7px; border-bottom-right-radius: 7px; border-bottom-left-radius: 7px; background-color: #f6f6f6; border: 1px solid #e6e6e6; border-collapse: separate; margin: 10px; font-size: 12px; line-height: 1.2; overflow: auto; padding: 20px; ">INCLUDEPATH <span class="operator">+</span><span class="operator">=</span> <span class="number">3rdparty</span><span class="operator">/</span>CatWhisperer<span class="operator">/</span><span class="keyword">include</span></pre>
<a name="library-files" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a>
<h2 style="font-size: 16px; line-height: 1.2; font-family: Arial; font-weight: 100; background-color: #f2f3f4; padding: 4px; margin-bottom: 30px; margin-top: 30px; border-top-color: #e0e0de; border-top-width: 1px; border-top-style: solid; border-bottom-color: #e0e0de; border-bottom-width: 1px; border-bottom-style: solid; max-width: 99%; overflow: hidden; ">Library files</h2>
<p style="line-height: 20px;">To let&nbsp;<tt style="text-align: left;">qmake</tt>&nbsp;know where to find the&nbsp;<tt style="text-align: left;">CatWhisperer</tt>&nbsp;library files, use the LIBS variable:</p>
<pre class="cpp" style="border-top-left-radius: 7px; border-top-right-radius: 7px; border-bottom-right-radius: 7px; border-bottom-left-radius: 7px; background-color: #f6f6f6; border: 1px solid #e6e6e6; border-collapse: separate; margin: 10px; font-size: 12px; line-height: 1.2; overflow: auto; padding: 20px; ">LIBS <span class="operator">+</span><span class="operator">=</span> <span class="operator">-</span>L<span class="string">"3rdparty/CatWhisperer/lib"</span> <span class="operator">-</span>lCatWhisperer</pre>
<p style="line-height: 20px;">The first part of the expression lets the linker know in which directory it should look for the library files. The double quotes are only necessary when the path contains spaces, so we could have omitted them in this example.</p>
<p style="line-height: 20px;">The second part tells the linker which libraries to link against. We have two different library files for UNIX platforms and Windows, respectively:<tt style="text-align: left;">libCatWhisperer.so</tt>&nbsp;and&nbsp;<tt style="text-align: left;">CatWhisperer.lib</tt>. It is not necessary to specify the&nbsp;<tt style="text-align: left;">.lib</tt>&nbsp;extension, nor the&nbsp;<tt style="text-align: left;">lib</tt>&nbsp;prefix (on UNIX platforms).</p>
<a name="destination-directory" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a>
<h2 style="font-size: 16px; line-height: 1.2; font-family: Arial; font-weight: 100; background-color: #f2f3f4; padding: 4px; margin-bottom: 30px; margin-top: 30px; border-top-color: #e0e0de; border-top-width: 1px; border-top-style: solid; border-bottom-color: #e0e0de; border-bottom-width: 1px; border-bottom-style: solid; max-width: 99%; overflow: hidden; ">Destination directory</h2>
<p style="line-height: 20px;">By default,&nbsp;<tt style="text-align: left;">qmake</tt>&nbsp;creates the executable in the same directory as the&nbsp;<tt style="text-align: left;">.pro</tt>&nbsp;file. We can choose our own directory using the DESTDIR variable:</p>
<pre class="cpp" style="border-top-left-radius: 7px; border-top-right-radius: 7px; border-bottom-right-radius: 7px; border-bottom-left-radius: 7px; background-color: #f6f6f6; border: 1px solid #e6e6e6; border-collapse: separate; margin: 10px; font-size: 12px; line-height: 1.2; overflow: auto; padding: 20px; ">DESTDIR <span class="operator">=</span> bin</pre>
<p style="line-height: 20px;">That's it! You can now use the&nbsp;<tt style="text-align: left;">CatWhisperer</tt>&nbsp;library in your project. The final&nbsp;<tt style="text-align: left;">.pro</tt>&nbsp;file looks like this:</p>
<pre class="cpp" style="border-top-left-radius: 7px; border-top-right-radius: 7px; border-bottom-right-radius: 7px; border-bottom-left-radius: 7px; background-color: #f6f6f6; border: 1px solid #e6e6e6; border-collapse: separate; margin: 10px; font-size: 12px; line-height: 1.2; overflow: auto; padding: 20px; ">TARGET <span class="operator">=</span> MyQtApp
TEMPLATE <span class="operator">=</span> app
INCLUDEPATH <span class="operator">+</span><span class="operator">=</span> <span class="number">3rdparty</span><span class="operator">/</span>CatWhisperer<span class="operator">/</span><span class="keyword">include</span>
SOURCES <span class="operator">+</span><span class="operator">=</span> src<span class="operator">/</span>main<span class="operator">.</span>cpp
LIBS <span class="operator">+</span><span class="operator">=</span> <span class="operator">-</span>L<span class="string">"3rdparty/CatWhisperer/lib"</span> <span class="operator">-</span>lCatWhisperer</pre>
</div>
<p style="line-height: 20px; color: #313131; font-family: Arial, Helvetica; text-align: justify; "><strong>See also&nbsp;</strong><a href="qthelp://org.qt-project.qtdoc.510/qtdoc/qmake-manual.html" style="color: #007330; text-decoration: none; text-align: left;">qmake Manual</a>&nbsp;and Adding Libraries to Projects.</p>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/201358.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-06-28 07:31 <a href="http://www.cppblog.com/biao/archive/2013/06/28/201358.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java：使用JDK中的安全包对数据进行加解密</title><link>http://www.cppblog.com/biao/archive/2013/06/27/201337.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 27 Jun 2013 00:05:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/06/27/201337.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/201337.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/06/27/201337.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/201337.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/201337.html</trackback:ping><description><![CDATA[From:&nbsp;<a href="http://blog.csdn.net/noliyo/article/details/9166365">http://blog.csdn.net/noliyo/article/details/9166365</a>@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div><br />
</div>
<div>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">本文以使用DES对称加密算法为例使用jdk对数据进行加密解密。</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />
</p>
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">首先需要了解Provider类，它是jdk引入的密码服务提供者概念，实现了Java安全性的一部分或者全部。Provider 可能实现的服务包括：&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">算法（如DES、RSA、MD5）；密钥的生成、转换和管理。&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">通常java运行时环境至少安装了一个名字为&#8220;SUN&#8221;的预设Provider，如果查看本机支持的Provider类型可以通过以下代码：</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<div class="dp-highlighter bg_java" style="width: 687px; overflow: hidden; text-align: left; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/black/java.gif); background-color: #333333; opacity: 0.6; color: #333333; line-height: 26px; background-position: 100% 0%; background-repeat: no-repeat no-repeat; ">
<div class="bar" style="position: relative; ">
<div class="tools" style="border-left-color: #999999; "><strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="ViewSource" title="view plain" style="color: #cccccc; ">view plain</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="CopyToClipboard" title="copy" style="color: #cccccc; ">copy</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="PrintSource" title="print" style="color: #cccccc; ">print</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="About" title="?" style="color: #cccccc; ">?</a>
<div style="position: absolute; left: 416px; top: 627px; width: 21px; height: 11px; z-index: 99; "><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="21" height="11" name="ZeroClipboardMovie_1" align="center" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&amp;width=21&amp;height=11" wmode="transparent"></div>
</div>
</div>
<ol start="1" class="dp-j" style="position: relative; ">
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">for</span><span style="color: #ffffff; ">(Provider&nbsp;p&nbsp;:&nbsp;Security.getProviders()){&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(p.getName()+<span class="string" style="color: #ff9900; ">"："</span><span style="color: #ffffff; ">+p.getInfo());&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">}&nbsp;&nbsp;</span></li>
</ol>
</div>
<br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">我本机运行结果为：</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<div class="dp-highlighter bg_java" style="width: 687px; overflow: hidden; text-align: left; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/black/java.gif); background-color: #333333; opacity: 0.6; color: #333333; line-height: 26px; background-position: 100% 0%; background-repeat: no-repeat no-repeat; ">
<div class="bar" style="position: relative; ">
<div class="tools" style="border-left-color: #999999; "><strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="ViewSource" title="view plain" style="color: #cccccc; ">view plain</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="CopyToClipboard" title="copy" style="color: #cccccc; ">copy</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="PrintSource" title="print" style="color: #cccccc; ">print</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="About" title="?" style="color: #cccccc; ">?</a>
<div style="position: absolute; left: 416px; top: 795px; width: 21px; height: 11px; z-index: 99; "><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="21" height="11" name="ZeroClipboardMovie_2" align="center" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&amp;width=21&amp;height=11" wmode="transparent"></div>
</div>
</div>
<ol start="1" class="dp-j" style="position: relative; ">
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span style="color: #ffffff; ">SUN：SUN&nbsp;(DSA&nbsp;key/parameter&nbsp;generation;&nbsp;DSA&nbsp;signing;&nbsp;SHA-</span><span class="number" style="color: #c00000; ">1</span><span style="color: #ffffff; ">,&nbsp;MD5&nbsp;digests;&nbsp;SecureRandom;&nbsp;X.</span><span class="number" style="color: #c00000; ">509</span><span style="color: #ffffff; ">&nbsp;certificates;&nbsp;JKS&nbsp;keystore;&nbsp;PKIX&nbsp;CertPathValidator;&nbsp;PKIX&nbsp;CertPathBuilder;&nbsp;LDAP,&nbsp;Collection&nbsp;CertStores,&nbsp;JavaPolicy&nbsp;Policy;&nbsp;JavaLoginConfig&nbsp;Configuration)&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">SunRsaSign：Sun&nbsp;RSA&nbsp;signature&nbsp;provider&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">SunJSSE：Sun&nbsp;JSSE&nbsp;provider(PKCS12,&nbsp;SunX509&nbsp;key/trust&nbsp;factories,&nbsp;SSLv3,&nbsp;TLSv1)&nbsp;&nbsp;</span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">SunJCE：SunJCE&nbsp;Provider&nbsp;(<span class="keyword" style="color: #66ccff; ">implements</span><span style="color: #ffffff; ">&nbsp;RSA,&nbsp;DES,&nbsp;Triple&nbsp;DES,&nbsp;AES,&nbsp;Blowfish,&nbsp;ARCFOUR,&nbsp;RC2,&nbsp;PBE,&nbsp;Diffie-Hellman,&nbsp;HMAC)&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">SunJGSS：Sun&nbsp;(Kerberos&nbsp;v5,&nbsp;SPNEGO)&nbsp;&nbsp;</span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">SunSASL：Sun&nbsp;SASL&nbsp;provider(<span class="keyword" style="color: #66ccff; ">implements</span><span style="color: #ffffff; ">&nbsp;client&nbsp;mechanisms&nbsp;</span><span class="keyword" style="color: #66ccff; ">for</span><span style="color: #ffffff; ">:&nbsp;DIGEST-MD5,&nbsp;GSSAPI,&nbsp;EXTERNAL,&nbsp;PLAIN,&nbsp;CRAM-MD5;&nbsp;server&nbsp;mechanisms&nbsp;</span><span class="keyword" style="color: #66ccff; ">for</span><span style="color: #ffffff; ">:&nbsp;DIGEST-MD5,&nbsp;GSSAPI,&nbsp;CRAM-MD5)&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">XMLDSig：XMLDSig&nbsp;(DOM&nbsp;XMLSignatureFactory;&nbsp;DOM&nbsp;KeyInfoFactory)&nbsp;&nbsp;</span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">SunPCSC：Sun&nbsp;PC/SC&nbsp;provider&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">SunMSCAPI：Sun's&nbsp;Microsoft&nbsp;Crypto&nbsp;API&nbsp;provider&nbsp;&nbsp;</span></li>
</ol>
</div>
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">从结果中可以看到名称为&#8220;SunJCE&#8221;的Provider提供DES、Triple DES（即3DES）, AES、RSA、Diffie-Hellman等算法的实现，下面使用此Provider完成对称加密。</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">KeyGenerator类提供（对称）密钥生成器的功能，使用getInstance 类方法构造，代码如下：</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<div class="dp-highlighter bg_java" style="width: 687px; overflow: hidden; text-align: left; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/black/java.gif); background-color: #333333; opacity: 0.6; color: #333333; line-height: 26px; background-position: 100% 0%; background-repeat: no-repeat no-repeat; ">
<div class="bar" style="position: relative; ">
<div class="tools" style="border-left-color: #999999; "><strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="ViewSource" title="view plain" style="color: #cccccc; ">view plain</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="CopyToClipboard" title="copy" style="color: #cccccc; ">copy</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="PrintSource" title="print" style="color: #cccccc; ">print</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="About" title="?" style="color: #cccccc; ">?</a>
<div style="position: absolute; left: 416px; top: 1213px; width: 21px; height: 11px; z-index: 99; "><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="21" height="11" name="ZeroClipboardMovie_3" align="center" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&amp;width=21&amp;height=11" wmode="transparent"></div>
</div>
</div>
<ol start="1" class="dp-j" style="position: relative; ">
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span style="color: #ffffff; ">KeyGenerator&nbsp;kg&nbsp;=&nbsp;KeyGenerator.getInstance(</span><span class="string" style="color: #ff9900; ">"DES"</span><span style="color: #ffffff; ">,</span><span class="string" style="color: #ff9900; ">"SunJCE"</span><span style="color: #ffffff; ">);&nbsp;&nbsp;</span></span></li>
</ol>
</div>
<br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">SecretKey类是对称密钥的封装类，它不包含方法或常量，其唯一目的是分组对称密钥（并为其提供类型安全），从kg中获取&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<div class="dp-highlighter bg_java" style="width: 687px; overflow: hidden; text-align: left; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/black/java.gif); background-color: #333333; opacity: 0.6; color: #333333; line-height: 26px; background-position: 100% 0%; background-repeat: no-repeat no-repeat; ">
<div class="bar" style="position: relative; ">
<div class="tools" style="border-left-color: #999999; "><strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="ViewSource" title="view plain" style="color: #cccccc; ">view plain</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="CopyToClipboard" title="copy" style="color: #cccccc; ">copy</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="PrintSource" title="print" style="color: #cccccc; ">print</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="About" title="?" style="color: #cccccc; ">?</a>
<div style="position: absolute; left: 416px; top: 1371px; width: 21px; height: 11px; z-index: 99; "><embed id="ZeroClipboardMovie_4" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="21" height="11" name="ZeroClipboardMovie_4" align="center" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&amp;width=21&amp;height=11" wmode="transparent"></div>
</div>
</div>
<ol start="1" class="dp-j" style="position: relative; ">
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span style="color: #ffffff; ">SecretKey&nbsp;key&nbsp;=&nbsp;kg.generateKey();&nbsp;&nbsp;</span></span></li>
</ol>
</div>
<br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">以上是产生密钥的过程，真正实现对数据加解密功能的类是Cipher，此类为加密和解密提供密码功能，它构成了 Java Cryptographic Extension (JCE) 框架的核心。</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">为创建 Cipher 对象，应用程序调用 Cipher 的 getInstance 方法并将所请求&#8220;转换&#8221;的名称传递给它，还可以指定Provider的名称，本文中采用&#8220;SunJCE&#8221;。</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&#8220;转换&#8221;是一个字符串，它描述为产生某种输出而在给定的输入上执行的操作（或一组操作）。转换始终包括加密算法的名称（例如，DES），后面可能跟有一个反馈模式和填充方案（反馈模式和填充方案可以理解为 加密前对数据的预处理）</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&#8220;转换&#8221;具有以下两种形式：&#8220;算法/模式/填充&#8221;或&#8220;算法&#8221;（这种情况，使用模式和填充方案特定于Provider的默认值）。本例中采用以下有效的转换：</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<div class="dp-highlighter bg_java" style="width: 687px; overflow: hidden; text-align: left; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/black/java.gif); background-color: #333333; opacity: 0.6; color: #333333; line-height: 26px; background-position: 100% 0%; background-repeat: no-repeat no-repeat; ">
<div class="bar" style="position: relative; ">
<div class="tools" style="border-left-color: #999999; "><strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="ViewSource" title="view plain" style="color: #cccccc; ">view plain</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="CopyToClipboard" title="copy" style="color: #cccccc; ">copy</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="PrintSource" title="print" style="color: #cccccc; ">print</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="About" title="?" style="color: #cccccc; ">?</a>
<div style="position: absolute; left: 416px; top: 1711px; width: 21px; height: 11px; z-index: 99; "><embed id="ZeroClipboardMovie_5" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="21" height="11" name="ZeroClipboardMovie_5" align="center" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&amp;width=21&amp;height=11" wmode="transparent"></div>
</div>
</div>
<ol start="1" class="dp-j" style="position: relative; ">
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span style="color: #ffffff; ">Cipher&nbsp;c&nbsp;=&nbsp;Cipher.getInstance(</span><span class="string" style="color: #ff9900; ">"DES/CBC/PKCS5Padding"</span><span style="color: #ffffff; ">,</span><span class="string" style="color: #ff9900; ">"SunJCE"</span><span style="color: #ffffff; ">);</span><span class="comment" style="color: #cccccc; ">//DES加密算法，CBC的反馈模式，PKCS5Padding的填充方案</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
</ol>
</div>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">使用CBC反馈模式需要一个初始化向量，由类IvParameterSpec来完成，它需要是一个长度为8的byte数组。<br />
</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />
</p>
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">Cipher的init方法负责初始化，初始化需要指定模式和密钥，密钥本文采用上面已经生成的key，模式有四种ENCRYPT_MODE、DECRYPT_MODE、WRAP_MODE、UNWRAP_MODE分别表示加密、解密、密钥包装或密钥解包。</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">如果加密则采用方法init(Cipher.ENCRYPT_MODE, key)；</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">如果解密则采用方法init(Cipher.DECRYPT_MODE, key)；</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">WRAP_MODE、UNWRAP_MODE模式是用来实现数字信封用的，本文不作介绍。</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">通常对数据进行加解密，使用方法doFinal(byte[] input)，传入参数是byte数组。</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">为了在http协议下快速传输数据，且某些系统中只能使用ASCII字符，通常采用Base64编码。Base64就是用来将非ASCII字符的数据转换成ASCII字符的一种方法。还可以提高可视性。</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">通过以上分析可以得到以下代码：&nbsp;</span><br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<div class="dp-highlighter bg_java" style="width: 687px; overflow: hidden; text-align: left; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/black/java.gif); background-color: #333333; opacity: 0.6; color: #333333; line-height: 26px; background-position: 100% 0%; background-repeat: no-repeat no-repeat; ">
<div class="bar" style="position: relative; ">
<div class="tools" style="border-left-color: #999999; "><strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="ViewSource" title="view plain" style="color: #cccccc; ">view plain</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="CopyToClipboard" title="copy" style="color: #cccccc; ">copy</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="PrintSource" title="print" style="color: #cccccc; ">print</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="About" title="?" style="color: #cccccc; ">?</a>
<div style="position: absolute; left: 416px; top: 2175px; width: 21px; height: 11px; z-index: 99; "><embed id="ZeroClipboardMovie_6" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="21" height="11" name="ZeroClipboardMovie_6" align="center" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=6&amp;width=21&amp;height=11" wmode="transparent"></div>
</div>
</div>
<ol start="1" class="dp-j" style="position: relative; ">
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;java.io.IOException;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;java.io.UnsupportedEncodingException;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;java.security.InvalidAlgorithmParameterException;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;java.security.InvalidKeyException;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;java.security.NoSuchAlgorithmException;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;java.security.NoSuchProviderException;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;javax.crypto.BadPaddingException;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;javax.crypto.Cipher;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;javax.crypto.IllegalBlockSizeException;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;javax.crypto.KeyGenerator;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;javax.crypto.NoSuchPaddingException;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;javax.crypto.SecretKey;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;javax.crypto.spec.IvParameterSpec;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;sun.misc.BASE64Decoder;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">import</span><span style="color: #ffffff; ">&nbsp;sun.misc.BASE64Encoder;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="keyword" style="color: #66ccff; ">public</span><span style="color: #ffffff; ">&nbsp;</span><span class="keyword" style="color: #66ccff; ">class</span><span style="color: #ffffff; ">&nbsp;SecurityTest&nbsp;{&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">//密钥生成器</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">private</span><span style="color: #ffffff; ">&nbsp;KeyGenerator&nbsp;kg;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">//对称密钥&nbsp;&nbsp;</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">private</span><span style="color: #ffffff; ">&nbsp;SecretKey&nbsp;key;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">//加解密时的初始化向量must&nbsp;be&nbsp;8&nbsp;bytes&nbsp;long</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">private</span><span style="color: #ffffff; ">&nbsp;IvParameterSpec&nbsp;iv;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">//Cipher,加解密主体实例</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">private</span><span style="color: #ffffff; ">&nbsp;Cipher&nbsp;c;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">//该构造方法初始化DES密钥和Cipher&nbsp;&nbsp;</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">public</span><span style="color: #ffffff; ">&nbsp;SecurityTest()&nbsp;</span><span class="keyword" style="color: #66ccff; ">throws</span><span style="color: #ffffff; ">&nbsp;NoSuchAlgorithmException,&nbsp;NoSuchProviderException,&nbsp;NoSuchPaddingException,&nbsp;UnsupportedEncodingException{&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;kg&nbsp;=&nbsp;KeyGenerator.getInstance(<span class="string" style="color: #ff9900; ">"DES"</span><span style="color: #ffffff; ">,</span><span class="string" style="color: #ff9900; ">"SunJCE"</span><span style="color: #ffffff; ">);&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key&nbsp;=&nbsp;kg.generateKey();&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iv&nbsp;=&nbsp;<span class="keyword" style="color: #66ccff; ">new</span><span style="color: #ffffff; ">&nbsp;IvParameterSpec(</span><span class="string" style="color: #ff9900; ">"12345678"</span><span style="color: #ffffff; ">.getBytes(</span><span class="string" style="color: #ff9900; ">"UTF-8"</span><span style="color: #ffffff; ">));&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;=&nbsp;Cipher.getInstance(<span class="string" style="color: #ff9900; ">"DES/CBC/PKCS5Padding"</span><span style="color: #ffffff; ">,</span><span class="string" style="color: #ff9900; ">"SunJCE"</span><span style="color: #ffffff; ">);</span><span class="comment" style="color: #cccccc; ">//DES加密算法，CBC的反馈模式，PKCS5Padding的填充方案&nbsp;，SunJCE：Provider</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;</span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">/**&nbsp;</span>&nbsp;</span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="comment" style="color: #cccccc; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;DES加密</span>&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="comment" style="color: #cccccc; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">public</span><span style="color: #ffffff; ">&nbsp;String&nbsp;encrypt(String&nbsp;src)&nbsp;</span><span class="keyword" style="color: #66ccff; ">throws</span><span style="color: #ffffff; ">&nbsp;InvalidKeyException,&nbsp;IllegalBlockSizeException,&nbsp;BadPaddingException,&nbsp;InvalidAlgorithmParameterException{&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">//初始化--&nbsp;ENCRYPT_MODE：加密模式，&nbsp;key：密钥，iv：初始化向量</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.init(Cipher.ENCRYPT_MODE,&nbsp;key,&nbsp;iv);&nbsp;&nbsp;&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">byte</span><span style="color: #ffffff; ">[]&nbsp;srcByte&nbsp;=&nbsp;src.getBytes();&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">//加密</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">byte</span><span style="color: #ffffff; ">[]&nbsp;targetByte&nbsp;=&nbsp;c.doFinal(srcByte);&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">//Base64编码</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;targetString&nbsp;=&nbsp;<span class="keyword" style="color: #66ccff; ">new</span><span style="color: #ffffff; ">&nbsp;BASE64Encoder().encode(targetByte);&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">return</span><span style="color: #ffffff; ">&nbsp;targetString;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;</span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">/**&nbsp;</span>&nbsp;</span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="comment" style="color: #cccccc; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;DES解密&nbsp;</span>&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span class="comment" style="color: #cccccc; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">public</span><span style="color: #ffffff; ">&nbsp;String&nbsp;decrypt(String&nbsp;srcString)&nbsp;</span><span class="keyword" style="color: #66ccff; ">throws</span><span style="color: #ffffff; ">&nbsp;InvalidKeyException,&nbsp;IllegalBlockSizeException,&nbsp;BadPaddingException,&nbsp;IOException,&nbsp;InvalidAlgorithmParameterException&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">//初始化--&nbsp;DECRYPT_MODE：解密模式，&nbsp;key：密钥，iv：初始化向量</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c.init(Cipher.DECRYPT_MODE,&nbsp;key,&nbsp;iv);&nbsp;&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">//Base64解码</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">byte</span><span style="color: #ffffff; ">[]&nbsp;srcByte&nbsp;=&nbsp;</span><span class="keyword" style="color: #66ccff; ">new</span><span style="color: #ffffff; ">&nbsp;BASE64Decoder().decodeBuffer(srcString);&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="color: #cccccc; ">//解密</span><span style="color: #ffffff; ">&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">byte</span><span style="color: #ffffff; ">[]&nbsp;targetByte&nbsp;=&nbsp;c.doFinal(srcByte);&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">return</span><span style="color: #ffffff; ">&nbsp;</span><span class="keyword" style="color: #66ccff; ">new</span><span style="color: #ffffff; ">&nbsp;String(targetByte);&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="color: #66ccff; ">public</span><span style="color: #ffffff; ">&nbsp;</span><span class="keyword" style="color: #66ccff; ">static</span><span style="color: #ffffff; ">&nbsp;</span><span class="keyword" style="color: #66ccff; ">void</span><span style="color: #ffffff; ">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span class="keyword" style="color: #66ccff; ">throws</span><span style="color: #ffffff; ">&nbsp;Exception&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SecurityTest&nbsp;test&nbsp;=&nbsp;<span class="keyword" style="color: #66ccff; ">new</span><span style="color: #ffffff; ">&nbsp;SecurityTest();&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;src&nbsp;=&nbsp;<span class="string" style="color: #ff9900; ">"使用JKD进行加解密--DES算法测试！__小印"</span><span style="color: #ffffff; ">;&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string" style="color: #ff9900; ">"原文:"</span><span style="color: #ffffff; ">&nbsp;+&nbsp;src);&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;secret&nbsp;=&nbsp;test.encrypt(src);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string" style="color: #ff9900; ">"密文:"</span><span style="color: #ffffff; ">&nbsp;+&nbsp;secret);&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;target&nbsp;=&nbsp;test.decrypt(secret);&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span class="string" style="color: #ff9900; ">"解密得到原文:"</span><span style="color: #ffffff; ">&nbsp;+&nbsp;target);&nbsp;&nbsp;&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;</span></li>
</ol>
</div>
<br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">我本机运行结果为：</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "></p>
<div class="dp-highlighter bg_java" style="width: 687px; overflow: hidden; text-align: left; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/black/java.gif); background-color: #333333; opacity: 0.6; color: #333333; line-height: 26px; background-position: 100% 0%; background-repeat: no-repeat no-repeat; ">
<div class="bar" style="position: relative; ">
<div class="tools" style="border-left-color: #999999; "><strong>[java]</strong>&nbsp;<a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="ViewSource" title="view plain" style="color: #cccccc; ">view plain</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="CopyToClipboard" title="copy" style="color: #cccccc; ">copy</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="PrintSource" title="print" style="color: #cccccc; ">print</a><a href="http://blog.csdn.net/noliyo/article/details/9166365#" class="About" title="?" style="color: #cccccc; ">?</a>
<div style="position: absolute; left: 416px; top: 3653px; width: 21px; height: 11px; z-index: 99; "><embed id="ZeroClipboardMovie_7" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="21" height="11" name="ZeroClipboardMovie_7" align="center" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=7&amp;width=21&amp;height=11" wmode="transparent"></div>
</div>
</div>
<ol start="1" class="dp-j" style="position: relative; ">
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; "><span style="color: #ffffff; ">原文:使用JKD进行加解密--DES算法测试！__小印&nbsp;&nbsp;</span></span></li>
    <li class="" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">密文:P8Xi4Jfvq9Eus0c1OTE32wK60rjHwPxZ8tRiElxOd5m69+vI37IGtw==&nbsp;&nbsp;</span></li>
    <li class="alt" style="border-left-color: #999999; color: #eeeeee; line-height: 18px; "><span style="color: #ffffff; ">解密得到原文:使用JKD进行加解密--DES算法测试！__小印&nbsp;&nbsp;</span></li>
</ol>
</div>
<br style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; " />
<span style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">注意：本文采用的是随机密钥，所以每次运行得到的密文是不一样的，jdk也提供自定义的密钥 ^_^</span></div><img src ="http://www.cppblog.com/biao/aggbug/201337.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-06-27 08:05 <a href="http://www.cppblog.com/biao/archive/2013/06/27/201337.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Qt：解读Q_GLOBAL_STATIC(QFontDatabasePrivate, privateDb)  </title><link>http://www.cppblog.com/biao/archive/2013/06/26/201312.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Wed, 26 Jun 2013 02:02:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/06/26/201312.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/201312.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/06/26/201312.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/201312.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/201312.html</trackback:ping><description><![CDATA[<span style="color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 24.545454025268555px; background-color: #ffffff;">From:&nbsp;</span><a href="http://zhgw01.blog.163.com/blog/static/1041481220105253491586/">http://zhgw01.blog.163.com/blog/static/1041481220105253491586/</a><br /><span style="color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 24.545454025268555px; background-color: #ffffff;">根据 src/corelib/global.h</span><br style="line-height: 24.545454025268555px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff;" /><table border="1" cellpadding="1" cellspacing="1" width="80%" style="line-height: 24.545454025268555px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; font-size: 13.63636302947998px; background-color: #ffffff;"><tbody><tr><td>&nbsp;template &lt;typename T&gt;<br />class QGlobalStatic<br />{<br />public:<br />&nbsp;&nbsp;&nbsp; T *pointer;<br />&nbsp;&nbsp;&nbsp; inline QGlobalStatic(T *p) : pointer(p) { }<br />&nbsp;&nbsp;&nbsp; inline ~QGlobalStatic() { pointer = 0; }<br />};<br /><br />#define Q_GLOBAL_STATIC(TYPE, NAME)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br />&nbsp;&nbsp;&nbsp; static TYPE *NAME()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br />&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static TYPE this_##NAME;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static QGlobalStatic&lt;TYPE &gt; global_##NAME(&amp;this_##NAME); \<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return global_##NAME.pointer;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \<br />&nbsp;&nbsp;&nbsp; }<br /><br /></td></tr></tbody></table><span style="color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 24.545454025268555px; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;</span><br style="line-height: 24.545454025268555px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff;" /><br style="line-height: 24.545454025268555px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff;" /><span style="color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 24.545454025268555px; background-color: #ffffff;">上面的展开成</span><br style="line-height: 24.545454025268555px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff;" /><table border="1" cellpadding="1" cellspacing="1" height="120" width="616" style="line-height: 24.545454025268555px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; font-size: 13.63636302947998px; background-color: #ffffff;"><tbody><tr><td>&nbsp;static&nbsp; QFontDatabasePrivate *privateDb()<br />{<br />&nbsp;&nbsp;&nbsp; static QFontDatabasePrivate this_privateDb;&nbsp; // 注意第一次引用的时候会开始进行初始化, 以后不再生成<br />&nbsp;&nbsp; static QGLobalStatic&lt;QFontDatabasePrivate&gt; global_privateDb(&amp;this_privateDb); //对上面生成的对象的指针进行包装， 同上，只有第一次才会生成<br /><br />&nbsp; return global_privateDb;<br />}<br /></td></tr></tbody></table><p style="line-height: 24.545454025268555px; margin: 0px 0px 10px; padding: 0px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff;">QFontDatabase的构造函数中会调用createDatabase （里面调用了initializeDb), initializeDb里调用了privateDb(),一个这个时候是第一次调用， 会生成QFontDatabasePrivate对象</p><p style="line-height: 24.545454025268555px; margin: 0px 0px 10px; padding: 0px; color: #333333; font-family: Arial, Helvetica, simsun, u5b8bu4f53; background-color: #ffffff;">QFontDatabasePrivate中的QDataStream *stream成员才是真正处理qte的字体</p><img src ="http://www.cppblog.com/biao/aggbug/201312.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-06-26 10:02 <a href="http://www.cppblog.com/biao/archive/2013/06/26/201312.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++：QT插件开发方式</title><link>http://www.cppblog.com/biao/archive/2013/06/25/201308.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Tue, 25 Jun 2013 15:26:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/06/25/201308.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/201308.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/06/25/201308.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/201308.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/201308.html</trackback:ping><description><![CDATA[From:&nbsp;http://blog.csdn.net/henreash/article/details/7264489@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">创建一个QT的库项目,删除自动生成的.h和.cpp文件,添加一个接口定义.h文件和一个接口实现类(一个.h一个.cpp).代码如下:</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">1.接口文件源码</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#ifndef PLUGININTERFACE_H<br />
#define PLUGININTERFACE_H</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#include &lt;QString&gt;</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />
class EchoInterface<br />
{<br />
public:<br />
&nbsp;virtual ~EchoInterface() {}<br />
&nbsp;virtual QString echo(const QString &amp;message) = 0;<br />
};</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">QT_BEGIN_NAMESPACE<br />
Q_DECLARE_INTERFACE(EchoInterface, "com.hollysys.plugin.EchoIntrface/1.0");<br />
QT_END_NAMESPACE</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />
#endif</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">2.接口实现类头文件</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#ifndef PLUGIN_H<br />
#define PLUGIN_H</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">//#include "plugin_global.h"<br />
#include &lt;QObject&gt;<br />
#include "plugininterface.h"</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">class Plugin : public QObject, EchoInterface<br />
{<br />
&nbsp;Q_OBJECT<br />
&nbsp;Q_INTERFACES(EchoInterface)<br />
public:<br />
&nbsp;Plugin();<br />
&nbsp;~Plugin();<br />
public:<br />
&nbsp;QString echo(const QString &amp;message);</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />
};</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#endif // PLUGIN_H</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">3.接口实现类cpp文件</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#include "plugin.h"<br />
#include &lt;QtGui&gt;</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; "><br />
Plugin::Plugin()<br />
{</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">}</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">Plugin::~Plugin()<br />
{</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">}</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">QString Plugin::echo(const QString &amp;message)<br />
{<br />
&nbsp;return message;<br />
}</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">Q_EXPORT_PLUGIN2("echoPlugin", Plugin);<br />
</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">编译生成dll格式的插件.这里可能会遇到LNK2001错误,因为插件接口定义依赖于QT的元数据,而在代码中手动添加Q_OBJECT宏后,编译器不会自动为我们生成moc_XXXX.cpp文件,因此需要使用命令行生成moc元数据文件:cmd--&gt;cd 源码所在命令 --&gt;moc -o moc_XXXX.cpp XXXX.h.这样就可以手动创建出moc元数据文件,解决LNK2001编译错误.</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">建立一个GUI测试项目,代码如下:</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">#include "test.h"<br />
#include &lt;QtGui&gt;<br />
#include &lt;QMessageBox&gt;<br />
#include &lt;QDir&gt;</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">Test::Test(QWidget *parent, Qt::WFlags flags)<br />
&nbsp;: QMainWindow(parent, flags)<br />
{<br />
&nbsp;ui.setupUi(this);<br />
&nbsp;QObject::connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(ButtonClicked()));<br />
&nbsp;loadPlugin();<br />
}</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">Test::~Test()<br />
{</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">}</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">bool Test::loadPlugin()<br />
{<br />
&nbsp;QDir pluginsDir(qApp-&gt;applicationDirPath());<br />
&nbsp;foreach (QString filename, pluginsDir.entryList(QDir::Files))<br />
&nbsp;{<br />
&nbsp;&nbsp;QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(filename));<br />
&nbsp;&nbsp;QObject *plugin = pluginLoader.instance();<br />
&nbsp;&nbsp;if(plugin)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;echoInterface = qobject_cast&lt;EchoInterface *&gt;(plugin);//echoInterface是成员变量<br />
&nbsp;&nbsp;&nbsp;if(echoInterface)<br />
&nbsp;&nbsp;&nbsp;&nbsp;return true;<br />
&nbsp;&nbsp;}<br />
&nbsp;}<br />
}</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">void Test::ButtonClicked()<br />
{<br />
&nbsp;QString plugin = ui.lineEdit-&gt;text();<br />
&nbsp;QMessageBox::information(NULL, "", echoInterface-&gt;echo(plugin));<br />
}</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">分析代码可见,这里对所有与exe文件同目录的文件尝试加载插件,如果加载成功则返回.代码实现非常简洁.在正式开发中可将已加载的插件接口存放在列表中,在需要的时候依次调用即可.</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">另外如果想获取插件中类定义的元数据,可以使插件接口类从QObject继承,并使用Q_CLASSINFO宏添加键值对,如Q_CLASSINFO("Author**", "Henreash**").并从主程序中使用插件对象元数据获取这些键值对:</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">&nbsp;const QMetaObject *mo = echoInterface-&gt;metaObject();<br />
&nbsp;for(int i = 0; i &lt; mo-&gt;classInfoCount(); i++)<br />
&nbsp;{<br />
&nbsp;&nbsp;qDebug() &lt;&lt; mo-&gt;classInfo(i).name() &lt;&lt; mo-&gt;classInfo(i).value();<br />
&nbsp;}</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">使用这个机制可以在定义插件类的时候向主程序传递一些特殊信息.</p>
<p style="color: #333333; font-family: Arial; line-height: 26px; text-align: left; background-color: #ffffff; ">测试发现,如果在插件接口类中定义一个静态变量,那么这个静态变量在插件中的地址和在主程序中的地址是不相同的.</p>
</div><img src ="http://www.cppblog.com/biao/aggbug/201308.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-06-25 23:26 <a href="http://www.cppblog.com/biao/archive/2013/06/25/201308.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++：单例 Singleton</title><link>http://www.cppblog.com/biao/archive/2013/06/25/201306.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Tue, 25 Jun 2013 13:46:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/06/25/201306.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/201306.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/06/25/201306.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/201306.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/201306.html</trackback:ping><description><![CDATA[<p style="margin: 0.4em 0px 0.5em; line-height: 22px; font-family: sans-serif; font-size: 15px; background-color: #ffffff; ">From:&nbsp;http://zh.wikipedia.org/wiki/单例模式</p>
<p style="margin: 0.4em 0px 0.5em; line-height: 22px; font-family: sans-serif; font-size: 15px; background-color: #ffffff; ">在<a href="http://zh.wikipedia.org/wiki/C%2B%2B" title="C++" style="text-decoration: none; color: #0b0080; background-image: none; background-position: initial initial; background-repeat: initial initial; ">C++</a>编程语言中，单例模式应用的例子如下述代码所示（这里仅仅提供一个示例，这个例子对多线程的情况并不是安全的）：</p>
<div dir="ltr" class="mw-geshi mw-code mw-content-ltr" style="direction: ltr; font-family: monospace, Courier; padding: 1em; border-left-color: #2f6fab; background-color: #f9f9f9; line-height: 1.1em; font-size: 15px; ">
<div class="cpp source-cpp" style="line-height: normal; font-family: monospace; ">
<pre class="de1" style="font-family: monospace, monospace; padding: 0px; border: 0px none white; line-height: 1.2em; font-size: 1em; margin-top: 0px; margin-bottom: 0px; background-image: none; vertical-align: top; "><span class="co1" style="color: #666666; ">// ...</span>
<span class="kw2" style="color: #0000ff; ">class</span> lock
<span class="br0" style="color: #008000; ">{</span>
<span class="kw2" style="color: #0000ff; ">public</span><span class="sy4" style="color: #008080; ">:</span>
lock<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
lock<span class="br0" style="color: #008000; ">(</span>lock <span class="kw4" style="color: #0000ff; ">const</span> <span class="sy3" style="color: #000040; ">&amp;</span> l<span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
~lock<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
lock <span class="sy3" style="color: #000040; ">&amp;</span> operator <span class="sy1" style="color: #000080; ">=</span><span class="br0" style="color: #008000; ">(</span>lock <span class="kw4" style="color: #0000ff; ">const</span> <span class="sy3" style="color: #000040; ">&amp;</span> l<span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw4" style="color: #0000ff; ">void</span> request<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw4" style="color: #0000ff; ">void</span> release<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span><span class="sy4" style="color: #008080; ">;</span>
lock<span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">lock</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="co1" style="color: #666666; ">// ...</span>
lock<span class="sy4" style="color: #008080; ">::</span>~lock<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="kw4" style="color: #0000ff; ">void</span> lock<span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">request</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="kw4" style="color: #0000ff; ">void</span> lock<span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">release</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="co1" style="color: #666666; ">// assumes _DATA_TYPE_ has a default constructor</span>
<span class="kw2" style="color: #0000ff; ">template</span><span class="sy1" style="color: #000080; ">&lt;</span><span class="kw2" style="color: #0000ff; ">typename</span> _DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span>
<span class="kw2" style="color: #0000ff; ">class</span> singleton
<span class="br0" style="color: #008000; ">{</span>
<span class="kw2" style="color: #0000ff; ">public</span><span class="sy4" style="color: #008080; ">:</span>
<span class="kw4" style="color: #0000ff; ">static</span> _DATA_TYPE_ <span class="sy2" style="color: #000040; ">*</span> request<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw4" style="color: #0000ff; ">static</span> <span class="kw4" style="color: #0000ff; ">void</span> release<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw2" style="color: #0000ff; ">private</span><span class="sy4" style="color: #008080; ">:</span>
singleton<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
singleton<span class="br0" style="color: #008000; ">(</span>singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span> <span class="kw4" style="color: #0000ff; ">const</span> <span class="sy3" style="color: #000040; ">&amp;</span> s<span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
~singleton<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span> <span class="sy3" style="color: #000040; ">&amp;</span> operator <span class="sy1" style="color: #000080; ">=</span><span class="br0" style="color: #008000; ">(</span>singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span> <span class="kw4" style="color: #0000ff; ">const</span> <span class="sy3" style="color: #000040; ">&amp;</span> s<span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw4" style="color: #0000ff; ">static</span> _DATA_TYPE_ <span class="sy2" style="color: #000040; ">*</span> pointer<span class="sy4" style="color: #008080; ">;</span>
<span class="kw4" style="color: #0000ff; ">static</span> lock mutex<span class="sy4" style="color: #008080; ">;</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw2" style="color: #0000ff; ">template</span><span class="sy1" style="color: #000080; ">&lt;</span><span class="kw2" style="color: #0000ff; ">typename</span> _DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span>
_DATA_TYPE_ <span class="sy2" style="color: #000040; ">*</span> singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy1" style="color: #000080; ">=</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw2" style="color: #0000ff; ">template</span><span class="sy1" style="color: #000080; ">&lt;</span><span class="kw2" style="color: #0000ff; ">typename</span> _DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span>
lock singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">mutex</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw2" style="color: #0000ff; ">template</span><span class="sy1" style="color: #000080; ">&lt;</span><span class="kw2" style="color: #0000ff; ">typename</span> _DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span>
_DATA_TYPE_ <span class="sy2" style="color: #000040; ">*</span> singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">request</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="kw1" style="color: #0000ff; ">if</span><span class="br0" style="color: #008000; ">(</span>singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy1" style="color: #000080; ">==</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">mutex</span>.<span class="me1" style="color: #007788; ">request</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw1" style="color: #0000ff; ">if</span><span class="br0" style="color: #008000; ">(</span>singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy1" style="color: #000080; ">==</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy1" style="color: #000080; ">=</span> <span class="kw3" style="color: #0000dd; ">new</span> _DATA_TYPE_<span class="sy4" style="color: #008080; ">;</span>
<span class="br0" style="color: #008000; ">}</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">mutex</span>.<span class="me1" style="color: #007788; ">release</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="kw1" style="color: #0000ff; ">return</span> singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span><span class="sy4" style="color: #008080; ">;</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="kw2" style="color: #0000ff; ">template</span><span class="sy1" style="color: #000080; ">&lt;</span><span class="kw2" style="color: #0000ff; ">typename</span> _DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span>
<span class="kw4" style="color: #0000ff; ">void</span> singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">release</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="kw1" style="color: #0000ff; ">if</span><span class="br0" style="color: #008000; ">(</span>singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy3" style="color: #000040; ">!</span><span class="sy1" style="color: #000080; ">=</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">mutex</span>.<span class="me1" style="color: #007788; ">request</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw1" style="color: #0000ff; ">if</span><span class="br0" style="color: #008000; ">(</span>singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy3" style="color: #000040; ">!</span><span class="sy1" style="color: #000080; ">=</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="kw3" style="color: #0000dd; ">delete</span> singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span><span class="sy4" style="color: #008080; ">;</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">pointer</span> <span class="sy1" style="color: #000080; ">=</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="sy4" style="color: #008080; ">;</span>
<span class="br0" style="color: #008000; ">}</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">mutex</span>.<span class="me1" style="color: #007788; ">release</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="kw2" style="color: #0000ff; ">template</span><span class="sy1" style="color: #000080; ">&lt;</span><span class="kw2" style="color: #0000ff; ">typename</span> _DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span>_DATA_TYPE_<span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">singleton</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="br0" style="color: #008000; ">}</span>
<span class="co1" style="color: #666666; ">// ...</span>
<span class="kw4" style="color: #0000ff; ">int</span> main<span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span>
<span class="br0" style="color: #008000; ">{</span>
<span class="kw4" style="color: #0000ff; ">int</span> <span class="sy2" style="color: #000040; ">*</span> s<span class="sy4" style="color: #008080; ">;</span>
s <span class="sy1" style="color: #000080; ">=</span> singleton<span class="sy1" style="color: #000080; ">&lt;</span><span class="kw4" style="color: #0000ff; ">int</span><span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">request</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="co1" style="color: #666666; ">// ...</span>
singleton<span class="sy1" style="color: #000080; ">&lt;</span><span class="kw4" style="color: #0000ff; ">int</span><span class="sy1" style="color: #000080; ">&gt;</span><span class="sy4" style="color: #008080; ">::</span><span class="me2" style="color: #007788; ">release</span><span class="br0" style="color: #008000; ">(</span><span class="br0" style="color: #008000; ">)</span><span class="sy4" style="color: #008080; ">;</span>
<span class="kw1" style="color: #0000ff; ">return</span> <span class="nu0" style="color: #0000dd; ">0</span><span class="sy4" style="color: #008080; ">;</span>
<span class="br0" style="color: #008000; ">}</span>
</pre>
<div><span class="br0" style="color: #008000; "><br />
</span></div>
</div>
</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/201306.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-06-25 21:46 <a href="http://www.cppblog.com/biao/archive/2013/06/25/201306.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>