📄 ioutils.html
字号:
<a name="99" href="#99">99</a> <em>/**<em>*</em></em><a name="100" href="#100">100</a> <em> * Ensure writeable directory.</em><a name="101" href="#101">101</a> <em> *</em><a name="102" href="#102">102</a> <em> * If doesn't exist, we attempt creation.</em><a name="103" href="#103">103</a> <em> *</em><a name="104" href="#104">104</a> <em> * @param dir Directory to test for exitence and is writeable.</em><a name="105" href="#105">105</a> <em> *</em><a name="106" href="#106">106</a> <em> * @return The passed <code>dir</code>.</em><a name="107" href="#107">107</a> <em> *</em><a name="108" href="#108">108</a> <em> * @exception IOException If passed directory does not exist and is not</em><a name="109" href="#109">109</a> <em> * createable, or directory is not writeable or is not a directory.</em><a name="110" href="#110">110</a> <em> */</em><a name="111" href="#111">111</a> <strong>public</strong> <strong>static</strong> File ensureWriteableDirectory(File dir)<a name="112" href="#112">112</a> throws IOException {<a name="113" href="#113">113</a> <strong>if</strong> (!dir.exists()) {<a name="114" href="#114">114</a> dir.mkdirs();<a name="115" href="#115">115</a> } <strong>else</strong> {<a name="116" href="#116">116</a> <strong>if</strong> (!dir.canWrite()) {<a name="117" href="#117">117</a> <strong>throw</strong> <strong>new</strong> IOException(<span class="string">"Dir "</span> + dir.getAbsolutePath() +<a name="118" href="#118">118</a> <span class="string">" not writeable."</span>);<a name="119" href="#119">119</a> } <strong>else</strong> <strong>if</strong> (!dir.isDirectory()) {<a name="120" href="#120">120</a> <strong>throw</strong> <strong>new</strong> IOException(<span class="string">"Dir "</span> + dir.getAbsolutePath() +<a name="121" href="#121">121</a> <span class="string">" is not a directory."</span>);<a name="122" href="#122">122</a> }<a name="123" href="#123">123</a> }<a name="124" href="#124">124</a> <a name="125" href="#125">125</a> <strong>return</strong> dir;<a name="126" href="#126">126</a> }<a name="127" href="#127">127</a> <a name="128" href="#128">128</a> <em>/**<em>*</em></em><a name="129" href="#129">129</a> <em> * Read the entire stream to EOF, returning what's read as a String.</em><a name="130" href="#130">130</a> <em> * </em><a name="131" href="#131">131</a> <em> * @param inputStream</em><a name="132" href="#132">132</a> <em> * @return String of the whole inputStream's contents</em><a name="133" href="#133">133</a> <em> * @throws IOException</em><a name="134" href="#134">134</a> <em> */</em><a name="135" href="#135">135</a> <strong>public</strong> <strong>static</strong> String readFullyAsString(InputStream inputStream)<a name="136" href="#136">136</a> throws IOException {<a name="137" href="#137">137</a> StringBuffer sb = <strong>new</strong> StringBuffer();<a name="138" href="#138">138</a> <strong>int</strong> c;<a name="139" href="#139">139</a> <strong>while</strong>((c = inputStream.read()) > -1) {<a name="140" href="#140">140</a> sb.append((<strong>char</strong>)c);<a name="141" href="#141">141</a> }<a name="142" href="#142">142</a> <strong>return</strong> sb.toString();<a name="143" href="#143">143</a> }<a name="144" href="#144">144</a> <a name="145" href="#145">145</a> <em>/**<em>*</em></em><a name="146" href="#146">146</a> <em> * Read the entire stream to EOF into the passed file.</em><a name="147" href="#147">147</a> <em> * @param is</em><a name="148" href="#148">148</a> <em> * @param toFile File to read into .</em><a name="149" href="#149">149</a> <em> * @throws IOException </em><a name="150" href="#150">150</a> <em> * @throws IOException</em><a name="151" href="#151">151</a> <em> */</em><a name="152" href="#152">152</a> <strong>public</strong> <strong>static</strong> <strong>void</strong> readFullyToFile(InputStream is,<a name="153" href="#153">153</a> File toFile) throws IOException {<a name="154" href="#154">154</a> readFullyToFile(is, toFile, <strong>new</strong> byte[4096]);<a name="155" href="#155">155</a> }<a name="156" href="#156">156</a> <a name="157" href="#157">157</a> <em>/**<em>*</em></em><a name="158" href="#158">158</a> <em> * Read the entire stream to EOF into the passed file.</em><a name="159" href="#159">159</a> <em> * Closes <code>is</code> when done or if an exception.</em><a name="160" href="#160">160</a> <em> * @param is Stream to read.</em><a name="161" href="#161">161</a> <em> * @param toFile File to read into .</em><a name="162" href="#162">162</a> <em> * @param buffer Buffer to use reading.</em><a name="163" href="#163">163</a> <em> * @return Count of bytes read.</em><a name="164" href="#164">164</a> <em> * @throws IOException</em><a name="165" href="#165">165</a> <em> */</em><a name="166" href="#166">166</a> <strong>public</strong> <strong>static</strong> <strong>long</strong> readFullyToFile(<strong>final</strong> InputStream is, <strong>final</strong> File toFile,<a name="167" href="#167">167</a> <strong>final</strong> byte [] buffer)<a name="168" href="#168">168</a> throws IOException {<a name="169" href="#169">169</a> <strong>long</strong> totalcount = -1;<a name="170" href="#170">170</a> OutputStream os =<a name="171" href="#171">171</a> <strong>new</strong> FastBufferedOutputStream(<strong>new</strong> FileOutputStream(toFile));<a name="172" href="#172">172</a> InputStream localIs = (is instanceof BufferedInputStream)?<a name="173" href="#173">173</a> is: <strong>new</strong> BufferedInputStream(is);<a name="174" href="#174">174</a> <strong>try</strong> {<a name="175" href="#175">175</a> <strong>for</strong> (<strong>int</strong> count = -1;<a name="176" href="#176">176</a> (count = localIs.read(buffer, 0, buffer.length)) != -1;<a name="177" href="#177">177</a> totalcount += count) {<a name="178" href="#178">178</a> os.write(buffer, 0, count); <a name="179" href="#179">179</a> }<a name="180" href="#180">180</a> } <strong>finally</strong> {<a name="181" href="#181">181</a> os.close();<a name="182" href="#182">182</a> <strong>if</strong> (localIs != <strong>null</strong>) {<a name="183" href="#183">183</a> localIs.close();<a name="184" href="#184">184</a> }<a name="185" href="#185">185</a> }<a name="186" href="#186">186</a> <strong>return</strong> totalcount;<a name="187" href="#187">187</a> }<a name="188" href="#188">188</a> <a name="189" href="#189">189</a> <em>/**<em>*</em></em><a name="190" href="#190">190</a> <em> * Wrap generic Throwable as a checked IOException</em><a name="191" href="#191">191</a> <em> * @param e wrapped exception</em><a name="192" href="#192">192</a> <em> * @return IOException</em><a name="193" href="#193">193</a> <em> */</em><a name="194" href="#194">194</a> <strong>public</strong> <strong>static</strong> IOException wrapAsIOException(Throwable e) {<a name="195" href="#195">195</a> IOException ioe = <strong>new</strong> IOException(e.toString());<a name="196" href="#196">196</a> ioe.initCause(e);<a name="197" href="#197">197</a> <strong>return</strong> ioe;<a name="198" href="#198">198</a> }<a name="199" href="#199">199</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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -