characterencoder.java

来自「用纯java语言实现的数字证书制作工具。」· Java 代码 · 共 176 行

JAVA
176
字号
package data;
import java.io.*;

public abstract class CharacterEncoder
{

    protected PrintStream pStream;

    public CharacterEncoder()
    {
    }

    protected abstract int bytesPerAtom();

    protected abstract int bytesPerLine();

    public void encode(InputStream inputstream, OutputStream outputstream)
        throws IOException
    {
        byte abyte0[] = new byte[bytesPerLine()];
        encodeBufferPrefix(outputstream);
        do
        {
            int j = readFully(inputstream, abyte0);
            if(j == 0)
            {
                break;
            }
            encodeLinePrefix(outputstream, j);
            for(int i = 0; i < j; i += bytesPerAtom())
            {
                if(i + bytesPerAtom() <= j)
                {
                    encodeAtom(outputstream, abyte0, i, bytesPerAtom());
                } else
                {
                    encodeAtom(outputstream, abyte0, i, j - i);
                }
            }

            if(j < bytesPerLine())
            {
                break;
            }
            encodeLineSuffix(outputstream);
        } while(true);
        encodeBufferSuffix(outputstream);
    }

    public String encode(byte abyte0[])
    {
        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
        ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
        String s = null;
        try
        {
            encode(((InputStream) (bytearrayinputstream)), ((OutputStream) (bytearrayoutputstream)));
            s = bytearrayoutputstream.toString("8859_1");
        }
        catch(Exception _ex)
        {
            throw new Error("ChracterEncoder::encodeBuffer internal error");
        }
        return s;
    }

    public void encode(byte abyte0[], OutputStream outputstream)
        throws IOException
    {
        ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
        encode(((InputStream) (bytearrayinputstream)), outputstream);
    }

    protected abstract void encodeAtom(OutputStream outputstream, byte abyte0[], int i, int j)
        throws IOException;

    public void encodeBuffer(InputStream inputstream, OutputStream outputstream)
        throws IOException
    {
        byte abyte0[] = new byte[bytesPerLine()];
        encodeBufferPrefix(outputstream);
        int j;
        do
        {
            j = readFully(inputstream, abyte0);
            if(j == 0)
            {
                break;
            }
            encodeLinePrefix(outputstream, j);
            for(int i = 0; i < j; i += bytesPerAtom())
            {
                if(i + bytesPerAtom() <= j)
                {
                    encodeAtom(outputstream, abyte0, i, bytesPerAtom());
                } else
                {
                    encodeAtom(outputstream, abyte0, i, j - i);
                }
            }

            encodeLineSuffix(outputstream);
        } while(j >= bytesPerLine());
        encodeBufferSuffix(outputstream);
    }

    public String encodeBuffer(byte abyte0[])
    {
        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
        ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
        try
        {
            encodeBuffer(((InputStream) (bytearrayinputstream)), ((OutputStream) (bytearrayoutputstream)));
        }
        catch(Exception _ex)
        {
            throw new Error("ChracterEncoder::encodeBuffer internal error");
        }
        String s = null;
        try
        {
            s = bytearrayoutputstream.toString("8859_1");
        }
        catch(UnsupportedEncodingException _ex)
        {
            s = bytearrayoutputstream.toString();
        }
        return s;
    }

    public void encodeBuffer(byte abyte0[], OutputStream outputstream)
        throws IOException
    {
        ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
        encodeBuffer(((InputStream) (bytearrayinputstream)), outputstream);
    }

    protected void encodeBufferPrefix(OutputStream outputstream)
        throws IOException
    {
        pStream = new PrintStream(outputstream);
    }

    protected void encodeBufferSuffix(OutputStream outputstream)
        throws IOException
    {
    }

    protected void encodeLinePrefix(OutputStream outputstream, int i)
        throws IOException
    {
    }

    protected void encodeLineSuffix(OutputStream outputstream)
        throws IOException
    {
        outputstream.write(13);
        outputstream.write(10);
    }

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

        return abyte0.length;
    }
}

⌨️ 快捷键说明

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