001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.spi.xml.cookies;
043:
044: import org.xml.sax.InputSource;
045: import java.io.*;
046:
047: /**
048: * Input source that can be sequentially shared including its steams.
049: * Use {@link #reset} before passing it to subsequent procesor. It
050: * is read only.
051: */
052: final class ShareableInputSource extends InputSource {
053:
054: private ByteStream stream;
055: private CharacterStream reader;
056: private boolean initialized[] = new boolean[2];
057:
058: private final InputSource peer;
059: private final int bufferSize;
060: // #32939 keep the buffer big enough to be able to validate large XML documets
061: private final static int BUFFER_SIZE = 1024 * 1024 + 7;
062: private IOException resetException;
063:
064: public static ShareableInputSource create(InputSource peer) {
065: if (peer == null)
066: throw new NullPointerException();
067: if (peer instanceof ShareableInputSource) {
068: return (ShareableInputSource) peer;
069: } else {
070: return new ShareableInputSource(peer, BUFFER_SIZE);
071: }
072: }
073:
074: private ShareableInputSource(InputSource peer, int bufferSize) {
075: this .peer = peer;
076: this .bufferSize = bufferSize;
077: }
078:
079: public InputStream getByteStream() {
080: InputStream in = peer.getByteStream();
081: if (initialized[1] == false && in != null) {
082: stream = new ByteStream(in, bufferSize);
083: stream.mark(bufferSize);
084: initialized[1] = true;
085: }
086: return stream;
087: }
088:
089: public Reader getCharacterStream() {
090: Reader in = peer.getCharacterStream();
091: if (initialized[0] == false && in != null) {
092: reader = new CharacterStream(in, bufferSize / 2);
093: initialized[0] = true;
094: try {
095: reader.mark(bufferSize / 2);
096: } catch (IOException ex) {
097: resetException = ex;
098: }
099: }
100: return reader;
101: }
102:
103: /**
104: * Prepate this instance for next parser
105: */
106: public void reset() throws IOException {
107: if (resetException != null)
108: throw resetException;
109: if (initialized[1])
110: stream.reset();
111: if (initialized[0])
112: reader.reset();
113: }
114:
115: /**
116: * Close shared streams
117: */
118: public void closeAll() throws IOException {
119: if (initialized[1])
120: stream.internalClose();
121: if (initialized[0])
122: reader.internalClose();
123: }
124:
125: public String getEncoding() {
126: return peer.getEncoding();
127: }
128:
129: public String getSystemId() {
130: return peer.getSystemId();
131: }
132:
133: public String getPublicId() {
134: return peer.getPublicId();
135: }
136:
137: private static class ByteStream extends BufferedInputStream {
138: public ByteStream(InputStream peer, int buffer) {
139: super (peer, buffer);
140: }
141:
142: public void close() throws IOException {
143: // nothing, we are shared
144: }
145:
146: private void internalClose() throws IOException {
147: super .close();
148: }
149: }
150:
151: private static class CharacterStream extends BufferedReader {
152: public CharacterStream(Reader peer, int buffer) {
153: super (peer, buffer);
154: }
155:
156: public void close() throws IOException {
157: // nothing, we are shared
158: }
159:
160: private void internalClose() throws IOException {
161: super.close();
162: }
163: }
164:
165: }
|