一、使用ByteArrayOutputStream的方式
在Java語言中,InputStream是讀取數據的基本方式。但是,有時候我們需要將InputStream轉化為Byte數組。而使用ByteArrayOutputStream
是最常見的方法之一。下面是代碼示例:
public byte[] readBytesFromInputStream(InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int n = 0; while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); } return output.toByteArray(); }
首先,我們創建了ByteArrayOutputStream
對象output
,並定義了一個byte
類型的buffer
數組。接下來,我們使用input.read(buffer)
讀取input
中的數據到buffer
中,並通過output.write(buffer, 0, n)
將buffer
中的數據寫入到output
中。最後,調用output.toByteArray()
將output
轉換成byte
數組。該方法的缺點是需要在內存中保持所有數據。
二、使用ByteArrayInputStream的方式
除了使用ByteArrayOutputStream
實現InputStream轉換成byte數組,還可以使用ByteArrayInputStream
。下面是代碼示例:
public byte[] readBytesFromInputStream(InputStream input) { try { return org.apache.commons.io.IOUtils.toByteArray(input); } catch (IOException e) { e.printStackTrace(); } return null; }
在這個方法中,我們使用了Apache Commons IO庫,調用org.apache.commons.io.IOUtils
的toByteArray
方法將input
轉換為byte
數組。這種方法不需要擔心內存限制,因為只有需要數據時才會從input
對象中讀取。
三、使用BufferedInputStream的方式
Java標準庫中,還提供了BufferedInputStream
,可以實現輸入流的緩存。下面是代碼示例:
public byte[] readBytesFromInputStream(InputStream input) { try { BufferedInputStream bufferedInput = new BufferedInputStream(input); byte[] result = new byte[bufferedInput.available()]; bufferedInput.read(result, 0, result.length); return result; } catch (IOException e) { e.printStackTrace(); } return null; }
在這個方法中,我們首先創建了BufferedInputStream
對象bufferedInput
,然後調用available()
獲取輸入流的位元組數,創建相應大小的byte
數組result
。最後,調用bufferedInput.read(result, 0, result.length)
從輸入流中讀取數據。
四、InputStream轉ByteArray的性能比較
下面的代碼演示了三個方法的性能比較:
public static void main(String[] args) { int count = 100; long start1 = System.currentTimeMillis(); for (int i = 0; i < count; i++) { InputStream input = new ByteArrayInputStream("Hello World!".getBytes()); readBytesFromInputStream(input); } long end1 = System.currentTimeMillis(); long start2 = System.currentTimeMillis(); for (int i = 0; i < count; i++) { InputStream input = new ByteArrayInputStream("Hello World!".getBytes()); readBytesFromInputStream2(input); } long end2 = System.currentTimeMillis(); long start3 = System.currentTimeMillis(); for (int i = 0; i < count; i++) { InputStream input = new ByteArrayInputStream("Hello World!".getBytes()); readBytesFromInputStream3(input); } long end3 = System.currentTimeMillis(); System.out.println("ByteArrayOutputStream 方法耗時:" + (end1 - start1) + " ms"); System.out.println("IOUtils.toByteArray 方法耗時:" + (end2 - start2) + " ms"); System.out.println("BufferedInputStream 方法耗時:" + (end3 - start3) + " ms"); } private static byte[] readBytesFromInputStream(InputStream input) { try { ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int n = 0; while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); } return output.toByteArray(); } catch (IOException e) { e.printStackTrace(); } return null; } private static byte[] readBytesFromInputStream2(InputStream input) { try { return org.apache.commons.io.IOUtils.toByteArray(input); } catch (IOException e) { e.printStackTrace(); } return null; } private static byte[] readBytesFromInputStream3(InputStream input) { try { BufferedInputStream bufferedInput = new BufferedInputStream(input); byte[] result = new byte[bufferedInput.available()]; bufferedInput.read(result, 0, result.length); return result; } catch (IOException e) { e.printStackTrace(); } return null; }
可以看到,使用ByteArrayOutputStream
的方式是最慢的,使用BufferedInputStream
的方式較快,而使用IOUtils.toByteArray
方法的方式最快。在實際情況中,需要根據自己的需求選擇合適的方法。
五、總結
本文介紹了三種將InputStream
轉化為Byte
數組的方式,分別是使用ByteArrayOutputStream
、ByteArrayInputStream
以及BufferedInputStream
。此外,我們還對三種方法進行了性能比較。在實際使用中需要根據自己的需求選擇最適合的方法。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/186283.html