You can use Apache Commons IO to handle this and similar tasks.
The IOUtils type has a static method to read an InputStream and return a byte[].
InputStream is;
byte[] bytes = IOUtils.toByteArray(is);
Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls toByteArray(). It handles large files by copying the bytes in blocks of 4KiB.
You can use Apache Commons IO to handle this and similar tasks.
The IOUtils type has a static method to read an InputStream and return a byte[].
InputStream is;
byte[] bytes = IOUtils.toByteArray(is);
Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls toByteArray(). It handles large files by copying the bytes in blocks of 4KiB.
You need to read each byte from your InputStream and write it to a ByteArrayOutputStream.
You can then retrieve the underlying byte array by calling toByteArray():
InputStream is = ...
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
return buffer.toByteArray();
Videos
You can't. FileInputStream is a type of InputStream that expects a file as input.
To use a byte array, you would use java.io.ByteArrayInputStream, which is also another type of InputStream.
Just make sure that whatever is expecting an input stream is defined to accept the more generic InputStream. (e.g.: public HSSFWorkbook(InputStream inputStream) { // HSSFWorkbook constructor definition)
Documentation: ByteArrayInputStream.
EDIT: A more complete example
If your HSSFWorkbook class has the constructor currently defined as:
public HSSFWorkbook(FileInputStream inputStream) {
// ...
}
... you would want to change it to accept the more generic InputStream class, which would now allow you to pass it either a FileInputStream or a ByteArrayInputStream instance depending on where you call it from. Like this:
public HSSFWorkbook(InputStream inputStream) {
// ...
}
Then you can instantiate your HSSFWorkbook using either option:
FileInputStream fileStream = new FileInputStream(filename);
workbook = new HSSFWorkbook(fileStream); // still works
... or ...
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(excelByteStream);
workbook = new HSSFWorkbook(byteArrayInputStream ); // now also works.
Use ByteArrayInputStream instead of FileInputStream:
workbook = new HSSFWorkbook(excelByteStream);
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.
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.
Use the FileUtils#readFileToByteArray(File) from Apache Commons IO, and then create the ByteArrayInputStream using the ByteArrayInputStream(byte[]) constructor.
public static ByteArrayInputStream retrieveByteArrayInputStream(File file) {
return new ByteArrayInputStream(FileUtils.readFileToByteArray(file));
}
The general idea is that a File would yield a FileInputStream and a byte[] a ByteArrayInputStream. Both implement InputStream so they should be compatible with any method that uses InputStream as a parameter.
Methods should generally accept InputStream rather than any implementing classes such as ByteArrayInputStream for reading bytes otherwise the whole generalization / raison d'être of byte streams is lost.
Putting all of the file contents in a ByteArrayInputStream can be done of course:
- read in the full file into a
byte[]; Java version >= 7 contains a convenience method calledreadAllBytesto read all data from a file; - create a
ByteArrayInputStreamaround the file content, which is now in memory.
Note that this may not be optimal solution for very large files - all the file will stored in memory at the same point in time. Using the right stream for the job is important.