📄 replaycharsequencefactory.html
字号:
<a name="584" href="#584">584</a> <em>/**<em>*</em></em><a name="585" href="#585">585</a> <em> * Provides a (Replay)CharSequence view on recorded streams (a prefix</em><a name="586" href="#586">586</a> <em> * buffer and overflow backing file) that can handle streams of multibyte</em><a name="587" href="#587">587</a> <em> * characters.</em><a name="588" href="#588">588</a> <em> *</em><a name="589" href="#589">589</a> <em> * If possible, use {@link ByteReplayCharSequence}. It performs better even</em><a name="590" href="#590">590</a> <em> * for the single byte case (Decoding is an expensive process).</em><a name="591" href="#591">591</a> <em> *</em><a name="592" href="#592">592</a> <em> * <p>Call close on this class when done so can clean up resources.</em><a name="593" href="#593">593</a> <em> *</em><a name="594" href="#594">594</a> <em> * <p>Implementation currently works by checking to see if content to read</em><a name="595" href="#595">595</a> <em> * all fits the in-memory buffer. If so, we decode into a CharBuffer and</em><a name="596" href="#596">596</a> <em> * keep this around for CharSequence operations. This CharBuffer is</em><a name="597" href="#597">597</a> <em> * discarded on close.</em><a name="598" href="#598">598</a> <em> *</em><a name="599" href="#599">599</a> <em> * <p>If content length is greater than in-memory buffer, we decode the</em><a name="600" href="#600">600</a> <em> * buffer plus backing file into a new file named for the backing file w/</em><a name="601" href="#601">601</a> <em> * a suffix of the encoding we write the file as. We then run w/ a</em><a name="602" href="#602">602</a> <em> * memory-mapped CharBuffer against this file to implement CharSequence.</em><a name="603" href="#603">603</a> <em> * Reasons for this implemenation are that CharSequence wants to return the</em><a name="604" href="#604">604</a> <em> * length of the CharSequence.</em><a name="605" href="#605">605</a> <em> *</em><a name="606" href="#606">606</a> <em> * <p>Obvious optimizations would keep around decodings whether the</em><a name="607" href="#607">607</a> <em> * in-memory decoded buffer or the file of decodings written to disk but the</em><a name="608" href="#608">608</a> <em> * general usage pattern processing URIs is that the decoding is used by one</em><a name="609" href="#609">609</a> <em> * processor only. Also of note, files usually fit into the in-memory</em><a name="610" href="#610">610</a> <em> * buffer.</em><a name="611" href="#611">611</a> <em> *</em><a name="612" href="#612">612</a> <em> * <p>We might also be able to keep up 3 windows that moved across the file</em><a name="613" href="#613">613</a> <em> * decoding a window at a time trying to keep one of the buffers just in</em><a name="614" href="#614">614</a> <em> * front of the regex processing returning it a length that would be only</em><a name="615" href="#615">615</a> <em> * the length of current position to end of current block or else the length</em><a name="616" href="#616">616</a> <em> * could be got by multipling the backing files length by the decoders'</em><a name="617" href="#617">617</a> <em> * estimate of average character size. This would save us writing out the</em><a name="618" href="#618">618</a> <em> * decoded file. We'd have to do the latter for files that are</em><a name="619" href="#619">619</a> <em> * > Integer.MAX_VALUE.</em><a name="620" href="#620">620</a> <em> *</em><a name="621" href="#621">621</a> <em> * @author stack</em><a name="622" href="#622">622</a> <em> * @version $Revision: 1.39 $, $Date: 2006/06/01 05:58:37 $</em><a name="623" href="#623">623</a> <em> */</em><a name="624" href="#624">624</a> <strong>private</strong> <strong>class</strong> MultiByteReplayCharSequence implements <a href="../../../org/archive/io/ReplayCharSequence.html">ReplayCharSequence</a> {<a name="625" href="#625">625</a> <a name="626" href="#626">626</a> <em>/**<em>*</em></em><a name="627" href="#627">627</a> <em> * Name of the encoding we use writing out concatenated decoded prefix</em><a name="628" href="#628">628</a> <em> * buffer and decoded backing file.</em><a name="629" href="#629">629</a> <em> *</em><a name="630" href="#630">630</a> <em> * <p>This define is also used as suffix for the file that holds the</em><a name="631" href="#631">631</a> <em> * decodings. The name of the file that holds the decoding is the name</em><a name="632" href="#632">632</a> <em> * of the backing file w/ this encoding for a suffix.</em><a name="633" href="#633">633</a> <em> *</em><a name="634" href="#634">634</a> <em> * <p>See <a ref="<a href="http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html" target="alexandria_uri">http://java.sun.com/j2se/1.4.2/docs/guide/intl/encoding.doc.html</a>">Encoding</a>.</em><a name="635" href="#635">635</a> <em> */</em><a name="636" href="#636">636</a> <strong>private</strong> <strong>static</strong> <strong>final</strong> String WRITE_ENCODING = <span class="string">"UTF-16BE"</span>;<a name="637" href="#637">637</a> <a name="638" href="#638">638</a> <em>/**<em>*</em></em><a name="639" href="#639">639</a> <em> * CharBuffer of decoded content.</em><a name="640" href="#640">640</a> <em> *</em><a name="641" href="#641">641</a> <em> * Content of this buffer is unicode.</em><a name="642" href="#642">642</a> <em> */</em><a name="643" href="#643">643</a> <strong>private</strong> CharBuffer content = <strong>null</strong>;<a name="644" href="#644">644</a> <a name="645" href="#645">645</a> <em>/**<em>*</em></em><a name="646" href="#646">646</a> <em> * File that has decoded content.</em><a name="647" href="#647">647</a> <em> *</em><a name="648" href="#648">648</a> <em> * Keep it around so we can remove on close.</em><a name="649" href="#649">649</a> <em> */</em><a name="650" href="#650">650</a> <strong>private</strong> File decodedFile = <strong>null</strong>;<a name="651" href="#651">651</a> <a name="652" href="#652">652</a> <a name="653" href="#653">653</a> <em>/**<em>*</em></em><a name="654" href="#654">654</a> <em> * Constructor.</em><a name="655" href="#655">655</a> <em> *</em><a name="656" href="#656">656</a> <em> * @param buffer In-memory buffer of recordings prefix. We read from</em><a name="657" href="#657">657</a> <em> * here first and will only go to the backing file if <code>size</code></em><a name="658" href="#658">658</a> <em> * requested is greater than <code>buffer.length</code>.</em><a name="659" href="#659">659</a> <em> * @param size Total size of stream to replay in bytes. Used to find</em><a name="660" href="#660">660</a> <em> * EOS. This is total length of content including HTTP headers if</em><a name="661" href="#661">661</a> <em> * present.</em><a name="662" href="#662">662</a> <em> * @param responseBodyStart Where the response body starts in bytes.</em><a name="663" href="#663">663</a> <em> * Used to skip over the HTTP headers if present.</em><a name="664" href="#664">664</a> <em> * @param backingFilename Path to backing file with content in excess of</em><a name="665" href="#665">665</a> <em> * whats in <code>buffer</code>.</em><a name="666" href="#666">666</a> <em> * @param encoding Encoding to use reading the passed prefix buffer and</em><a name="667" href="#667">667</a> <em> * backing file. For now, should be java canonical name for the</em><a name="668" href="#668">668</a> <em> * encoding. (If null is passed, we will default to</em><a name="669" href="#669">669</a> <em> * ByteReplayCharSequence).</em><a name="670" href="#670">670</a> <em> *</em><a name="671" href="#671">671</a> <em> * @throws IOException</em><a name="672" href="#672">672</a> <em> */</em><a name="673" href="#673">673</a> <strong>private</strong> MultiByteReplayCharSequence(byte[] buffer, <strong>long</strong> size,<a name="674" href="#674">674</a> <strong>long</strong> responseBodyStart, String backingFilename, String encoding)<a name="675" href="#675">675</a> throws IOException {<a name="676" href="#676">676</a> <strong>super</strong>();<a name="677" href="#677">677</a> <strong>if</strong> (encoding == <strong>null</strong>) {<a name="678" href="#678">678</a> <strong>throw</strong> <strong>new</strong> NullPointerException(<span class="string">"Character encoding is null."</span>);<a name="679" href="#679">679</a> }<a name="680" href="#680">680</a> <a name="681" href="#681">681</a> <strong>this</strong>.content = decode(buffer, backingFilename, size,<a name="682" href="#682">682</a> responseBodyStart, encoding);<a name="683" href="#683">683</a> }<a name="684" href="#684">684</a> <a name="685" href="#685">685</a> <em>/**<em>*</em></em><a name="686" href="#686">686</a> <em> * Decode passed buffer and backing file into a CharBuffer.</em><a name="687" href="#687">687</a> <em> *</em><a name="688" href="#688">688</a> <em> * This method writes a new file made of the decoded concatenation of</em><a name="689" href="#689">689</a> <em> * the in-memory prefix buffer and the backing file. Returns a</em><a name="690" href="#690">690</a> <em> * charSequence view onto this new file.</em><a name="691" href="#691">691</a> <em> *</em><a name="692" href="#692">692</a> <em> * @param buffer In-memory buffer of recordings prefix. We read from</em><a name="693" href="#693">693</a> <em> * here first and will only go to the backing file if <code>size</code></em><a name="694" href="#694">694</a> <em> * requested is greater than <code>buffer.length</code>.</em><a name="695" href="#695">695</a> <em> * @param size Total size of stream to replay in bytes. Used to find</em><a name="696" href="#696">696</a> <em> * EOS. This is total length of content including HTTP headers if</em><a name="697" href="#697">697</a> <em> * present.</em><a name="698" href="#698">698</a> <em> * @param responseBodyStart Where the response body starts in bytes.</em><a name="699" href="#699">699</a> <em> * Used to skip over the HTTP headers if present.</em><a name="700" href="#700">700</a> <em> * @param backingFilename Path to backing file with content in excess of</em><a name="701" href="#701">701</a> <em> * whats in <code>buffer</code>.</em><a name="702" href="#702">702</a> <em> * @param encoding Encoding to use reading the passed prefix buffer and</em><a name="703" href="#703">703</a> <em> * backing file. For now, should be java canonical name for the</em><a name="704" href="#704">704</a> <em> * encoding. (If null is passed, we will default to</em><a name="705" href="#705">705</a> <em> * ByteReplayCharSequence).</em><a name="706" href="#706">706</a> <em> *</em><a name="707" href="#707">707</a> <em> * @return A CharBuffer view on decodings of the contents of passed</em><a name="708" href="#708">708</a> <em> * buffer.</em><a name="709" href="#709">709</a> <em> * @throws IOException</em><a name="710" href="#710">710</a> <em> */</em><a name="711" href="#711">711</a> <strong>private</strong> CharBuffer decode(byte[] buffer, String backingFilename,<a name="712" href="#712">712</a> <strong>long</strong> size, <strong>long</strong> responseBodyStart, String encoding)<a name="713" href="#713">713</a> throws IOException {<a name="714" href="#714">714</a> <a name="715" href="#715">715</a> CharBuffer charBuffer = <strong>null</strong>;<a name="716" href="#716">716</a> <a name="717" href="#717">717</a> <strong>if</strong> (size <= buffer.length) {<a name="718" href="#718">718</a> charBuffer =<a name="719" href="#719">719</a> decodeInMemory(buffer, size, responseBodyStart, encoding);<a name="720" href="#720">720</a> } <strong>else</strong> {<a name="721" href="#721">721</a> File backingFile = <strong>new</strong> File(backingFilename);<a name="722" href="#722">722</a> <strong>if</strong> (!backingFile.exists()) {<a name="723" href="#723">723</a> <strong>throw</strong> <strong>new</strong> FileNotFoundException(backingFilename +<a name="724" href="#724">724</a> <span class="string">"doesn't exist"</span>);<a name="725" href="#725">725</a> }<a name="726" href="#726">726</a> <a name="727" href="#727">727</a> <em class="comment">// Get decoder. Will burp if encoding string is bad.</em><a name="728" href="#728">728</a> <em class="comment">// Should probably keep it around. I'm sure its not</em><a name="729" href="#729">729</a> <em class="comment">// cheap to construct. Tell decoder to replace bad
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -