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.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  
25  import net.sourceforge.javadpkg.io.DataSource;
26  import net.sourceforge.javadpkg.io.DataSwap;
27  import net.sourceforge.javadpkg.io.DataTarget;
28  
29  
30  /**
31   * <p>
32   * A {@link DataSwap} implementation which uses a byte array for storing the
33   * data.
34   * </p>
35   *
36   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
37   * @version <b>1.0</b>, 02.05.2016 by Gerrit Hohl
38   */
39  public class DataByteArraySwap implements DataSwap {
40  	
41  	
42  	/** The name. */
43  	private String					name;
44  
45  	/** The stream. */
46  	private ByteArrayOutputStream	out;
47  	/** The target. */
48  	private DataTarget				target;
49  	/** The source. */
50  	private DataSource				source;
51  	/** The flag if the swap was closed. */
52  	private boolean					closed;
53  	
54  	
55  	/**
56  	 * <p>
57  	 * Creates a swap.
58  	 * </p>
59  	 *
60  	 * @param name
61  	 *            The name of the target and source.
62  	 * @throws IllegalArgumentException
63  	 *             If the name is <code>null</code>.
64  	 */
65  	public DataByteArraySwap(String name) {
66  		super();
67  
68  		if (name == null)
69  			throw new IllegalArgumentException("Argument name is null.");
70  		
71  		this.name = name;
72  	}
73  	
74  	
75  	@Override
76  	public DataTarget getTarget() throws IOException {
77  		if (this.closed)
78  			throw new IllegalStateException("Can't return the target because the swap |" + this.name + "| was already closed.");
79  		if (this.source != null)
80  			throw new IllegalStateException("Can't return the target of the swap |" + this.name
81  					+ "| because the corresponding source is already open.");
82  		if (this.target != null)
83  			return this.target;
84  		
85  		this.out = new ByteArrayOutputStream();
86  		this.target = new DataStreamTarget(this.out, this.name, true);
87  		return this.target;
88  	}
89  
90  
91  	@Override
92  	public DataSource getSource() throws IOException {
93  		if (this.closed)
94  			throw new IllegalStateException("Can't return the source because the swap |" + this.name + "| was already closed.");
95  		if (this.source != null)
96  			return this.source;
97  		if (this.target == null)
98  			throw new IllegalStateException("Can't return the source of the swap |" + this.name
99  					+ "| because the corresponding target hasn't been opened yet.");
100 		
101 		this.target.close();
102 		this.source = new DataStreamSource(new ByteArrayInputStream(this.out.toByteArray()), this.name, true);
103 		return this.source;
104 	}
105 	
106 	
107 	@Override
108 	public void close() throws IOException {
109 		try {
110 			if (this.target != null) {
111 				try {
112 					this.target.close();
113 				} catch (IOException e) {
114 					throw new IOException("Couldn't close target of swap |" + this.name + "|: " + e.getMessage(), e);
115 				}
116 			}
117 			if (this.source != null) {
118 				try {
119 					this.source.close();
120 				} catch (IOException e) {
121 					throw new IOException("Couldn't close source of swap |" + this.name + "|: " + e.getMessage(), e);
122 				}
123 			}
124 		} finally {
125 			this.out = null;
126 			this.target = null;
127 			this.source = null;
128 			this.closed = true;
129 		}
130 	}
131 
132 
133 }