Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/idn/NFS4StringPrep.java
38840 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*******************************************************************************25* Copyright (C) 2003-2004, International Business Machines Corporation and *26* others. All Rights Reserved. *27*******************************************************************************28*/29import java.io.IOException;30import java.io.InputStream;31import java.io.UnsupportedEncodingException;32import java.text.ParseException;3334import sun.text.normalizer.ICUData;35import sun.net.idn.StringPrep;36import sun.text.normalizer.UCharacterIterator;3738/**39* @author ram40*41* This is a dumb implementation of NFS4 profiles. It is a direct port of42* C code, does not use Object Oriented principles. Quick and Dirty implementation43* for testing.44*/45public final class NFS4StringPrep {4647private StringPrep nfscss = null;48private StringPrep nfscsi = null;49private StringPrep nfscis = null;50private StringPrep nfsmxp = null;51private StringPrep nfsmxs = null;52//singleton instance53private static final NFS4StringPrep prep = new NFS4StringPrep();545556private NFS4StringPrep (){57ClassLoader loader = NFS4StringPrep.class.getClassLoader();58try{59InputStream nfscsiFile = loader.getResourceAsStream("nfscsi.spp");60nfscsi = new StringPrep(nfscsiFile);61nfscsiFile.close();6263InputStream nfscssFile = loader.getResourceAsStream("nfscss.spp");64nfscss = new StringPrep(nfscssFile);65nfscssFile.close();6667InputStream nfscisFile = loader.getResourceAsStream("nfscis.spp");68nfscis = new StringPrep(nfscisFile);69nfscisFile.close();7071InputStream nfsmxpFile = loader.getResourceAsStream("nfsmxp.spp");72nfsmxp = new StringPrep(nfsmxpFile);73nfsmxpFile.close();7475InputStream nfsmxsFile = loader.getResourceAsStream("nfsmxs.spp");76nfsmxs = new StringPrep(nfsmxsFile);77nfsmxsFile.close();78}catch(IOException e){79throw new RuntimeException(e.toString());80}81}8283private static byte[] prepare(byte[] src, StringPrep prep)84throws ParseException, UnsupportedEncodingException{85String s = new String(src, "UTF-8");86UCharacterIterator iter = UCharacterIterator.getInstance(s);87StringBuffer out = prep.prepare(iter,StringPrep.DEFAULT);88return out.toString().getBytes("UTF-8");89}9091public static byte[] cs_prepare(byte[] src, boolean isCaseSensitive)92throws ParseException, UnsupportedEncodingException{93if(isCaseSensitive == true ){94return prepare(src, prep.nfscss);95}else{96return prepare(src, prep.nfscsi);97}98}99100public static byte[] cis_prepare(byte[] src)101throws IOException, ParseException, UnsupportedEncodingException{102return prepare(src, prep.nfscis);103}104105/* sorted array for binary search*/106private static final String[] special_prefixes={107"ANONYMOUS",108"AUTHENTICATED",109"BATCH",110"DIALUP",111"EVERYONE",112"GROUP",113"INTERACTIVE",114"NETWORK",115"OWNER",116};117118119/* binary search the sorted array */120private static final int findStringIndex(String[] sortedArr,String target){121122int left, middle, right,rc;123124left =0;125right= sortedArr.length-1;126127while(left <= right){128middle = (left+right)/2;129rc= sortedArr[middle].compareTo(target);130131if(rc<0){132left = middle+1;133}else if(rc >0){134right = middle -1;135}else{136return middle;137}138}139return -1;140}141private static final char AT_SIGN = '@';142143public static byte[] mixed_prepare(byte[] src)144throws IOException, ParseException, UnsupportedEncodingException{145String s = new String(src, "UTF-8");146int index = s.indexOf(AT_SIGN);147StringBuffer out = new StringBuffer();148149if(index > -1){150/* special prefixes must not be followed by suffixes! */151String prefixString = s.substring(0,index);152int i= findStringIndex(special_prefixes, prefixString);153String suffixString = s.substring(index+1, s.length());154if(i>-1 && !suffixString.equals("")){155throw new ParseException("Suffix following a special index", -1);156}157UCharacterIterator prefix = UCharacterIterator.getInstance(prefixString);158UCharacterIterator suffix = UCharacterIterator.getInstance(suffixString);159out.append(prep.nfsmxp.prepare(prefix,StringPrep.DEFAULT));160out.append(AT_SIGN); // add the delimiter161out.append(prep.nfsmxs.prepare(suffix, StringPrep.DEFAULT));162}else{163UCharacterIterator iter = UCharacterIterator.getInstance(s);164out.append(prep.nfsmxp.prepare(iter,StringPrep.DEFAULT));165166}167return out.toString().getBytes("UTF-8");168}169170}171172173