How can I convert an OutputStream to a byte array? I have found that first I need to convert this OutputStream to a ByteArrayOutputStream. There is only write() method in this OutputStream class and I don't know what to do. Is there any idea?
Create a ByteArrayOutputStream
.
Grab its content by calling toByteArray()
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.writeTo(myOutputStream);
baos.toByteArray();
If the OutputStream
object supplied is not already a ByteArrayOutputStream
, one can wrap
it inside a delegate class that will "grab" the bytes supplied to the write()
methods, eg
public class DrainableOutputStream extends FilterOutputStream {
private final ByteArrayOutputStream buffer;
public DrainableOutputStream(OutputStream out) {
super(out);
this.buffer = new ByteArrayOutputStream();
}
@Override
public void write(byte b[]) throws IOException {
this.buffer.write(b);
super.write(b);
}
@Override
public void write(byte b[], int off, int len) throws IOException {
this.buffer.write(b, off, len);
super.write(b, off, len);
}
@Override
public void write(int b) throws IOException {
this.buffer.write(b);
super.write(b);
}
public byte[] toByteArray() {
return this.buffer.toByteArray();
}
}
To reduce the overhead, the calls to super
in the above class can be omitted - eg, if only the "conversion" to a byte array is desired.
A more detailed discussion can be found in another StackOverflow question .
You need to do 2 things
You could even extend it as mentioned here
您可以简单地将输出流声明为ByteArrayOutputStream
然后使用ByteArrayOutputStream#toByteArray()
。
If You try ByteArrayOutputStream bos=(ByteArrayOutputStream)outputStream
then throw ClassCastException
. I did it when I get OutputStream
from HttpServletResponse
and it is CoyoteOutputStream
.(You can create file so dont create file temp).
You can use Java Reflection
to convert OutputStream
to byte[]
:
byte[] bytes = this.getBytes(outStream);
/**
* Get byte from OutputStream
*
* @param outStream
* @return
*/
@SneakyThrows
private byte[] getBytes(OutputStream outStream) {
OutputBuffer outputBuffer = (OutputBuffer) this.getValueByName("ob", outStream);
ByteBuffer byteBuffer = (ByteBuffer) this.getValueByName("bb", outputBuffer);
return (byte[]) this.getValueByName("hb", byteBuffer);
}
/**
* Get value from property
*
* @param name
* @param value
* @return
*/
@SneakyThrows
@SuppressWarnings("unchecked")
private Object getValueByName(String name, Object value) {
List<Field> listFiled = new ArrayList<>();
if (value.getClass().getSuperclass() != null) {
listFiled.addAll(Arrays.asList(value.getClass().getSuperclass().getDeclaredFields()));
}
listFiled.addAll(Arrays.asList(value.getClass().getDeclaredFields()));
Optional<Field> fieldOb = listFiled.stream()
.filter(field -> StringUtils.equalsAnyIgnoreCase(name, field.getName()))
.findFirst();
if (fieldOb.isPresent()) {
Field field = fieldOb.get();
field.setAccessible(true);
return field.get(value);
}
return StringUtils.EMPTY; // FIXME
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.