1 package rydeen.io;
2
3 import rydeen.Destination;
4 import rydeen.ProcessTarget;
5
6 import java.io.File;
7 import java.io.FileOutputStream;
8 import java.io.FilterOutputStream;
9 import java.io.IOException;
10 import java.io.OutputStream;
11 import java.util.jar.JarOutputStream;
12 import java.util.jar.Manifest;
13 import java.util.zip.ZipEntry;
14
15
16
17
18
19
20 public class JarFileDestination extends AbstractDestination{
21 private File file;
22 private JarOutputStream jarOut;
23 private boolean closed = false;
24 private Manifest manifest;
25
26
27
28
29
30 public JarFileDestination(File file){
31 this.file = file;
32 }
33
34
35
36
37
38
39 public JarFileDestination(File file, Manifest manifest){
40 this.file = file;
41 this.manifest = manifest;
42 }
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 @Override
62 public OutputStream getOutput(String name) throws IOException{
63 if(closed){
64 throw new IOException("already closed");
65 }
66 if(name == null){
67 throw new NullPointerException();
68 }
69 if(jarOut == null){
70 if(manifest != null){
71 jarOut = new JarOutputStream(new FileOutputStream(file), manifest);
72 } else{
73 jarOut = new JarOutputStream(new FileOutputStream(file));
74 }
75 }
76 ZipEntry entry = new ZipEntry(name);
77 jarOut.putNextEntry(entry);
78 return new FilterOutputStream(jarOut){
79 @Override
80 public void close() throws IOException{
81 jarOut.closeEntry();
82 }
83 };
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 @Override
106 public OutputStream getOutput(ProcessTarget target) throws IOException{
107 return getOutput(target.getName());
108 }
109
110
111
112
113
114
115 @Override
116 public synchronized void close() throws IOException{
117 if(closed){
118 throw new IOException("already closed");
119 }
120 if(jarOut != null && !closed){
121 jarOut.close();
122 closed = true;
123 }
124 }
125
126
127
128
129
130
131 public boolean isClosed(){
132 return closed;
133 }
134 }