View Javadoc
1   /*
2    * dpkg - Debian Package library and the Debian Package Maven plugin
3    * (c) Copyright 2016 Gerrit Hohl
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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   * <p>
31   * A {@link DataSwap} implementation which uses temporary files for storing the
32   * data.
33   * </p>
34   *
35   * @see File#createTempFile(String, String)
36   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
37   * @version <b>1.0</b>, 29.04.2016 by Gerrit Hohl
38   */
39  public class DataTempFileSwap implements DataSwap {
40  	
41  	
42  	/** The name. */
43  	private String		name;
44  	/** The prefix. */
45  	private String		prefix;
46  	/** The suffix. */
47  	private String		suffix;
48  	
49  	/** The file. */
50  	private File		file;
51  	/** The target. */
52  	private DataTarget	target;
53  	/** The source. */
54  	private DataSource	source;
55  	/** The flag if the swap was closed. */
56  	private boolean		closed;
57  	
58  	
59  	/**
60  	 * <p>
61  	 * Creates a swap.
62  	 * </p>
63  	 *
64  	 * @param name
65  	 *            The name of the target and source.
66  	 * @throws IllegalArgumentException
67  	 *             If the name is <code>null</code>.
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  	 * <p>
88  	 * Creates a swap.
89  	 * </p>
90  	 *
91  	 * @param name
92  	 *            The name of the target and source.
93  	 * @param prefix
94  	 *            The prefix of the filename.
95  	 * @param suffix
96  	 *            The suffix of the filename.
97  	 * @throws IllegalArgumentException
98  	 *             If any of the parameters are <code>null</code>.
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 }