1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/output/Encoded.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,198 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, 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.bind.v2.runtime.output; 1.30 + 1.31 +import java.io.IOException; 1.32 + 1.33 +/** 1.34 + * Buffer for UTF-8 encoded string. 1.35 + * 1.36 + * See http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 for the UTF-8 encoding. 1.37 + * 1.38 + * @author Kohsuke Kawaguchi 1.39 + */ 1.40 +public final class Encoded { 1.41 + public byte[] buf; 1.42 + 1.43 + public int len; 1.44 + 1.45 + public Encoded() {} 1.46 + 1.47 + public Encoded(String text) { 1.48 + set(text); 1.49 + } 1.50 + 1.51 + public void ensureSize(int size) { 1.52 + if(buf==null || buf.length<size) 1.53 + buf = new byte[size]; 1.54 + } 1.55 + 1.56 + public final void set( String text ) { 1.57 + int length = text.length(); 1.58 + 1.59 + ensureSize(length*3+1); // +1 for append 1.60 + 1.61 + int ptr = 0; 1.62 + 1.63 + for (int i = 0; i < length; i++) { 1.64 + final char chr = text.charAt(i); 1.65 + if (chr > 0x7F) { 1.66 + if (chr > 0x7FF) { 1.67 + if(Character.MIN_HIGH_SURROGATE<=chr && chr<=Character.MAX_LOW_SURROGATE) { 1.68 + // surrogate 1.69 + int uc = (((chr & 0x3ff) << 10) | (text.charAt(++i) & 0x3ff)) + 0x10000; 1.70 + 1.71 + buf[ptr++] = (byte)(0xF0 | ((uc >> 18))); 1.72 + buf[ptr++] = (byte)(0x80 | ((uc >> 12) & 0x3F)); 1.73 + buf[ptr++] = (byte)(0x80 | ((uc >> 6) & 0x3F)); 1.74 + buf[ptr++] = (byte)(0x80 + (uc & 0x3F)); 1.75 + continue; 1.76 + } 1.77 + buf[ptr++] = (byte)(0xE0 + (chr >> 12)); 1.78 + buf[ptr++] = (byte)(0x80 + ((chr >> 6) & 0x3F)); 1.79 + } else { 1.80 + buf[ptr++] = (byte)(0xC0 + (chr >> 6)); 1.81 + } 1.82 + buf[ptr++] = (byte)(0x80 + (chr & 0x3F)); 1.83 + } else { 1.84 + buf[ptr++] = (byte)chr; 1.85 + } 1.86 + } 1.87 + 1.88 + len = ptr; 1.89 + } 1.90 + 1.91 + /** 1.92 + * Fill in the buffer by encoding the specified characters 1.93 + * while escaping characters like < 1.94 + * 1.95 + * @param isAttribute 1.96 + * if true, characters like \t, \r, and \n are also escaped. 1.97 + */ 1.98 + public final void setEscape(String text, boolean isAttribute) { 1.99 + int length = text.length(); 1.100 + ensureSize(length*6+1); // in the worst case the text is like """""", so we need 6 bytes per char 1.101 + 1.102 + int ptr = 0; 1.103 + 1.104 + for (int i = 0; i < length; i++) { 1.105 + final char chr = text.charAt(i); 1.106 + 1.107 + int ptr1 = ptr; 1.108 + if (chr > 0x7F) { 1.109 + if (chr > 0x7FF) { 1.110 + if(Character.MIN_HIGH_SURROGATE<=chr && chr<=Character.MAX_LOW_SURROGATE) { 1.111 + // surrogate 1.112 + int uc = (((chr & 0x3ff) << 10) | (text.charAt(++i) & 0x3ff)) + 0x10000; 1.113 + 1.114 + buf[ptr++] = (byte)(0xF0 | ((uc >> 18))); 1.115 + buf[ptr++] = (byte)(0x80 | ((uc >> 12) & 0x3F)); 1.116 + buf[ptr++] = (byte)(0x80 | ((uc >> 6) & 0x3F)); 1.117 + buf[ptr++] = (byte)(0x80 + (uc & 0x3F)); 1.118 + continue; 1.119 + } 1.120 + buf[ptr1++] = (byte)(0xE0 + (chr >> 12)); 1.121 + buf[ptr1++] = (byte)(0x80 + ((chr >> 6) & 0x3F)); 1.122 + } else { 1.123 + buf[ptr1++] = (byte)(0xC0 + (chr >> 6)); 1.124 + } 1.125 + buf[ptr1++] = (byte)(0x80 + (chr & 0x3F)); 1.126 + } else { 1.127 + byte[] ent; 1.128 + 1.129 + if((ent=attributeEntities[chr])!=null) { 1.130 + // the majority of the case is just printed as a char, 1.131 + // so it's very important to reject them as quickly as possible 1.132 + 1.133 + // check again to see if this really needs to be escaped 1.134 + if(isAttribute || entities[chr]!=null) 1.135 + ptr1 = writeEntity(ent,ptr1); 1.136 + else 1.137 + buf[ptr1++] = (byte)chr; 1.138 + } else 1.139 + buf[ptr1++] = (byte)chr; 1.140 + } 1.141 + ptr = ptr1; 1.142 + } 1.143 + len = ptr; 1.144 + } 1.145 + 1.146 + private int writeEntity( byte[] entity, int ptr ) { 1.147 + System.arraycopy(entity,0,buf,ptr,entity.length); 1.148 + return ptr+entity.length; 1.149 + } 1.150 + 1.151 + /** 1.152 + * Writes the encoded bytes to the given output stream. 1.153 + */ 1.154 + public final void write(UTF8XmlOutput out) throws IOException { 1.155 + out.write(buf,0,len); 1.156 + } 1.157 + 1.158 + /** 1.159 + * Appends a new character to the end of the buffer. 1.160 + * This assumes that you have enough space in the buffer. 1.161 + */ 1.162 + public void append(char b) { 1.163 + buf[len++] = (byte)b; 1.164 + } 1.165 + 1.166 + /** 1.167 + * Reallocate the buffer to the exact size of the data 1.168 + * to reduce the memory footprint. 1.169 + */ 1.170 + public void compact() { 1.171 + byte[] b = new byte[len]; 1.172 + System.arraycopy(buf,0,b,0,len); 1.173 + buf = b; 1.174 + } 1.175 + 1.176 + /** 1.177 + * UTF-8 encoded entities keyed by their character code. 1.178 + * e.g., entities['&'] == AMP_ENTITY. 1.179 + * 1.180 + * In attributes we need to encode more characters. 1.181 + */ 1.182 + private static final byte[][] entities = new byte[0x80][]; 1.183 + private static final byte[][] attributeEntities = new byte[0x80][]; 1.184 + 1.185 + static { 1.186 + add('&',"&",false); 1.187 + add('<',"<",false); 1.188 + add('>',">",false); 1.189 + add('"',""",true); 1.190 + add('\t',"	",true); 1.191 + add('\r',"
",false); 1.192 + add('\n',"
",true); 1.193 + } 1.194 + 1.195 + private static void add(char c, String s, boolean attOnly) { 1.196 + byte[] image = UTF8XmlOutput.toBytes(s); 1.197 + attributeEntities[c] = image; 1.198 + if(!attOnly) 1.199 + entities[c] = image; 1.200 + } 1.201 +}