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

📄 base64.java

📁 Encodes and decodes to and from Base64 notation
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                    e++;                    lineLength = 0;                }   // end if: end of line            }   // en dfor: each piece of array            if( d < len ) {                encode3to4( source, d+off, len - d, outBuff, e, options );                e += 4;            }   // end if: some padding needed            byte[] finalOut = new byte[e];            System.arraycopy(outBuff,0, finalOut,0,e);            return finalOut;                }   // end else: don't compress    }   // end encodeBytesToBytes                /* ********  D E C O D I N G   M E T H O D S  ******** */            /**     * Decodes four bytes from array <var>source</var>     * and writes the resulting bytes (up to three of them)     * to <var>destination</var>.     * The source and destination arrays can be manipulated     * anywhere along their length by specifying      * <var>srcOffset</var> and <var>destOffset</var>.     * This method does not check to make sure your arrays     * are large enough to accomodate <var>srcOffset</var> + 4 for     * the <var>source</var> array or <var>destOffset</var> + 3 for     * the <var>destination</var> array.     * This method returns the actual number of bytes that      * were converted from the Base64 encoding.	 * <p>This is the lowest level of the decoding methods with	 * all possible parameters.</p>     *      *     * @param source the array to convert     * @param srcOffset the index where conversion begins     * @param destination the array to hold the conversion     * @param destOffset the index where output will be put	 * @param options alphabet type is pulled from this (standard, url-safe, ordered)     * @return the number of decoded bytes converted     * @throws NullPointerException if source or destination arrays are null     * @throws IllegalArgumentException if srcOffset or destOffset are invalid     *         or there is not enough room in the array.     * @since 1.3     */    private static int decode4to3(     byte[] source, int srcOffset,     byte[] destination, int destOffset, int options ) {                // Lots of error checking and exception throwing        if( source == null ){            throw new NullPointerException( "Source array was null." );        }   // end if        if( destination == null ){            throw new NullPointerException( "Destination array was null." );        }   // end if        if( srcOffset < 0 || srcOffset + 3 >= source.length ){            throw new IllegalArgumentException( String.format(            "Source array with length %d cannot have offset of %d and still process four bytes.", source.length, srcOffset ) );        }   // end if        if( destOffset < 0 || destOffset +2 >= destination.length ){            throw new IllegalArgumentException( String.format(            "Destination array with length %d cannot have offset of %d and still store three bytes.", destination.length, destOffset ) );        }   // end if                        byte[] DECODABET = getDecodabet( options ); 	        // Example: Dk==        if( source[ srcOffset + 2] == EQUALS_SIGN ) {            // Two ways to do the same thing. Don't know which way I like best.          //int outBuff =   ( ( DECODABET[ source[ srcOffset    ] ] << 24 ) >>>  6 )          //              | ( ( DECODABET[ source[ srcOffset + 1] ] << 24 ) >>> 12 );            int outBuff =   ( ( DECODABET[ source[ srcOffset    ] ] & 0xFF ) << 18 )                          | ( ( DECODABET[ source[ srcOffset + 1] ] & 0xFF ) << 12 );                        destination[ destOffset ] = (byte)( outBuff >>> 16 );            return 1;        }                // Example: DkL=        else if( source[ srcOffset + 3 ] == EQUALS_SIGN ) {            // Two ways to do the same thing. Don't know which way I like best.          //int outBuff =   ( ( DECODABET[ source[ srcOffset     ] ] << 24 ) >>>  6 )          //              | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )          //              | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 );            int outBuff =   ( ( DECODABET[ source[ srcOffset     ] ] & 0xFF ) << 18 )                          | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 )                          | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) <<  6 );                        destination[ destOffset     ] = (byte)( outBuff >>> 16 );            destination[ destOffset + 1 ] = (byte)( outBuff >>>  8 );            return 2;        }                // Example: DkLE        else {            // Two ways to do the same thing. Don't know which way I like best.          //int outBuff =   ( ( DECODABET[ source[ srcOffset     ] ] << 24 ) >>>  6 )          //              | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )          //              | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 )          //              | ( ( DECODABET[ source[ srcOffset + 3 ] ] << 24 ) >>> 24 );            int outBuff =   ( ( DECODABET[ source[ srcOffset     ] ] & 0xFF ) << 18 )                          | ( ( DECODABET[ source[ srcOffset + 1 ] ] & 0xFF ) << 12 )                          | ( ( DECODABET[ source[ srcOffset + 2 ] ] & 0xFF ) <<  6)                          | ( ( DECODABET[ source[ srcOffset + 3 ] ] & 0xFF )      );                        destination[ destOffset     ] = (byte)( outBuff >> 16 );            destination[ destOffset + 1 ] = (byte)( outBuff >>  8 );            destination[ destOffset + 2 ] = (byte)( outBuff       );            return 3;        }    }   // end decodeToBytes        /**     * Low-level access to decoding ASCII characters in     * the form of a byte array. <strong>Ignores GUNZIP option, if     * it's set.</strong> This is not generally a recommended method,     * although it is used internally as part of the decoding process.     * Special case: if len = 0, an empty array is returned. Still,     * if you need more speed and reduced memory footprint (and aren't     * gzipping), consider this method.     *     * @param source The Base64 encoded data     * @return decoded data     * @since 2.3.1     */    public static byte[] decode( byte[] source ){        byte[] decoded = null;        try {            decoded = decode( source, 0, source.length, Base64.NO_OPTIONS );        } catch( IOException ex ) {            assert false : "IOExceptions only come from GZipping, which is turned off: " + ex.getMessage();        }        return decoded;    }            /**     * Low-level access to decoding ASCII characters in     * the form of a byte array. <strong>Ignores GUNZIP option, if     * it's set.</strong> This is not generally a recommended method,     * although it is used internally as part of the decoding process.     * Special case: if len = 0, an empty array is returned. Still,     * if you need more speed and reduced memory footprint (and aren't     * gzipping), consider this method.     *     * @param source The Base64 encoded data     * @param off    The offset of where to begin decoding     * @param len    The length of characters to decode     * @param options Can specify options such as alphabet type to use     * @return decoded data     * @throws java.io.IOException If bogus characters exist in source data     * @since 1.3     */    public static byte[] decode( byte[] source, int off, int len, int options )    throws java.io.IOException {                // Lots of error checking and exception throwing        if( source == null ){            throw new NullPointerException( "Cannot decode null source array." );        }   // end if        if( off < 0 || off + len > source.length ){            throw new IllegalArgumentException( String.format(            "Source array with length %d cannot have offset of %d and process %d bytes.", source.length, off, len ) );        }   // end if                if( len == 0 ){            return new byte[0];        }else if( len < 4 ){            throw new IllegalArgumentException(             "Base64-encoded string must have at least four characters, but length specified was " + len );        }   // end if                byte[] DECODABET = getDecodabet( options );	        int    len34   = len * 3 / 4;       // Estimate on array size        byte[] outBuff = new byte[ len34 ]; // Upper limit on size of output        int    outBuffPosn = 0;             // Keep track of where we're writing                byte[] b4        = new byte[4];     // Four byte buffer from source, eliminating white space        int    b4Posn    = 0;               // Keep track of four byte input buffer        int    i         = 0;               // Source array counter        byte   sbiCrop   = 0;               // Low seven bits (ASCII) of input        byte   sbiDecode = 0;               // Special value from DECODABET                for( i = off; i < off+len; i++ ) {  // Loop through source                        sbiCrop = (byte)(source[i] & 0x7f); // Only the low seven bits            sbiDecode = DECODABET[ sbiCrop ];   // Special value                        // White space, Equals sign, or legit Base64 character            // Note the values such as -5 and -9 in the            // DECODABETs at the top of the file.            if( sbiDecode >= WHITE_SPACE_ENC )  {                if( sbiDecode >= EQUALS_SIGN_ENC ) {                    b4[ b4Posn++ ] = sbiCrop;           // Save non-whitespace                    if( b4Posn > 3 ) {                  // Time to decode?                        outBuffPosn += decode4to3( b4, 0, outBuff, outBuffPosn, options );                        b4Posn = 0;                                                // If that was the equals sign, break out of 'for' loop                        if( sbiCrop == EQUALS_SIGN ) {                            break;                        }   // end if: equals sign                    }   // end if: quartet built                }   // end if: equals sign or better            }   // end if: white space, equals sign or better            else {                // There's a bad input character in the Base64 stream.                throw new java.io.IOException( String.format(                "Bad Base64 input character '%c' in array position %d", source[i], i ) );            }   // end else:         }   // each input character                                           byte[] out = new byte[ outBuffPosn ];        System.arraycopy( outBuff, 0, out, 0, outBuffPosn );         return out;    }   // end decode        		    /**     * Decodes data from Base64 notation, automatically     * detecting gzip-compressed data and decompressing it.     *     * @param s the string to decode     * @return the decoded data     * @throws java.io.IOException If there is a problem     * @since 1.4     */    public static byte[] decode( String s ) throws java.io.IOException {        return decode( s, NO_OPTIONS );    }            /**     * Decodes data from Base64 notation, automatically     * detecting gzip-compressed data and decompressing it.     *     * @param s the string to decode     * @param options encode options such as URL_SAFE     * @return the decoded data     * @throws java.io.IOException if there is an error     * @throws NullPointerException if <tt>s</tt> is null     * @since 1.4     */    public static byte[] decode( String s, int options ) throws java.io.IOException {                if( s == null ){            throw new NullPointerException( "Input string was null." );        }   // end if                byte[] bytes;        try {            bytes = s.getBytes( PREFERRED_ENCODING );        }   // end try        catch( java.io.UnsupportedEncodingException uee ) {            bytes = s.getBytes();        }   // end catch		//</change>                // Decode        bytes = decode( bytes, 0, bytes.length, options );                        // Check to see if it's gzip-compressed        // GZIP Magic Two-Byte Number: 0x8b1f (35615)        if( bytes != null && bytes.length >= 4 ) {                        int head = ((int)bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);                   if( java.util.zip.GZIPInputStream.GZIP_MAGIC == head )  {                java.io.ByteArrayInputStream  bais = null;                java.util.zip.GZIPInputStream gzis = null;                java.io.ByteArrayOutputStream baos = null;                byte[] buffer = new byte[2048];                int    length = 0;                try {                    baos = new java.io.ByteArrayOutputStream();                    bais = new java.io.ByteArrayInputStream( bytes );                    gzis = new java.util.zip.GZIPInputStream( bais );                    while( ( length = gzis.read( buffer ) ) >= 0 ) {                        baos.write(buffer,0,length);                    }   // end while: reading input                    // No error? Get new bytes.                    bytes = baos.toByteArray();

⌨️ 快捷键说明

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