1 /***
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $Id: Buffer.java 409 2006-01-02 08:50:37Z mat007 $
29 */
30
31 package palmed.buffer;
32
33 import java.io.ByteArrayOutputStream;
34 import java.io.DataInputStream;
35 import java.io.DataOutputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.OutputStream;
39 import palmed.edit.ITextBox;
40 import palmed.file.File;
41 import palmed.file.IFile;
42
43 /***
44 * This class implements an association between a text box content and a file.
45 *
46 * @author Mathieu Champlon
47 * @version $Revision: 409 $ $Date: 2006-01-02 17:50:37 +0900 (lun., 02 janv. 2006) $
48 */
49 public final class Buffer implements IBuffer
50 {
51 /***
52 * The text box.
53 */
54 private final ITextBox textBox_;
55 /***
56 * The file.
57 */
58 private IFile file_;
59
60 /***
61 * Create an empty buffer.
62 *
63 * @param textBox the text box.
64 */
65 public Buffer( final ITextBox textBox )
66 {
67 if( textBox == null )
68 throw new IllegalArgumentException( "parameter 'textBox' is null" );
69 textBox_ = textBox;
70 textBox_.clear();
71 }
72
73 /***
74 * {@inheritDoc}
75 */
76 public void open( final IFile file )
77 {
78 try
79 {
80 final InputStream stream = file.openInputStream();
81 textBox_.read( stream );
82 stream.close();
83 file_ = file;
84 }
85 catch( IOException e )
86 {
87 throw new BufferException( e );
88 }
89 }
90
91 /***
92 * {@inheritDoc}
93 */
94 public boolean save()
95 {
96 if( !hasBeenModified() )
97 return true;
98 if( file_ != null )
99 {
100 save( file_ );
101 return true;
102 }
103 return false;
104 }
105
106 /***
107 * {@inheritDoc}
108 */
109 public void save( final IFile file )
110 {
111 try
112 {
113 final OutputStream stream = file.openOutputStream();
114 final ByteArrayOutputStream bufferedStream = new ByteArrayOutputStream();
115 textBox_.write( bufferedStream );
116 stream.write( bufferedStream.toByteArray() );
117 stream.close();
118 file_ = file;
119 }
120 catch( IOException e )
121 {
122 throw new BufferException( e );
123 }
124 }
125
126 /***
127 * {@inheritDoc}
128 */
129 public boolean hasBeenModified()
130 {
131 return textBox_.hasBeenModified();
132 }
133
134 /***
135 * {@inheritDoc}
136 */
137 public boolean hasFile( final IFile file )
138 {
139 return file.equals( file_ );
140 }
141
142 /***
143 * {@inheritDoc}
144 */
145 public void marshall( final OutputStream stream ) throws IOException
146 {
147 writeFile( stream );
148 textBox_.marshall( stream );
149 }
150
151 private void writeFile( final OutputStream stream ) throws IOException
152 {
153 final DataOutputStream output = new DataOutputStream( stream );
154 if( file_ == null )
155 output.writeUTF( "" );
156 else
157 output.writeUTF( file_.toString() );
158 }
159
160 /***
161 * {@inheritDoc}
162 */
163 public void unmarshall( final InputStream stream ) throws IOException
164 {
165 readFile( stream );
166 textBox_.unmarshall( stream );
167 }
168
169 private void readFile( final InputStream stream ) throws IOException
170 {
171 final DataInputStream input = new DataInputStream( stream );
172 final String path = input.readUTF();
173 if( !path.equals( "" ) )
174 file_ = new File( path );
175 }
176
177 /***
178 * {@inheritDoc}
179 */
180 public void delete()
181 {
182 textBox_.delete();
183 }
184 }