Solved

Files and Streams

Posted on 2004-08-09
5
335 Views
Last Modified: 2010-03-31
Question -

A method used AnyMethod(java.io.File , java.io.File ) signature
I have InputStream and string of the data in the file as I  create them on the fly and there is no "file" in the server.
How do I pass the InputStream/string to the method with the above signature.

Context
I am using FOP and it uses xml and fo .
To emberd itin a servelt and convert to PDF it uses

 XSLTInputHandler inputHandler = new XSLTInputHandler(xmlFile, xslFile);

where the xmlFile, xslFile are java.io.File, I have the XML and XSL as InputStreams / strings in my servers memory as they are generated dynamically, how to pass them

Thx
0
Comment
Question by:applekanna
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11750452
Create a file from this input stream:

FileOutputStream fos = new FileOutputStream(new File("myfile.pdf"));
// get the bytes from the input stream
fos.write(<bytes>);
fos.flush();
fos.close();

Then use this file (myfile.pdf) to pass it in the method.
0
 
LVL 3

Author Comment

by:applekanna
ID: 11750518
FileOutputStream fos = new FileOutputStream(new File("myfile.pdf"));

1. when I do something like this , would not I run into concurrency issues in the servlet because of the file name, so to make it use I need to give each file a unique file name (say system time - any other ideas)

2. Also I need to keep deleting the files to take care of memory issues.

I am currently doing the above both

Out of curiosity
Is this the only way ...or is there any other method
0
 
LVL 35

Accepted Solution

by:
girionis earned 100 total points
ID: 11750545
1. It depends if the FileOutputStream is an instance variable or not. If it is you can always synchronize it.

2. Delete them from disk first and then setting them to null would be enough for memory.

I am not sure if there is any other way, it might be though. If the method accepts two parameters of type "File" then you either need to pass a File or a subclass of it. It would be easier if you could change the method's signature and make it an Object so you can either pass a File or an InputStream (and check inside your method for the type of Object passed).
0
 
LVL 3

Author Comment

by:applekanna
ID: 11750613
Thx for ur comments
0
 
LVL 35

Expert Comment

by:girionis
ID: 11750648
Thank you for accepting, glad I was of help :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now