Top Description Fields Constructors Methods
org.bouncycastle.bcpg

public Class ArmoredInputStream

extends InputStream
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .EOFException, .IOException, .InputStream, org.bouncycastle.util.StringList, .Strings

reader for Base64 armored objects - read the headers and then start returning bytes when the data is reached. An IOException is thrown if the CRC check is detected and fails.

By default a missing CRC will not cause an exception. To force CRC detection use:

    ArmoredInputStream aIn = ...

    aIn.setDetectMissingCRC(true);

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv boolean
pack-priv CRC24
pack-priv boolean
private static final byte[]
private boolean
pack-priv boolean
pack-priv String
pack-priv StringList
pack-priv InputStream
pack-priv boolean
pack-priv int
pack-priv boolean
pack-priv int[]
pack-priv boolean
pack-priv boolean

Constructor Summary

AccessConstructor and Description
public
ArmoredInputStream(InputStream in)

Create a stream for reading a PGP armoured message, parsing up to a header and then reading the data that follows.

public
ArmoredInputStream(InputStream in, boolean
true if headers are to be looked for, false otherwise.
hasHeaders
)

Create an armoured input stream which will assume the data starts straight away, or parse for headers first depending on the value of hasHeaders.

Method Summary

Modifier and TypeMethod and Description
public int
available()

Overrides java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.
private void
checkIndexSize(int size, int off, int len)

public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.
private static int

Returns:

the offset the data starts in out.
decode
(int in0, int in1, int in2, int in3, int[] out)

decode the base 64 encoded input data.

public String

Returns:

the armor header line, null if none present.
getArmorHeaderLine
()

Return the armor header line (if there is one)

public String[]

Returns:

an array of armor headers, null if there aren't any.
getArmorHeaders
()

Return the armor headers (the lines after the armor header line),

public boolean

Returns:

true if we are inside the clear text section of a PGP signed message.
isClearText
()

public boolean

Returns:

true if the stream is actually at end of file.
isEndOfStream
()

private boolean
public int
read()

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the input stream.
public int

Returns:

total number of bytes read into the buffer
read
(byte[]
byte array
b
,
int
offset at which we start writing data to the array
off
,
int
number of bytes we write into the array
len
)

Overrides java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes.
private int
public void
setDetectMissingCRC(boolean
ignore missing CRC sums
detectMissing
)

Change how the stream should react if it encounters missing CRC checksum.

Inherited from java.io.InputStream:
markmarkSupportednullInputStreamreadreadAllBytesreadNBytesreadNBytesresetskipskipNBytestransferTo

Field Detail

bufPtrback to summary
pack-priv int bufPtr
clearTextback to summary
pack-priv boolean clearText
crcback to summary
pack-priv CRC24 crc
crcFoundback to summary
pack-priv boolean crcFound
decodingTableback to summary
private static final byte[] decodingTable
detectMissingChecksumback to summary
private boolean detectMissingChecksum
hasHeadersback to summary
pack-priv boolean hasHeaders
headerListback to summary
pack-priv StringList headerList
inback to summary
pack-priv InputStream in
isEndOfStreamback to summary
pack-priv boolean isEndOfStream
lastCback to summary
pack-priv int lastC
newLineFoundback to summary
pack-priv boolean newLineFound
outBufback to summary
pack-priv int[] outBuf
restartback to summary
pack-priv boolean restart
startback to summary
pack-priv boolean start

Constructor Detail

ArmoredInputStreamback to summary
public ArmoredInputStream(InputStream in) throws IOException

Create a stream for reading a PGP armoured message, parsing up to a header and then reading the data that follows.

ArmoredInputStreamback to summary
public ArmoredInputStream(InputStream in, boolean hasHeaders) throws IOException

Create an armoured input stream which will assume the data starts straight away, or parse for headers first depending on the value of hasHeaders.

Parameters
hasHeaders:boolean

true if headers are to be looked for, false otherwise.

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.InputStream.available.

Doc from java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected. The read might be on the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

A subclass's implementation of this method may choose to throw an IOException if this input stream has been closed by invoking the close() method.

Returns:int

an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or 0 when it reaches the end of the input stream.

Exceptions
IOException:
if an I/O error occurs.
checkIndexSizeback to summary
private void checkIndexSize(int size, int off, int len)
closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.InputStream.close.

Closes this input stream and releases any system resources associated with the stream.

Exceptions
IOException:
if an I/O error occurs.
decodeback to summary
private static int decode(int in0, int in1, int in2, int in3, int[] out) throws IOException

decode the base 64 encoded input data.

Returns:int

the offset the data starts in out.

getArmorHeaderLineback to summary
public String getArmorHeaderLine()

Return the armor header line (if there is one)

Returns:String

the armor header line, null if none present.

getArmorHeadersback to summary
public String[] getArmorHeaders()

Return the armor headers (the lines after the armor header line),

Returns:String[]

an array of armor headers, null if there aren't any.

isClearTextback to summary
public boolean isClearText()
Returns:boolean

true if we are inside the clear text section of a PGP signed message.

isEndOfStreamback to summary
public boolean isEndOfStream()
Returns:boolean

true if the stream is actually at end of file.

parseHeadersback to summary
private boolean parseHeaders() throws IOException
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Returns:int

the next byte of data, or -1 if the end of the stream is reached.

Exceptions
IOException:
if an I/O error occurs.
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer. The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len.

Note

We need to override the custom behavior of Java's InputStream#read(byte[], int, int), as the upstream method silently swallows IOExceptions. This would cause CRC checksum errors to go unnoticed.

Parameters
b:byte[]

byte array

off:int

offset at which we start writing data to the array

len:int

number of bytes we write into the array

Returns:int

total number of bytes read into the buffer

Exceptions
IOException:
if an exception happens AT ANY POINT
See Also
Related BC bug report
readIgnoreSpaceback to summary
private int readIgnoreSpace() throws IOException
setDetectMissingCRCback to summary
public void setDetectMissingCRC(boolean detectMissing)

Change how the stream should react if it encounters missing CRC checksum. The default value is false (ignore missing CRC checksums). If the behavior is set to true, an IOException will be thrown if a missing CRC checksum is encountered.

Parameters
detectMissing:boolean

ignore missing CRC sums