⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multipartstream.html

📁 最好的java上传组件
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="496" href="#496">496</a>                IOException {<a name="497" href="#497">497</a>         <strong>boolean</strong> done = false;<a name="498" href="#498">498</a>         <strong>int</strong> pad;<a name="499" href="#499">499</a>         <strong>int</strong> pos;<a name="500" href="#500">500</a>         <strong>int</strong> bytesRead;<a name="501" href="#501">501</a>         <strong>int</strong> total = 0;<a name="502" href="#502">502</a>         <strong>while</strong> (!done) {<a name="503" href="#503">503</a>             <em class="comment">// Is boundary token present somewere in the buffer?</em><a name="504" href="#504">504</a>             pos = findSeparator();<a name="505" href="#505">505</a>             <strong>if</strong> (pos != -1) {<a name="506" href="#506">506</a>                 <em class="comment">// Write the rest of the data before the boundary.</em><a name="507" href="#507">507</a>                 output.write(buffer, head, pos - head);<a name="508" href="#508">508</a>                 total += pos - head;<a name="509" href="#509">509</a>                 head = pos;<a name="510" href="#510">510</a>                 done = <strong>true</strong>;<a name="511" href="#511">511</a>             } <strong>else</strong> {<a name="512" href="#512">512</a>                 <em class="comment">// Determine how much data should be kept in the</em><a name="513" href="#513">513</a>                 <em class="comment">// buffer.</em><a name="514" href="#514">514</a>                 <strong>if</strong> (tail - head > keepRegion) {<a name="515" href="#515">515</a>                     pad = keepRegion;<a name="516" href="#516">516</a>                 } <strong>else</strong> {<a name="517" href="#517">517</a>                     pad = tail - head;<a name="518" href="#518">518</a>                 }<a name="519" href="#519">519</a>                 <em class="comment">// Write out the data belonging to the body-data.</em><a name="520" href="#520">520</a>                 output.write(buffer, head, tail - head - pad);<a name="521" href="#521">521</a> <a name="522" href="#522">522</a>                 <em class="comment">// Move the data to the beginning of the buffer.</em><a name="523" href="#523">523</a>                 total += tail - head - pad;<a name="524" href="#524">524</a>                 System.arraycopy(buffer, tail - pad, buffer, 0, pad);<a name="525" href="#525">525</a> <a name="526" href="#526">526</a>                 <em class="comment">// Refill buffer with new data.</em><a name="527" href="#527">527</a>                 head = 0;<a name="528" href="#528">528</a>                 bytesRead = input.read(buffer, pad, bufSize - pad);<a name="529" href="#529">529</a> <a name="530" href="#530">530</a>                 <em class="comment">// [pprrrrrrr]</em><a name="531" href="#531">531</a>                 <strong>if</strong> (bytesRead != -1) {<a name="532" href="#532">532</a>                     tail = pad + bytesRead;<a name="533" href="#533">533</a>                 } <strong>else</strong> {<a name="534" href="#534">534</a>                     <em class="comment">// The last pad amount is left in the buffer.</em><a name="535" href="#535">535</a>                     <em class="comment">// Boundary can't be in there so write out the</em><a name="536" href="#536">536</a>                     <em class="comment">// data you have and signal an error condition.</em><a name="537" href="#537">537</a>                     output.write(buffer, 0, pad);<a name="538" href="#538">538</a>                     output.flush();<a name="539" href="#539">539</a>                     total += pad;<a name="540" href="#540">540</a>                     <strong>throw</strong> <strong>new</strong> MalformedStreamException(<a name="541" href="#541">541</a>                             <span class="string">"Stream ended unexpectedly"</span>);<a name="542" href="#542">542</a>                 }<a name="543" href="#543">543</a>             }<a name="544" href="#544">544</a>         }<a name="545" href="#545">545</a>         output.flush();<a name="546" href="#546">546</a>         <strong>return</strong> total;<a name="547" href="#547">547</a>     }<a name="548" href="#548">548</a> <a name="549" href="#549">549</a> <a name="550" href="#550">550</a>     <em>/**<em>*</em></em><a name="551" href="#551">551</a> <em>     * &lt;p> Reads &lt;code>body-data&lt;/code> from the current</em><a name="552" href="#552">552</a> <em>     * &lt;code>encapsulation&lt;/code> and discards it.</em><a name="553" href="#553">553</a> <em>     *</em><a name="554" href="#554">554</a> <em>     * &lt;p>Use this method to skip encapsulations you don't need or don't</em><a name="555" href="#555">555</a> <em>     * understand.</em><a name="556" href="#556">556</a> <em>     *</em><a name="557" href="#557">557</a> <em>     * @return The amount of data discarded.</em><a name="558" href="#558">558</a> <em>     *</em><a name="559" href="#559">559</a> <em>     * @throws MalformedStreamException if the stream ends unexpectedly.</em><a name="560" href="#560">560</a> <em>     * @throws IOException              if an i/o error occurs.</em><a name="561" href="#561">561</a> <em>     */</em><a name="562" href="#562">562</a>     <strong>public</strong> <strong>int</strong> discardBodyData()<a name="563" href="#563">563</a>         throws MalformedStreamException,<a name="564" href="#564">564</a>                IOException {<a name="565" href="#565">565</a>         <strong>boolean</strong> done = false;<a name="566" href="#566">566</a>         <strong>int</strong> pad;<a name="567" href="#567">567</a>         <strong>int</strong> pos;<a name="568" href="#568">568</a>         <strong>int</strong> bytesRead;<a name="569" href="#569">569</a>         <strong>int</strong> total = 0;<a name="570" href="#570">570</a>         <strong>while</strong> (!done) {<a name="571" href="#571">571</a>             <em class="comment">// Is boundary token present somewere in the buffer?</em><a name="572" href="#572">572</a>             pos = findSeparator();<a name="573" href="#573">573</a>             <strong>if</strong> (pos != -1) {<a name="574" href="#574">574</a>                 <em class="comment">// Write the rest of the data before the boundary.</em><a name="575" href="#575">575</a>                 total += pos - head;<a name="576" href="#576">576</a>                 head = pos;<a name="577" href="#577">577</a>                 done = <strong>true</strong>;<a name="578" href="#578">578</a>             } <strong>else</strong> {<a name="579" href="#579">579</a>                 <em class="comment">// Determine how much data should be kept in the</em><a name="580" href="#580">580</a>                 <em class="comment">// buffer.</em><a name="581" href="#581">581</a>                 <strong>if</strong> (tail - head > keepRegion) {<a name="582" href="#582">582</a>                     pad = keepRegion;<a name="583" href="#583">583</a>                 } <strong>else</strong> {<a name="584" href="#584">584</a>                     pad = tail - head;<a name="585" href="#585">585</a>                 }<a name="586" href="#586">586</a>                 total += tail - head - pad;<a name="587" href="#587">587</a> <a name="588" href="#588">588</a>                 <em class="comment">// Move the data to the beginning of the buffer.</em><a name="589" href="#589">589</a>                 System.arraycopy(buffer, tail - pad, buffer, 0, pad);<a name="590" href="#590">590</a> <a name="591" href="#591">591</a>                 <em class="comment">// Refill buffer with new data.</em><a name="592" href="#592">592</a>                 head = 0;<a name="593" href="#593">593</a>                 bytesRead = input.read(buffer, pad, bufSize - pad);<a name="594" href="#594">594</a> <a name="595" href="#595">595</a>                 <em class="comment">// [pprrrrrrr]</em><a name="596" href="#596">596</a>                 <strong>if</strong> (bytesRead != -1) {<a name="597" href="#597">597</a>                     tail = pad + bytesRead;<a name="598" href="#598">598</a>                 } <strong>else</strong> {<a name="599" href="#599">599</a>                     <em class="comment">// The last pad amount is left in the buffer.</em><a name="600" href="#600">600</a>                     <em class="comment">// Boundary can't be in there so signal an error</em><a name="601" href="#601">601</a>                     <em class="comment">// condition.</em><a name="602" href="#602">602</a>                     total += pad;<a name="603" href="#603">603</a>                     <strong>throw</strong> <strong>new</strong> MalformedStreamException(<a name="604" href="#604">604</a>                             <span class="string">"Stream ended unexpectedly"</span>);<a name="605" href="#605">605</a>                 }<a name="606" href="#606">606</a>             }<a name="607" href="#607">607</a>         }<a name="608" href="#608">608</a>         <strong>return</strong> total;<a name="609" href="#609">609</a>     }<a name="610" href="#610">610</a> <a name="611" href="#611">611</a> <a name="612" href="#612">612</a>     <em>/**<em>*</em></em><a name="613" href="#613">613</a> <em>     * Finds the beginning of the first &lt;code>encapsulation&lt;/code>.</em><a name="614" href="#614">614</a> <em>     *</em><a name="615" href="#615">615</a> <em>     * @return &lt;code>true&lt;/code> if an &lt;code>encapsulation&lt;/code> was found in</em><a name="616" href="#616">616</a> <em>     *         the stream.</em><a name="617" href="#617">617</a> <em>     *</em><a name="618" href="#618">618</a> <em>     * @throws IOException if an i/o error occurs.</em><a name="619" href="#619">619</a> <em>     */</em><a name="620" href="#620">620</a>     <strong>public</strong> <strong>boolean</strong> skipPreamble()<a name="621" href="#621">621</a>         throws IOException {<a name="622" href="#622">622</a>         <em class="comment">// First delimiter may be not preceeded with a CRLF.</em><a name="623" href="#623">623</a>         System.arraycopy(boundary, 2, boundary, 0, boundary.length - 2);<a name="624" href="#624">624</a>         boundaryLength = boundary.length - 2;<a name="625" href="#625">625</a>         <strong>try</strong> {<a name="626" href="#626">626</a>             <em class="comment">// Discard all data up to the delimiter.</em><a name="627" href="#627">627</a>             discardBodyData();<a name="628" href="#628">628</a> <a name="629" href="#629">629</a>             <em class="comment">// Read boundary - if succeded, the stream contains an</em><a name="630" href="#630">630</a>             <em class="comment">// encapsulation.</em><a name="631" href="#631">631</a>             <strong>return</strong> readBoundary();<a name="632" href="#632">632</a>         } <strong>catch</strong> (MalformedStreamException e) {<a name="633" href="#633">633</a>             <strong>return</strong> false;<a name="634" href="#634">634</a>         } <strong>finally</strong> {<a name="635" href="#635">635</a>             <em class="comment">// Restore delimiter.</em><a name="636" href="#636">636</a>             System.arraycopy(boundary, 0, boundary, 2, boundary.length - 2);<a name="637" href="#637">637</a>             boundaryLength = boundary.length;<a name="638" href="#638">638</a>             boundary[0] = CR;<a name="639" href="#639">639</a>             boundary[1] = LF;<a name="640" href="#640">640</a>         }<a name="641" href="#641">641</a>     }<a name="642" href="#642">642</a> <a name="643" href="#643">643</a> <a name="644" href="#644">644</a>     <em>/**<em>*</em></em><a name="645" href="#645">645</a> <em>     * Compares &lt;code>count&lt;/code> first bytes in the arrays</em><a name="646" href="#646">646</a> <em>     * &lt;code>a&lt;/code> and &lt;code>b&lt;/code>.</em><a name="647" href="#647">647</a> <em>     *</em><a name="648" href="#648">648</a> <em>     * @param a     The first array to compare.</em><a name="649" href="#649">649</a> <em>     * @param b     The second array to compare.</em><a name="650" href="#650">650</a> <em>     * @param count How many bytes should be compared.</em><a name="651" href="#651">651</a> <em>     *</em><a name="652" href="#652">652</a> <em>     * @return &lt;code>true&lt;/code> if &lt;code>count&lt;/code> first bytes in arrays</em><a name="653" href="#653">653</a> <em>     *         &lt;code>a&lt;/code> and &lt;code>b&lt;/code> are equal.</em><a name="654" href="#654">654</a> <em>     */</em><a name="655" href="#655">655</a>     <strong>public</strong> <strong>static</strong> <strong>boolean</strong> arrayequals(byte[] a,<a name="656" href="#656">656</a>                                       byte[] b,<a name="657" href="#657">657</a>                                       <strong>int</strong> count) {<a name="658" href="#658">658</a>         <strong>for</strong> (<strong>int</strong> i = 0; i &lt; count; i++) {<a name="659" href="#659">659</a>             <strong>if</strong> (a[i] != b[i]) {<a name="660" href="#660">660</a>                 <strong>return</strong> false;<a name="661" href="#661">661</a>             }<a name="662" href="#662">662</a>         }<a name="663" href="#663">663</a>         <strong>return</strong> <strong>true</strong>;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -