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 23 * questions. 24 */ 25 26 package com.sun.xml.internal.bind.v2.runtime.unmarshaller; 27 28 import java.io.IOException; 29 30 import com.sun.xml.internal.bind.v2.runtime.output.Pcdata; 31 import com.sun.xml.internal.bind.v2.runtime.output.UTF8XmlOutput; 32 33 /** 34 * Typed {@link CharSequence} for int[]. 35 * 36 * <p> 37 * Fed to unmarshaller when the 'text' data is actually 38 * a virtual image of int array. 39 * 40 * <p> 41 * This class holds int[] as a triplet of (data,start,len) 42 * where 'start' and 'len' represents the start position of the 43 * data and the length. 44 * 45 * @author Kohsuke Kawaguchi 46 */ 47 public final class IntArrayData extends Pcdata { 48 49 private int[] data; 50 private int start; 51 private int len; 52 53 /** 54 * String representation of the data. Lazily computed. 55 */ 56 private StringBuilder literal; 57 58 59 public IntArrayData(int[] data, int start, int len) { 60 set(data, start, len); 61 } 62 63 public IntArrayData() { 64 } 65 66 /** 67 * Sets the int[] data to this object. 68 * 69 * <p> 70 * This method doesn't make a copy for a performance reason. 71 * The caller is still free to modify the array it passed to this method, 72 * but he should do so with a care. The unmarshalling code isn't expecting 73 * the value to be changed while it's being routed. 74 */ 75 public void set(int[] data, int start, int len) { 76 this.data = data; 77 this.start = start; 78 this.len = len; 79 this.literal = null; 80 } 81 82 public int length() { 83 return getLiteral().length(); 84 } 85 86 public char charAt(int index) { 87 return getLiteral().charAt(index); 88 } 89 90 public CharSequence subSequence(int start, int end) { 91 return getLiteral().subSequence(start,end); 92 } 93 94 /** 95 * Computes the literal form from the data. 96 */ 97 private StringBuilder getLiteral() { 98 if(literal!=null) return literal; 99 100 literal = new StringBuilder(); 101 int p = start; 102 for( int i=len; i>0; i-- ) { 103 if(literal.length()>0) literal.append(' '); 104 literal.append(data[p++]); 105 } 106 107 return literal; 108 } 109 110 public String toString() { 111 return literal.toString(); 112 } 113 114 public void writeTo(UTF8XmlOutput output) throws IOException { 115 int p = start; 116 for( int i=len; i>0; i-- ) { 117 if(i!=len) 118 output.write(' '); 119 output.text(data[p++]); 120 } 121 } 122 }