1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.geronimo.ews.ws4j2ee.context.impl;
18
19 import org.apache.geronimo.ews.ws4j2ee.context.InputOutputFile;
20 import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29
30 /***
31 * @author hemapani@opensource.lk
32 */
33 public class InputOutputFileImpl implements InputOutputFile {
34 private InputStream instream;
35 private String fileName;
36 private OutputStream outstream;
37
38 public InputOutputFileImpl(InputStream instream) {
39 this.instream = instream;
40 }
41
42 public InputOutputFileImpl(String fileName) throws GenerationFault {
43 this.fileName = fileName;
44 }
45
46 public InputOutputFileImpl(String fileName, InputStream instream) {
47 this.instream = instream;
48 this.fileName = fileName;
49 }
50
51 public String fileName() {
52 if (fileName == null)
53 throw new UnsupportedOperationException("asking for file name when input/output is a stream");
54 return fileName;
55 }
56
57 public InputStream getInputStream() throws GenerationFault {
58 try {
59 if (instream == null) {
60 File file = new File(fileName);
61 this.instream = new FileInputStream(file);
62 }
63 return instream;
64 } catch (FileNotFoundException e) {
65 e.printStackTrace();
66 throw GenerationFault.createGenerationFault(e);
67 }
68 }
69
70 public OutputStream getOutStream() throws GenerationFault {
71 try {
72 if (outstream == null) {
73 File file = new File(fileName);
74 File parent = file.getParentFile();
75 if (!parent.exists())
76 parent.mkdirs();
77 this.outstream = new FileOutputStream(file);
78 }
79 return outstream;
80 } catch (FileNotFoundException e) {
81 e.printStackTrace();
82 throw GenerationFault.createGenerationFault(e);
83 }
84 }
85
86
87
88
89 protected void finalize() throws Throwable {
90 try {
91 if (outstream != null)
92 outstream.close();
93 if (instream != null) {
94 instream.close();
95 }
96 } catch (IOException e) {
97 }
98 super.finalize();
99 }
100
101 }