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

📄 utilities.java

📁 这个weblogging 设计得比较精巧
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    public static void copyFile(File from, File to) throws IOException    {        InputStream in = null;        OutputStream out = null;        try        {            in = new FileInputStream(from);        }        catch (IOException ex)        {            throw new IOException(                "Utilities.copyFile: opening input stream '"                    + from.getPath()                    + "', "                    + ex.getMessage());        }        try        {            out = new FileOutputStream(to);        }        catch (Exception ex)        {            try            {                in.close();            }            catch (IOException ex1)            {            }            throw new IOException(                "Utilities.copyFile: opening output stream '"                    + to.getPath()                    + "', "                    + ex.getMessage());        }        copyInputToOutput(in, out, from.length());    }    //------------------------------------------------------------------------    /**     * Utility method to copy an input stream to an output stream.     * Wraps both streams in buffers. Ensures right numbers of bytes copied.     */    public static void copyInputToOutput(        InputStream input,        OutputStream output,        long byteCount)        throws IOException    {        int bytes;        long length;        BufferedInputStream in = new BufferedInputStream(input);        BufferedOutputStream out = new BufferedOutputStream(output);        byte[] buffer;        buffer = new byte[8192];        for (length = byteCount; length > 0;)        {            bytes = (int) (length > 8192 ? 8192 : length);            try            {                bytes = in.read(buffer, 0, bytes);            }            catch (IOException ex)            {                try                {                    in.close();                    out.close();                }                catch (IOException ex1)                {                }                throw new IOException(                    "Reading input stream, " + ex.getMessage());            }            if (bytes < 0)                break;            length -= bytes;            try            {                out.write(buffer, 0, bytes);            }            catch (IOException ex)            {                try                {                    in.close();                    out.close();                }                catch (IOException ex1)                {                }                throw new IOException(                    "Writing output stream, " + ex.getMessage());            }        }        try        {            in.close();            out.close();        }        catch (IOException ex)        {            throw new IOException("Closing file streams, " + ex.getMessage());        }    }    //------------------------------------------------------------------------    public static void copyInputToOutput(        InputStream input,        OutputStream output)        throws IOException    {        BufferedInputStream in = new BufferedInputStream(input);        BufferedOutputStream out = new BufferedOutputStream(output);        byte buffer[] = new byte[8192];        for (int count = 0; count != -1;)        {            count = in.read(buffer, 0, 8192);            if (count != -1)                out.write(buffer, 0, count);        }        try        {            in.close();            out.close();        }        catch (IOException ex)        {            throw new IOException("Closing file streams, " + ex.getMessage());        }    }        /**     * Encode a string using algorithm specified in web.xml and return the     * resulting encrypted password. If exception, the plain credentials     * string is returned     *     * @param password Password or other credentials to use in authenticating     *        this username     * @param algorithm Algorithm used to do the digest     *     * @return encypted password based on the algorithm.     */    public static String encodePassword(String password, String algorithm)     {        byte[] unencodedPassword = password.getBytes();        MessageDigest md = null;        try         {            // first create an instance, given the provider            md = MessageDigest.getInstance(algorithm);        }         catch (Exception e)         {            mLogger.error("Exception: " + e);            return password;        }        md.reset();        // call the update method one or more times        // (useful when you don't know the size of your data, eg. stream)        md.update(unencodedPassword);        // now calculate the hash        byte[] encodedPassword = md.digest();        StringBuffer buf = new StringBuffer();        for (int i = 0; i < encodedPassword.length; i++)         {            if ((encodedPassword[i] & 0xff) < 0x10)             {                buf.append("0");            }            buf.append(Long.toString(encodedPassword[i] & 0xff, 16));        }        return buf.toString();    }    /**     * Encode a string using Base64 encoding. Used when storing passwords     * as cookies.     *     * This is weak encoding in that anyone can use the decodeString     * routine to reverse the encoding.     *     * @param str     * @return String     * @throws IOException     */    public static String encodeString(String str) throws IOException     {        sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();        String encodedStr = encoder.encodeBuffer(str.getBytes());        return (encodedStr.trim());    }    /**     * Decode a string using Base64 encoding.     *     * @param str     * @return String     * @throws IOException     */    public static String decodeString(String str) throws IOException     {        sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();        String value = new String(dec.decodeBuffer(str));        return (value);    }        /**     * Strips HTML and truncates.     */    public static String truncate(            String str, int lower, int upper, String appendToEnd)    {        // strip markup from the string        String str2 = removeHTML(str, false);                // quickly adjust the upper if it is set lower than 'lower'        if (upper < lower)         {            upper = lower;        }                       // now determine if the string fits within the upper limit        // if it does, go straight to return, do not pass 'go' and collect $200        if(str2.length() > upper)         {            // the magic location int            int loc;                    // first we determine where the next space appears after lower            loc = str2.lastIndexOf(' ', upper);                        // now we'll see if the location is greater than the lower limit            if(loc >= lower)             {                // yes it was, so we'll cut it off here                str2 = str2.substring(0, loc);            }             else             {                // no it wasnt, so we'll cut it off at the upper limit                str2 = str2.substring(0, upper);                loc = upper;            }                       // the string was truncated, so we append the appendToEnd String                            str2 = str2 + appendToEnd;        }                return str2;    }        /**     * This method based on code from the String taglib at Apache Jakarta:     * http://cvs.apache.org/viewcvs/jakarta-taglibs/string/src/org/apache/taglibs/string/util/StringW.java?rev=1.16&content-type=text/vnd.viewcvs-markup     * Copyright (c) 1999 The Apache Software Foundation.     * Author: Henri Yandell bayard@generationjava.com     *      * @param str     * @param lower     * @param upper     * @param appendToEnd     * @return     */    public static String truncateNicely(String str, int lower, int upper, String appendToEnd)    {        // strip markup from the string        String str2 = removeHTML(str, false);        boolean diff = (str2.length() < str.length());                // quickly adjust the upper if it is set lower than 'lower'        if(upper < lower) {            upper = lower;        }                       // now determine if the string fits within the upper limit        // if it does, go straight to return, do not pass 'go' and collect $200        if(str2.length() > upper) {            // the magic location int            int loc;                    // first we determine where the next space appears after lower            loc = str2.lastIndexOf(' ', upper);                        // now we'll see if the location is greater than the lower limit            if(loc >= lower) {                // yes it was, so we'll cut it off here                str2 = str2.substring(0, loc);            } else {                // no it wasnt, so we'll cut it off at the upper limit                str2 = str2.substring(0, upper);                loc = upper;            }                        // HTML was removed from original str            if (diff)            {                                // location of last space in truncated string                loc = str2.lastIndexOf(' ', loc);                                // get last "word" in truncated string (add 1 to loc to eliminate space                String str3 = str2.substring(loc+1);                                // find this fragment in original str, from 'loc' position                loc = str.indexOf(str3, loc) + str3.length();                                // get truncated string from original str, given new 'loc'                str2 = str.substring(0, loc);                                // get all the HTML from original str after loc                str3 = extractHTML(str.substring(loc));                                // remove any tags which generate visible HTML                // This call is unecessary, all HTML has already been stripped                //str3 = removeVisibleHTMLTags(str3);                                // append the appendToEnd String and                // add extracted HTML back onto truncated string                str = str2 + appendToEnd + str3;            }            else            {                // the string was truncated, so we append the appendToEnd String                                str = str2 + appendToEnd;            }            }                return str;    }        public static String truncateText(String str, int lower, int upper, String appendToEnd)    {        // strip markup from the string        String str2 = removeHTML(str, false);        boolean diff = (str2.length() < str.length());                // quickly adjust the upper if it is set lower than 'lower'        if(upper < lower) {            upper = lower;        }                       // now determine if the string fits within the upper limit        // if it does, go straight to return, do not pass 'go' and collect $200        if(str2.length() > upper) {            // the magic location int            int loc;                    // first we determine where the next space appears after lower            loc = str2.lastIndexOf(' ', upper);

⌨️ 快捷键说明

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