KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > xml > 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.spi.xml.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     // #32939 keep the buffer big enough to be able to validate large XML documets
39
private final static int BUFFER_SIZE = 1024 * 1024 + 7;
40     private IOException resetException;
41
42     public static ShareableInputSource create(InputSource JavaDoc peer) {
43         if (peer == null) throw new NullPointerException JavaDoc();
44         if (peer instanceof ShareableInputSource) {
45             return (ShareableInputSource) peer;
46         } else {
47             return new ShareableInputSource(peer, BUFFER_SIZE);
48         }
49     }
50
51     private ShareableInputSource(InputSource JavaDoc peer, int bufferSize) {
52         this.peer = peer;
53         this.bufferSize = bufferSize;
54     }
55
56     public InputStream getByteStream() {
57         InputStream in = peer.getByteStream();
58         if (initialized[1] == false && in != null) {
59            stream = new ByteStream(in , bufferSize);
60            stream.mark(bufferSize);
61            initialized[1] = true;
62         }
63         return stream;
64     }
65
66     public Reader getCharacterStream() {
67         Reader in = peer.getCharacterStream();
68         if (initialized[0] == false && in != null) {
69             reader = new CharacterStream(in, bufferSize/2);
70             initialized[0] = true;
71             try {
72                 reader.mark(bufferSize/2);
73             } catch (IOException ex) {
74                 resetException = ex;
75             }
76         }
77         return reader;
78     }
79
80     /**
81      * Prepate this instance for next parser
82      */

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

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