Byte[] to InputStream or OutputStream
Asked Answered
P

8

138

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. 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?

Phony answered 19/1, 2010 at 6:3 Comment(2)
Shouldn't the title be "array of bytes ..." or "byte array..." or "byte[]..." instaed of "byte of array..."?Kosygin
See the reverse here: stackoverflow.com/questions/1264709/…Clipclop
U
208

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

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 methods1, and you can also get and set the bytes directly.

(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; e.g. file descriptors, sockets, database connections.)

1 - The setBinaryStream method is really a getter. Go figure.

Uruguay answered 19/1, 2010 at 6:20 Comment(0)
B
14

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 ByteArrayInputStream which is basically a mechanism to supply the bytes to something in sequence.

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

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

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.

Brock answered 19/1, 2010 at 6:25 Comment(2)
didnt you mean - ByteArrayOutputStream out = new ByteArrayOutputStream(); instead ByteArrayOutputStream out = new ByteArrayInputStream();Guideboard
CloseQuietly should probably be in a finally clause.Llywellyn
H
5

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

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

Hex answered 1/9, 2015 at 22:50 Comment(0)
F
4

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.

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.

Feint answered 19/1, 2010 at 6:20 Comment(0)
E
2
output = new ByteArrayOutputStream();
...
input = new ByteArrayInputStream( output.toByteArray() )
Ealing answered 21/9, 2016 at 11:48 Comment(0)
C
0

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.

Cordie answered 2/10, 2013 at 18:51 Comment(1)
Circular buffers do not solve the OP's problem. In the Question as written, the OP needs the entire content in a single byte array.Uruguay
N
0
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.

Noach answered 28/1, 2015 at 16:2 Comment(0)
R
0
String info = "somedata";       
OutputStream os=new FileOutputStream("mydata.bin");
        byte [] datas= info.getBytes();
        os.write(datas);
    
InputStream is=new FileInputStream("mydata.bin");

        datas=new byte[1000];
        int counts = is.read(datas); //reads all data from bin to datas then returns size

        info = new String(datas,0,counts); //
        is.close();;


        System.out.println("From Binary File: " + info);
Resolute answered 14/7, 2023 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.