BASE64Decoder,BASE64Encoder及相关类

话不多说,直接上干货
BASE64Decoder 类

import java.io.IOException;
import java.io.OutputStream;
import java.io.PushbackInputStream;

public class BASE64Decoder extends CharacterDecoder {
  private static final char[] pem_array = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
  private static final byte[] pem_convert_array = new byte[256];
  byte[] decode_buffer = new byte[4];

  static {
    int i;
    for(i = 0; i < 255; ++i) {
      pem_convert_array[i] = -1;
    }

    for(i = 0; i < pem_array.length; ++i) {
      pem_convert_array[pem_array[i]] = (byte)i;
    }

  }

  public BASE64Decoder() {
  }

  protected int bytesPerAtom() {
    return 4;
  }

  protected int bytesPerLine() {
    return 72;
  }

  protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int rem) throws IOException {
    byte a = -1;
    byte b = -1;
    byte c = -1;
    byte d = -1;
    if (rem < 2) {
      throw new CEFormatException("BASE64Decoder: Not enough bytes for an atom.");
    } else {
      int i;
      do {
        i = inStream.read();
        if (i == -1) {
          throw new CEStreamExhausted();
        }
      } while(i == 10 || i == 13);

      this.decode_buffer[0] = (byte)i;
      i = this.readFully(inStream, this.decode_buffer, 1, rem - 1);
      if (i == -1) {
        throw new CEStreamExhausted();
      } else {
        if (rem > 3 && this.decode_buffer[3] == 61) {
          rem = 3;
        }

        if (rem > 2 && this.decode_buffer[2] == 61) {
          rem = 2;
        }

        switch(rem) {
        case 4:
          d = pem_convert_array[this.decode_buffer[3] & 255];
        case 3:
          c = pem_convert_array[this.decode_buffer[2] & 255];
        case 2:
          b = pem_convert_array[this.decode_buffer[1] & 255];
          a = pem_convert_array[this.decode_buffer[0] & 255];
        default:
          switch(rem) {
          case 2:
            outStream.write((byte)(a << 2 & 252 | b >>> 4 & 3));
            break;
          case 3:
            outStream.write((byte)(a << 2 & 252 | b >>> 4 & 3));
            outStream.write((byte)(b << 4 & 240 | c >>> 2 & 15));
            break;
          case 4:
            outStream.write((byte)(a << 2 & 252 | b >>> 4 & 3));
            outStream.write((byte)(b << 4 & 240 | c >>> 2 & 15));
            outStream.write((byte)(c << 6 & 192 | d & 63));
          }

        }
      }
    }
  }
}
BASE64Encoder类

import java.io.IOException;
import java.io.OutputStream;

public class BASE64Encoder extends CharacterEncoder {
  private static final char[] pem_array = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};

  public BASE64Encoder() {
  }

  protected int bytesPerAtom() {
    return 3;
  }

  protected int bytesPerLine() {
    return 57;
  }

  protected void encodeAtom(OutputStream outStream, byte[] data, int offset, int len) throws IOException {
    byte a;
    if (len == 1) {
      a = data[offset];
      byte b = 0;
      byte c = false;
      outStream.write(pem_array[a >>> 2 & 63]);
      outStream.write(pem_array[(a << 4 & 48) + (b >>> 4 & 15)]);
      outStream.write(61);
      outStream.write(61);
    } else {
      byte b;
      if (len == 2) {
        a = data[offset];
        b = data[offset + 1];
        byte c = 0;
        outStream.write(pem_array[a >>> 2 & 63]);
        outStream.write(pem_array[(a << 4 & 48) + (b >>> 4 & 15)]);
        outStream.write(pem_array[(b << 2 & 60) + (c >>> 6 & 3)]);
        outStream.write(61);
      } else {
        a = data[offset];
        b = data[offset + 1];
        byte c = data[offset + 2];
        outStream.write(pem_array[a >>> 2 & 63]);
        outStream.write(pem_array[(a << 4 & 48) + (b >>> 4 & 15)]);
        outStream.write(pem_array[(b << 2 & 60) + (c >>> 6 & 3)]);
        outStream.write(pem_array[c & 63]);
      }
    }

  }
}
CharacterEncoder类

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.ByteBuffer;

public abstract class CharacterEncoder {
  protected PrintStream pStream;

  public CharacterEncoder() {
  }

  protected abstract int bytesPerAtom();

  protected abstract int bytesPerLine();

  protected void encodeBufferPrefix(OutputStream aStream) throws IOException {
    this.pStream = new PrintStream(aStream);
  }

  protected void encodeBufferSuffix(OutputStream aStream) throws IOException {
  }

  protected void encodeLinePrefix(OutputStream aStream, int aLength) throws IOException {
  }

  protected void encodeLineSuffix(OutputStream aStream) throws IOException {
    this.pStream.println();
  }

  protected abstract void encodeAtom(OutputStream var1, byte[] var2, int var3, int var4) throws IOException;

  protected int readFully(InputStream in, byte[] buffer) throws IOException {
    for(int i = 0; i < buffer.length; ++i) {
      int q = in.read();
      if (q == -1) {
        return i;
      }

      buffer[i] = (byte)q;
    }

    return buffer.length;
  }

  public void encode(InputStream inStream, OutputStream outStream) throws IOException {
    byte[] tmpbuffer = new byte[this.bytesPerLine()];
    this.encodeBufferPrefix(outStream);

    while(true) {
      int numBytes = this.readFully(inStream, tmpbuffer);
      if (numBytes == 0) {
        break;
      }

      this.encodeLinePrefix(outStream, numBytes);

      for(int j = 0; j < numBytes; j += this.bytesPerAtom()) {
        if (j + this.bytesPerAtom() <= numBytes) {
          this.encodeAtom(outStream, tmpbuffer, j, this.bytesPerAtom());
        } else {
          this.encodeAtom(outStream, tmpbuffer, j, numBytes - j);
        }
      }

      if (numBytes < this.bytesPerLine()) {
        break;
      }

      this.encodeLineSuffix(outStream);
    }

    this.encodeBufferSuffix(outStream);
  }

  public void encode(byte[] aBuffer, OutputStream aStream) throws IOException {
    ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
    this.encode((InputStream)inStream, aStream);
  }

  public String encode(byte[] aBuffer) {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
    String retVal = null;

    try {
      this.encode((InputStream)inStream, outStream);
      retVal = outStream.toString("8859_1");
      return retVal;
    } catch (Exception var6) {
      throw new Error("CharacterEncoder.encode internal error");
    }
  }

  private byte[] getBytes(ByteBuffer bb) {
    byte[] buf = null;
    if (bb.hasArray()) {
      byte[] tmp = bb.array();
      if (tmp.length == bb.capacity() && tmp.length == bb.remaining()) {
        buf = tmp;
        bb.position(bb.limit());
      }
    }

    if (buf == null) {
      buf = new byte[bb.remaining()];
      bb.get(buf);
    }

    return buf;
  }

  public void encode(ByteBuffer aBuffer, OutputStream aStream) throws IOException {
    byte[] buf = this.getBytes(aBuffer);
    this.encode(buf, aStream);
  }

  public String encode(ByteBuffer aBuffer) {
    byte[] buf = this.getBytes(aBuffer);
    return this.encode(buf);
  }

  public void encodeBuffer(InputStream inStream, OutputStream outStream) throws IOException {
    byte[] tmpbuffer = new byte[this.bytesPerLine()];
    this.encodeBufferPrefix(outStream);

    int numBytes;
    do {
      numBytes = this.readFully(inStream, tmpbuffer);
      if (numBytes == 0) {
        break;
      }

      this.encodeLinePrefix(outStream, numBytes);

      for(int j = 0; j < numBytes; j += this.bytesPerAtom()) {
        if (j + this.bytesPerAtom() <= numBytes) {
          this.encodeAtom(outStream, tmpbuffer, j, this.bytesPerAtom());
        } else {
          this.encodeAtom(outStream, tmpbuffer, j, numBytes - j);
        }
      }

      this.encodeLineSuffix(outStream);
    } while(numBytes >= this.bytesPerLine());

    this.encodeBufferSuffix(outStream);
  }

  public void encodeBuffer(byte[] aBuffer, OutputStream aStream) throws IOException {
    ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
    this.encodeBuffer((InputStream)inStream, aStream);
  }

  public String encodeBuffer(byte[] aBuffer) {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);

    try {
      this.encodeBuffer((InputStream)inStream, outStream);
    } catch (Exception var5) {
      throw new Error("CharacterEncoder.encodeBuffer internal error");
    }

    return outStream.toString();
  }

  public void encodeBuffer(ByteBuffer aBuffer, OutputStream aStream) throws IOException {
    byte[] buf = this.getBytes(aBuffer);
    this.encodeBuffer(buf, aStream);
  }

  public String encodeBuffer(ByteBuffer aBuffer) {
    byte[] buf = this.getBytes(aBuffer);
    return this.encodeBuffer(buf);
  }
}
CharacterDecoder类

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.nio.ByteBuffer;

public abstract class CharacterDecoder {
  public CharacterDecoder() {
  }

  protected abstract int bytesPerAtom();

  protected abstract int bytesPerLine();

  protected void decodeBufferPrefix(PushbackInputStream aStream, OutputStream bStream) throws IOException {
  }

  protected void decodeBufferSuffix(PushbackInputStream aStream, OutputStream bStream) throws IOException {
  }

  protected int decodeLinePrefix(PushbackInputStream aStream, OutputStream bStream) throws IOException {
    return this.bytesPerLine();
  }

  protected void decodeLineSuffix(PushbackInputStream aStream, OutputStream bStream) throws IOException {
  }

  protected void decodeAtom(PushbackInputStream aStream, OutputStream bStream, int l) throws IOException {
    throw new CEStreamExhausted();
  }

  protected int readFully(InputStream in, byte[] buffer, int offset, int len) throws IOException {
    for(int i = 0; i < len; ++i) {
      int q = in.read();
      if (q == -1) {
        return i == 0 ? -1 : i;
      }

      buffer[i + offset] = (byte)q;
    }

    return len;
  }

  public void decodeBuffer(InputStream aStream, OutputStream bStream) throws IOException {
    int totalBytes = 0;
    PushbackInputStream ps = new PushbackInputStream(aStream);
    this.decodeBufferPrefix(ps, bStream);

    while(true) {
      try {
        int length = this.decodeLinePrefix(ps, bStream);

        int i;
        for(i = 0; i + this.bytesPerAtom() < length; i += this.bytesPerAtom()) {
          this.decodeAtom(ps, bStream, this.bytesPerAtom());
          totalBytes += this.bytesPerAtom();
        }

        if (i + this.bytesPerAtom() == length) {
          this.decodeAtom(ps, bStream, this.bytesPerAtom());
          totalBytes += this.bytesPerAtom();
        } else {
          this.decodeAtom(ps, bStream, length - i);
          totalBytes += length - i;
        }

        this.decodeLineSuffix(ps, bStream);
      } catch (CEStreamExhausted var8) {
        this.decodeBufferSuffix(ps, bStream);
        return;
      }
    }
  }

  public byte[] decodeBuffer(String inputString) throws IOException {
    byte[] inputBuffer = new byte[inputString.length()];
    inputString.getBytes(0, inputString.length(), inputBuffer, 0);
    ByteArrayInputStream inStream = new ByteArrayInputStream(inputBuffer);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    this.decodeBuffer(inStream, outStream);
    return outStream.toByteArray();
  }

  public byte[] decodeBuffer(InputStream in) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    this.decodeBuffer(in, outStream);
    return outStream.toByteArray();
  }

  public ByteBuffer decodeBufferToByteBuffer(String inputString) throws IOException {
    return ByteBuffer.wrap(this.decodeBuffer(inputString));
  }

  public ByteBuffer decodeBufferToByteBuffer(InputStream in) throws IOException {
    return ByteBuffer.wrap(this.decodeBuffer(in));
  }
}
CEFormatException类

import java.io.IOException;

public class CEFormatException extends IOException {
  public CEFormatException(String s) {
    super(s);
  }
}
CEStreamExhausted类
import java.io.IOException;

public class CEStreamExhausted extends IOException {
  public CEStreamExhausted() {
  }
}

 


版权声明:本文为baidu_39510658原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。