1 package com.sun.org.apache.xml.internal.security.c14n.implementations; 2 3 import java.io.IOException; 4 import java.io.OutputStream; 5 import java.util.Map; 6 7 public class UtfHelpper { 8 9 final static void writeByte(final String str,final OutputStream out,Map<String,byte[]> cache) throws IOException { 10 byte []result= cache.get(str); 11 if (result==null) { 12 result=getStringInUtf8(str); 13 cache.put(str,result); 14 } 15 16 out.write(result); 17 18 } 19 20 final static void writeCharToUtf8(final char c,final OutputStream out) throws IOException{ 21 if (c < 0x80) { 22 out.write(c); 23 return; 24 } 25 if ((c >= 0xD800 && c <= 0xDBFF) || (c >= 0xDC00 && c <= 0xDFFF) ){ 26 //No Surrogates in sun java 27 out.write(0x3f); 28 return; 29 } 30 int bias; 31 int write; 32 char ch; 33 if (c > 0x07FF) { 34 ch=(char)(c>>>12); 35 write=0xE0; 36 if (ch>0) { 37 write |= ( ch & 0x0F); 38 } 39 out.write(write); 40 write=0x80; 41 bias=0x3F; 42 } else { 43 write=0xC0; 44 bias=0x1F; 45 } 46 ch=(char)(c>>>6); 47 if (ch>0) { 48 write|= (ch & bias); 49 } 50 out.write(write); 51 out.write(0x80 | ((c) & 0x3F)); 52 53 } 54 55 final static void writeStringToUtf8(final String str,final OutputStream out) throws IOException{ 56 final int length=str.length(); 57 int i=0; 58 char c; 59 while (i<length) { 60 c=str.charAt(i++); 61 if (c < 0x80) { 62 out.write(c); 63 continue; 64 } 65 if ((c >= 0xD800 && c <= 0xDBFF) || (c >= 0xDC00 && c <= 0xDFFF) ){ 66 //No Surrogates in sun java 67 out.write(0x3f); 68 continue; 69 } 70 char ch; 71 int bias; 72 int write; 73 if (c > 0x07FF) { 74 ch=(char)(c>>>12); 75 write=0xE0; 76 if (ch>0) { 77 write |= ( ch & 0x0F); 78 } 79 out.write(write); 80 write=0x80; 81 bias=0x3F; 82 } else { 83 write=0xC0; 84 bias=0x1F; 85 } 86 ch=(char)(c>>>6); 87 if (ch>0) { 88 write|= (ch & bias); 89 } 90 out.write(write); 91 out.write(0x80 | ((c) & 0x3F)); 92 93 } 94 95 } 96 public final static byte[] getStringInUtf8(final String str) { 97 final int length=str.length(); 98 boolean expanded=false; 99 byte []result=new byte[length]; 100 int i=0; 101 int out=0; 102 char c; 103 while (i<length) { 104 c=str.charAt(i++); 105 if ( c < 0x80 ) { 106 result[out++]=(byte)c; 107 continue; 108 } 109 if ((c >= 0xD800 && c <= 0xDBFF) || (c >= 0xDC00 && c <= 0xDFFF) ){ 110 //No Surrogates in sun java 111 result[out++]=0x3f; 112 113 continue; 114 } 115 if (!expanded) { 116 byte newResult[]=new byte[3*length]; 117 System.arraycopy(result, 0, newResult, 0, out); 118 result=newResult; 119 expanded=true; 120 } 121 char ch; 122 int bias; 123 byte write; 124 if (c > 0x07FF) { 125 ch=(char)(c>>>12); 126 write=(byte)0xE0; 127 if (ch>0) { 128 write |= ( ch & 0x0F); 129 } 130 result[out++]=write; 131 write=(byte)0x80; 132 bias=0x3F; 133 } else { 134 write=(byte)0xC0; 135 bias=0x1F; 136 } 137 ch=(char)(c>>>6); 138 if (ch>0) { 139 write|= (ch & bias); 140 } 141 result[out++]=write; 142 result[out++]=(byte)(0x80 | ((c) & 0x3F));/**/ 143 144 } 145 if (expanded) { 146 byte newResult[]=new byte[out]; 147 System.arraycopy(result, 0, newResult, 0, out); 148 result=newResult; 149 } 150 return result; 151 } 152 153 154 155 }