KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > guards > GuardedWriter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.guards;
21
22 import java.io.CharArrayWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.util.List JavaDoc;
29 import org.netbeans.api.editor.guards.GuardedSection;
30 import org.netbeans.spi.editor.guards.support.AbstractGuardedSectionsProvider;
31
32 /** This stream is able to insert special guarded comments.
33 */

34 final class GuardedWriter extends Writer JavaDoc {
35     /** Encapsulated writer. */
36     private Writer JavaDoc writer;
37
38     private CharArrayWriter JavaDoc buffer;
39
40     private final AbstractGuardedSectionsProvider gw;
41
42     private boolean isClosed = false;
43
44     private final List JavaDoc<GuardedSection> sections;
45
46     
47     /** Creates new GuardedWriter.
48     * @param os Encapsulated output stream.
49     * @param list The list of the guarded sections.
50     */

51     public GuardedWriter(AbstractGuardedSectionsProvider gw, OutputStream JavaDoc os, List JavaDoc<GuardedSection> list, String JavaDoc encoding) throws UnsupportedEncodingException JavaDoc {
52         if (encoding == null)
53             writer = new OutputStreamWriter JavaDoc(os);
54         else
55             writer = new OutputStreamWriter JavaDoc(os, encoding);
56         this.gw = gw;
57         sections = list;
58     }
59
60     /** Writes chars to underlying writer */
61     public void write(char[] cbuf, int off, int len) throws IOException JavaDoc {
62         
63         if (buffer == null) {
64             buffer = new CharArrayWriter JavaDoc(10240);
65         }
66         
67         buffer.write(cbuf, off, len);
68     }
69
70     /** Calls underlying writer flush */
71     public void close() throws IOException JavaDoc {
72         if (isClosed) {
73             return;
74         }
75         isClosed = true;
76         if (buffer != null) {
77             char[] content = this.gw.writeSections(sections, buffer.toCharArray());
78             writer.write(content);
79         }
80         writer.close();
81     }
82
83     /** Calls underlying writer flush */
84     public void flush() throws IOException JavaDoc {
85     }
86     
87 }
88
Popular Tags