一、ioutils.write
ioutils.write方法用於將指定字節數組的所有字節寫入輸出流中。這個方法有四個參數:輸出流,要寫入的字節數組,開始寫入的下標和要寫入的字節數。必要時,這個方法會等待輸出流變成可用的。
/** * 將指定字節數組的所有字節寫入輸出流中。 注意: 當前方法調用後會調用流的flush()方法,強制將所有緩衝輸出到流中。 * * @param data 要寫入的字節數組。 * @param output 寫入的輸出流。 * @throws IOException 如果向流中寫入字節出錯。 */ public static void write(byte[] data, OutputStream output) throws IOException { if (data != null) { output.write(data); } }
一般來說,這個方法是很安全的,因為 它對開發者來說非常方便——它做了所有必要的流管理操作,開發者完全不必自己寫寫緩衝區之類的代碼。 實際上,這個方法將自動優化緩存策略,儘可能快地推送字節流到輸出流。
二、ioutils.tobytearray內存溢出
ioutils.tobytearray方法用於將指定輸入流的字節內容讀取到字節數組中。 請注意,此方法會將整個輸入流讀入內存,這就意味着 如果輸入流非常大,那麼可能會遇到內存耗盡的問題。
/** * 將指定輸入流的字節內容讀取到字節數組中。 請注意,此方法會將整個輸入流讀入內存,因此可能會造成內存耗盡的風險。 * * @param input 要讀取的輸入流 * @return 讀取到的字節數組。 * @throws IOException 如果讀取輸入流出錯 */ public static byte[] toByteArray(InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); copy(input, output); return output.toByteArray(); }
如果您確定要將大量數據讀入字節數組,那麼可以考慮分段處理數據。要做到這一點,您可以傳遞一個帶有偏移和長度參數的方法調用。這可以使分段操作成為可能。
三、ioutils.close
ioutils.close方法可以安全而簡單地關閉任何可能拋出異常的流對象。使用try-with-resources塊調用ioutils.close方法可以確保在所使用的流上正確地關閉所有資源。必須注意的是,如果列表中有多個流對象被打開,那麼可以使用該方法將它們同時關閉。
/** * 安全地關閉指定的輸入輸出流。這個方法是安全的:如果流對象為空,它什麼也沒做。 * * @param outputStream 輸出流對象 * @param inputStream 輸入流對象 */ public static void close(OutputStream outputStream, InputStream inputStream) { try { if (outputStream != null) { outputStream.close(); } if (inputStream != null) { inputStream.close(); } } catch (Exception e) { // ignore exception. } }
四、ioutils.tostring
ioutils.tostring方法用於將指定輸入流的內容轉換為字符串。 這個方法還可以允許程序員設置字符編碼,以確保所有字節正確地轉換為字符串。 如果未設置字符編碼,則默認使用UTF-8。
/** * 將指定輸入流的內容轉換為字符串。 這個方法還可以允許程序員設置字符編碼,以確保所有字節正確地轉換為字符串。 * * @param input 輸入流 * @param charsetName 字符編碼, 默認為UTF-8 * @return 讀取到的字符串。 * @throws IOException 如果讀取輸入流出錯 */ public static String toString(InputStream input, String charsetName) throws IOException { StringBuilderWriter sw = new StringBuilderWriter(); copy(input, sw, charsetName); return sw.toString(); }
這個方法可以用於對網絡流和文件流進行編碼轉換和字符序列操作。建議在我們的應用程序中使用這個方法時使用try-with-resource結構。
五、ioutils.readfully
ioutils.readfully方法用於從指定的輸入流中讀取完整的字節數組。這個方法保證將讀取到指定字節數量的所有字節,並支持在讀取到末尾時關閉流。
/** * 從輸入流中讀取完整的字節數據。這個方法保證將讀取到指定字節數量的所有字節,並支持在讀取到末尾時關閉流。 * * @param input 輸入流. * @param byteCount 讀取的字節數. * @return 讀取到的字節數組. * @throws IOException 如果源流出現故障或無法讀取到完整字節. */ public static byte[] readFully(InputStream input, int byteCount) throws IOException { byte[] result = new byte[byteCount]; int remaining = byteCount; int offset = 0; int readCount; while (remaining > 0) { readCount = input.read(result, offset, remaining); if (readCount < 0) { break; } remaining -= readCount; offset += readCount; } if (remaining != 0) { throw new EOFException("InputStream ended before reading required bytes"); } return result; }
使用這個方法的時候,建議加上一個合理的字符編碼選擇,以便能夠在UTF-8或其他字符編碼模式中正常讀寫所需的整字節數量。
六、ioutils.tobytearray
ioutils.tobytearray是ioutils.toByteArray的重載版本,支持設置最大長度。這個方法將讀取並返回輸入流中的所有可用數據,並且不會關閉流。
/** * 將指定輸入流的內容完整數據讀入到字節數組中。 注意:數據讀取後,流不會被關閉。 * * @param input 要讀取的輸入流 * @param size 讀取的字節數 * @return 讀取的字節數組。 * @throws IOException 如果讀取輸入流出錯 */ public static byte[] toByteArray(InputStream input, int size) throws IOException { if (size < 0) { throw new IllegalArgumentException("Invalid size: " + size); } byte[] data = new byte[size]; int offset = 0; int readCount; while (offset < size) { readCount = input.read(data, offset, size - offset); if (readCount < 0) { break; } offset += readCount; } if (offset != size) { throw new IOException("Failure creating byte array: expected " + size + " bytes, but read " + offset + " bytes"); } return data; }
這個方法可以用於讀取大對象等情形,但是在讀取到末尾之前始終需要處理批量讀取所覆蓋的字節數。
七、IOUtils.copy
IOUtils.copy方法用於將一個輸入流複製到另一個輸出流中,並允許在複製過程中選擇設置緩衝區的大小。
/** * 從一個輸入流複製到另一個輸出流。緩衝區的大小指定為DEFAULT_BUFFER_SIZE。這個方法保證在複製源流時使用緩衝區。 * * @param input 輸入流. * @param output 輸出流. * @throws IOException 如果複製時出現故障. */ public static void copy(InputStream input, OutputStream output) throws IOException { copy(input, output, DEFAULT_BUFFER_SIZE); } /** * 從一個輸入流複製到另一個輸出流。這個方法可以指定輸出流的緩衝區大小,以確保優化數據傳輸速度。 * * @param input 輸入流. * @param output 輸出流. * @param bufferSize 緩衝區大小. * @throws IOException 如果複製時出現故障. */ public static void copy(InputStream input, OutputStream output, int bufferSize) throws IOException { byte[] buffer = new byte[bufferSize]; int count; while ((count = input.read(buffer)) != -1) { output.write(buffer, 0, count); } }
使用這個方法時,可以根據實際情況選擇合適的緩存大小以及輸出流的類型。
原創文章,作者:NMVR,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/145793.html