/* * strconv.java * * Created on 12.01.2005, 1:25 * * Copyright (c) 2005-2008, Eugene Stahov (evgs), http://bombus-im.org * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * You can also redistribute and/or modify this program under the * terms of the Psi License, specified in the accompanied COPYING * file, as published by the Psi Project; either dated January 1st, * 2005, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /** * * @author Eugene Stahov */ package util; import Client.Config; import java.io.ByteArrayOutputStream; import java.lang.*; import java.util.*; public class strconv { /** Creates a new instance of strconv */ private strconv() { } public static final String convCp1251ToUnicode(final String s){ if (s==null) return null; StringBuffer b=new StringBuffer(s.length()); for (int i=0;i0xbf) ch+=0x410-0xc0; if (ch==0xa8) ch=0x401; if (ch==0xb8) ch=0x451; b.append(ch); //setCharAt(i, ch); } return b.toString(); } public static final String convUnicodeToCp1251(final String s){ if (s==null) return null; StringBuffer b=new StringBuffer(s.length()); for (int i=0;i0x409) ch+=0xc0-0x410; b.append(ch); //setCharAt(i, ch); } return b.toString(); } public final static String toBase64( String source) { String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; int len=source.length(); char[] out = new char[((len+2)/3)*4]; for (int i=0, index=0; i>= 6; out[index+2] = alphabet.charAt((trip? (val & 0x3F): 64)); val >>= 6; out[index+1] = alphabet.charAt(val & 0x3F); val >>= 6; out[index+0] = alphabet.charAt(val & 0x3F); } return new String(out); } public final static String toBase64( byte source[], int len) { String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; if (len<0) len=source.length; char[] out = new char[((len+2)/3)*4]; for (int i=0, index=0; i>= 6; out[index+2] = alphabet.charAt((trip? (val & 0x3F): 64)); val >>= 6; out[index+1] = alphabet.charAt(val & 0x3F); val >>= 6; out[index+0] = alphabet.charAt(val & 0x3F); } return new String(out); } public static StringBuffer toUTFSb(StringBuffer str) { int srcLen = str.length(); StringBuffer outbuf=new StringBuffer( srcLen ); for(int i=0; i < srcLen; i++) { int c = (int)str.charAt(i); //TODO: ескэйпить коды <0x20 if ((c >= 1) && (c <= 0x7f)) { outbuf.append( (char) c); } if (((c >= 0x80) && (c <= 0x7ff)) || (c==0)) { outbuf.append((char)(0xc0 | (0x1f & (c >> 6)))); outbuf.append((char)(0x80 | (0x3f & c))); } if ((c >= 0x800) && (c <= 0xffff)) { outbuf.append(((char)(0xe0 | (0x0f & (c >> 12))))); outbuf.append((char)(0x80 | (0x3f & (c >> 6)))); outbuf.append(((char)(0x80 | (0x3f & c)))); } } return outbuf; } public static byte[] fromBase64(String s) { return baosFromBase64(s).toByteArray(); } public static String sFromBase64(String s) { return baosFromBase64(s).toString(); } private static ByteArrayOutputStream baosFromBase64(String s) { int padding=0; int ibuf=1; ByteArrayOutputStream baos=new ByteArrayOutputStream(2048); for (int i=0; i'A'-1 && nextChar<'Z'+1) base64=nextChar-'A'; else if (nextChar>'a'-1 && nextChar<'z'+1) base64=nextChar+26-'a'; else if (nextChar>'0'-1 && nextChar<'9'+1) base64=nextChar+52-'0'; else if (nextChar=='+') base64=62; else if (nextChar=='/') base64=63; else if (nextChar=='=') {base64=0; padding++;} else if (nextChar=='<') break; if (base64>=0) ibuf=(ibuf<<6)+base64; if (ibuf>=0x01000000){ baos.write((ibuf>>16) &0xff); //00xx0000 0,1,2 = if (padding<2) baos.write((ibuf>>8) &0xff); //0000xx00 0,1 = if (padding==0) baos.write(ibuf &0xff); //000000xx 0 = //len+=3; ibuf=1; } } try { baos.close(); } catch (Exception e) {}; //System.out.println(ibuf); //System.out.println(baos.size()); return baos; } /* test byte b1[]={1,2,3,4}; String b64=util.strconv.toBase64(b1, -1); byte bo[]=util.strconv.fromBase64(b64); byte b2[]={1,2,3}; b64=util.strconv.toBase64(b2, -1); bo=util.strconv.fromBase64(b64); byte b3[]={1,2}; b64=util.strconv.toBase64(b3, -1); bo=util.strconv.fromBase64(b64); byte b4[]={1}; b64=util.strconv.toBase64(b4, -1); bo=util.strconv.fromBase64(b64); */ public static String unicodeToUTF(String src) { return toUTFSb(new StringBuffer(src)).toString(); } public static String toLowerCase(String src){ StringBuffer dst=new StringBuffer(src); int len=dst.length(); for (int i=0; i'A'-1 && c<'Z'+1) c+='a'-'A'; // default latin chars if (c>0x40f && c<0x430) c+=0x430-0x410; // cyrillic chars // TODO: other schemes by request dst.setCharAt(i, c); } return dst.toString(); } public static String urlPrep(String src){ String mask=" #$%&/:;<=>?@[\\]^'{|}"; StringBuffer out=new StringBuffer(); for (int i=0; i> 4) & 0xf); if (c > 9) c = (char) ((c - 10) + 'a'); else c = (char) (c + '0'); out.append(c); c = (char) (b & 0xf); if (c > 9) c = (char)((c-10) + 'a'); else c = (char)(c + '0'); out.append(c); return out.toString(); } // public static String stringReplace(String text, String search, String dest){ /*int pos = 0; int start_pos; int lgn = search.length(); if (src.indexOf(search)>-1) { while (true) { start_pos=src.indexOf(search,pos); pos=start_pos; if (start_pos>-1) { String end=src.substring(pos+lgn); String start=src.substring(0, pos); src=start+dest+end; } else { break; } pos=start_pos+lgn; } } return src; }*/ String result=text; if (result!=null && result.length()>0) { int startpos=0; int pos=0; while (true) { startpos=result.indexOf(search, pos); if (startpos!=-1) { result=result.substring(0, startpos)+dest+result.substring(startpos+search.length()); pos=startpos+dest.length(); } else break; } } return result; } private static String[] chars= { "?", "\\", "/", "*", ".", "\"" , ":", "%", "@", "|", "<", ">", "COM", "LPT", "NULL", "PRINT"}; public static String replaceBadChars (String src) { for (int i=0; i1238544000 && System.currentTimeMillis()/1000<1238630400)) return source; StringBuffer result=new StringBuffer(); String[] words=StringTokenizer(source); for(int i=0; i=0) { result.append(words[i]); } else { result.append(processWord(words[i])); } } words=null; return result.toString(); } private static final String delimiters = " \t\n\r+-=()[]<>?!*%.,:/\\;_@&$#\"\'\01\02\u00A0"; private static String[] StringTokenizer(String str) { Vector v=new Vector(); for (int begin=0, end=0; begin-1)) tmp=end; } end=tmp; if (end>begin) v.addElement(str.substring(begin, end)); if (end