📄 textlinedecoder.html
字号:
<a name="235" href="#235">235</a> <em class="jxr_comment">// Convert delimiter to ByteBuffer if not done yet.</em><a name="236" href="#236">236</a> <strong class="jxr_keyword">if</strong> (delimBuf == <strong class="jxr_keyword">null</strong>) {<a name="237" href="#237">237</a> <a href="../../../../../../org/apache/mina/common/IoBuffer.html">IoBuffer</a> tmp = IoBuffer.allocate(2).setAutoExpand(<strong class="jxr_keyword">true</strong>);<a name="238" href="#238">238</a> tmp.putString(delimiter.getValue(), charset.newEncoder());<a name="239" href="#239">239</a> tmp.flip();<a name="240" href="#240">240</a> delimBuf = tmp;<a name="241" href="#241">241</a> }<a name="242" href="#242">242</a> <a name="243" href="#243">243</a> <em class="jxr_comment">// Try to find a match</em><a name="244" href="#244">244</a> <strong class="jxr_keyword">int</strong> oldPos = in.position();<a name="245" href="#245">245</a> <strong class="jxr_keyword">int</strong> oldLimit = in.limit();<a name="246" href="#246">246</a> <strong class="jxr_keyword">while</strong> (in.hasRemaining()) {<a name="247" href="#247">247</a> byte b = in.get();<a name="248" href="#248">248</a> <strong class="jxr_keyword">if</strong> (delimBuf.get(matchCount) == b) {<a name="249" href="#249">249</a> matchCount++;<a name="250" href="#250">250</a> <strong class="jxr_keyword">if</strong> (matchCount == delimBuf.limit()) {<a name="251" href="#251">251</a> <em class="jxr_comment">// Found a match.</em><a name="252" href="#252">252</a> <strong class="jxr_keyword">int</strong> pos = in.position();<a name="253" href="#253">253</a> in.limit(pos);<a name="254" href="#254">254</a> in.position(oldPos);<a name="255" href="#255">255</a> <a name="256" href="#256">256</a> ctx.append(in);<a name="257" href="#257">257</a> <a name="258" href="#258">258</a> in.limit(oldLimit);<a name="259" href="#259">259</a> in.position(pos);<a name="260" href="#260">260</a> <strong class="jxr_keyword">if</strong> (ctx.getOverflowPosition() == 0) {<a name="261" href="#261">261</a> <a href="../../../../../../org/apache/mina/common/IoBuffer.html">IoBuffer</a> buf = ctx.getBuffer();<a name="262" href="#262">262</a> buf.flip();<a name="263" href="#263">263</a> buf.limit(buf.limit() - matchCount);<a name="264" href="#264">264</a> <strong class="jxr_keyword">try</strong> {<a name="265" href="#265">265</a> out.write(buf.getString(ctx.getDecoder()));<a name="266" href="#266">266</a> } <strong class="jxr_keyword">finally</strong> {<a name="267" href="#267">267</a> buf.clear();<a name="268" href="#268">268</a> }<a name="269" href="#269">269</a> } <strong class="jxr_keyword">else</strong> {<a name="270" href="#270">270</a> <strong class="jxr_keyword">int</strong> overflowPosition = ctx.getOverflowPosition();<a name="271" href="#271">271</a> ctx.reset();<a name="272" href="#272">272</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> <a href="../../../../../../org/apache/mina/filter/codec/RecoverableProtocolDecoderException.html">RecoverableProtocolDecoderException</a>(<a name="273" href="#273">273</a> <span class="jxr_string">"Line is too long: "</span> + overflowPosition);<a name="274" href="#274">274</a> }<a name="275" href="#275">275</a> <a name="276" href="#276">276</a> oldPos = pos;<a name="277" href="#277">277</a> matchCount = 0;<a name="278" href="#278">278</a> }<a name="279" href="#279">279</a> } <strong class="jxr_keyword">else</strong> {<a name="280" href="#280">280</a> <em class="jxr_comment">// fix for DIRMINA-506</em><a name="281" href="#281">281</a> in.position(in.position()-matchCount);<a name="282" href="#282">282</a> matchCount = 0;<a name="283" href="#283">283</a> }<a name="284" href="#284">284</a> }<a name="285" href="#285">285</a> <a name="286" href="#286">286</a> <em class="jxr_comment">// Put remainder to buf.</em><a name="287" href="#287">287</a> in.position(oldPos);<a name="288" href="#288">288</a> ctx.append(in);<a name="289" href="#289">289</a> <a name="290" href="#290">290</a> ctx.setMatchCount(matchCount);<a name="291" href="#291">291</a> }<a name="292" href="#292">292</a> <a name="293" href="#293">293</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">class</strong> <a href="../../../../../../org/apache/mina/filter/codec/textline/TextLineDecoder.html">Context</a> {<a name="294" href="#294">294</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">final</strong> CharsetDecoder decoder;<a name="295" href="#295">295</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">final</strong> <a href="../../../../../../org/apache/mina/common/IoBuffer.html">IoBuffer</a> buf;<a name="296" href="#296">296</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">int</strong> matchCount = 0;<a name="297" href="#297">297</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">int</strong> overflowPosition = 0;<a name="298" href="#298">298</a> <a name="299" href="#299">299</a> <strong class="jxr_keyword">private</strong> <a href="../../../../../../org/apache/mina/filter/codec/textline/TextLineDecoder.html">Context</a>() {<a name="300" href="#300">300</a> decoder = charset.newDecoder();<a name="301" href="#301">301</a> buf = IoBuffer.allocate(80).setAutoExpand(<strong class="jxr_keyword">true</strong>);<a name="302" href="#302">302</a> }<a name="303" href="#303">303</a> <a name="304" href="#304">304</a> <strong class="jxr_keyword">public</strong> CharsetDecoder getDecoder() {<a name="305" href="#305">305</a> <strong class="jxr_keyword">return</strong> decoder;<a name="306" href="#306">306</a> }<a name="307" href="#307">307</a> <a name="308" href="#308">308</a> <strong class="jxr_keyword">public</strong> <a href="../../../../../../org/apache/mina/common/IoBuffer.html">IoBuffer</a> getBuffer() {<a name="309" href="#309">309</a> <strong class="jxr_keyword">return</strong> buf;<a name="310" href="#310">310</a> }<a name="311" href="#311">311</a> <a name="312" href="#312">312</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">int</strong> getOverflowPosition() {<a name="313" href="#313">313</a> <strong class="jxr_keyword">return</strong> overflowPosition;<a name="314" href="#314">314</a> }<a name="315" href="#315">315</a> <a name="316" href="#316">316</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">int</strong> getMatchCount() {<a name="317" href="#317">317</a> <strong class="jxr_keyword">return</strong> matchCount;<a name="318" href="#318">318</a> }<a name="319" href="#319">319</a> <a name="320" href="#320">320</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> setMatchCount(<strong class="jxr_keyword">int</strong> matchCount) {<a name="321" href="#321">321</a> <strong class="jxr_keyword">this</strong>.matchCount = matchCount;<a name="322" href="#322">322</a> }<a name="323" href="#323">323</a> <a name="324" href="#324">324</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> reset() {<a name="325" href="#325">325</a> overflowPosition = 0;<a name="326" href="#326">326</a> matchCount = 0;<a name="327" href="#327">327</a> decoder.reset();<a name="328" href="#328">328</a> }<a name="329" href="#329">329</a> <a name="330" href="#330">330</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> append(<a href="../../../../../../org/apache/mina/common/IoBuffer.html">IoBuffer</a> in) {<a name="331" href="#331">331</a> <strong class="jxr_keyword">if</strong> (overflowPosition != 0) {<a name="332" href="#332">332</a> discard(in);<a name="333" href="#333">333</a> } <strong class="jxr_keyword">else</strong> <strong class="jxr_keyword">if</strong> (buf.position() > maxLineLength - in.remaining()) {<a name="334" href="#334">334</a> overflowPosition = buf.position();<a name="335" href="#335">335</a> buf.clear();<a name="336" href="#336">336</a> discard(in);<a name="337" href="#337">337</a> } <strong class="jxr_keyword">else</strong> {<a name="338" href="#338">338</a> getBuffer().put(in);<a name="339" href="#339">339</a> }<a name="340" href="#340">340</a> }<a name="341" href="#341">341</a> <a name="342" href="#342">342</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">void</strong> discard(<a href="../../../../../../org/apache/mina/common/IoBuffer.html">IoBuffer</a> in) {<a name="343" href="#343">343</a> <strong class="jxr_keyword">if</strong> (Integer.MAX_VALUE - in.remaining() < overflowPosition) {<a name="344" href="#344">344</a> overflowPosition = Integer.MAX_VALUE;<a name="345" href="#345">345</a> } <strong class="jxr_keyword">else</strong> {<a name="346" href="#346">346</a> overflowPosition += in.remaining();<a name="347" href="#347">347</a> }<a name="348" href="#348">348</a> in.position(in.limit());<a name="349" href="#349">349</a> }<a name="350" href="#350">350</a> }<a name="351" href="#351">351</a> }</pre><hr/><div id="footer">This page was automatically generated by <a href="http://maven.apache.org/">Maven</a></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -