archivereaderfactory.html
来自「网络爬虫开源代码」· HTML 代码 · 共 331 行 · 第 1/2 页
HTML
331 行
<a name="158" href="#158">158</a> }<a name="159" href="#159">159</a> <em class="comment">// RepositionableInputStream calls mark on each read so can back up at</em><a name="160" href="#160">160</a> <em class="comment">// least the read amount. Needed for gzip inflater overinflations</em><a name="161" href="#161">161</a> <em class="comment">// reading into the next gzip member.</em><a name="162" href="#162">162</a> <strong>return</strong> <strong>new</strong> <a href="../../../org/archive/io/RepositionableInputStream.html">RepositionableInputStream</a>(is, 16 * 1024);<a name="163" href="#163">163</a> }<a name="164" href="#164">164</a> <a name="165" href="#165">165</a> <strong>protected</strong> <a href="../../../org/archive/io/ArchiveReader.html">ArchiveReader</a> getArchiveReader(<strong>final</strong> String id, <a name="166" href="#166">166</a> <strong>final</strong> InputStream is, <strong>final</strong> <strong>boolean</strong> atFirstRecord)<a name="167" href="#167">167</a> throws IOException {<a name="168" href="#168">168</a> <strong>final</strong> InputStream stream = asRepositionable(is);<a name="169" href="#169">169</a> <strong>if</strong> (ARCReaderFactory.isARCSuffix(id)) {<a name="170" href="#170">170</a> <strong>return</strong> ARCReaderFactory.get(id, stream, atFirstRecord);<a name="171" href="#171">171</a> } <strong>else</strong> <strong>if</strong> (WARCReaderFactory.isWARCSuffix(id)) {<a name="172" href="#172">172</a> <strong>return</strong> WARCReaderFactory.get(id, stream, atFirstRecord);<a name="173" href="#173">173</a> }<a name="174" href="#174">174</a> <strong>throw</strong> <strong>new</strong> IOException(<span class="string">"Unknown extension (Not ARC nor WARC): "</span> + id);<a name="175" href="#175">175</a> }<a name="176" href="#176">176</a> <a name="177" href="#177">177</a> <em>/**<em>*</em></em><a name="178" href="#178">178</a> <em> * Get an Archive Reader aligned at <code>offset</code>.</em><a name="179" href="#179">179</a> <em> * This version of get will not bring the file local but will try to</em><a name="180" href="#180">180</a> <em> * stream across the net making an HTTP 1.1 Range request on remote</em><a name="181" href="#181">181</a> <em> * http server (RFC1435 Section 14.35).</em><a name="182" href="#182">182</a> <em> * @param u HTTP URL for an Archive file.</em><a name="183" href="#183">183</a> <em> * @param offset Offset into file at which to start fetching.</em><a name="184" href="#184">184</a> <em> * @return An ArchiveReader aligned at offset.</em><a name="185" href="#185">185</a> <em> * @throws IOException</em><a name="186" href="#186">186</a> <em> */</em><a name="187" href="#187">187</a> <strong>public</strong> <strong>static</strong> <a href="../../../org/archive/io/ArchiveReader.html">ArchiveReader</a> get(<strong>final</strong> URL u, <strong>final</strong> <strong>long</strong> offset)<a name="188" href="#188">188</a> throws IOException {<a name="189" href="#189">189</a> <strong>return</strong> ArchiveReaderFactory.factory.getArchiveReader(u, offset);<a name="190" href="#190">190</a> }<a name="191" href="#191">191</a> <a name="192" href="#192">192</a> <strong>protected</strong> <a href="../../../org/archive/io/ArchiveReader.html">ArchiveReader</a> getArchiveReader(<strong>final</strong> URL f, <strong>final</strong> <strong>long</strong> offset)<a name="193" href="#193">193</a> throws IOException {<a name="194" href="#194">194</a> <em class="comment">// Get URL connection.</em><a name="195" href="#195">195</a> URLConnection connection = f.openConnection();<a name="196" href="#196">196</a> <strong>if</strong> (!(connection instanceof HttpURLConnection)) {<a name="197" href="#197">197</a> <strong>throw</strong> <strong>new</strong> IOException(<span class="string">"This method only handles HTTP connections."</span>);<a name="198" href="#198">198</a> }<a name="199" href="#199">199</a> addUserAgent((HttpURLConnection)connection);<a name="200" href="#200">200</a> <strong>if</strong> (offset != STREAM_ALL) {<a name="201" href="#201">201</a> <em class="comment">// Use a Range request (Assumes HTTP 1.1 on other end). If</em><a name="202" href="#202">202</a> <em class="comment">// length >= 0, add open-ended range header to the request. Else,</em><a name="203" href="#203">203</a> <em class="comment">// because end-byte is inclusive, subtract 1.</em><a name="204" href="#204">204</a> connection.addRequestProperty(<span class="string">"Range"</span>, <span class="string">"bytes="</span> + offset + <span class="string">"-"</span>);<a name="205" href="#205">205</a> }<a name="206" href="#206">206</a> <a name="207" href="#207">207</a> <strong>return</strong> getArchiveReader(f.toString(), connection.getInputStream(),<a name="208" href="#208">208</a> (offset == 0));<a name="209" href="#209">209</a> }<a name="210" href="#210">210</a> <a name="211" href="#211">211</a> <em>/**<em>*</em></em><a name="212" href="#212">212</a> <em> * Get an ARCReader.</em><a name="213" href="#213">213</a> <em> * Pulls the ARC local into whereever the System Property</em><a name="214" href="#214">214</a> <em> * <code>java.io.tmpdir</code> points. It then hands back an ARCReader that</em><a name="215" href="#215">215</a> <em> * points at this local copy. A close on this ARCReader instance will</em><a name="216" href="#216">216</a> <em> * remove the local copy.</em><a name="217" href="#217">217</a> <em> * @param u An URL that points at an ARC.</em><a name="218" href="#218">218</a> <em> * @return An ARCReader.</em><a name="219" href="#219">219</a> <em> * @throws IOException </em><a name="220" href="#220">220</a> <em> */</em><a name="221" href="#221">221</a> <strong>public</strong> <strong>static</strong> <a href="../../../org/archive/io/ArchiveReader.html">ArchiveReader</a> get(<strong>final</strong> URL u)<a name="222" href="#222">222</a> throws IOException {<a name="223" href="#223">223</a> <strong>return</strong> ArchiveReaderFactory.factory.getArchiveReader(u);<a name="224" href="#224">224</a> }<a name="225" href="#225">225</a> <a name="226" href="#226">226</a> <strong>protected</strong> <a href="../../../org/archive/io/ArchiveReader.html">ArchiveReader</a> getArchiveReader(<strong>final</strong> URL u)<a name="227" href="#227">227</a> throws IOException {<a name="228" href="#228">228</a> <em class="comment">// If url represents a local file then return file it points to.</em><a name="229" href="#229">229</a> <strong>if</strong> (u.getPath() != <strong>null</strong>) {<a name="230" href="#230">230</a> <em class="comment">// TODO: Add scheme check and host check.</em><a name="231" href="#231">231</a> File f = <strong>new</strong> File(u.getPath());<a name="232" href="#232">232</a> <strong>if</strong> (f.exists()) {<a name="233" href="#233">233</a> <strong>return</strong> get(f, 0);<a name="234" href="#234">234</a> }<a name="235" href="#235">235</a> }<a name="236" href="#236">236</a> <a name="237" href="#237">237</a> String scheme = u.getProtocol();<a name="238" href="#238">238</a> <strong>if</strong> (scheme.startsWith(<span class="string">"http"</span>) || scheme.equals(<span class="string">"s3"</span>)) {<a name="239" href="#239">239</a> <em class="comment">// Try streaming if http or s3 URLs rather than copying local</em><a name="240" href="#240">240</a> <em class="comment">// and then reading (Passing an offset will get us an Reader</em><a name="241" href="#241">241</a> <em class="comment">// that wraps a Stream).</em><a name="242" href="#242">242</a> <strong>return</strong> get(u, STREAM_ALL);<a name="243" href="#243">243</a> }<a name="244" href="#244">244</a> <a name="245" href="#245">245</a> <strong>return</strong> makeARCLocal(u.openConnection());<a name="246" href="#246">246</a> }<a name="247" href="#247">247</a> <a name="248" href="#248">248</a> <strong>protected</strong> <a href="../../../org/archive/io/ArchiveReader.html">ArchiveReader</a> makeARCLocal(<strong>final</strong> URLConnection connection)<a name="249" href="#249">249</a> throws IOException {<a name="250" href="#250">250</a> File localFile = <strong>null</strong>;<a name="251" href="#251">251</a> <strong>if</strong> (connection instanceof HttpURLConnection) {<a name="252" href="#252">252</a> <em class="comment">// If http url connection, bring down the resource local.</em><a name="253" href="#253">253</a> String p = connection.getURL().getPath();<a name="254" href="#254">254</a> <strong>int</strong> index = p.lastIndexOf('/');<a name="255" href="#255">255</a> <strong>if</strong> (index >= 0) {<a name="256" href="#256">256</a> <em class="comment">// Name file for the file we're making local.</em><a name="257" href="#257">257</a> localFile = <strong>new</strong> File(FileUtils.TMPDIR, p.substring(index + 1));<a name="258" href="#258">258</a> <strong>if</strong> (localFile.exists()) {<a name="259" href="#259">259</a> <em class="comment">// If file of same name already exists in TMPDIR, then</em><a name="260" href="#260">260</a> <em class="comment">// clean it up (Assuming only reason a file of same name in</em><a name="261" href="#261">261</a> <em class="comment">// TMPDIR is because we failed a previous download).</em><a name="262" href="#262">262</a> localFile.delete();<a name="263" href="#263">263</a> }<a name="264" href="#264">264</a> } <strong>else</strong> {<a name="265" href="#265">265</a> localFile = File.createTempFile(ArchiveReader.<strong>class</strong>.getName(),<a name="266" href="#266">266</a> <span class="string">".tmp"</span>, FileUtils.TMPDIR);<a name="267" href="#267">267</a> }<a name="268" href="#268">268</a> addUserAgent((HttpURLConnection)connection);<a name="269" href="#269">269</a> connection.connect();<a name="270" href="#270">270</a> <strong>try</strong> {<a name="271" href="#271">271</a> IoUtils.readFullyToFile(connection.getInputStream(), localFile,<a name="272" href="#272">272</a> <strong>new</strong> byte[16 * 1024]);<a name="273" href="#273">273</a> } <strong>catch</strong> (IOException ioe) {<a name="274" href="#274">274</a> localFile.delete();<a name="275" href="#275">275</a> <strong>throw</strong> ioe;<a name="276" href="#276">276</a> }<a name="277" href="#277">277</a> } <strong>else</strong> <strong>if</strong> (connection instanceof RsyncURLConnection) {<a name="278" href="#278">278</a> <em class="comment">// Then, connect and this will create a local file.</em><a name="279" href="#279">279</a> <em class="comment">// See implementation of the rsync handler.</em><a name="280" href="#280">280</a> connection.connect();<a name="281" href="#281">281</a> localFile = ((RsyncURLConnection)connection).getFile();<a name="282" href="#282">282</a> } <strong>else</strong> <strong>if</strong> (connection instanceof Md5URLConnection) {<a name="283" href="#283">283</a> <em class="comment">// Then, connect and this will create a local file.</em><a name="284" href="#284">284</a> <em class="comment">// See implementation of the md5 handler.</em><a name="285" href="#285">285</a> connection.connect();<a name="286" href="#286">286</a> localFile = ((Md5URLConnection)connection).getFile();<a name="287" href="#287">287</a> } <strong>else</strong> {<a name="288" href="#288">288</a> <strong>throw</strong> <strong>new</strong> UnsupportedOperationException(<span class="string">"No support for "</span> +<a name="289" href="#289">289</a> connection);<a name="290" href="#290">290</a> }<a name="291" href="#291">291</a> <a name="292" href="#292">292</a> <a href="../../../org/archive/io/ArchiveReader.html">ArchiveReader</a> reader = <strong>null</strong>;<a name="293" href="#293">293</a> <strong>try</strong> {<a name="294" href="#294">294</a> reader = get(localFile, 0);<a name="295" href="#295">295</a> } <strong>catch</strong> (IOException e) {<a name="296" href="#296">296</a> localFile.delete();<a name="297" href="#297">297</a> <strong>throw</strong> e;<a name="298" href="#298">298</a> }<a name="299" href="#299">299</a> <a name="300" href="#300">300</a> <em class="comment">// Return a delegate that does cleanup of downloaded file on close.</em><a name="301" href="#301">301</a> <strong>return</strong> reader.getDeleteFileOnCloseReader(localFile);<a name="302" href="#302">302</a> }<a name="303" href="#303">303</a> <a name="304" href="#304">304</a> <strong>protected</strong> <strong>void</strong> addUserAgent(<strong>final</strong> HttpURLConnection connection) {<a name="305" href="#305">305</a> connection.addRequestProperty(<span class="string">"User-Agent"</span>, <strong>this</strong>.getClass().getName());<a name="306" href="#306">306</a> }<a name="307" href="#307">307</a> <a name="308" href="#308">308</a> <em>/**<em>*</em></em><a name="309" href="#309">309</a> <em> * @param f File to test.</em><a name="310" href="#310">310</a> <em> * @return True if <code>f</code> is compressed.</em><a name="311" href="#311">311</a> <em> * @throws IOException</em><a name="312" href="#312">312</a> <em> */</em><a name="313" href="#313">313</a> <strong>protected</strong> <strong>boolean</strong> isCompressed(<strong>final</strong> File f) throws IOException {<a name="314" href="#314">314</a> <strong>return</strong> f.getName().toLowerCase().<a name="315" href="#315">315</a> endsWith(DOT_COMPRESSED_FILE_EXTENSION);<a name="316" href="#316">316</a> }<a name="317" href="#317">317</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 + =
减小字号Ctrl + -
显示快捷键?