1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package net.sourceforge.javadpkg.io.impl;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import net.sourceforge.javadpkg.io.DataSource;
25  import net.sourceforge.javadpkg.io.DataSwap;
26  import net.sourceforge.javadpkg.io.DataTarget;
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  public class DataTempFileSwap implements DataSwap {
40  	
41  	
42  	
43  	private String		name;
44  	
45  	private String		prefix;
46  	
47  	private String		suffix;
48  	
49  	
50  	private File		file;
51  	
52  	private DataTarget	target;
53  	
54  	private DataSource	source;
55  	
56  	private boolean		closed;
57  	
58  	
59  	
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  	public DataTempFileSwap(String name) {
70  		super();
71  		
72  		if (name == null)
73  			throw new IllegalArgumentException("Argument name is null.");
74  		
75  		this.name = name;
76  		this.prefix = "data-temp-file-swap-";
77  		this.suffix = ".tmp";
78  
79  		this.file = null;
80  		this.target = null;
81  		this.source = null;
82  		this.closed = false;
83  	}
84  	
85  	
86  	
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100 	public DataTempFileSwap(String name, String prefix, String suffix) {
101 		this(name);
102 		
103 		if (prefix == null)
104 			throw new IllegalArgumentException("Argument prefix is null.");
105 		if (suffix == null)
106 			throw new IllegalArgumentException("Argument suffix is null.");
107 		
108 		this.prefix = prefix;
109 		this.suffix = suffix;
110 	}
111 	
112 	
113 	@Override
114 	public DataTarget getTarget() throws IOException {
115 		if (this.closed)
116 			throw new IllegalStateException("Can't return the target because the swap |" + this.name + "| was already closed.");
117 		if (this.source != null)
118 			throw new IllegalStateException("Can't return the target of the swap |" + this.name
119 					+ "| because the corresponding source is already open.");
120 		if (this.target != null)
121 			return this.target;
122 		
123 		this.file = File.createTempFile(this.prefix, this.suffix);
124 		this.target = new DataFileTarget(this.file);
125 		return this.target;
126 	}
127 
128 
129 	@Override
130 	public DataSource getSource() throws IOException {
131 		if (this.closed)
132 			throw new IllegalStateException("Can't return the source because the swap |" + this.name + "| was already closed.");
133 		if (this.source != null)
134 			return this.source;
135 		if (this.target == null)
136 			throw new IllegalStateException("Can't return the source of the swap |" + this.name
137 					+ "| because the corresponding target hasn't been opened yet.");
138 		
139 		this.target.close();
140 		this.source = new DataFileSource(this.file, true);
141 		return this.source;
142 	}
143 	
144 	
145 	@Override
146 	public void close() throws IOException {
147 		try {
148 			if (this.target != null) {
149 				try {
150 					this.target.close();
151 				} catch (IOException e) {
152 					throw new IOException("Couldn't close target of swap |" + this.name + "|: " + e.getMessage(), e);
153 				}
154 			}
155 			if (this.source != null) {
156 				try {
157 					this.source.close();
158 				} catch (IOException e) {
159 					throw new IOException("Couldn't close source of swap |" + this.name + "|: " + e.getMessage(), e);
160 				}
161 			}
162 			if ((this.file != null) && this.file.exists()) {
163 				if (!this.file.delete())
164 					throw new IOException(
165 							"Couldn't delete swap |" + this.name + "| (Path: |" + this.file.getAbsolutePath() + "|).");
166 			}
167 		} finally {
168 			this.file = null;
169 			this.target = null;
170 			this.source = null;
171 			this.closed = true;
172 		}
173 	}
174 
175 
176 }