arcrecord.html
来自「网络爬虫开源代码」· HTML 代码 · 共 394 行 · 第 1/3 页
HTML
394 行
<a name="124" href="#124">124</a> <em> * at any time though before reading any of the arc record</em><a name="125" href="#125">125</a> <em> * content is only time that it makes sense.</em><a name="126" href="#126">126</a> <em> * </em><a name="127" href="#127">127</a> <em> * <p>After calling this method, you can call</em><a name="128" href="#128">128</a> <em> * {@link #getHttpHeaders()} to get the read http header.</em><a name="129" href="#129">129</a> <em> * </em><a name="130" href="#130">130</a> <em> * @throws IOException</em><a name="131" href="#131">131</a> <em> */</em><a name="132" href="#132">132</a> <strong>public</strong> <strong>void</strong> skipHttpHeader() throws IOException {<a name="133" href="#133">133</a> <strong>if</strong> (<strong>this</strong>.httpHeaderStream != <strong>null</strong>) {<a name="134" href="#134">134</a> <em class="comment">// Empty the httpHeaderStream</em><a name="135" href="#135">135</a> <strong>for</strong> (<strong>int</strong> available = <strong>this</strong>.httpHeaderStream.available();<a name="136" href="#136">136</a> <strong>this</strong>.httpHeaderStream != <strong>null</strong> &&<a name="137" href="#137">137</a> (available = <strong>this</strong>.httpHeaderStream.available()) > 0;) {<a name="138" href="#138">138</a> <em class="comment">// We should be in this loop once only we should only do this</em><a name="139" href="#139">139</a> <em class="comment">// buffer allocation once.</em><a name="140" href="#140">140</a> byte [] buffer = <strong>new</strong> byte[available];<a name="141" href="#141">141</a> <em class="comment">// The read nulls out httpHeaderStream when done with it so</em><a name="142" href="#142">142</a> <em class="comment">// need check for null in the loop control line.</em><a name="143" href="#143">143</a> read(buffer, 0, available);<a name="144" href="#144">144</a> }<a name="145" href="#145">145</a> }<a name="146" href="#146">146</a> }<a name="147" href="#147">147</a> <a name="148" href="#148">148</a> <strong>public</strong> <strong>void</strong> dumpHttpHeader() throws IOException {<a name="149" href="#149">149</a> <strong>if</strong> (<strong>this</strong>.httpHeaderStream == <strong>null</strong>) {<a name="150" href="#150">150</a> <strong>return</strong>;<a name="151" href="#151">151</a> }<a name="152" href="#152">152</a> <em class="comment">// Dump the httpHeaderStream to STDOUT</em><a name="153" href="#153">153</a> <strong>for</strong> (<strong>int</strong> available = <strong>this</strong>.httpHeaderStream.available();<a name="154" href="#154">154</a> <strong>this</strong>.httpHeaderStream != <strong>null</strong><a name="155" href="#155">155</a> && (available = <strong>this</strong>.httpHeaderStream.available()) > 0;) {<a name="156" href="#156">156</a> <em class="comment">// We should be in this loop only once and should do this</em><a name="157" href="#157">157</a> <em class="comment">// buffer allocation once.</em><a name="158" href="#158">158</a> byte[] buffer = <strong>new</strong> byte[available];<a name="159" href="#159">159</a> <em class="comment">// The read nulls out httpHeaderStream when done with it so</em><a name="160" href="#160">160</a> <em class="comment">// need check for null in the loop control line.</em><a name="161" href="#161">161</a> <strong>int</strong> read = read(buffer, 0, available);<a name="162" href="#162">162</a> System.out.write(buffer, 0, read);<a name="163" href="#163">163</a> }<a name="164" href="#164">164</a> }<a name="165" href="#165">165</a> <a name="166" href="#166">166</a> <em>/**<em>*</em></em><a name="167" href="#167">167</a> <em> * Read http header if present. Technique borrowed from HttpClient HttpParse</em><a name="168" href="#168">168</a> <em> * class.</em><a name="169" href="#169">169</a> <em> * </em><a name="170" href="#170">170</a> <em> * @return ByteArrayInputStream with the http header in it or null if no</em><a name="171" href="#171">171</a> <em> * http header.</em><a name="172" href="#172">172</a> <em> * @throws IOException</em><a name="173" href="#173">173</a> <em> */</em><a name="174" href="#174">174</a> <strong>private</strong> InputStream readHttpHeader() throws IOException {<a name="175" href="#175">175</a> <em class="comment">// If judged a record that doesn't have an http header, return</em><a name="176" href="#176">176</a> <em class="comment">// immediately.</em><a name="177" href="#177">177</a> <strong>if</strong>(!getHeader().getUrl().startsWith(<span class="string">"http"</span>) ||<a name="178" href="#178">178</a> getHeader().getLength() <= MIN_HTTP_HEADER_LENGTH) {<a name="179" href="#179">179</a> <strong>return</strong> <strong>null</strong>;<a name="180" href="#180">180</a> }<a name="181" href="#181">181</a> byte [] statusBytes = HttpParser.readRawLine(getIn());<a name="182" href="#182">182</a> <strong>int</strong> eolCharCount = getEolCharsCount(statusBytes);<a name="183" href="#183">183</a> <strong>if</strong> (eolCharCount <= 0) {<a name="184" href="#184">184</a> <strong>throw</strong> <strong>new</strong> IOException(<span class="string">"Failed to read http status where one "</span> +<a name="185" href="#185">185</a> <span class="string">" was expected: "</span> + <strong>new</strong> String(statusBytes));<a name="186" href="#186">186</a> }<a name="187" href="#187">187</a> String statusLine = EncodingUtil.getString(statusBytes, 0,<a name="188" href="#188">188</a> statusBytes.length - eolCharCount, ARCConstants.DEFAULT_ENCODING);<a name="189" href="#189">189</a> <strong>if</strong> ((statusLine == <strong>null</strong>) ||<a name="190" href="#190">190</a> !StatusLine.startsWithHTTP(statusLine)) {<a name="191" href="#191">191</a> <strong>if</strong> (statusLine.startsWith(<span class="string">"DELETED"</span>)) {<a name="192" href="#192">192</a> <em class="comment">// Some old ARCs have deleted records like following:</em><a name="193" href="#193">193</a> <em class="comment">// http://vireo.gatech.edu:80/ebt-bin/nph-dweb/dynaweb/SGI_Developer/SGITCL_PG/@Generic__BookTocView/11108%3Btd%3D2 130.207.168.42 19991010131803 text/html 29202</em><a name="194" href="#194">194</a> <em class="comment">// DELETED_TIME=20000425001133_DELETER=Kurt_REASON=alexalist</em><a name="195" href="#195">195</a> <em class="comment">// (follows ~29K spaces)</em><a name="196" href="#196">196</a> <em class="comment">// For now, throw a RecoverableIOException so if iterating over</em><a name="197" href="#197">197</a> <em class="comment">// records, we keep going. TODO: Later make a legitimate</em><a name="198" href="#198">198</a> <em class="comment">// ARCRecord from the deleted record rather than throw</em><a name="199" href="#199">199</a> <em class="comment">// exception.</em><a name="200" href="#200">200</a> <strong>throw</strong> <strong>new</strong> DeletedARCRecordIOException(statusLine);<a name="201" href="#201">201</a> } <strong>else</strong> {<a name="202" href="#202">202</a> <strong>throw</strong> <strong>new</strong> IOException(<span class="string">"Failed parse of http status line."</span>);<a name="203" href="#203">203</a> }<a name="204" href="#204">204</a> }<a name="205" href="#205">205</a> <strong>this</strong>.httpStatus = <strong>new</strong> StatusLine(statusLine);<a name="206" href="#206">206</a> <a name="207" href="#207">207</a> <em class="comment">// Save off all bytes read. Keep them as bytes rather than</em><a name="208" href="#208">208</a> <em class="comment">// convert to strings so we don't have to worry about encodings</em><a name="209" href="#209">209</a> <em class="comment">// though this should never be a problem doing http headers since</em><a name="210" href="#210">210</a> <em class="comment">// its all supposed to be ascii.</em><a name="211" href="#211">211</a> ByteArrayOutputStream baos =<a name="212" href="#212">212</a> <strong>new</strong> ByteArrayOutputStream(statusBytes.length + 4 * 1024);<a name="213" href="#213">213</a> baos.write(statusBytes);<a name="214" href="#214">214</a> <a name="215" href="#215">215</a> <em class="comment">// Now read rest of the header lines looking for the separation</em><a name="216" href="#216">216</a> <em class="comment">// between header and body.</em><a name="217" href="#217">217</a> <strong>for</strong> (byte [] lineBytes = <strong>null</strong>; <strong>true</strong>;) {<a name="218" href="#218">218</a> lineBytes = HttpParser.readRawLine(getIn());<a name="219" href="#219">219</a> eolCharCount = getEolCharsCount(lineBytes);<a name="220" href="#220">220</a> <strong>if</strong> (eolCharCount <= 0) {<a name="221" href="#221">221</a> <strong>throw</strong> <strong>new</strong> IOException(<span class="string">"Failed reading http headers: "</span> +<a name="222" href="#222">222</a> ((lineBytes != <strong>null</strong>)? <strong>new</strong> String(lineBytes): <strong>null</strong>));<a name="223" href="#223">223</a> }<a name="224" href="#224">224</a> <em class="comment">// Save the bytes read.</em><a name="225" href="#225">225</a> baos.write(lineBytes);<a name="226" href="#226">226</a> <strong>if</strong> ((lineBytes.length - eolCharCount) <= 0) {<a name="227" href="#227">227</a> <em class="comment">// We've finished reading the http header.</em><a name="228" href="#228">228</a> <strong>break</strong>;<a name="229" href="#229">229</a> }<a name="230" href="#230">230</a> }<a name="231" href="#231">231</a> <a name="232" href="#232">232</a> byte [] headerBytes = baos.toByteArray();<a name="233" href="#233">233</a> <em class="comment">// Save off where body starts.</em><a name="234" href="#234">234</a> <strong>this</strong>.getMetaData().setContentBegin(headerBytes.length);<a name="235" href="#235">235</a> ByteArrayInputStream bais =<a name="236" href="#236">236</a> <strong>new</strong> ByteArrayInputStream(headerBytes);<a name="237" href="#237">237</a> <strong>if</strong> (!bais.markSupported()) {<a name="238" href="#238">238</a> <strong>throw</strong> <strong>new</strong> IOException(<span class="string">"ByteArrayInputStream does not support mark"</span>);<a name="239" href="#239">239</a> }<a name="240" href="#240">240</a> bais.mark(headerBytes.length);<a name="241" href="#241">241</a> <em class="comment">// Read the status line. Don't let it into the parseHeaders function.</em><a name="242" href="#242">242</a> <em class="comment">// It doesn't know what to do with it.</em><a name="243" href="#243">243</a> bais.read(statusBytes, 0, statusBytes.length);<a name="244" href="#244">244</a> <strong>this</strong>.httpHeaders = HttpParser.parseHeaders(bais,<a name="245" href="#245">245</a> ARCConstants.DEFAULT_ENCODING);<a name="246" href="#246">246</a> <strong>this</strong>.getMetaData().setStatusCode(Integer.toString(getStatusCode()));<a name="247" href="#247">247</a> bais.reset();<a name="248" href="#248">248</a> <strong>return</strong> bais;<a name="249" href="#249">249</a> }<a name="250" href="#250">250</a> <a name="251" href="#251">251</a> <strong>private</strong> <strong>static</strong> <strong>class</strong> DeletedARCRecordIOException<a name="252" href="#252">252</a> <strong>extends</strong> <a href="../../../../org/archive/io/RecoverableIOException.html">RecoverableIOException</a> {<a name="253" href="#253">253</a> <strong>public</strong> DeletedARCRecordIOException(<strong>final</strong> String reason) {<a name="254" href="#254">254</a> <strong>super</strong>(reason);<a name="255" href="#255">255</a> }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?