KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > content > NodeRevisionContent


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/content/NodeRevisionContent.java,v 1.16 2004/07/28 09:37:55 ib Exp $
3  * $Revision: 1.16 $
4  * $Date: 2004/07/28 09:37:55 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.content;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.CharArrayReader JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.io.Serializable JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.apache.slide.common.ObjectValidationFailedException;
37 import org.apache.slide.util.Messages;
38
39 /**
40  * Encapsultes the contents of a revision.
41  *
42  * @version $Revision: 1.16 $
43  */

44 public final class NodeRevisionContent implements Serializable JavaDoc {
45
46
47     // -------------------------------------------------------------- Constants
48

49
50     private static final int CHUNK = 1024*4;
51
52
53     // ----------------------------------------------------- Instance Variables
54

55
56     /**
57      * Content.
58      */

59     private char[] content = null;
60     private byte[] contentBytes = null;
61
62
63     /**
64      * Reader.
65      */

66     private transient Reader JavaDoc reader = null;
67
68
69     /**
70      * Input stream.
71      */

72     private transient InputStream JavaDoc inputStream = null;
73
74
75     // ------------------------------------------------------------- Properties
76

77
78     /**
79      * Content accessor.
80      *
81      * @return char[] Content
82      */

83     public char[] getContent() {
84         char[] result = null;
85         if (content != null) {
86             result = content;
87             inputStream = null;
88             reader = null;
89         }
90         else if (reader != null) {
91             try {
92                 content = read(reader);
93             } catch (IOException JavaDoc e) {
94                 e.printStackTrace();
95             }
96             result = content;
97             inputStream = null;
98             reader = null;
99         }
100         else if (contentBytes != null) {
101             content = new String JavaDoc(contentBytes).toCharArray();
102             result = content;
103             inputStream = null;
104             reader = null;
105         }
106         else if (inputStream != null) {
107             try {
108                 contentBytes = read(inputStream);
109             } catch (IOException JavaDoc e) {
110                 e.printStackTrace();
111             } finally {
112                 try {
113                     inputStream.close();
114                 } catch (IOException JavaDoc e) {
115                 }
116             }
117             content = new String JavaDoc(contentBytes).toCharArray();
118             result = content;
119             inputStream = null;
120             reader = null;
121         }
122         return result;
123     }
124
125     /**
126      * Content accessor.
127      *
128      * @return char[] Content
129      */

130     public byte[] getContentBytes() {
131         byte[] result = null;
132         if (contentBytes != null) {
133             result = contentBytes;
134             inputStream = null;
135             reader = null;
136         }
137         else if (inputStream != null) {
138             try {
139                 contentBytes = read(inputStream);
140             } catch (IOException JavaDoc e) {
141                 e.printStackTrace();
142             } finally {
143                 try {
144                     inputStream.close();
145                 } catch (IOException JavaDoc e) {
146                 }
147             }
148             result = contentBytes;
149             inputStream = null;
150             reader = null;
151         }
152         else if (content != null) {
153             contentBytes = new String JavaDoc(content).getBytes();
154             result = contentBytes;
155             inputStream = null;
156             reader = null;
157         }
158         else if (reader != null) {
159             try {
160                 content = read(reader);
161             } catch (IOException JavaDoc e) {
162                 e.printStackTrace();
163             }
164             contentBytes = new String JavaDoc(content).getBytes();
165             result = contentBytes;
166             inputStream = null;
167             reader = null;
168         }
169         return result;
170     }
171
172
173
174     /**
175      * Content accessor.
176      *
177      * @return Reader
178      */

179     public Reader JavaDoc readContent()
180         throws IOException JavaDoc {
181         Reader JavaDoc result = null;
182         if (reader != null) {
183             result = reader;
184             inputStream = null;
185         }
186         else if (content != null) {
187             result = new CharArrayReader JavaDoc(content);
188             inputStream = null;
189             reader = null;
190         }
191         else if (inputStream != null) {
192             result = new InputStreamReader JavaDoc(inputStream);
193             reader = null;
194         }
195         else if (contentBytes != null) {
196             result = new CharArrayReader JavaDoc(new String JavaDoc(contentBytes).toCharArray());
197             inputStream = null;
198             reader = null;
199         }
200         return result;
201     }
202
203
204     /**
205      * Content accessor.
206      *
207      * @return InputStream
208      */

209     public InputStream JavaDoc streamContent()
210         throws IOException JavaDoc {
211         InputStream JavaDoc result = null;
212         if (inputStream != null) {
213             result = inputStream;
214             content = null;
215             reader = null;
216         }
217         else if (contentBytes != null) {
218             result = new ByteArrayInputStream JavaDoc( contentBytes );
219             reader = null;
220             inputStream = null;
221         }
222 // else if( reader != null ) {
223
// result = ???;
224
// inputStream = null;
225
// }
226
else if (content != null) {
227 // Class StringBufferInputStream is deprecated !!
228
// result = new StringBufferInputStream(new String(content));
229
result = new ByteArrayInputStream JavaDoc( new String JavaDoc(content).getBytes() );
230             reader = null;
231             inputStream = null;
232         }
233         return result;
234     }
235
236
237     /**
238      * Content mutator.
239      *
240      * @param contentBytes New content
241      */

242     public void setContent(byte[] contentBytes) {
243         this.contentBytes = contentBytes;
244         this.reader = null;
245         this.inputStream = null;
246         this.content = null;
247     }
248
249     /**
250      * Content mutator.
251      *
252      * @param content New content
253      */

254     public void setContent(char[] content) {
255         this.content = content;
256         this.reader = null;
257         this.inputStream = null;
258         this.contentBytes = null;
259     }
260
261
262     /**
263      * Content mutator.
264      *
265      * @param reader New reader
266      */

267     public void setContent(Reader JavaDoc reader) {
268         this.reader = reader;
269         this.inputStream = null;
270         this.content = null;
271         this.contentBytes = null;
272     }
273
274
275     /**
276      * Content mutator.
277      *
278      * @param inputStream New input stream
279      */

280     public void setContent(InputStream JavaDoc inputStream) {
281         this.inputStream = inputStream;
282         this.reader = null;
283         this.content = null;
284         this.contentBytes = null;
285     }
286
287
288     // TODO : Add real serialization support
289

290
291     /**
292      * Validate.
293      */

294     public void validate() {
295
296         if ((content == null) && (contentBytes == null) && (reader == null) && (inputStream == null))
297             throw new ObjectValidationFailedException
298                 (Messages.message
299                  (NodeRevisionContent.class.getName() + ".noContent"));
300
301     }
302
303
304     // -------------------------------------------------------- Private Methods
305

306
307     /**
308      ** Read the data from the stream and return the result in a byte array.
309      ** Return null in case of an error.
310      **/

311     public static byte[] read(InputStream JavaDoc inputStream) throws IOException JavaDoc {
312         byte[] chunk;
313         byte[] all;
314         int len;
315         List JavaDoc chunks;
316         int i;
317         int last;
318
319         chunks = new ArrayList JavaDoc();
320         do {
321             chunk = new byte[CHUNK];
322             chunks.add(chunk);
323             len = read(inputStream, chunk);
324         } while (len == CHUNK);
325         last = chunks.size() - 1;
326         all = new byte[last * CHUNK + len ];
327         for (i = 0; i <= last; i++) {
328             chunk = (byte[]) chunks.get(i);
329             System.arraycopy(chunk, 0, all, CHUNK * i, (i == last)? len : CHUNK);
330         }
331         return all;
332     }
333
334     /**
335      ** Read until EOF or the buffer is filled.
336      **
337      ** @return bytes actually read; != buffer.length for eof
338      **/

339     private static int read(InputStream JavaDoc stream, byte[] buffer) throws IOException JavaDoc {
340         int ofs;
341         int len;
342
343         ofs = 0;
344         while (true) {
345             len = stream.read(buffer, ofs, buffer.length - ofs);
346             if (len == -1) {
347                 return ofs;
348             }
349             ofs += len;
350             if (ofs == buffer.length) {
351                 return ofs;
352             }
353         }
354     }
355
356     /**
357      ** Read the data from the reader and return the result in a char array.
358      ** Return null in case of an error.
359      **/

360     public static char[] read(Reader JavaDoc reader) throws IOException JavaDoc {
361         char[] chunk;
362         char[] all;
363         int len;
364         List JavaDoc chunks;
365         int i;
366         int last;
367
368         chunks = new ArrayList JavaDoc();
369         do {
370             chunk = new char[CHUNK];
371             chunks.add(chunk);
372             len = read(reader, chunk);
373         } while (len == CHUNK);
374         last = chunks.size() - 1;
375         all = new char[last * CHUNK + len];
376         for (i = 0; i <= last; i++) {
377             System.arraycopy(chunks.get(i), 0, all, CHUNK * i, (i == last)? len : CHUNK);
378         }
379         return all;
380     }
381
382
383
384     /**
385      ** Reads until EOF or the buffer is filled.
386      **
387      ** @return chars actually read; != buffer.length for EOF
388      **/

389     private static int read(Reader JavaDoc dest, char[] buffer) throws IOException JavaDoc {
390         int ofs;
391         int len;
392
393         for (ofs = 0; ofs < buffer.length; ofs += len) {
394             len = dest.read(buffer, ofs, buffer.length - ofs);
395             if (len == -1) {
396                 break;
397             }
398         }
399         return ofs;
400     }
401
402
403 }
404
Popular Tags