< prev index next >

src/java.xml.bind/share/classes/com/sun/xml/internal/bind/v2/runtime/output/FastInfosetStreamWriterOutput.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  25 
  26 package com.sun.xml.internal.bind.v2.runtime.output;
  27 
  28 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
  29 import com.sun.xml.internal.bind.v2.runtime.Name;
  30 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
  31 import javax.xml.stream.XMLStreamException;
  32 
  33 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data;
  34 import com.sun.xml.internal.fastinfoset.EncodingConstants;
  35 import com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer;
  36 import java.io.IOException;
  37 import java.util.Collection;
  38 import java.util.Map;
  39 import java.util.WeakHashMap;
  40 import javax.xml.bind.JAXBContext;
  41 import com.sun.xml.internal.org.jvnet.fastinfoset.VocabularyApplicationData;
  42 import org.xml.sax.SAXException;
  43 
  44 /**
  45  * {@link XmlOutput} for {@link LowLevelStAXDocumentSerializer}.
  46  * <p>
  47  * This class is responsible for managing the indexing of elements, attributes
  48  * and local names that are known to JAXB by way of the JAXBContext (generated
  49  * from JAXB beans or schema). The pre-encoded UTF-8 representations of known
  50  * local names are also utilized.
  51  * <p>
  52  * The lookup of  elements, attributes and local names with respect to a context
  53  * is very efficient. It relies on an incrementing base line so that look up is
  54  * performed in O(1) time and only uses static memory. When the base line reaches
  55  * a point where integer overflow will occur the arrays and base line are reset
  56  * (such an event is rare and will have little impact on performance).
  57  * <p>
  58  * A weak map of JAXB contexts to optimized tables for attributes, elements and
  59  * local names is utilized and stored on the LowLevel StAX serializer. Thus,
  60  * optimized serializing can work other multiple serializing of JAXB beans using
  61  * the same LowLevel StAX serializer instance. This approach works best when JAXB
  62  * contexts are only created once per schema or JAXB beans (which is the recommended
  63  * practice as the creation JAXB contexts are expensive, they are thread safe and
  64  * can be reused).
  65  *


  82         final int[] localNameIndexes;
  83 
  84         /**
  85          * The offset of the index
  86          */
  87         int indexOffset;
  88 
  89         /**
  90          * The the maximum known value of an index
  91          */
  92         int maxIndex;
  93 
  94         /**
  95          * True if the tables require clearing
  96          */
  97         boolean requiresClear;
  98 
  99         /**
 100          * Create a new set of tables for a JAXB context.
 101          * <p>
 102          * @param content the JAXB context.
 103          * @param initialIndexOffset the initial index offset to calculate
 104          *                           the maximum possible index
 105          *
 106          */
 107         TablesPerJAXBContext(JAXBContextImpl context, int initialIndexOffset) {
 108             elementIndexes = new int[context.getNumberOfElementNames()];
 109             elementIndexPrefixes = new int[context.getNumberOfElementNames()];
 110             attributeIndexes = new int[context.getNumberOfAttributeNames()];
 111             localNameIndexes = new int[context.getNumberOfLocalNames()];
 112 
 113             indexOffset = 1;
 114             maxIndex = initialIndexOffset + elementIndexes.length + attributeIndexes.length;
 115         }
 116 
 117         /**
 118          * Require that tables are cleared.
 119          */
 120         public void requireClearTables() {
 121             requiresClear = true;
 122         }
 123 
 124         /**
 125          * Clear or reset the tables.
 126          * <p>
 127          * @param initialIndexOffset the initial index offset to calculate
 128          *                           the maximum possible index
 129          */
 130         public void clearOrResetTables(int intialIndexOffset) {
 131             if (requiresClear) {
 132                 requiresClear = false;
 133 
 134                 // Increment offset to new position
 135                 indexOffset += maxIndex;
 136                 // Reset the maximum known value of an index
 137                 maxIndex = intialIndexOffset + elementIndexes.length + attributeIndexes.length;
 138                 // Check if there is enough free space
 139                 // If overflow
 140                 if ((indexOffset + maxIndex) < 0) {
 141                     clearAll();
 142                 }
 143             } else {
 144                 // Reset the maximum known value of an index
 145                 maxIndex = intialIndexOffset + elementIndexes.length + attributeIndexes.length;
 146                 // Check if there is enough free space
 147                 // If overflow


 185             clear(localNameIndexes);
 186             indexOffset = 1;
 187         }
 188 
 189         private void reset(int[] array) {
 190             for (int i = 0; i < array.length; i++) {
 191                 if (array[i] > indexOffset) {
 192                     array[i] = array[i] - indexOffset + 1;
 193                 } else {
 194                     array[i] = 0;
 195                 }
 196             }
 197         }
 198 
 199     }
 200 
 201     /**
 202      * Holder of JAXB contexts -> tables.
 203      * <p>
 204      * An instance will be registered with the
 205      * {@link LowLevelStAXDocumentSerializer}.
 206      */
 207     final static class AppData implements VocabularyApplicationData {
 208         final Map<JAXBContext, TablesPerJAXBContext> contexts =
 209                 new WeakHashMap<JAXBContext, TablesPerJAXBContext>();
 210         final Collection<TablesPerJAXBContext> collectionOfContexts = contexts.values();
 211 
 212         /**
 213          * Clear all the tables.
 214          */
 215         public void clear() {
 216             for(TablesPerJAXBContext c : collectionOfContexts)
 217                 c.requireClearTables();
 218         }
 219     }
 220 
 221     public FastInfosetStreamWriterOutput(StAXDocumentSerializer out,
 222             JAXBContextImpl context) {
 223         super(out);
 224 
 225         this.fiout = out;


   1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  25 
  26 package com.sun.xml.internal.bind.v2.runtime.output;
  27 
  28 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
  29 import com.sun.xml.internal.bind.v2.runtime.Name;
  30 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
  31 import javax.xml.stream.XMLStreamException;
  32 
  33 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data;
  34 import com.sun.xml.internal.fastinfoset.EncodingConstants;
  35 import com.sun.xml.internal.fastinfoset.stax.StAXDocumentSerializer;
  36 import java.io.IOException;
  37 import java.util.Collection;
  38 import java.util.Map;
  39 import java.util.WeakHashMap;
  40 import javax.xml.bind.JAXBContext;
  41 import com.sun.xml.internal.org.jvnet.fastinfoset.VocabularyApplicationData;
  42 import org.xml.sax.SAXException;
  43 
  44 /**
  45  * {@link XmlOutput} for {@link StAXDocumentSerializer}.
  46  * <p>
  47  * This class is responsible for managing the indexing of elements, attributes
  48  * and local names that are known to JAXB by way of the JAXBContext (generated
  49  * from JAXB beans or schema). The pre-encoded UTF-8 representations of known
  50  * local names are also utilized.
  51  * <p>
  52  * The lookup of  elements, attributes and local names with respect to a context
  53  * is very efficient. It relies on an incrementing base line so that look up is
  54  * performed in O(1) time and only uses static memory. When the base line reaches
  55  * a point where integer overflow will occur the arrays and base line are reset
  56  * (such an event is rare and will have little impact on performance).
  57  * <p>
  58  * A weak map of JAXB contexts to optimized tables for attributes, elements and
  59  * local names is utilized and stored on the LowLevel StAX serializer. Thus,
  60  * optimized serializing can work other multiple serializing of JAXB beans using
  61  * the same LowLevel StAX serializer instance. This approach works best when JAXB
  62  * contexts are only created once per schema or JAXB beans (which is the recommended
  63  * practice as the creation JAXB contexts are expensive, they are thread safe and
  64  * can be reused).
  65  *


  82         final int[] localNameIndexes;
  83 
  84         /**
  85          * The offset of the index
  86          */
  87         int indexOffset;
  88 
  89         /**
  90          * The the maximum known value of an index
  91          */
  92         int maxIndex;
  93 
  94         /**
  95          * True if the tables require clearing
  96          */
  97         boolean requiresClear;
  98 
  99         /**
 100          * Create a new set of tables for a JAXB context.
 101          * <p>
 102          * @param context the JAXB context.
 103          * @param initialIndexOffset the initial index offset to calculate
 104          *                           the maximum possible index
 105          *
 106          */
 107         TablesPerJAXBContext(JAXBContextImpl context, int initialIndexOffset) {
 108             elementIndexes = new int[context.getNumberOfElementNames()];
 109             elementIndexPrefixes = new int[context.getNumberOfElementNames()];
 110             attributeIndexes = new int[context.getNumberOfAttributeNames()];
 111             localNameIndexes = new int[context.getNumberOfLocalNames()];
 112 
 113             indexOffset = 1;
 114             maxIndex = initialIndexOffset + elementIndexes.length + attributeIndexes.length;
 115         }
 116 
 117         /**
 118          * Require that tables are cleared.
 119          */
 120         public void requireClearTables() {
 121             requiresClear = true;
 122         }
 123 
 124         /**
 125          * Clear or reset the tables.
 126          * <p>
 127          * @param intialIndexOffset the initial index offset to calculate
 128          *                           the maximum possible index
 129          */
 130         public void clearOrResetTables(int intialIndexOffset) {
 131             if (requiresClear) {
 132                 requiresClear = false;
 133 
 134                 // Increment offset to new position
 135                 indexOffset += maxIndex;
 136                 // Reset the maximum known value of an index
 137                 maxIndex = intialIndexOffset + elementIndexes.length + attributeIndexes.length;
 138                 // Check if there is enough free space
 139                 // If overflow
 140                 if ((indexOffset + maxIndex) < 0) {
 141                     clearAll();
 142                 }
 143             } else {
 144                 // Reset the maximum known value of an index
 145                 maxIndex = intialIndexOffset + elementIndexes.length + attributeIndexes.length;
 146                 // Check if there is enough free space
 147                 // If overflow


 185             clear(localNameIndexes);
 186             indexOffset = 1;
 187         }
 188 
 189         private void reset(int[] array) {
 190             for (int i = 0; i < array.length; i++) {
 191                 if (array[i] > indexOffset) {
 192                     array[i] = array[i] - indexOffset + 1;
 193                 } else {
 194                     array[i] = 0;
 195                 }
 196             }
 197         }
 198 
 199     }
 200 
 201     /**
 202      * Holder of JAXB contexts -> tables.
 203      * <p>
 204      * An instance will be registered with the
 205      * {@link StAXDocumentSerializer}.
 206      */
 207     final static class AppData implements VocabularyApplicationData {
 208         final Map<JAXBContext, TablesPerJAXBContext> contexts =
 209                 new WeakHashMap<JAXBContext, TablesPerJAXBContext>();
 210         final Collection<TablesPerJAXBContext> collectionOfContexts = contexts.values();
 211 
 212         /**
 213          * Clear all the tables.
 214          */
 215         public void clear() {
 216             for(TablesPerJAXBContext c : collectionOfContexts)
 217                 c.requireClearTables();
 218         }
 219     }
 220 
 221     public FastInfosetStreamWriterOutput(StAXDocumentSerializer out,
 222             JAXBContextImpl context) {
 223         super(out);
 224 
 225         this.fiout = out;


< prev index next >