如何将整个 InputStream
读入字节数组?
您可以使用 Apache Commons IO 来处理此任务和类似任务。
IOUtils
类型具有读取 InputStream
并返回 byte[]
的静态方法。
InputStream is;
byte[] bytes = IOUtils.toByteArray(is);
这在内部会创建一个 ByteArrayOutputStream
并将字节复制到输出,然后调用 toByteArray()
。它通过复制 4KiB 块中的字节来处理大文件。
您需要从 InputStream
读取每个字节并将其写入 ByteArrayOutputStream
。
然后,您可以通过调用 toByteArray()
来检索底层字节数组:
InputStream is = ...
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();
最后,经过 20 年,有了一个不需要第三方库的简单解决方案,这要感谢 Java 9:
InputStream is;
…
byte[] array = is.readAllBytes();
另请注意解决重复需求的便捷方法 readNBytes(byte[] b, int off, int len)
和 transferTo(OutputStream)
。
InputStream
是否覆盖 readAllBytes()
或 readNBytes
?
使用 vanilla Java 的 DataInputStream
及其 readFully
方法(至少从 Java 1.4 开始存在):
...
byte[] bytes = new byte[(int) file.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.readFully(bytes);
...
这种方法还有其他一些风格,但我一直在这个用例中使用它。
DataInputStream
主要用于从流中读取主要类型(Longs、Shorts、Chars...),因此我们可以将这种用法视为对类的滥用。
InputStream.read
好。
安全解决方案(正确关闭流):
Java 9+:最终字节 [] 字节;尝试 (inputStream) { 字节 = inputStream.readAllBytes(); }
Java 8: public static byte[] readAllBytes(InputStream inputStream) throws IOException { final int bufLen = 4 * 0x400; // 4KB byte[] buf = new byte[bufLen]; int readLen; IOException异常=空;尝试 { 尝试 (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { while ((readLen = inputStream.read(buf, 0, bufLen)) != -1) outputStream.write(buf, 0, readLen);返回 outputStream.toByteArray(); } } catch (IOException e) { 异常 = e;扔 e; } 最后 { if (exception == null) inputStream.close();否则尝试 { inputStream.close(); } catch (IOException e) { exception.addSuppressed(e); } } }
Kotlin(当 Java 9+ 无法访问时):@Throws(IOException::class) fun InputStream.readAllBytes(): ByteArray { val bufLen = 4 * 0x400 // 4KB val buf = ByteArray(bufLen) var readLen: Int = 0 ByteArrayOutputStream().use { o -> this.use { i -> while (i.read(buf, 0, bufLen).also { readLen = it } != -1) o.write(buf, 0, readLen ) } return o.toByteArray() } } 为避免嵌套使用,请参见此处。
Scala(当 Java 9+ 无法访问时)(由 @Joan.Thx 提供):def readAllBytes(inputStream: InputStream): Array[Byte] = Stream.continually(inputStream.read).takeWhile(_ != -1)。地图(_.toByte).toArray
与往常一样,Spring framework(自 3.2.2 以来的 spring-core)也为您提供了一些东西:StreamUtils.copyToByteArray()
public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buffer = new byte[0xFFFF];
for (int len = is.read(buffer); len != -1; len = is.read(buffer)) {
os.write(buffer, 0, len);
}
return os.toByteArray();
}
万一有人仍在寻找没有依赖关系的解决方案,并且如果您有文件。
数据输入流
byte[] data = new byte[(int) file.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.readFully(data);
dis.close();
字节数组输出流
InputStream is = new FileInputStream(file);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[(int) file.length()];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
随机存取文件
RandomAccessFile raf = new RandomAccessFile(file, "r");
byte[] data = new byte[(int) raf.length()];
raf.readFully(data);
您真的需要将图像作为 byte[]
吗?您对 byte[]
的期望究竟是什么 - 图像文件的完整内容,以图像文件的任何格式编码,还是 RGB 像素值?
此处的其他答案向您展示了如何将文件读入 byte[]
。您的 byte[]
将包含文件的确切内容,您需要对其进行解码才能对图像数据执行任何操作。
Java 用于读取(和写入)图像的标准 API 是 ImageIO API,您可以在包 javax.imageio
中找到它。您只需一行代码即可从文件中读取图像:
BufferedImage image = ImageIO.read(new File("image.jpg"));
这将为您提供 BufferedImage
,而不是 byte[]
。要获取图像数据,您可以在 BufferedImage
上调用 getRaster()
。这将为您提供一个 Raster
对象,该对象具有访问像素数据的方法(它具有多个 getPixel()
/ getPixels()
方法)。
查找 javax.imageio.ImageIO
、java.awt.image.BufferedImage
、java.awt.image.Raster
等的 API 文档。
ImageIO 默认支持多种图像格式:JPEG、PNG、BMP、WBMP 和 GIF。可以添加对更多格式的支持(您需要一个实现 ImageIO 服务提供者接口的插件)。
另请参阅以下教程:Working with Images
如果您不想使用 Apache commons-io 库,则此代码段取自 sun.misc.IOUtils 类。它几乎是使用 ByteBuffers 的常见实现的两倍:
public static byte[] readFully(InputStream is, int length, boolean readAll)
throws IOException {
byte[] output = {};
if (length == -1) length = Integer.MAX_VALUE;
int pos = 0;
while (pos < length) {
int bytesToRead;
if (pos >= output.length) { // Only expand when there's no room
bytesToRead = Math.min(length - pos, output.length + 1024);
if (output.length < pos + bytesToRead) {
output = Arrays.copyOf(output, pos + bytesToRead);
}
} else {
bytesToRead = output.length - pos;
}
int cc = is.read(output, pos, bytesToRead);
if (cc < 0) {
if (readAll && length != Integer.MAX_VALUE) {
throw new EOFException("Detect premature EOF");
} else {
if (output.length != pos) {
output = Arrays.copyOf(output, pos);
}
break;
}
}
pos += cc;
}
return output;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (true) {
int r = in.read(buffer);
if (r == -1) break;
out.write(buffer, 0, r);
}
byte[] ret = out.toByteArray();
@Adamski:您可以完全避免缓冲。
从 http://www.exampledepot.com/egs/java.io/File2ByteArray.html 复制的代码(是的,它非常冗长,但需要的内存大小是其他解决方案的一半。)
// Returns the contents of the file in a byte array.
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
// You cannot create an array using a long type.
// It needs to be an int type.
// Before converting to an int type, check
// to ensure that file is not larger than Integer.MAX_VALUE.
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
offset < bytes.length
或 InputStream
将不会在引发该异常时关闭,则应添加 is.close()
。
Input Stream is ...
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int next = in.read();
while (next > -1) {
bos.write(next);
next = in.read();
}
bos.flush();
byte[] result = bos.toByteArray();
bos.close();
InputStream
包装在 BufferedInputStream
中会减少操作系统调用并显着减轻性能缺陷,但该代码仍会执行从一个缓冲区到另一个缓冲区的不必要的手动复制工作。
Java 9 最终会给你一个不错的方法:
InputStream in = ...;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
in.transferTo( bos );
byte[] bytes = bos.toByteArray();
InputStram.readAllBytes()
有什么区别?
ByteArrayOutputStream
中调整很多数组的大小,然后是数据的完整副本。
在将 S3 对象转换为 ByteArray 时,我们看到一些 AWS 事务出现延迟。
注意:S3 对象是 PDF 文档(最大大小为 3 mb)。
我们使用选项 #1 (org.apache.commons.io.IOUtils) 将 S3 对象转换为 ByteArray。我们注意到 S3 提供了内置 IOUtils 方法来将 S3 对象转换为 ByteArray,我们要求您确认将 S3 对象转换为 ByteArray 的最佳方法是什么,以避免延迟。
选项1:
import org.apache.commons.io.IOUtils;
is = s3object.getObjectContent();
content =IOUtils.toByteArray(is);
选项#2:
import com.amazonaws.util.IOUtils;
is = s3object.getObjectContent();
content =IOUtils.toByteArray(is);
如果我们有任何其他更好的方法将 s3 对象转换为字节数组,请告诉我
我知道为时已晚,但我认为这是更清晰的解决方案,更具可读性......
/**
* method converts {@link InputStream} Object into byte[] array.
*
* @param stream the {@link InputStream} Object.
* @return the byte[] array representation of received {@link InputStream} Object.
* @throws IOException if an error occurs.
*/
public static byte[] streamToByteArray(InputStream stream) throws IOException {
byte[] buffer = new byte[1024];
ByteArrayOutputStream os = new ByteArrayOutputStream();
int line = 0;
// read bytes from stream, and store them in buffer
while ((line = stream.read(buffer)) != -1) {
// Writes bytes from byte array (buffer) into output stream.
os.write(buffer, 0, line);
}
stream.close();
os.flush();
os.close();
return os.toByteArray();
}
我试图通过编写垃圾数据的修复程序来编辑@numan 的答案,但编辑被拒绝。虽然这段简短的代码并不出色,但我看不到任何其他更好的答案。这对我来说最有意义:
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024]; // you can configure the buffer size
int length;
while ((length = in.read(buffer)) != -1) out.write(buffer, 0, length); //copy streams
in.close(); // call this in a finally block
byte[] result = out.toByteArray();
btw ByteArrayOutputStream 不需要关闭。为便于阅读,省略了 try/finally 结构
请参阅 InputStream.available()
文档:
尤其重要的是要意识到您不能使用此方法来调整容器的大小,并假设您可以读取整个流而不需要调整容器的大小。这样的调用者可能应该将他们读取的所有内容写入 ByteArrayOutputStream 并将其转换为字节数组。或者,如果您正在从文件中读取, File.length 返回文件的当前长度(尽管假设文件的长度不能改变可能是不正确的,读取文件本质上是活泼的)。
如果由于某种原因不在表格中,则将其包装在 DataInputStream 中,只需使用 read 对其进行锤击,直到它为您提供 -1 或您要求的整个块。
public int readFully(InputStream in, byte[] data) throws IOException {
int offset = 0;
int bytesRead;
boolean read = false;
while ((bytesRead = in.read(data, offset, data.length - offset)) != -1) {
read = true;
offset += bytesRead;
if (offset >= data.length) {
break;
}
}
return (read) ? offset : -1;
}
Java 8 方式(感谢 BufferedReader 和 Adam Bien)
private static byte[] readFully(InputStream input) throws IOException {
try (BufferedReader buffer = new BufferedReader(new InputStreamReader(input))) {
return buffer.lines().collect(Collectors.joining("\n")).getBytes(<charset_can_be_specified>);
}
}
请注意,此解决方案会擦除回车符 ('\r') 并且可能不合适。
String
。 OP 要求 byte[]
。
\r
可能是个问题。此方法将字节转换为字符并再次转换回来(使用 InputStreamReader 的默认字符集)。在默认字符编码中无效的任何字节(例如,Linux 上的 UTF-8 为 -1)都将被损坏,甚至可能更改字节数。
另一种情况是在向服务器发送请求并等待响应后,通过流获取正确的字节数组。
/**
* Begin setup TCP connection to PC app
* to open integrate connection between mobile app and pc app (or mobile app)
*/
mSocket = new Socket(IP, port);
// mSocket.setSoTimeout(30000);
DataOutputStream mDos = new DataOutputStream(mSocket.getOutputStream());
String str = "MobileRequest#" + params[0] + "#<EOF>";
mDos.write(str.getBytes());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
/* Since data are accepted as byte, all of them will be collected in the
following byte array which initialised with accepted data length. */
DataInputStream mDis = new DataInputStream(mSocket.getInputStream());
byte[] data = new byte[mDis.available()];
// Collecting data into byte array
for (int i = 0; i < data.length; i++)
data[i] = mDis.readByte();
// Converting collected data in byte array into String.
String RESPONSE = new String(data);
如果您使用 ByteArrayOutputStream,您正在做一个额外的副本。如果你在开始读之前就知道了流的长度(比如InputStream实际上是一个FileInputStream,你可以在文件上调用file.length(),或者InputStream是一个zipfile入口InputStream,你可以调用zipEntry. length()),那么直接写入 byte[] 数组要好得多——它使用了一半的内存,并且节省了时间。
// Read the file contents into a byte[] array
byte[] buf = new byte[inputStreamLength];
int bytesRead = Math.max(0, inputStream.read(buf));
// If needed: for safety, truncate the array if the file may somehow get
// truncated during the read operation
byte[] contents = bytesRead == inputStreamLength ? buf
: Arrays.copyOf(buf, bytesRead);
注意,上面的最后一行处理在读取流时文件被截断,如果您需要处理这种可能性,但是如果在读取流时文件变长,则 byte[] 数组中的内容不会被延长为了包含新的文件内容,数组将被简单地截断为旧长度 inputStreamLength。
我用这个。
public static byte[] toByteArray(InputStream is) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
byte[] b = new byte[4096];
int n = 0;
while ((n = is.read(b)) != -1) {
output.write(b, 0, n);
}
return output.toByteArray();
} finally {
output.close();
}
}
这是我的复制粘贴版本:
@SuppressWarnings("empty-statement")
public static byte[] inputStreamToByte(InputStream is) throws IOException {
if (is == null) {
return null;
}
// Define a size if you have an idea of it.
ByteArrayOutputStream r = new ByteArrayOutputStream(2048);
byte[] read = new byte[512]; // Your buffer size.
for (int i; -1 != (i = is.read(read)); r.write(read, 0, i));
is.close();
return r.toByteArray();
}
Java 7 及更高版本:
import sun.misc.IOUtils;
...
InputStream in = ...;
byte[] buf = IOUtils.readFully(in, -1, false);
sun.misc.IOUtils
不是“Java 7”。它是一个专有的、特定于实现的类,可能不存在于其他 JRE 实现中,并且可以在下一个版本中消失而没有任何警告。
这是一个优化版本,它尽量避免复制数据字节:
private static byte[] loadStream (InputStream stream) throws IOException {
int available = stream.available();
int expectedSize = available > 0 ? available : -1;
return loadStream(stream, expectedSize);
}
private static byte[] loadStream (InputStream stream, int expectedSize) throws IOException {
int basicBufferSize = 0x4000;
int initialBufferSize = (expectedSize >= 0) ? expectedSize : basicBufferSize;
byte[] buf = new byte[initialBufferSize];
int pos = 0;
while (true) {
if (pos == buf.length) {
int readAhead = -1;
if (pos == expectedSize) {
readAhead = stream.read(); // test whether EOF is at expectedSize
if (readAhead == -1) {
return buf;
}
}
int newBufferSize = Math.max(2 * buf.length, basicBufferSize);
buf = Arrays.copyOf(buf, newBufferSize);
if (readAhead != -1) {
buf[pos++] = (byte)readAhead;
}
}
int len = stream.read(buf, pos, buf.length - pos);
if (len < 0) {
return Arrays.copyOf(buf, pos);
}
pos += len;
}
}
Kotlin 中的解决方案(当然也可以在 Java 中使用),其中包括您是否知道大小的两种情况:
fun InputStream.readBytesWithSize(size: Long): ByteArray? {
return when {
size < 0L -> this.readBytes()
size == 0L -> ByteArray(0)
size > Int.MAX_VALUE -> null
else -> {
val sizeInt = size.toInt()
val result = ByteArray(sizeInt)
readBytesIntoByteArray(result, sizeInt)
result
}
}
}
fun InputStream.readBytesIntoByteArray(byteArray: ByteArray,bytesToRead:Int=byteArray.size) {
var offset = 0
while (true) {
val read = this.read(byteArray, offset, bytesToRead - offset)
if (read == -1)
break
offset += read
if (offset >= bytesToRead)
break
}
}
如果您知道大小,与其他解决方案相比,它可以节省两倍的内存使用量(在短时间内,但仍然可能有用)。那是因为您必须将整个流读取到最后,然后将其转换为字节数组(类似于 ArrayList 将其转换为数组)。
因此,例如,如果您使用的是 Android,并且需要处理一些 Uri,则可以尝试使用以下方法获取大小:
fun getStreamLengthFromUri(context: Context, uri: Uri): Long {
context.contentResolver.query(uri, arrayOf(MediaStore.MediaColumns.SIZE), null, null, null)?.use {
if (!it.moveToNext())
return@use
val fileSize = it.getLong(it.getColumnIndex(MediaStore.MediaColumns.SIZE))
if (fileSize > 0)
return fileSize
}
//if you wish, you can also get the file-path from the uri here, and then try to get its size, using this: https://stackoverflow.com/a/61835665/878126
FileUtilEx.getFilePathFromUri(context, uri, false)?.use {
val file = it.file
val fileSize = file.length()
if (fileSize > 0)
return fileSize
}
context.contentResolver.openInputStream(uri)?.use { inputStream ->
if (inputStream is FileInputStream)
return inputStream.channel.size()
else {
var bytesCount = 0L
while (true) {
val available = inputStream.available()
if (available == 0)
break
val skip = inputStream.skip(available.toLong())
if (skip < 0)
break
bytesCount += skip
}
if (bytesCount > 0L)
return bytesCount
}
}
return -1L
}
您可以将 cactoos 库与提供可重用的 object-oriented Java 组件一起使用。该库强调 OOP,因此没有静态方法、NULL 等,只有 real objects 及其合约(接口)。像读取InputStream这样简单的操作,可以这样进行
final InputStream input = ...;
final Bytes bytes = new BytesOf(input);
final byte[] array = bytes.asBytes();
Assert.assertArrayEquals(
array,
new byte[]{65, 66, 67}
);
拥有用于处理数据结构 byte[]
的专用类型 Bytes
使我们能够使用 OOP 策略来解决手头的任务。程序“实用”方法将禁止我们做的事情。例如,您需要将从 InputStream
读取的字节编码为 Base64。在这种情况下,您将使用 Decorator pattern 并将 Bytes 对象包装在 Base64 的实现中。 cactoos 已经提供了这样的实现:
final Bytes encoded = new BytesBase64(
new BytesOf(
new InputStreamOf("XYZ")
)
);
Assert.assertEquals(new TextOf(encoded).asString(), "WFla");
您可以使用装饰器模式以相同的方式对它们进行解码
final Bytes decoded = new Base64Bytes(
new BytesBase64(
new BytesOf(
new InputStreamOf("XYZ")
)
)
);
Assert.assertEquals(new TextOf(decoded).asString(), "XYZ");
无论您的任务是什么,您都可以创建自己的 Bytes
实现来解决它。
不定期副业成功案例分享
FastArrayList
或他们的软 &弱参考 Maps 并回来告诉我这个库是如何“经过良好测试”的。这是一堆垃圾InputStream is;
byte[] filedata=ByteStreams.toByteArray(is);