All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.mime.LineReader Maven / Gradle / Ivy

Go to download

The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. This jar contains APIs for JDK 1.5 to JDK 11. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs.

There is a newer version: 1.70
Show newest version
package org.bouncycastle.mime;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.bouncycastle.util.Strings;

/**
 * Read regular text lines, allowing for a single character look ahead.
 */
class LineReader
{
    private final InputStream src;

    private int lastC = -1;

    LineReader(InputStream src)
    {
        this.src = src;
    }

    String readLine()
        throws IOException
    {
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();

        int ch;

        if (lastC != -1)
        {
            if (lastC == '\r')   // to get this we must have '\r\r' so blank line
            {
                return "";
            }
            ch = lastC;
            lastC = -1;
        }
        else
        {
            ch = src.read();
        }

        while (ch >= 0 && ch != '\r' && ch != '\n')
        {
            bOut.write(ch);
            ch = src.read();
        }

        if (ch == '\r')
        {
            int c = src.read();
            if (c != '\n' && c >= 0)
            {
                lastC = c;
            }
        }

        if (ch < 0)
        {
            return null;
        }

        return Strings.fromUTF8ByteArray(bOut.toByteArray());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy