zanegray
zanegray

Reputation: 768

File(s) and InputStream/OutputStream

Hello Stack Overflow community,

I am doing multistep processing on some data I am receiving with a java Servlet. The current process I have is that I input the files to a server using Apache File Upload and convert them to a File. Then once input1 is populated with data, I run through a flow similar to this (where the process functions are xsl transforms):

File input1 = new File(FILE_NAME);  // <---this is populated with data
File output1 = new File(TEMP_FILE); // <---this is the temporary file

InputStream read = new FileInputStream(input1); 
OuputStream out = new FileOutputStream(output1);

process1ThatReadsProcessesOutputs( read, out);

out.close();
read.close();

//this is basically a repeat of the above process!
File output2 = new File(RESULT_FILE);  // <--- This is the result file 
InputStream read1 = new FileInputStream(output1);
OutputStream out1 = new FileOutputStream(output2);
Process2ThatReadsProcessesOutputs( read1, out1);
read1.close();
out1.close();
…

So my question is if there is a better way to do this so I do not have to create those temporary Files and recreate streams to those Files? (I am assuming I am incurring a decent performace penatly)

I saw this Most Efficient Way to create InputStream from OutputStream but I am not sure if this is the best route to go...

Upvotes: 1

Views: 6920

Answers (3)

Gangnus
Gangnus

Reputation: 24484

Java 9 brings a new answer to the question:

// All bytes from an InputStream at once
byte[] result = new ByteArrayInputStream(buf)
    .readAllBytes();

// Directly redirect an InputStream to an OutputStream
new ByteArrayInputStream(buf)
    .transferTo(System.out);

Upvotes: 0

Nacho Cougil
Nacho Cougil

Reputation: 562

I don't know why are you converting the FileItem retrieved with Apache Commons if you don't really needed. You can use the same InputStream that each FileItem has to using and read the content of the uploaded file:

// create/retrieve a new file upload handler
ServletFileUpload upload = ...;

// parse the request
List<FileItem> items = (List<FileItem>) upload.parseRequest(request);

/* get the FileItem from the List. Yes, it's not a best practice because you must verify 
   how many you receive, and check everything is ok, etc. 
   Let's suppose you've done it */
//...
FileItem item = items.get(0); 

// get the InputStrem to read the contents of the file 
InputStream is = item.getInputStream();

So finally, you can use the InputStream object to read the uploaded stream sent by the client avoiding unnecessary instantiations.

And yes, it's really recommended to use Buffered clases like BufferedInputStream and BufferedOutputStream.

The other idea could be to avoid FileOutputStream (the middle one) and replace it with ByteArrayOutputStream if you don't need to be written in disk (always is slower than working in memory).

Upvotes: 1

Rosdi Kasim
Rosdi Kasim

Reputation: 26036

Just replace FileOutputStream to ByteArrayInputStream vice/versa.

Example:

ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());

Upvotes: 1

Related Questions