예제 #1
0
  /**
   * Decodes a byte[] containing containing characters in the Base64 alphabet.
   *
   * @param pArray A byte array containing Base64 character data
   * @return a byte array containing binary data
   */
  public byte[] decode(final byte[] pArray) {
    reset();
    if ((pArray == null) || (pArray.length == 0)) {
      return pArray;
    }
    long len = Math.max(1, (pArray.length * 3) / 4);
    byte[] buf = new byte[(int) len];
    setInitialBuffer(buf, 0, buf.length);
    decode(pArray, 0, pArray.length);
    decode(pArray, 0, -1); // Notify decoder of EOF.

    // Would be nice to just return buf (like we sometimes do in the encode
    // logic), but we have no idea what the line-length was (could even be
    // variable). So we cannot determine ahead of time exactly how big an
    // array is necessary. Hence the need to construct a 2nd byte array to
    // hold the final result:

    byte[] result = new byte[pos];
    readResults(result, 0, result.length);
    return result;
  }
예제 #2
0
 /**
  * Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64
  * alphabet.
  *
  * @param pArray a byte array containing binary data
  * @return A byte array containing only Base64 character data
  */
 public byte[] encode(final byte[] pArray) {
   reset();
   if ((pArray == null) || (pArray.length == 0)) {
     return pArray;
   }
   long len = getEncodeLength(pArray, lineLength, lineSeparator);
   byte[] buf = new byte[(int) len];
   setInitialBuffer(buf, 0, buf.length);
   encode(pArray, 0, pArray.length);
   encode(pArray, 0, -1); // Notify encoder of EOF.
   // Encoder might have resized, even though it was unnecessary.
   if (buffer != buf) {
     readResults(buf, 0, buf.length);
   }
   // In URL-SAFE mode we skip the padding characters, so sometimes our
   // final length is a bit smaller.
   if (isUrlSafe() && (pos < buf.length)) {
     byte[] smallerBuf = new byte[pos];
     System.arraycopy(buf, 0, smallerBuf, 0, pos);
     buf = smallerBuf;
   }
   return buf;
 }