src/share/jaxws_classes/com/sun/xml/internal/stream/buffer/MutableXMLStreamBuffer.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/stream/buffer/MutableXMLStreamBuffer.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,247 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.stream.buffer;
    1.30 +
    1.31 +import com.sun.xml.internal.stream.buffer.sax.Properties;
    1.32 +import com.sun.xml.internal.stream.buffer.sax.SAXBufferCreator;
    1.33 +import com.sun.xml.internal.stream.buffer.stax.StreamReaderBufferCreator;
    1.34 +import com.sun.xml.internal.stream.buffer.stax.StreamWriterBufferCreator;
    1.35 +import org.xml.sax.ContentHandler;
    1.36 +import org.xml.sax.SAXException;
    1.37 +import org.xml.sax.XMLReader;
    1.38 +
    1.39 +import javax.xml.stream.XMLStreamException;
    1.40 +import javax.xml.stream.XMLStreamReader;
    1.41 +import javax.xml.stream.XMLStreamWriter;
    1.42 +import java.io.IOException;
    1.43 +import java.io.InputStream;
    1.44 +
    1.45 +/**
    1.46 + *
    1.47 + * A mutable stream-based buffer of an XML infoset.
    1.48 + *
    1.49 + * <p>
    1.50 + * A MutableXMLStreamBuffer is created using specific SAX and StAX-based
    1.51 + * creators. Utility methods on MutableXMLStreamBuffer are provided for
    1.52 + * such functionality that utilize SAX and StAX-based creators.
    1.53 + *
    1.54 + * <p>
    1.55 + * Once instantiated the same instance of a MutableXMLStreamBuffer may be reused for
    1.56 + * creation to reduce the amount of Objects instantiated and garbage
    1.57 + * collected that are required for internally representing an XML infoset.
    1.58 + *
    1.59 + * <p>
    1.60 + * A MutableXMLStreamBuffer is not designed to be created and processed
    1.61 + * concurrently. If done so unspecified behaviour may occur.
    1.62 + */
    1.63 +public class MutableXMLStreamBuffer extends XMLStreamBuffer {
    1.64 +    /**
    1.65 +     * The default array size for the arrays used in internal representation
    1.66 +     * of the XML infoset.
    1.67 +     */
    1.68 +    public static final int DEFAULT_ARRAY_SIZE = 512;
    1.69 +
    1.70 +    /**
    1.71 +     * Create a new MutableXMLStreamBuffer using the
    1.72 +     * {@link MutableXMLStreamBuffer#DEFAULT_ARRAY_SIZE}.
    1.73 +     */
    1.74 +    public MutableXMLStreamBuffer() {
    1.75 +        this(DEFAULT_ARRAY_SIZE);
    1.76 +    }
    1.77 +
    1.78 +    /**
    1.79 +     * Set the system identifier for this buffer.
    1.80 +     * @param systemId The system identifier.
    1.81 +     */
    1.82 +    public void setSystemId(String systemId) {
    1.83 +        this.systemId = systemId;
    1.84 +    }
    1.85 +
    1.86 +    /**
    1.87 +     * Create a new MutableXMLStreamBuffer.
    1.88 +     *
    1.89 +     * @param size
    1.90 +     * The size of the arrays used in the internal representation
    1.91 +     * of the XML infoset.
    1.92 +     * @throws NegativeArraySizeException
    1.93 +     * If the <code>size</code> argument is less than <code>0</code>.
    1.94 +     */
    1.95 +    public MutableXMLStreamBuffer(int size) {
    1.96 +        _structure = new FragmentedArray<byte[]>(new byte[size]);
    1.97 +        _structureStrings = new FragmentedArray<String[]>(new String[size]);
    1.98 +        _contentCharactersBuffer = new FragmentedArray<char[]>(new char[4096]);
    1.99 +        _contentObjects = new FragmentedArray<Object[]>(new Object[size]);
   1.100 +
   1.101 +        // Set the first element of structure array to indicate an empty buffer
   1.102 +        // that has not been created
   1.103 +        _structure.getArray()[0] = (byte) AbstractCreatorProcessor.T_END;
   1.104 +    }
   1.105 +
   1.106 +    /**
   1.107 +     * Create contents of a buffer from a XMLStreamReader.
   1.108 +     *
   1.109 +     * <p>
   1.110 +     * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation.
   1.111 +     *
   1.112 +     * <p>
   1.113 +     * The MutableXMLStreamBuffer is created by consuming the events on the XMLStreamReader using
   1.114 +     * an instance of {@link StreamReaderBufferCreator}.
   1.115 +     *
   1.116 +     * @param reader
   1.117 +     * A XMLStreamReader to read from to create.
   1.118 +     */
   1.119 +    public void createFromXMLStreamReader(XMLStreamReader reader) throws XMLStreamException {
   1.120 +        reset();
   1.121 +        StreamReaderBufferCreator c = new StreamReaderBufferCreator(this);
   1.122 +        c.create(reader);
   1.123 +    }
   1.124 +
   1.125 +    /**
   1.126 +     * Create contents of a buffer from a XMLStreamWriter.
   1.127 +     *
   1.128 +     * <p>
   1.129 +     * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation.
   1.130 +     *
   1.131 +     * <p>
   1.132 +     * The MutableXMLStreamBuffer is created by consuming events on a XMLStreamWriter using
   1.133 +     * an instance of {@link StreamWriterBufferCreator}.
   1.134 +     */
   1.135 +    public XMLStreamWriter createFromXMLStreamWriter() {
   1.136 +        reset();
   1.137 +        return new StreamWriterBufferCreator(this);
   1.138 +    }
   1.139 +
   1.140 +    /**
   1.141 +     * Create contents of a buffer from a {@link SAXBufferCreator}.
   1.142 +     *
   1.143 +     * <p>
   1.144 +     * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation.
   1.145 +     *
   1.146 +     * <p>
   1.147 +     * The MutableXMLStreamBuffer is created by consuming events from a {@link ContentHandler} using
   1.148 +     * an instance of {@link SAXBufferCreator}.
   1.149 +     *
   1.150 +     * @return The {@link SAXBufferCreator} to create from.
   1.151 +     */
   1.152 +    public SAXBufferCreator createFromSAXBufferCreator() {
   1.153 +        reset();
   1.154 +        SAXBufferCreator c = new SAXBufferCreator();
   1.155 +        c.setBuffer(this);
   1.156 +        return c;
   1.157 +    }
   1.158 +
   1.159 +    /**
   1.160 +     * Create contents of a buffer from a {@link XMLReader} and {@link InputStream}.
   1.161 +     *
   1.162 +     * <p>
   1.163 +     * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation.
   1.164 +     *
   1.165 +     * <p>
   1.166 +     * The MutableXMLStreamBuffer is created by using an instance of {@link SAXBufferCreator}
   1.167 +     * and registering associated handlers on the {@link XMLReader}.
   1.168 +     *
   1.169 +     * @param reader
   1.170 +     * The {@link XMLReader} to use for parsing.
   1.171 +     * @param in
   1.172 +     * The {@link InputStream} to be parsed.
   1.173 +     */
   1.174 +    public void createFromXMLReader(XMLReader reader, InputStream in) throws SAXException, IOException {
   1.175 +        createFromXMLReader(reader, in, null);
   1.176 +    }
   1.177 +
   1.178 +    /**
   1.179 +     * Create contents of a buffer from a {@link XMLReader} and {@link InputStream}.
   1.180 +     *
   1.181 +     * <p>
   1.182 +     * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation.
   1.183 +     *
   1.184 +     * <p>
   1.185 +     * The MutableXMLStreamBuffer is created by using an instance of {@link SAXBufferCreator}
   1.186 +     * and registering associated handlers on the {@link XMLReader}.
   1.187 +     *
   1.188 +     * @param reader
   1.189 +     * The {@link XMLReader} to use for parsing.
   1.190 +     * @param in
   1.191 +     * The {@link InputStream} to be parsed.
   1.192 +     * @param systemId
   1.193 +     * The system ID of the input stream.
   1.194 +     */
   1.195 +    public void createFromXMLReader(XMLReader reader, InputStream in, String systemId) throws SAXException, IOException {
   1.196 +        reset();
   1.197 +        SAXBufferCreator c = new SAXBufferCreator(this);
   1.198 +
   1.199 +        reader.setContentHandler(c);
   1.200 +        reader.setDTDHandler(c);
   1.201 +        reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, c);
   1.202 +
   1.203 +        c.create(reader, in, systemId);
   1.204 +    }
   1.205 +
   1.206 +    /**
   1.207 +     * Reset the MutableXMLStreamBuffer.
   1.208 +     *
   1.209 +     * <p>
   1.210 +     * This method will reset the MutableXMLStreamBuffer to a state of being "uncreated"
   1.211 +     * similar to the state of a newly instantiated MutableXMLStreamBuffer.
   1.212 +     *
   1.213 +     * <p>
   1.214 +     * As many Objects as possible will be retained for reuse in future creation.
   1.215 +     */
   1.216 +    public void reset() {
   1.217 +        // Reset the ptrs in arrays to 0
   1.218 +        _structurePtr =
   1.219 +                _structureStringsPtr =
   1.220 +                _contentCharactersBufferPtr =
   1.221 +                _contentObjectsPtr = 0;
   1.222 +
   1.223 +        // Set the first element of structure array to indicate an empty buffer
   1.224 +        // that has not been created
   1.225 +        _structure.getArray()[0] = (byte)AbstractCreatorProcessor.T_END;
   1.226 +
   1.227 +        // Clean up content objects
   1.228 +        _contentObjects.setNext(null);
   1.229 +        final Object[] o = _contentObjects.getArray();
   1.230 +        for (int i = 0; i < o.length; i++) {
   1.231 +            if (o[i] != null) {
   1.232 +                o[i] = null;
   1.233 +            } else {
   1.234 +                break;
   1.235 +            }
   1.236 +        }
   1.237 +
   1.238 +        treeCount = 0;
   1.239 +
   1.240 +        /*
   1.241 +         * TODO consider truncating the size of _structureStrings and
   1.242 +         * _contentCharactersBuffer to limit the memory used by the buffer
   1.243 +         */
   1.244 +    }
   1.245 +
   1.246 +
   1.247 +    protected void setHasInternedStrings(boolean hasInternedStrings) {
   1.248 +        _hasInternedStrings = hasInternedStrings;
   1.249 +    }
   1.250 +}

mercurial