org.bouncycastle.mime.LineReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcpkix-fips Show documentation
Show all versions of bcpkix-fips Show documentation
The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. The APIs are designed primarily to be used in conjunction with the BC FIPS provider. The APIs may also be used with other providers although if being used in a FIPS context it is the responsibility of the user to ensure that any other providers used are FIPS certified.
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