简体   繁体   English

Byte []到InputStream或OutputStream

[英]Byte[] to InputStream or OutputStream

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 InputStream or OutputStream . 我的数据库表中有一个blob列,我必须在我的Java程序中使用byte[]作为映射,并使用这些数据将其转换为InputStreamOutputStream 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? 任何人都能简单地向我解释一下当我进行这种转换时会发生什么吗?

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 also connect a InputStream or OutputStream to a blob using the getBinaryStream and setBinaryStream methods 1 , and you can also get and set the bytes directly. 假设您正在使用实现标准JDBC Blob接口的JDBC驱动程序(并非所有操作),您可以使用getBinaryStreamsetBinaryStream方法1InputStreamOutputStream连接到blob,您还可以直接获取和设置字节。

(In general, you should take appropriate steps to handle any exceptions, and close streams. However, closing bis and bos in the example above is unnecessary, since they aren't associated with any external resources; eg file descriptors, sockets, database connections.) (通常,您应该采取适当的步骤来处理任何异常,并关闭流。但是,在上面的示例中关闭bisbos是不必要的,因为它们不与任何外部资源相关联;例如文件描述符,套接字,数据库连接。)

1 - The setBinaryStream method is really a getter. 1 - setBinaryStream方法实际上是一个getter。 Go figure. 去搞清楚。

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[]. 所以你最终得到一个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 ByteArrayInputStream which is basically a mechanism to supply the bytes to something in sequence. 首先,您可以创建一个ByteArrayInputStream ,它基本上是一个按字节顺序提供字节的机制。

then you could create a FileOutputStream for the file you want to create. 然后,您可以为要创建的文件创建FileOutputStream there are many types of InputStreams and OutputStreams for different data sources and destinations. 对于不同的数据源和目的地,有许多类型的InputStream和OutputStream。

lastly you would write the InputStream to the OutputStream. 最后,您将InputStream写入OutputStream。 in this case, the array of bytes would be sent in sequence to the FileOutputStream for writing. 在这种情况下,字节数组将按顺序发送到FileOutputStream进行写入。 For this i recommend using IOUtils 为此,我建议使用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块中执行'closing'。

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

There is no conversion between InputStream/OutputStream and the bytes they are working with. InputStream / OutputStream与它们使用的字节之间没有转换。 They are made for binary data, and just read (or write) the bytes one by one as is. 它们是为二进制数据而制作的,只是按原样逐个读取(或写入)字节。

A conversion needs to happen when you want to go from byte to char. 当你想从byte转到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时会发生这种情况,这是为字符数据而生成的。

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

我确实意识到我对这个问题的回答是迟到的,但我认为社区希望对这个问题有一个更新的方法

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

I think this is better since you already have an existing OutputStream in the response object. 我认为这更好,因为你已经在响应对象中有一个现有的OutputStream。 no need to create a new OutputStream. 无需创建新的OutputStream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM