ioutils.html
来自「网络爬虫开源代码」· HTML 代码 · 共 349 行 · 第 1/2 页
HTML
349 行
<a name="167" href="#167">167</a> readFullyToFile(is, toFile, <strong>new</strong> byte[4096]);<a name="168" href="#168">168</a> }<a name="169" href="#169">169</a> <a name="170" href="#170">170</a> <em>/**<em>*</em></em><a name="171" href="#171">171</a> <em> * Read the entire stream to EOF into the passed file.</em><a name="172" href="#172">172</a> <em> * Closes <code>is</code> when done or if an exception.</em><a name="173" href="#173">173</a> <em> * @param is Stream to read.</em><a name="174" href="#174">174</a> <em> * @param toFile File to read into .</em><a name="175" href="#175">175</a> <em> * @param buffer Buffer to use reading.</em><a name="176" href="#176">176</a> <em> * @return Count of bytes read.</em><a name="177" href="#177">177</a> <em> * @throws IOException</em><a name="178" href="#178">178</a> <em> */</em><a name="179" href="#179">179</a> <strong>public</strong> <strong>static</strong> <strong>long</strong> readFullyToFile(<strong>final</strong> InputStream is, <strong>final</strong> File toFile,<a name="180" href="#180">180</a> <strong>final</strong> byte [] buffer)<a name="181" href="#181">181</a> throws IOException {<a name="182" href="#182">182</a> <strong>long</strong> totalcount = -1;<a name="183" href="#183">183</a> OutputStream os =<a name="184" href="#184">184</a> <strong>new</strong> FastBufferedOutputStream(<strong>new</strong> FileOutputStream(toFile));<a name="185" href="#185">185</a> InputStream localIs = (is instanceof BufferedInputStream)?<a name="186" href="#186">186</a> is: <strong>new</strong> BufferedInputStream(is);<a name="187" href="#187">187</a> <strong>try</strong> {<a name="188" href="#188">188</a> <strong>for</strong> (<strong>int</strong> count = -1;<a name="189" href="#189">189</a> (count = localIs.read(buffer, 0, buffer.length)) != -1;<a name="190" href="#190">190</a> totalcount += count) {<a name="191" href="#191">191</a> os.write(buffer, 0, count); <a name="192" href="#192">192</a> }<a name="193" href="#193">193</a> } <strong>finally</strong> {<a name="194" href="#194">194</a> os.close();<a name="195" href="#195">195</a> <strong>if</strong> (localIs != <strong>null</strong>) {<a name="196" href="#196">196</a> localIs.close();<a name="197" href="#197">197</a> }<a name="198" href="#198">198</a> }<a name="199" href="#199">199</a> <strong>return</strong> totalcount;<a name="200" href="#200">200</a> }<a name="201" href="#201">201</a> <a name="202" href="#202">202</a> <em>/**<em>*</em></em><a name="203" href="#203">203</a> <em> * Wrap generic Throwable as a checked IOException</em><a name="204" href="#204">204</a> <em> * @param e wrapped exception</em><a name="205" href="#205">205</a> <em> * @return IOException</em><a name="206" href="#206">206</a> <em> */</em><a name="207" href="#207">207</a> <strong>public</strong> <strong>static</strong> IOException wrapAsIOException(Throwable e) {<a name="208" href="#208">208</a> IOException ioe = <strong>new</strong> IOException(e.toString());<a name="209" href="#209">209</a> ioe.initCause(e);<a name="210" href="#210">210</a> <strong>return</strong> ioe;<a name="211" href="#211">211</a> }<a name="212" href="#212">212</a> <a name="213" href="#213">213</a> <a name="214" href="#214">214</a> <strong>public</strong> <strong>static</strong> <strong>void</strong> readFully(InputStream input, byte[] buf) <a name="215" href="#215">215</a> throws IOException {<a name="216" href="#216">216</a> <strong>int</strong> max = buf.length;<a name="217" href="#217">217</a> <strong>int</strong> ofs = 0;<a name="218" href="#218">218</a> <strong>while</strong> (ofs < max) {<a name="219" href="#219">219</a> <strong>int</strong> l = input.read(buf, ofs, max - ofs);<a name="220" href="#220">220</a> <strong>if</strong> (l == 0) {<a name="221" href="#221">221</a> <strong>throw</strong> <strong>new</strong> EOFException();<a name="222" href="#222">222</a> }<a name="223" href="#223">223</a> ofs += l;<a name="224" href="#224">224</a> }<a name="225" href="#225">225</a> }<a name="226" href="#226">226</a> <a name="227" href="#227">227</a> <em>/**<em>*</em></em><a name="228" href="#228">228</a> <em> * Return the maximum number of bytes per character in the named</em><a name="229" href="#229">229</a> <em> * encoding, or 0 if encoding is invalid or unsupported. </em><a name="230" href="#230">230</a> <em> *</em><a name="231" href="#231">231</a> <em> * @param encoding Encoding to consider. For now, should be java </em><a name="232" href="#232">232</a> <em> * canonical name for the encoding.</em><a name="233" href="#233">233</a> <em> *</em><a name="234" href="#234">234</a> <em> * @return True if multibyte encoding.</em><a name="235" href="#235">235</a> <em> */</em><a name="236" href="#236">236</a> <strong>public</strong> <strong>static</strong> <strong>float</strong> encodingMaxBytesPerChar(String encoding) {<a name="237" href="#237">237</a> <strong>boolean</strong> isMultibyte = false;<a name="238" href="#238">238</a> <strong>final</strong> Charset cs;<a name="239" href="#239">239</a> <strong>try</strong> {<a name="240" href="#240">240</a> <strong>if</strong> (encoding != <strong>null</strong> && encoding.length() > 0) {<a name="241" href="#241">241</a> cs = Charset.forName(encoding);<a name="242" href="#242">242</a> <strong>if</strong>(cs.canEncode()) {<a name="243" href="#243">243</a> <strong>return</strong> cs.newEncoder().maxBytesPerChar();<a name="244" href="#244">244</a> } <strong>else</strong> {<a name="245" href="#245">245</a> logger.info(<span class="string">"Encoding not fully supported: "</span> + encoding<a name="246" href="#246">246</a> + <span class="string">". Defaulting to single byte."</span>);<a name="247" href="#247">247</a> }<a name="248" href="#248">248</a> }<a name="249" href="#249">249</a> } <strong>catch</strong> (IllegalArgumentException e) {<a name="250" href="#250">250</a> <em class="comment">// Unsupported encoding</em><a name="251" href="#251">251</a> logger.log(Level.INFO,<span class="string">"Illegal encoding name: "</span> + encoding,e);<a name="252" href="#252">252</a> }<a name="253" href="#253">253</a> <a name="254" href="#254">254</a> logger.fine(<span class="string">"Encoding "</span> + encoding + <span class="string">" is multibyte: "</span><a name="255" href="#255">255</a> + ((isMultibyte) ? Boolean.TRUE : Boolean.FALSE));<a name="256" href="#256">256</a> <em class="comment">// default: return 0</em><a name="257" href="#257">257</a> <strong>return</strong> 0;<a name="258" href="#258">258</a> }<a name="259" href="#259">259</a> <a name="260" href="#260">260</a> <em>/**<em>*</em></em><a name="261" href="#261">261</a> <em> * Utility method to serialize an object to the given File. </em><a name="262" href="#262">262</a> <em> * </em><a name="263" href="#263">263</a> <em> * @param object Object to serialize</em><a name="264" href="#264">264</a> <em> * @param file File to receive serialized copy</em><a name="265" href="#265">265</a> <em> * @throws IOException</em><a name="266" href="#266">266</a> <em> */</em><a name="267" href="#267">267</a> <strong>public</strong> <strong>static</strong> <strong>void</strong> serializeToFile(Object object, File file) throws IOException {<a name="268" href="#268">268</a> ObjectOutputStream oos = <strong>new</strong> ObjectOutputStream(<strong>new</strong> BufferedOutputStream(<strong>new</strong> FileOutputStream(file)));<a name="269" href="#269">269</a> oos.writeObject(object);<a name="270" href="#270">270</a> oos.close();<a name="271" href="#271">271</a> }<a name="272" href="#272">272</a> <a name="273" href="#273">273</a> <em>/**<em>*</em></em><a name="274" href="#274">274</a> <em> * Utility method to deserialize an Object from given File. </em><a name="275" href="#275">275</a> <em> * </em><a name="276" href="#276">276</a> <em> * @param file File source</em><a name="277" href="#277">277</a> <em> * @return deserialized Object</em><a name="278" href="#278">278</a> <em> * @throws IOException</em><a name="279" href="#279">279</a> <em> */</em><a name="280" href="#280">280</a> <strong>public</strong> <strong>static</strong> Object deserializeFromFile(File file) throws IOException {<a name="281" href="#281">281</a> ObjectInputStream ois = <strong>new</strong> ObjectInputStream(<strong>new</strong> BufferedInputStream(<strong>new</strong> FileInputStream(file)));<a name="282" href="#282">282</a> Object object;<a name="283" href="#283">283</a> <strong>try</strong> {<a name="284" href="#284">284</a> object = ois.readObject();<a name="285" href="#285">285</a> } <strong>catch</strong> (ClassNotFoundException e) {<a name="286" href="#286">286</a> <em class="comment">// TODO Auto-generated catch block</em><a name="287" href="#287">287</a> <strong>throw</strong> <strong>new</strong> RuntimeException(e);<a name="288" href="#288">288</a> }<a name="289" href="#289">289</a> ois.close();<a name="290" href="#290">290</a> <strong>return</strong> object;<a name="291" href="#291">291</a> }<a name="292" href="#292">292</a> <a name="293" href="#293">293</a> <em>/**<em>*</em></em><a name="294" href="#294">294</a> <em> * Utility method to serialize Object to byte[]. </em><a name="295" href="#295">295</a> <em> * </em><a name="296" href="#296">296</a> <em> * @param object Object to be serialized</em><a name="297" href="#297">297</a> <em> * @return byte[] serialized form</em><a name="298" href="#298">298</a> <em> */</em><a name="299" href="#299">299</a> <strong>public</strong> <strong>static</strong> byte[] serializeToByteArray(Object object) {<a name="300" href="#300">300</a> ByteArrayOutputStream baos = <strong>new</strong> ByteArrayOutputStream(); <a name="301" href="#301">301</a> <strong>try</strong> {<a name="302" href="#302">302</a> ObjectOutputStream oos = <strong>new</strong> ObjectOutputStream(baos);<a name="303" href="#303">303</a> oos.writeObject(object);<a name="304" href="#304">304</a> oos.close();<a name="305" href="#305">305</a> } <strong>catch</strong> (IOException e) {<a name="306" href="#306">306</a> <em class="comment">// shouldn't be possible</em><a name="307" href="#307">307</a> <strong>throw</strong> <strong>new</strong> RuntimeException(e);<a name="308" href="#308">308</a> }<a name="309" href="#309">309</a> <strong>return</strong> baos.toByteArray();<a name="310" href="#310">310</a> }<a name="311" href="#311">311</a> <a name="312" href="#312">312</a> <em>/**<em>*</em></em><a name="313" href="#313">313</a> <em> * Utility method to deserialize Object from byte[]. </em><a name="314" href="#314">314</a> <em> * </em><a name="315" href="#315">315</a> <em> * @param in byte[] source</em><a name="316" href="#316">316</a> <em> * @return Object deserialized</em><a name="317" href="#317">317</a> <em> */</em><a name="318" href="#318">318</a> <strong>public</strong> <strong>static</strong> Object deserializeFromByteArray(byte[] in) {<a name="319" href="#319">319</a> Object object;<a name="320" href="#320">320</a> <strong>try</strong> {<a name="321" href="#321">321</a> ObjectInputStream ois = <strong>new</strong> ObjectInputStream(<strong>new</strong> ByteArrayInputStream(in));<a name="322" href="#322">322</a> <strong>try</strong> {<a name="323" href="#323">323</a> object = ois.readObject();<a name="324" href="#324">324</a> } <strong>catch</strong> (ClassNotFoundException e) {<a name="325" href="#325">325</a> <em class="comment">// TODO Auto-generated catch block</em><a name="326" href="#326">326</a> <strong>throw</strong> <strong>new</strong> RuntimeException(e);<a name="327" href="#327">327</a> }<a name="328" href="#328">328</a> ois.close();<a name="329" href="#329">329</a> } <strong>catch</strong> (IOException e) {<a name="330" href="#330">330</a> <em class="comment">// shouldn't be possible</em><a name="331" href="#331">331</a> <strong>throw</strong> <strong>new</strong> RuntimeException(e);<a name="332" href="#332">332</a> }<a name="333" href="#333">333</a> <strong>return</strong> object;<a name="334" href="#334">334</a> }<a name="335" href="#335">335</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 + -
显示快捷键?