cancel
Showing results for 
Search instead for 
Did you mean: 

JAVA Mapping: Convert the InputStream in to OutputStream out

Former Member
0 Kudos

Hi everybody,

I'd like to code my first JAVA Mapping. For this I would just like to convert the InputStream in to OutputStream out.

This does not work:

out = new FileOutputStream(new File(in));

How has the code look like?

Thanks, regards

Mario

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Mario.

Do you want to convert input into output directly without doing any transformation?

The way I've always used is to load the input document first:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setNamespaceAware(true);

DocumentBuilder builder = factory.newDocumentBuilder();

Document doc = builder.parse(inputStream);

After loading the document you can parse it's content with DOM API.

Finally you create the transformed document and return it through outputstream:

Document resultDoc = builder.newDocument();

...

...

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

DOMSource source = new DOMSource(resultDoc);

StreamResult result = new StreamResult(outputStream);

transformer.transform(source, result);

If you want to convert the input to output you can use:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setNamespaceAware(true);

DocumentBuilder builder = factory.newDocumentBuilder();

Document doc = builder.parse(inputStream);

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

DOMSource source = new DOMSource(doc);

StreamResult result = new StreamResult(outputStream);

transformer.transform(source, result);

At this point, I think you will have the input into the output.

Regards,

Gari.

Answers (3)

Answers (3)

Former Member
0 Kudos

Mario Müller:

If you just pass inputStream to outputStream without any transformation, you can just use following codes:

public void execute (InputStream in, OutputStream out) {

try {

int buffer;

while ((buffer = in.read()) != -1) {

out.write(buffer);

} //end of while

out.flush();

} catch (Exception e) {

throw new StreamTransformationException(e.getMessage(), e);

}

}

Former Member
0 Kudos

Btw: inputstream and outputstream are just abstract classes.

What exactly do you want to acjieve in the mapping? Why are you creating FileOutputStream? You want to create a file during the mapping?

Peter

Former Member
0 Kudos

Hi Mario!

You are using wrong constructor parameter. There is no constructor for object FIle with InpuStreamObject parameter)

http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html

Check the JavaDocs.

http://java.sun.com/j2se/1.4.2/docs/api/

Peter

Edited by: Peter Jarunek on Feb 22, 2008 10:43 AM