Java 将 OutputStream 转换为 ByteArrayOutputStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26960997/
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-11 03:35:09  来源:igfitidea点击:

Convert OutputStream to ByteArrayOutputStream

javaoutputstreamwavebytearrayoutputstream

提问by Bill Smith

I am trying to convert an OutputStreamto a ByteArrayOutputStream. I was unable to find any clear simple answers on how to do this. This question was asked in the title of the question on StackOverflow, but the body of the question aske how to change a ByteArrayStreamto OuputStream. I have an OutputStreamthat is already created and this example given in the answer will not compile!

我正在尝试将 an 转换OutputStreamByteArrayOutputStream。我无法找到有关如何执行此操作的任何明确的简单答案。这个问题是在 StackOverflow 上的问题标题中提出的,但问题的主体是询问如何将 a 更改ByteArrayStreamOuputStream. 我有一个OutputStream已经创建的,答案中给出的这个例子将无法编译!

That Question is Here

这个问题在这里

I have an OutputStream that is already constructed and has a length of 44 bytes called waveHeader. I want to convert that to a ByteArrayOutputStream because I want to be able to change that into a byte[] with waveHeader.ToByteArray() for simplicity in later processes;

我有一个已经构建的 OutputStream,它的长度为 44 个字节,称为 waveHeader。我想将其转换为 ByteArrayOutputStream,因为我希望能够使用 waveHeader.ToByteArray() 将其更改为 byte[],以便在以后的过程中简化;

Is there a simple type of casting or something that will allow this?

是否有一种简单的铸造类型或允许这样做的东西?

If not then:

如果没有,那么:

  • Is there a way to construct a pointer to the data in the original OutputStream if it is not possible to convert it?

  • How would someone go about accessing the data that is contained in the OutputStream?

  • 如果无法转换,有没有办法构造指向原始 OutputStream 中数据的指针?

  • 有人将如何访问包含在 OutputStream 中的数据?

I am new to JAVA. This is just a hobby for me. Streams In VisualBasic .net where much easier!

我是 JAVA 新手。这对我来说只是一种爱好。流在 VisualBasic .net 中更容易!

采纳答案by eckes

There are multiple possible scenarios:

有多种可能的场景:

a) You have a ByteArrayOutputStream, but it was declared as OutputStream. Then you can do a cast like this:

a) 您有一个 ByteArrayOutputStream,但它被声明为 OutputStream。然后你可以做这样的演员:

void doSomething(OutputStream os)
{
    // fails with ClassCastException if it is not a BOS
    ByteArrayOutputStream bos = (ByteArrayOutputStream)os;
...

b) if you have any other type of output stream, it does not really make sense to convert it to a BOS. (You typically want to cast it, because you want to access the result array). So in this case you simple set up a new stream and use it.

b) 如果您有任何其他类型的输出流,将其转换为 BOS 没有任何意义。(您通常要强制转换它,因为您要访问结果数组)。因此,在这种情况下,您只需设置一个新流并使用它。

void doSomething(OutputStream os)
{
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(something);
    bos.close();
    byte[] arr = bos.toByteArray();
    // what do you want to do?
    os.write(arr); // or: bos.writeTo(os);
...

c) If you have written something to any kind of OutputStream (which you do not know what it is, for example because you get it from a servlet), there is no way to get that information back. You must not write something you need later. A solution is the answer b) where you write it in your own stream, and then you can use the array for your own purpose as well as writing it to the actual output stream.

c) 如果您向任何类型的 OutputStream 写入了一些内容(您不知道它是什么,例如因为您从 servlet 中获取它),则无法获取该信息。你不能写一些你以后需要的东西。一个解决方案是答案 b) 在您自己的流中写入它,然后您可以将数组用于自己的目的以及将其写入实际的输出流。

Keep in mind ByteArrayOutputStreams keep all Data in Memory.

请记住 ByteArrayOutputStreams 将所有数据保存在内存中。

回答by Rahul Bobhate

You could use the writeTomethod of ByteArrayOutputStream.

你可以使用 的writeTo方法ByteArrayOutputStream

ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = new byte[8];
bos.write(bytes);
bos.writeTo(oos);

You can create an instance of ByteArrayOutputStream. You then need to write the data to this ByteOutputStreaminstance and then using the writeTomethod, which accepts an OutputStream, you can enable the ByteArrayOutputStreamto write the output, to the instance of OutputStreamwhich you passed as the argument.

您可以创建一个ByteArrayOutputStream. 然后,您需要将数据写入此ByteOutputStream实例,然后使用writeTo接受的方法,OutputStream您可以启用ByteArrayOutputStream将输出写入OutputStream作为参数传递的实例。

Hope it works!

希望它有效!

回答by cauchy

You can use toByteArrayfunction on the output stream you have.That's is let say you have outputStream buffer So you can do buffer.toByteArray.

你可以toByteArray在你拥有的输出流上使用函数。也就是说你有 outputStream 缓冲区所以你可以做buffer.toByteArray

For more you can look at the answer of Convert InputStream to byte array in Java.

有关更多信息,您可以查看Convert InputStream to byte array in Java的答案。