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

📄 multipartstream.html

📁 ava 项目中
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="493" href="#493">493</a> <em>     *</em><a name="494" href="#494">494</a> <em>     * &lt;p>Restoring the parent stream boundary token after processing of a</em><a name="495" href="#495">495</a> <em>     * nested stream is left to the application.</em><a name="496" href="#496">496</a> <em>     *</em><a name="497" href="#497">497</a> <em>     * @param boundary The boundary to be used for parsing of the nested</em><a name="498" href="#498">498</a> <em>     *                 stream.</em><a name="499" href="#499">499</a> <em>     *</em><a name="500" href="#500">500</a> <em>     * @throws IllegalBoundaryException if the &lt;code>boundary&lt;/code></em><a name="501" href="#501">501</a> <em>     *                                  has a different length than the one</em><a name="502" href="#502">502</a> <em>     *                                  being currently parsed.</em><a name="503" href="#503">503</a> <em>     */</em><a name="504" href="#504">504</a>     <strong>public</strong> <strong>void</strong> setBoundary(byte[] boundary)<a name="505" href="#505">505</a>             throws IllegalBoundaryException {<a name="506" href="#506">506</a>         <strong>if</strong> (boundary.length != boundaryLength - BOUNDARY_PREFIX.length) {<a name="507" href="#507">507</a>             <strong>throw</strong> <strong>new</strong> IllegalBoundaryException(<a name="508" href="#508">508</a>             <span class="string">"The length of a boundary token can not be changed"</span>);<a name="509" href="#509">509</a>         }<a name="510" href="#510">510</a>         System.arraycopy(boundary, 0, <strong>this</strong>.boundary, BOUNDARY_PREFIX.length,<a name="511" href="#511">511</a>                 boundary.length);<a name="512" href="#512">512</a>     }<a name="513" href="#513">513</a> <a name="514" href="#514">514</a> <a name="515" href="#515">515</a>     <em>/**</em><a name="516" href="#516">516</a> <em>     * &lt;p>Reads the &lt;code>header-part&lt;/code> of the current</em><a name="517" href="#517">517</a> <em>     * &lt;code>encapsulation&lt;/code>.</em><a name="518" href="#518">518</a> <em>     *</em><a name="519" href="#519">519</a> <em>     * &lt;p>Headers are returned verbatim to the input stream, including the</em><a name="520" href="#520">520</a> <em>     * trailing &lt;code>CRLF&lt;/code> marker. Parsing is left to the</em><a name="521" href="#521">521</a> <em>     * application.</em><a name="522" href="#522">522</a> <em>     *</em><a name="523" href="#523">523</a> <em>     * &lt;p>&lt;strong>TODO&lt;/strong> allow limiting maximum header size to</em><a name="524" href="#524">524</a> <em>     * protect against abuse.</em><a name="525" href="#525">525</a> <em>     *</em><a name="526" href="#526">526</a> <em>     * @return The &lt;code>header-part&lt;/code> of the current encapsulation.</em><a name="527" href="#527">527</a> <em>     *</em><a name="528" href="#528">528</a> <em>     * @throws MalformedStreamException if the stream ends unexpecetedly.</em><a name="529" href="#529">529</a> <em>     */</em><a name="530" href="#530">530</a>     <strong>public</strong> String readHeaders()<a name="531" href="#531">531</a>     throws MalformedStreamException {<a name="532" href="#532">532</a>         <strong>int</strong> i = 0;<a name="533" href="#533">533</a>         byte[] b = <strong>new</strong> byte[1];<a name="534" href="#534">534</a>         <em class="comment">// to support multi-byte characters</em><a name="535" href="#535">535</a>         ByteArrayOutputStream baos = <strong>new</strong> ByteArrayOutputStream();<a name="536" href="#536">536</a>         <strong>int</strong> sizeMax = HEADER_PART_SIZE_MAX;<a name="537" href="#537">537</a>         <strong>int</strong> size = 0;<a name="538" href="#538">538</a>         <strong>while</strong> (i &lt; HEADER_SEPARATOR.length) {<a name="539" href="#539">539</a>             <strong>try</strong> {<a name="540" href="#540">540</a>                 b[0] = readByte();<a name="541" href="#541">541</a>             } <strong>catch</strong> (IOException e) {<a name="542" href="#542">542</a>                 <strong>throw</strong> <strong>new</strong> MalformedStreamException(<span class="string">"Stream ended unexpectedly"</span>);<a name="543" href="#543">543</a>             }<a name="544" href="#544">544</a>             size++;<a name="545" href="#545">545</a>             <strong>if</strong> (b[0] == HEADER_SEPARATOR[i]) {<a name="546" href="#546">546</a>                 i++;<a name="547" href="#547">547</a>             } <strong>else</strong> {<a name="548" href="#548">548</a>                 i = 0;<a name="549" href="#549">549</a>             }<a name="550" href="#550">550</a>             <strong>if</strong> (size &lt;= sizeMax) {<a name="551" href="#551">551</a>                 baos.write(b[0]);<a name="552" href="#552">552</a>             }<a name="553" href="#553">553</a>         }<a name="554" href="#554">554</a> <a name="555" href="#555">555</a>         String headers = <strong>null</strong>;<a name="556" href="#556">556</a>         <strong>if</strong> (headerEncoding != <strong>null</strong>) {<a name="557" href="#557">557</a>             <strong>try</strong> {<a name="558" href="#558">558</a>                 headers = baos.toString(headerEncoding);<a name="559" href="#559">559</a>             } <strong>catch</strong> (UnsupportedEncodingException e) {<a name="560" href="#560">560</a>                 <em class="comment">// Fall back to platform default if specified encoding is not</em><a name="561" href="#561">561</a>                 <em class="comment">// supported.</em><a name="562" href="#562">562</a>                 headers = baos.toString();<a name="563" href="#563">563</a>             }<a name="564" href="#564">564</a>         } <strong>else</strong> {<a name="565" href="#565">565</a>             headers = baos.toString();<a name="566" href="#566">566</a>         }<a name="567" href="#567">567</a> <a name="568" href="#568">568</a>         <strong>return</strong> headers;<a name="569" href="#569">569</a>     }<a name="570" href="#570">570</a> <a name="571" href="#571">571</a> <a name="572" href="#572">572</a>     <em>/**</em><a name="573" href="#573">573</a> <em>     * &lt;p>Reads &lt;code>body-data&lt;/code> from the current</em><a name="574" href="#574">574</a> <em>     * &lt;code>encapsulation&lt;/code> and writes its contents into the</em><a name="575" href="#575">575</a> <em>     * output &lt;code>Stream&lt;/code>.</em><a name="576" href="#576">576</a> <em>     *</em><a name="577" href="#577">577</a> <em>     * &lt;p>Arbitrary large amounts of data can be processed by this</em><a name="578" href="#578">578</a> <em>     * method using a constant size buffer. (see {@link</em><a name="579" href="#579">579</a> <em>     * #MultipartStream(InputStream,byte[],int, ProgressNotifier) constructor}).</em><a name="580" href="#580">580</a> <em>     *</em><a name="581" href="#581">581</a> <em>     * @param output The &lt;code>Stream&lt;/code> to write data into. May</em><a name="582" href="#582">582</a> <em>     *               be null, in which case this method is equivalent</em><a name="583" href="#583">583</a> <em>     *               to {@link #discardBodyData()}.</em><a name="584" href="#584">584</a> <em>     *</em><a name="585" href="#585">585</a> <em>     * @return the amount of data written.</em><a name="586" href="#586">586</a> <em>     *</em><a name="587" href="#587">587</a> <em>     * @throws MalformedStreamException if the stream ends unexpectedly.</em><a name="588" href="#588">588</a> <em>     * @throws IOException              if an i/o error occurs.</em><a name="589" href="#589">589</a> <em>     */</em><a name="590" href="#590">590</a>     <strong>public</strong> <strong>int</strong> readBodyData(OutputStream output)<a name="591" href="#591">591</a>             throws MalformedStreamException, IOException {<a name="592" href="#592">592</a>         <strong>final</strong> InputStream istream = newInputStream();<a name="593" href="#593">593</a>         <strong>return</strong> (<strong>int</strong>) Streams.copy(istream, output, false);<a name="594" href="#594">594</a>     }<a name="595" href="#595">595</a> <a name="596" href="#596">596</a>     <em>/**</em><a name="597" href="#597">597</a> <em>     * Creates a new {@link ItemInputStream}.</em><a name="598" href="#598">598</a> <em>     * @return A new instance of {@link ItemInputStream}.</em><a name="599" href="#599">599</a> <em>     */</em><a name="600" href="#600">600</a>     ItemInputStream newInputStream() {<a name="601" href="#601">601</a>         <strong>return</strong> <strong>new</strong> ItemInputStream();<a name="602" href="#602">602</a>     }<a name="603" href="#603">603</a> <a name="604" href="#604">604</a>     <em>/**</em><a name="605" href="#605">605</a> <em>     * &lt;p> Reads &lt;code>body-data&lt;/code> from the current</em><a name="606" href="#606">606</a> <em>     * &lt;code>encapsulation&lt;/code> and discards it.</em><a name="607" href="#607">607</a> <em>     *</em><a name="608" href="#608">608</a> <em>     * &lt;p>Use this method to skip encapsulations you don't need or don't</em><a name="609" href="#609">609</a> <em>     * understand.</em><a name="610" href="#610">610</a> <em>     *</em><a name="611" href="#611">611</a> <em>     * @return The amount of data discarded.</em><a name="612" href="#612">612</a> <em>     *</em><a name="613" href="#613">613</a> <em>     * @throws MalformedStreamException if the stream ends unexpectedly.</em><a name="614" href="#614">614</a> <em>     * @throws IOException              if an i/o error occurs.</em><a name="615" href="#615">615</a> <em>     */</em><a name="616" href="#616">616</a>     <strong>public</strong> <strong>int</strong> discardBodyData()<a name="617" href="#617">617</a>     throws MalformedStreamException,<a name="618" href="#618">618</a>     IOException {<a name="619" href="#619">619</a>         <strong>return</strong> readBodyData(<strong>null</strong>);<a name="620" href="#620">620</a>     }<a name="621" href="#621">621</a> <a name="622" href="#622">622</a> <a name="623" href="#623">623</a>     <em>/**</em><a name="624" href="#624">624</a> <em>     * Finds the beginning of the first &lt;code>encapsulation&lt;/code>.</em><a name="625" href="#625">625</a> <em>     *</em><a name="626" href="#626">626</a> <em>     * @return &lt;code>true&lt;/code> if an &lt;code>encapsulation&lt;/code> was found in</em><a name="627" href="#627">627</a> <em>     *         the stream.</em><a name="628" href="#628">628</a> <em>     *</em><a name="629" href="#629">629</a> <em>     * @throws IOException if an i/o error occurs.</em><a name="630" href="#630">630</a> <em>     */</em><a name="631" href="#631">631</a>     <strong>public</strong> <strong>boolean</strong> skipPreamble()<a name="632" href="#632">632</a>     throws IOException {<a name="633" href="#633">633</a>         <em class="comment">// First delimiter may be not preceeded with a CRLF.</em><a name="634" href="#634">634</a>         System.arraycopy(boundary, 2, boundary, 0, boundary.length - 2);<a name="635" href="#635">635</a>         boundaryLength = boundary.length - 2;<a name="636" href="#636">636</a>         <strong>try</strong> {<a name="637" href="#637">637</a>             <em class="comment">// Discard all data up to the delimiter.</em><a name="638" href="#638">638</a>             discardBodyData();<a name="639" href="#639">639</a> <a name="640" href="#640">640</a>             <em class="comment">// Read boundary - if succeded, the stream contains an</em><a name="641" href="#641">641</a>             <em class="comment">// encapsulation.</em><a name="642" href="#642">642</a>             <strong>return</strong> readBoundary();<a name="643" href="#643">643</a>         } <strong>catch</strong> (MalformedStreamException e) {<a name="644" href="#644">644</a>             <strong>return</strong> false;<a name="645" href="#645">645</a>         } <strong>finally</strong> {<a name="646" href="#646">646</a>             <em class="comment">// Restore delimiter.</em><a name="647" href="#647">647</a>             System.arraycopy(boundary, 0, boundary, 2, boundary.length - 2);<a name="648" href="#648">648</a>             boundaryLength = boundary.length;<a name="649" href="#649">649</a>             boundary[0] = CR;<a name="650" href="#650">650</a>             boundary[1] = LF;<a name="651" href="#651">651</a>         }<a name="652" href="#652">652</a>     }<a name="653" href="#653">653</a> <a name="654" href="#654">654</a> <a name="655" href="#655">655</a>     <em>/**</em><a name="656" href="#656">656</a> <em>     * Compares &lt;code>count&lt;/code> first bytes in the arrays</em><a name="657" href="#657">657</a> <em>     * &lt;code>a&lt;/code> and &lt;code>b&lt;/code>.</em><a name="658" href="#658">658</a> <em>     *</em><a name="659" href="#659">659</a> <em>     * @param a     The first array to compare.</em>

⌨️ 快捷键说明

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