Search code examples
javaperformancejakarta-eearraysfileinputstream

How to build FileInputStream object with byte array as a parameter


I have a zip file and after decoding it I get a byte array now I want to create a FileInputStream object with that byte[] object. I dont want to create a file instead pass data content do FileInputStream. Is there any way ?

following is the code:

byte[] decodedHeaderFileZip = decodeHeaderZipFile(headerExportFile);
FileInputStream fileInputStream = new FileInputStream(decodedHeaderZipFileString);

EDIT: I wanted to build a ZipInputStream object with a FileInputStream.


Solution

  • I have a zip file and after decoding it I get a byte array now I want to create a FileInputStream object with that byte[] object.

    But you don't have a file. You have some data in memory. So a FileInputStream is inappropriate - there's no file for it to read from.

    If possible, use a ByteArrayInputStream instead:

    InputStream input = new ByteArrayInputStream(decodedHeaderFileZip);
    

    Where possible, express your API in terms of InputStream, Reader etc rather than any specific implementation - that allows you to be flexible in which implementation you use. (What I mean is that where possible, make method parameters and return types InputStream rather than FileInputStream - so that callers don't need to provide the specific types.)

    If you absolutely have to create a FileInputStream, you'll need to write the data to a file first.