⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typeutil.java

📁 jetty SERVER連接資料庫用的軟體
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /** Convert int to String using cache.      */    public static String toString(int i)    {        if (i>=0 && i<intCacheSize)        {            if (integerStrCache[i]==null)                integerStrCache[i]=Integer.toString(i);            return integerStrCache[i];        }        else if (i==-1)            return "-1";        return Integer.toString(i);    }        /* ------------------------------------------------------------ */    /** Convert long to String using cache.      */    public static String toString(long i)    {        if (i>=0 && i<intCacheSize)        {            if (integerStrCache[(int)i]==null)                integerStrCache[(int)i]=Long.toString(i);            return integerStrCache[(int)i];        }        else if (i==-1)            return "-1";        return Long.toString(i);    }    /* ------------------------------------------------------------ */    /** Parse an int from a substring.     * Negative numbers are not handled.     * @param s String     * @param offset Offset within string     * @param length Length of integer or -1 for remainder of string     * @param base base of the integer     * @exception NumberFormatException      */    public static int parseInt(String s, int offset, int length, int base)        throws NumberFormatException    {        int value=0;        if (length<0)            length=s.length()-offset;        for (int i=0;i<length;i++)        {            char c=s.charAt(offset+i);                        int digit=c-'0';            if (digit<0 || digit>=base || digit>=10)            {                digit=10+c-'A';                if (digit<10 || digit>=base)                    digit=10+c-'a';            }            if (digit<0 || digit>=base)                throw new NumberFormatException(s.substring(offset,offset+length));            value=value*base+digit;        }        return value;    }    /* ------------------------------------------------------------ */    /** Parse an int from a byte array of ascii characters.     * Negative numbers are not handled.     * @param b byte array     * @param offset Offset within string     * @param length Length of integer or -1 for remainder of string     * @param base base of the integer     * @exception NumberFormatException      */    public static int parseInt(byte[] b, int offset, int length, int base)        throws NumberFormatException    {        int value=0;        if (length<0)            length=b.length-offset;        for (int i=0;i<length;i++)        {            char c=(char)(0xff&b[offset+i]);                        int digit=c-'0';            if (digit<0 || digit>=base || digit>=10)            {                digit=10+c-'A';                if (digit<10 || digit>=base)                    digit=10+c-'a';            }            if (digit<0 || digit>=base)                throw new NumberFormatException(new String(b,offset,length));            value=value*base+digit;        }        return value;    }    /* ------------------------------------------------------------ */    public static byte[] parseBytes(String s, int base)    {        byte[] bytes=new byte[s.length()/2];        for (int i=0;i<s.length();i+=2)            bytes[i/2]=(byte)TypeUtil.parseInt(s,i,2,base);        return bytes;    }    /* ------------------------------------------------------------ */    public static String toString(byte[] bytes, int base)    {        StringBuffer buf = new StringBuffer();        for (int i=0;i<bytes.length;i++)        {            int bi=0xff&bytes[i];            int c='0'+(bi/base)%base;            if (c>'9')                c= 'a'+(c-'0'-10);            buf.append((char)c);            c='0'+bi%base;            if (c>'9')                c= 'a'+(c-'0'-10);            buf.append((char)c);        }        return buf.toString();    }    /* ------------------------------------------------------------ */    /**      * @param b An ASCII encoded character 0-9 a-f A-F     * @return The byte value of the character 0-16.     */    public static byte convertHexDigit( byte b )    {        if ((b >= '0') && (b <= '9')) return (byte)(b - '0');        if ((b >= 'a') && (b <= 'f')) return (byte)(b - 'a' + 10);        if ((b >= 'A') && (b <= 'F')) return (byte)(b - 'A' + 10);        return 0;    }    /* ------------------------------------------------------------ */    public static String toHexString(byte[] b)    {           StringBuffer buf = new StringBuffer();        for (int i=0;i<b.length;i++)        {            int bi=0xff&b[i];            int c='0'+(bi/16)%16;            if (c>'9')                c= 'A'+(c-'0'-10);            buf.append((char)c);            c='0'+bi%16;            if (c>'9')                c= 'a'+(c-'0'-10);            buf.append((char)c);        }        return buf.toString();    }        /* ------------------------------------------------------------ */    public static String toHexString(byte[] b,int offset,int length)    {           StringBuffer buf = new StringBuffer();        for (int i=offset;i<offset+length;i++)        {            int bi=0xff&b[i];            int c='0'+(bi/16)%16;            if (c>'9')                c= 'A'+(c-'0'-10);            buf.append((char)c);            c='0'+bi%16;            if (c>'9')                c= 'a'+(c-'0'-10);            buf.append((char)c);        }        return buf.toString();    }        /* ------------------------------------------------------------ */    public static byte[] fromHexString(String s)    {           if (s.length()%2!=0)            throw new IllegalArgumentException(s);        byte[] array = new byte[s.length()/2];        for (int i=0;i<array.length;i++)        {            int b = Integer.parseInt(s.substring(i*2,i*2+2),16);            array[i]=(byte)(0xff&b);        }            return array;    }        public static void dump(Class c)    {        System.err.println("Dump: "+c);        dump(c.getClassLoader());    }    public static void dump(ClassLoader cl)    {        System.err.println("Dump Loaders:");        while(cl!=null)        {            System.err.println("  loader "+cl);            cl = cl.getParent();        }    }        /* ------------------------------------------------------------ */    public static byte[] readLine(InputStream in) throws IOException    {        byte[] buf = new byte[256];                int i=0;        int loops=0;        int ch=0;                while (true)        {            ch=in.read();            if (ch<0)                break;            loops++;                        // skip a leading LF's            if (loops==1 && ch==LF)                continue;                        if (ch==CR || ch==LF)                break;                        if (i>=buf.length)            {                byte[] old_buf=buf;                buf=new byte[old_buf.length+256];                System.arraycopy(old_buf, 0, buf, 0, old_buf.length);            }            buf[i++]=(byte)ch;        }                if (ch==-1 && i==0)            return null;                // skip a trailing LF if it exists        if (ch==CR && in.available()>=1 && in.markSupported())        {            in.mark(1);            ch=in.read();            if (ch!=LF)                in.reset();        }        byte[] old_buf=buf;        buf=new byte[i];        System.arraycopy(old_buf, 0, buf, 0, i);                return buf;    }        public static URL jarFor(String className)    {        try        {            className=className.replace('.','/')+".class";            // hack to discover jstl libraries            URL url = Loader.getResource(null,className,false);            String s=url.toString();            if (s.startsWith("jar:file:"))                return new URL(s.substring(4,s.indexOf("!/")));        }        catch(Exception e)        {            Log.ignore(e);        }        return null;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -