Copying FileOutputStream to new FileInputStream

Discussions

General J2EE: Copying FileOutputStream to new FileInputStream

  1. Copying FileOutputStream to new FileInputStream (1 messages)

    We have a requirement for our users to be able to upload multiple xml files to our server for processing into our database. For security reasons, our network team does not want any files written to the servers (or any place accessible by the servers). Therefore, we are trying to work with the zipped files strictly within memory, without writing a physical file.

    The problem is this:
    The zip files exists in memory. The xml files have been unzipped into memory. But, after being unzipped, they exist in an output stream which can't be read (output streams are only for writing, right?). Therefore, to continue working with the xml (performing schema validations and loading into tables) they need to be in an input stream. We have not been able to find a way to convert an output stream into a new input stream.

    Can anyone suggest a way to resolve this? Can we perform schema validations and load to tables from the output stream? I don't believe so. It is my understanding that you can only write from an output stream, and not read from it.
  2. But, after being unzipped, they exist in an output stream which can't be read (output streams are only for writing, right?).

    Actually wrong, check out java.io.ByteArrayOutputStream. You output to the OutputStream, then you can retrieve the content as a byte array.

    Other options include:

    1/ another implementation:
    org.apache.commons.io.output.ByteArrayOutputStream
    2/ A copy utility:
    org.apache.commons.io.IOUtils
    with a copy that takes an input and output stream

    from jakarta commons io.

    HTH,
    sv