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.IOException;
9 import java.io.OutputStream;
10
11
12
13
14
15
16 public class DirectoryDestination extends AbstractDestination{
17 private File base;
18
19
20
21
22
23
24 public DirectoryDestination(File base) throws IOException{
25 if(base.exists() && !base.isDirectory()){
26 throw new IOException(base + ": not directory");
27 }
28 this.base = base;
29 }
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 @Override
46 public OutputStream getOutput(String name) throws IOException{
47 if(name == null){
48 throw new NullPointerException();
49 }
50 File file = new File(base, name);
51 File parent = file.getParentFile();
52 if(!parent.exists()){
53 boolean flag = parent.mkdirs();
54 if(!flag){
55 throw new IOException(parent.getPath() + ": mkdir failed");
56 }
57 }
58 return new FileOutputStream(file);
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 @Override
78 public OutputStream getOutput(ProcessTarget target) throws IOException{
79 if(target == null){
80 throw new NullPointerException();
81 }
82 return getOutput(target.getName());
83 }
84
85
86
87
88 @Override
89 public void close(){
90 }
91 }