Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need help to reuse InputStream on my code

628803Jun 6 2009 — edited Jun 8 2009
Hi everyone,

I'm working on JDev11g U2 with ADFRC

I'm using af:inputFile to upload read my file. for that i'm getting my Files InputStream on af:inputFile's valueChangeListener.

Now for the first time when i click a button to read content from this IS then it is working very well. button as i press the same button again without changing my file from af:InputFile, I'm getting InputStream as null, coz it closes itself at EOF.

Now problem is I can't reopen it, or can't make a clone, or can't use mark(int) with this..

So i used a wrapper method from apache's POI POIFSFileSystem.createNonClosingInputStream(InputStream is) to wrap my IS.

according to them it should realy solve my problem but it is not working...
InputStream wrappedStream = POIFSFileSystem.createNonClosingInputStream(is); <-------- IS read here
HSSFWorkbook wb = new HSSFWorkbook(wrappedStream);
is.reset(); <-------- getting null here as i already read above
doSomethingElse(is);
now tell me how can i implement this code too. once i read my IS, it becomes null. so how can i reset it.

Second way i did that i convert my IS as String so that i can carry it for any no. of use. coz i'm using my IS to covert my file From XML, CSV, XLS to XML

using this method-
    public static String convertInputStreamToString(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        return sb.toString();
    }
so in case of CSV and XML it is working way, but in case of XLS , I'm not getting anything from this coverted String.

I'm using this method to get my IS back from String -
for XML - 
            InputStream useIS =
                        new ByteArrayInputStream(inputStream.getBytes("UTF-8"));

for CSV - (inputStream is String of my IS)
            BufferedReader csvReader;
            csvReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(inputStream.getBytes("UTF-8"))));

for XLS - 
            POIFSFileSystem myFileSystem =
                new POIFSFileSystem(new ByteArrayInputStream(inputStream.getBytes("UTF-8")));
            //InputStream inStr = new ByteArrayInputStream(inputStream.getBytes("UTF-8"));
            HSSFWorkbook workbook = new HSSFWorkbook(myFileSystem);
Now can anybody help me to find out the right way to use my IS again and again.

Thanks
Fizzz...
This post has been answered by Timo Hahn on Jun 6 2009
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 6 2009
Added on Jun 6 2009
4 comments
2,492 views