base64.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 397 行 · 第 1/2 页

JAVA
397
字号
      }    return new String(outBuff, 0, e);  }  /**   * Decodes data from Base64 notation.   *   * @param s the string to decode.   * @return the decoded data.   */  public static final byte[] decode(final String s)      throws UnsupportedEncodingException  {    final byte[] bytes;    bytes = s.getBytes("US-ASCII");    return decode(bytes, 0, bytes.length);  }  /**   * Decodes Base64 content in byte array format and returns the decoded byte   * array.   *   * @param src the Base64 encoded data.   * @param off the offset of where to begin decoding.   * @param len the length of characters to decode.   * @return the decoded data.   * @throws IllegalArgumentException if <code>src</code> contains an illegal   * Base-64 character.   */  public static byte[] decode(final byte[] src, final int off, final int len)  {    final int len34 = len * 3 / 4;    final byte[] outBuff = new byte[len34]; // Upper limit on size of output    int outBuffPosn = 0;    final byte[] b4 = new byte[4];    int b4Posn = 0;    int i;    byte sbiCrop, sbiDecode;    for (i = off; i < off + len; i++)      {        sbiCrop = (byte) (src[i] & 0x7F); // Only the low seven bits        sbiDecode = DECODABET[sbiCrop];        if (sbiDecode >= WHITE_SPACE_ENC)          { // White space, Equals sign or better            if (sbiDecode >= EQUALS_SIGN_ENC)              {                b4[b4Posn++] = sbiCrop;                if (b4Posn > 3)                  {                    outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn);                    b4Posn = 0;                    // If that was the equals sign, break out of 'for' loop                    if (sbiCrop == EQUALS_SIGN)                      break;                  } // end if: quartet built              } // end if: equals sign or better          }        else          {            throw new IllegalArgumentException("Illegal BASE-64 character at #"                                               + i + ": " + src[i]                                               + "(decimal)");          }      }    final byte[] result = new byte[outBuffPosn];    System.arraycopy(outBuff, 0, result, 0, outBuffPosn);    return result;  }  /**   * <p>Encodes up to three bytes of the array <code>src</code> and writes   * the resulting four Base64 bytes to <code>dest</code>. The source and   * destination arrays can be manipulated anywhere along their length by   * specifying <code>sOffset</code> and <code>dOffset</code>.</p>   *   * <p>This method does not check to make sure the arrays are large enough to   * accomodate <code>sOffset + 3</code> for the <code>src</code> array or   * <code>dOffset + 4</code> for the <code>dest</code> array. The actual   * number of significant bytes in the input array is given by   * <code>numBytes</code>.</p>   *   * @param src the array to convert.   * @param sOffset the index where conversion begins.   * @param numBytes the number of significant bytes in your array.   * @param dest the array to hold the conversion.   * @param dOffset the index where output will be put.   * @return the <code>destination</code> array.   */  private static final byte[] encode3to4(final byte[] src, final int sOffset,                                         final int numBytes, final byte[] dest,                                         final int dOffset)  {    //           1         2         3    // 01234567890123456789012345678901 Bit position    // --------000000001111111122222222 Array position from threeBytes    // --------|    ||    ||    ||    | Six bit groups to index ALPHABET    //          >>18  >>12  >> 6  >> 0  Right shift necessary    //                0x3F  0x3F  0x3F  Additional AND    // Create buffer with zero-padding if there are only one or two    // significant bytes passed in the array.    // We have to shift left 24 in order to flush out the 1's that appear    // when Java treats a value as negative that is cast from a byte to an int.    final int inBuff = (numBytes > 0 ? ((src[sOffset] << 24) >>> 8) : 0)                       | (numBytes > 1 ? ((src[sOffset + 1] << 24) >>> 16) : 0)                       | (numBytes > 2 ? ((src[sOffset + 2] << 24) >>> 24) : 0);    switch (numBytes)      {      case 3:        dest[dOffset] = ALPHABET[(inBuff >>> 18)];        dest[dOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3F];        dest[dOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3F];        dest[dOffset + 3] = ALPHABET[(inBuff) & 0x3F];        break;      case 2:        dest[dOffset] = ALPHABET[(inBuff >>> 18)];        dest[dOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3F];        dest[dOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3F];        dest[dOffset + 3] = EQUALS_SIGN;        break;      case 1:        dest[dOffset] = ALPHABET[(inBuff >>> 18)];        dest[dOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3F];        dest[dOffset + 2] = EQUALS_SIGN;        dest[dOffset + 3] = EQUALS_SIGN;        break;      }    return dest;  }  /**   * <p>Decodes four bytes from array <code>src</code> and writes the   * resulting bytes (up to three of them) to <code>dest</code>.</p>   *   * <p>The source and destination arrays can be manipulated anywhere along   * their length by specifying <code>sOffset</code> and <code>dOffset</code>.   * </p>   *   * <p>This method does not check to make sure your arrays are large enough   * to accomodate <code>sOffset + 4</code> for the <code>src</code> array or   * <code>dOffset + 3</code> for the <code>dest</code> array. This method   * returns the actual number of bytes that were converted from the Base64   * encoding.</p>   *   * @param src the array to convert.   * @param sOffset the index where conversion begins.   * @param dest the array to hold the conversion.   * @param dOffset the index where output will be put.   * @return the number of decoded bytes converted.   */  private static final int decode4to3(final byte[] src, final int sOffset,                                      final byte[] dest, final int dOffset)  {    if (src[sOffset + 2] == EQUALS_SIGN)      { // Example: Dk==        final int outBuff = ((DECODABET[src[sOffset]] & 0xFF) << 18)                            | ((DECODABET[src[sOffset + 1]] & 0xFF) << 12);        dest[dOffset] = (byte) (outBuff >>> 16);        return 1;      }    if (src[sOffset + 3] == EQUALS_SIGN)      { // Example: DkL=        final int outBuff = ((DECODABET[src[sOffset]] & 0xFF) << 18)                            | ((DECODABET[src[sOffset + 1]] & 0xFF) << 12)                            | ((DECODABET[src[sOffset + 2]] & 0xFF) << 6);        dest[dOffset] = (byte) (outBuff >>> 16);        dest[dOffset + 1] = (byte) (outBuff >>> 8);        return 2;      }    try      { // Example: DkLE        final int outBuff = ((DECODABET[src[sOffset]] & 0xFF) << 18)                            | ((DECODABET[src[sOffset + 1]] & 0xFF) << 12)                            | ((DECODABET[src[sOffset + 2]] & 0xFF) << 6)                            | ((DECODABET[src[sOffset + 3]] & 0xFF));        dest[dOffset] = (byte) (outBuff >> 16);        dest[dOffset + 1] = (byte) (outBuff >> 8);        dest[dOffset + 2] = (byte) outBuff;        return 3;      }    catch (Exception x)      {        if (DEBUG && debuglevel > 8)          {            debug("" + src[sOffset] + ": " + (DECODABET[src[sOffset]]));            debug("" + src[sOffset + 1] + ": " + (DECODABET[src[sOffset + 1]]));            debug("" + src[sOffset + 2] + ": " + (DECODABET[src[sOffset + 2]]));            debug("" + src[sOffset + 3] + ": " + (DECODABET[src[sOffset + 3]]));          }        return -1;      }  }}

⌨️ 快捷键说明

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