on 02-22-2008 7:56 AM
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.