Java Byte[] 到 InputStream 或 OutputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2091454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:26:09  来源:igfitidea点击:

Byte[] to InputStream or OutputStream

javabytearrayinputstreamoutputstream

提问by GuruKulki

I have a blob column in my database table, for which I have to use byte[]in my Java program as a mapping and to use this data I have to convert it to InputStreamor OutputStream. But I don't know what happens internally when I do so. Can anyone briefly explain me what's happening when I do this conversion?

我的数据库表中有一个 blob 列,我必须byte[]在我的 Java 程序中将其用作映射并使用此数据,我必须将其转换为InputStreamOutputStream。但我不知道这样做时内部会发生什么。任何人都可以简要解释一下我进行这种转换时发生了什么?

采纳答案by Stephen C

You create and use byte array I/O streams as follows:

您可以按如下方式创建和使用字节数组 I/O 流:

byte[] source = ...;
ByteArrayInputStream bis = new ByteArrayInputStream(source);
// read bytes from bis ...

ByteArrayOutputStream bos = new ByteArrayOutputStream();
// write bytes to bos ...
byte[] sink = bos.toByteArray();

Assuming that you are using a JDBC driver that implements the standard JDBC Blob interface(not all do), you can alsoconnect a InputStreamor OutputStreamto a blob using the getBinaryStreamand setBinaryStreammethods1, and you can also get and set the bytes directly.

假设您使用的 JDBC 驱动程序实现了标准JDBC Blob 接口(并非所有接口都如此),您可以使用和方法1将 aInputStreamOutputStreamblob连接到 blob ,您还可以直接获取和设置字节。getBinaryStreamsetBinaryStream

(In general, you should take appropriate steps to handle any exceptions, and close streams. However, closing bisand bosin the example above is unnecessary, since they aren't associated with any external resources; e.g. file descriptors, sockets, database connections.)

(通常,您应该采取适当的步骤来处理任何异常并关闭流。但是,在上面的示例中关闭bisbos是不必要的,因为它们不与任何外部资源相关联;例如文件描述符、套接字、数据库连接。)

1 - The setBinaryStreammethod is really a getter. Go figure.

1 - 该setBinaryStream方法确实是一个吸气剂。去搞清楚。

回答by Thilo

There is no conversion between InputStream/OutputStream and the bytes they are working with. They are made for binary data, and just read (or write) the bytes one by one as is.

InputStream/OutputStream 和它们使用的字节之间没有转换。它们是为二进制数据而设计的,只需按原样一一读取(或写入)字节。

A conversion needs to happen when you want to go from byte to char. Then you need to convert using a character set. This happens when you make String or Reader from bytes, which are made for character data.

当您想从字节转换为字符时,需要进行转换。然后您需要使用字符集进行转换。当您从字节创建 String 或 Reader 时会发生这种情况,字节是为字符数据创建的。

回答by pstanton

I'm assuming you mean that 'use' means read, but what i'll explain for the read case can be basically reversed for the write case.

我假设你的意思是“使用”意味着读取,但我将解释的读取案例基本上可以颠倒写入案例。

so you end up with a byte[]. this could represent any kind of data which may need special types of conversions (character, encrypted, etc). let's pretend you want to write this data as is to a file.

所以你最终得到一个字节[]。这可以表示可能需要特殊类型转换(字符、加密等)的任何类型的数据。让我们假设您想将这些数据按原样写入文件。

firstly you could create a ByteArrayInputStreamwhich is basically a mechanism to supply the bytes to something in sequence.

首先,您可以创建一个ByteArrayInputStream,它基本上是一种按顺序向某事物提供字节的机制。

then you could create a FileOutputStreamfor the file you want to create. there are many types of InputStreams and OutputStreams for different data sources and destinations.

那么您可以为要创建的文件创建一个FileOutputStream。对于不同的数据源和目的地,有多种类型的 InputStreams 和 OutputStreams。

lastly you would write the InputStream to the OutputStream. in this case, the array of bytes would be sent in sequence to the FileOutputStream for writing. For this i recommend using IOUtils

最后,您会将 InputStream 写入 OutputStream。在这种情况下,字节数组将按顺序发送到 FileOutputStream 进行写入。为此,我建议使用IOUtils

byte[] bytes = ...;//
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
FileOutputStream out = new FileOutputStream(new File(...));
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);

and in reverse

反过来

FileInputStream in = new FileInputStream(new File(...));
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
byte[] bytes = out.toByteArray();

if you use the above code snippets you'll need to handle exceptions and i recommend you do the 'closes' in a finally block.

如果您使用上面的代码片段,您将需要处理异常,我建议您在 finally 块中执行“关闭”。

回答by Sym-Sym

I do realize that my answer is way late for this question but I think the community would like a newer approach to this issue.

我确实意识到我对这个问题的回答太晚了,但我认为社区希望对这个问题采取更新的方法

回答by Stas Wishnevetsky

byte[] data = dbEntity.getBlobData();
response.getOutputStream().write();

I think this is better since you already have an existing OutputStream in the response object. no need to create a new OutputStream.

我认为这更好,因为您已经在响应对象中有一个现有的 OutputStream。无需创建新的 OutputStream。

回答by 12cram

we can convert byte[] array into input stream by using ByteArrayInputStream

我们可以使用 ByteArrayInputStream 将 byte[] 数组转换为输入流

String str = "Welcome to awesome Java World";
    byte[] content = str.getBytes();
    int size = content.length;
    InputStream is = null;
    byte[] b = new byte[size];
    is = new ByteArrayInputStream(content);

For full example please check here http://www.onlinecodegeek.com/2015/09/how-to-convert-byte-into-inputstream.html

有关完整示例,请在此处查看http://www.onlinecodegeek.com/2015/09/how-to-convert-byte-into-inputstream.html

回答by DALDEI

output = new ByteArrayOutputStream();
...
input = new ByteArrayInputStream( output.toByteArray() )