KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > cookies > ShareableInputSource


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.xml.schema.cookies;
21
22 import org.xml.sax.InputSource JavaDoc;
23 import java.io.*;
24
25 /**
26  * Input source that can be sequentially shared including its steams.
27  * Use {@link #reset} before passing it to subsequent procesor. It
28  * is read only.
29  */

30 final class ShareableInputSource extends InputSource JavaDoc {
31
32     private ByteStream stream;
33     private CharacterStream reader;
34     private boolean initialized[] = new boolean[2];
35
36     private final InputSource JavaDoc peer;
37     private final int bufferSize;
38     private final static int BUFFER_SIZE = 256 * 1024 + 7; // #32939 keep big enough to avoid mark invalidation by wrapping BuffererReader
39
private IOException resetException;
40
41     public static ShareableInputSource create(InputSource JavaDoc peer) {
42         if (peer == null) throw new NullPointerException JavaDoc();
43         if (peer instanceof ShareableInputSource) {
44             return (ShareableInputSource) peer;
45         } else {
46             return new ShareableInputSource(peer, BUFFER_SIZE);
47         }
48     }
49
50     private ShareableInputSource(InputSource JavaDoc peer, int bufferSize) {
51         this.peer = peer;
52         this.bufferSize = bufferSize;
53     }
54
55     public InputStream getByteStream() {
56         InputStream in = peer.getByteStream();
57         if (initialized[1] == false && in != null) {
58            stream = new ByteStream(in , bufferSize);
59            stream.mark(bufferSize);
60            initialized[1] = true;
61         }
62         return stream;
63     }
64
65     public Reader getCharacterStream() {
66         Reader in = peer.getCharacterStream();
67         if (initialized[0] == false && in != null) {
68             reader = new CharacterStream(in, bufferSize/2);
69             initialized[0] = true;
70             try {
71                 reader.mark(bufferSize/2);
72             } catch (IOException ex) {
73                 resetException = ex;
74             }
75         }
76         return reader;
77     }
78
79     /**
80      * Prepate this instance for next parser
81      */

82     public void reset() throws IOException {
83         if (resetException != null) throw resetException;
84         if (initialized[1]) stream.reset();
85         if (initialized[0]) reader.reset();
86     }
87
88     /**
89      * Close shared streams
90      */

91     public void closeAll() throws IOException {
92         if (initialized[1]) stream.internalClose();
93         if (initialized[0]) reader.internalClose();
94     }
95     
96     public String JavaDoc getEncoding() {
97         return peer.getEncoding();
98     }
99
100     public String JavaDoc getSystemId() {
101         return peer.getSystemId();
102     }
103
104     public String JavaDoc getPublicId() {
105         return peer.getPublicId();
106     }
107     
108     private static class ByteStream extends BufferedInputStream {
109         public ByteStream(InputStream peer, int buffer) {
110             super(peer, buffer);
111         }
112         
113         public void close() throws IOException {
114             // nothing, we are shared
115
}
116         
117         private void internalClose() throws IOException {
118             super.close();
119         }
120     }
121     
122     private static class CharacterStream extends BufferedReader {
123         public CharacterStream(Reader peer, int buffer) {
124             super(peer, buffer);
125         }
126         
127         public void close() throws IOException {
128             // nothing, we are shared
129
}
130         
131         private void internalClose() throws IOException {
132             super.close();
133         }
134     }
135     
136 }
137
Popular Tags