//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cn.com.ty.lift.common.utils;

import java.io.*;
import java.nio.ByteBuffer;

public class Base64Encoder {
    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', '+', '/'};

    protected PrintStream printStream;

    public Base64Encoder() {
    }

    protected int bytesPerAtom() {
        return 3;
    }

    protected int bytesPerLine() {
        return 57;
    }

    protected void encodeAtom(OutputStream outputStream, byte[] bytes, int begin, int end) throws IOException {
        byte beginByte;
        if (end == 1) {
            beginByte = bytes[begin];
            byte var6 = 0;
            outputStream.write(pem_array[beginByte >>> 2 & 63]);
            outputStream.write(pem_array[(beginByte << 4 & 48) + (var6 >>> 4 & 15)]);
            outputStream.write(61);
            outputStream.write(61);
        } else {
            byte nextByte;
            if (end == 2) {
                beginByte = bytes[begin];
                nextByte = bytes[begin + 1];
                byte zero = 0;
                outputStream.write(pem_array[beginByte >>> 2 & 63]);
                outputStream.write(pem_array[(beginByte << 4 & 48) + (nextByte >>> 4 & 15)]);
                outputStream.write(pem_array[(nextByte << 2 & 60) + (zero >>> 6 & 3)]);
                outputStream.write(61);
            } else {
                beginByte = bytes[begin];
                nextByte = bytes[begin + 1];
                byte secondByte = bytes[begin + 2];
                outputStream.write(pem_array[beginByte >>> 2 & 63]);
                outputStream.write(pem_array[(beginByte << 4 & 48) + (nextByte >>> 4 & 15)]);
                outputStream.write(pem_array[(nextByte << 2 & 60) + (secondByte >>> 6 & 3)]);
                outputStream.write(pem_array[secondByte & 63]);
            }
        }

    }

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

        while (true) {
            int readFully = this.readFully(inputStream, bytesPerLine);
            if (readFully == 0) {
                break;
            }
            this.encodeLinePrefix(outputStream, readFully);
            for (int i = 0; i < readFully; i += this.bytesPerAtom()) {
                if (i + this.bytesPerAtom() <= readFully) {
                    this.encodeAtom(outputStream, bytesPerLine, i, this.bytesPerAtom());
                } else {
                    this.encodeAtom(outputStream, bytesPerLine, i, readFully - i);
                }
            }
            if (readFully < this.bytesPerLine()) {
                break;
            }
            this.encodeLineSuffix(outputStream);
        }
        this.encodeBufferSuffix(outputStream);
    }

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

    protected void encodeBufferSuffix(OutputStream outputStream) throws IOException {
    }

    protected void encodeLinePrefix(OutputStream outputStream, int var2) throws IOException {
    }

    protected void encodeLineSuffix(OutputStream outputStream) throws IOException {
        this.printStream.println();
    }

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

    public void encode(byte[] bytes, OutputStream outputStream) throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        this.encode(byteArrayInputStream, outputStream);
    }

    public String encode(byte[] bytes) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        try {
            this.encode(byteArrayInputStream, byteArrayOutputStream);
            String result = byteArrayOutputStream.toString("8859_1");
            return result;
        } catch (Exception var6) {
            throw new Error("CharacterEncoder.encode internal error");
        }
    }

    private byte[] getBytes(ByteBuffer byteBuffer) {
        byte[] bytes = null;
        if (byteBuffer.hasArray()) {
            byte[] byteArray = byteBuffer.array();
            if (byteArray.length == byteBuffer.capacity() && byteArray.length == byteBuffer.remaining()) {
                bytes = byteArray;
                byteBuffer.position(byteBuffer.limit());
            }
        }

        if (bytes == null) {
            bytes = new byte[byteBuffer.remaining()];
            byteBuffer.get(bytes);
        }

        return bytes;
    }

    public void encode(ByteBuffer byteBuffer, OutputStream outputStream) throws IOException {
        byte[] bytes = this.getBytes(byteBuffer);
        this.encode(bytes, outputStream);
    }

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

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

        int readFully;
        do {
            readFully = this.readFully(inputStream, bytesPerLine);
            if (readFully == 0) {
                break;
            }

            this.encodeLinePrefix(outputStream, readFully);

            for(int index = 0; index < readFully; index += this.bytesPerAtom()) {
                if (index + this.bytesPerAtom() <= readFully) {
                    this.encodeAtom(outputStream, bytesPerLine, index, this.bytesPerAtom());
                } else {
                    this.encodeAtom(outputStream, bytesPerLine, index, readFully - index);
                }
            }

            this.encodeLineSuffix(outputStream);
        } while(readFully >= this.bytesPerLine());

        this.encodeBufferSuffix(outputStream);
    }

    public void encodeBuffer(byte[] bytes, OutputStream outputStream) throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        this.encodeBuffer(byteArrayInputStream, outputStream);
    }

    public String encodeBuffer(byte[] var1) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(var1);

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

        return byteArrayOutputStream.toString();
    }

    public void encodeBuffer(ByteBuffer byteBuffer, OutputStream outputStream) throws IOException {
        byte[] bytes = this.getBytes(byteBuffer);
        this.encodeBuffer(bytes, outputStream);
    }

    public String encodeBuffer(ByteBuffer byteBuffer) {
        byte[] bytes = this.getBytes(byteBuffer);
        return this.encodeBuffer(bytes);
    }
}
