在Java中将InputStream转换为字节数组

时间:2009-08-12 07:27:22

标签: java bytearray inputstream

如何将整个InputStream读入字节数组?

35 个答案:

答案 0 :(得分:1044)

您可以使用Apache Commons IO来处理此类和类似的任务。

IOUtils类型有静态方法来阅读InputStream并返回byte[]

InputStream is;
byte[] bytes = IOUtils.toByteArray(is);

在内部创建ByteArrayOutputStream并将字节复制到输出,然后调用toByteArray()。它通过复制4KiB块中的字节来处理大文件。

答案 1 :(得分:422)

您需要阅读InputStream中的每个字节并将其写入ByteArrayOutputStream。然后,您可以通过调用toByteArray()来检索基础字节数组; e.g。

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();

答案 2 :(得分:248)

最后,经过二十年,有一个简单的解决方案,无需第三方库,感谢Java 9

InputStream is;
…
byte[] array = is.readAllBytes();

另请注意便利方法readNBytes(byte[] b, int off, int len)transferTo(OutputStream)解决重复需求。

答案 3 :(得分:119)

使用vanilla Java的DataInputStream及其readFully方法(至少从Java 1.4开始存在):

...
byte[] bytes = new byte[(int) file.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.readFully(bytes);
...

这种方法还有其他一些风格,但我一直都在使用这个用例。

答案 4 :(得分:113)

如果你碰巧使用google guava,它就会像以下一样简单:

byte[] bytes = ByteStreams.toByteArray(inputStream);

答案 5 :(得分:37)

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();
}

答案 6 :(得分:35)

与往常一样,Spring framework(自3.2.2以来的spring-core)也有适合你的东西:StreamUtils.copyToByteArray()

答案 7 :(得分:20)

您真的需要图片byte[]吗?您对byte[]的期望是什么 - 图像文件的完整内容,以图像文件所处的格式编码,还是RGB像素值?

此处的其他答案显示如何将文件读入byte[]。您的byte[]将包含文件的确切内容,您需要对其进行解码以对图像数据执行任何操作。

Java用于读取(和写入)图像的标准API是ImageIO API,您可以在包javax.imageio中找到它。您只需一行代码即可从文件中读取图像:

BufferedImage image = ImageIO.read(new File("image.jpg"));

这会为您提供BufferedImage,而不是byte[]。要获取图片数据,您可以在getRaster()上致电BufferedImage。这将为您提供一个Raster对象,该对象具有访问像素数据的方法(它有多种getPixel() / getPixels()方法。)

查找javax.imageio.ImageIOjava.awt.image.BufferedImagejava.awt.image.Raster等的API文档。

默认情况下,ImageIO支持多种图像格式:JPEG,PNG,BMP,WBMP和GIF。可以添加对更多格式的支持(您需要一个实现ImageIO服务提供程序接口的插件)。

另请参阅以下教程:Working with Images

答案 8 :(得分:14)

如果您不想使用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;
}

答案 9 :(得分:13)

如果某人仍在寻找没有依赖关系的解决方案,如果您有文件

  

1)DataInputStream

 byte[] data = new byte[(int) file.length()];
 DataInputStream dis = new DataInputStream(new FileInputStream(file));
 dis.readFully(data);
 dis.close();
  

2)ByteArrayOutputStream

 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);
 }
  

3)RandomAccessFile

 RandomAccessFile raf = new RandomAccessFile(file, "r");
 byte[] data = new byte[(int) raf.length()];
 raf.readFully(data);

答案 10 :(得分:9)

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();

答案 11 :(得分:8)

@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;
}

答案 12 :(得分:8)

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();

答案 13 :(得分:6)

安全解决方案(具有正确的close流功能):

  • Java 9+版本:

    final byte[] bytes;
    try (inputStream) {
        bytes = 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 exception = null;
    
        try {
            try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
                while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
                    outputStream.write(buf, 0, readLen);
    
                return outputStream.toByteArray();
            }
        } catch (IOException e) {
            exception = e;
            throw e;
        } finally {
            if (exception == null) inputStream.close();
            else try {
                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()
        }
    }
    

    为避免嵌套use,请参见here

答案 14 :(得分:3)

Java 9将为您提供一个很好的方法:

InputStream in = ...;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
in.transferTo( bos );
byte[] bytes = bos.toByteArray();

答案 15 :(得分:2)

我知道为时已晚,但我认为这是更清晰易懂的解决方案......

/**
 * 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();
}

答案 16 :(得分:1)

我尝试编辑@ 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结构

答案 17 :(得分:1)

Java 7及更高版本:

import sun.misc.IOUtils;
...
InputStream in = ...;
byte[] buf = IOUtils.readFully(in, -1, false);

答案 18 :(得分: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')并且可能不合适。

答案 19 :(得分:1)

请参阅InputStream.available()文档:

  

特别重要的是要意识到你不能使用它   调整容器大小的方法,并假设您可以阅读整个容器   无需调整容器大小的流。这样的来电者   应该把他们读到的所有东西写成ByteArrayOutputStream   并将其转换为字节数组。或者,如果您正在阅读   File.length从文件中返回文件的当前长度   (虽然假设文件的长度无法改变可能不正确,   阅读文件本质上很有趣。)

答案 20 :(得分:0)

您可以尝试Cactoos

byte[] array = new BytesOf(stream).bytes();

答案 21 :(得分:0)

在将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对象转换为bytearray

答案 22 :(得分:0)

没有第三方依赖的科特琳一线客:

var arr: ByteArray = stream.bufferedReader().use(BufferedReader::readText).toByteArray()

(我意识到问题是关于Java的,但是现在有很多人正在将它与Kotlin进行混合和匹配。)

答案 23 :(得分:0)

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
    }

答案 24 :(得分:0)

您可以将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}
);

具有专用类型Bytes用于处理数据结构byte[],使我们能够使用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实现来解决它。

答案 25 :(得分:0)

在新版本中,

IOUtils.readAllBytes(inputStream)

答案 26 :(得分:0)

如果出于某种原因离开桌面,请将其包装在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;
}

答案 27 :(得分:0)

这是我的复制粘贴版本:

<script type="text/javascript">    
    var TextLimitController= function ($scope) {
        $scope.message = '';
        $scope.remaining = function (countryPopulation) {
            return 100 - $scope.message.length;
        };
   };
</script>

答案 28 :(得分:0)

我用它。

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();
        }
    }

答案 29 :(得分:0)

如果您使用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);

N.B。上面的最后一行处理在读取流时被截断的文件,如果你需要处理这种可能性,但是如果文件在读取流时更长,则字节中的内容[ ]数组不会被加长以包含新文件内容,数组将被简单地截断为旧的长度 inputStreamLength

答案 30 :(得分:0)

另一种情况是在向服务器发送请求并等待响应后,通过流获取正确的字节数组。

/**
         * 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);

答案 31 :(得分:0)

这是一个优化版本,它试图尽可能避免复制数据字节:

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;
   }
}

答案 32 :(得分:-1)

这对我有用,

if(inputStream != null){
                ByteArrayOutputStream contentStream = readSourceContent(inputStream);
                String stringContent = contentStream.toString();
                byte[] byteArr = encodeString(stringContent);
            }

readSourceContent()

public static ByteArrayOutputStream readSourceContent(InputStream inputStream) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int nextChar;
        try {
            while ((nextChar = inputStream.read()) != -1) {
                outputStream.write(nextChar);
            }
            outputStream.flush();
        } catch (IOException e) {
            throw new IOException("Exception occurred while reading content", e);
        }

        return outputStream;
    }

encodeString()

public static byte[] encodeString(String content) throws UnsupportedEncodingException {
        byte[] bytes;
        try {
            bytes = content.getBytes();

        } catch (UnsupportedEncodingException e) {
            String msg = ENCODING + " is unsupported encoding type";
            log.error(msg,e);
            throw new UnsupportedEncodingException(msg, e);
        }
        return bytes;
    }

答案 33 :(得分:-1)

/*InputStream class_InputStream = null;
I am reading class from DB 
class_InputStream = rs.getBinaryStream(1);
Your Input stream could be from any source
*/
int thisLine;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((thisLine = class_InputStream.read()) != -1) {
    bos.write(thisLine);
}
bos.flush();
byte [] yourBytes = bos.toByteArray();

/*Don't forget in the finally block to close ByteArrayOutputStream & InputStream
 In my case the IS is from resultset so just closing the rs will do it*/

if (bos != null){
    bos.close();
}

答案 34 :(得分:-1)

代码

public static byte[] serializeObj(Object obj) throws IOException {
  ByteArrayOutputStream baOStream = new ByteArrayOutputStream();
  ObjectOutputStream objOStream = new ObjectOutputStream(baOStream);

  objOStream.writeObject(obj); 
  objOStream.flush();
  objOStream.close();
  return baOStream.toByteArray(); 
} 

OR

BufferedImage img = ...
ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
ImageIO.write(img, "jpeg", baos);
baos.flush();
byte[] result = baos.toByteArray();
baos.close();