Class ArmoredOutputStream

java.lang.Object
java.io.OutputStream
org.bouncycastle.bcpg.ArmoredOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class ArmoredOutputStream extends OutputStream
Output stream that writes data in ASCII Armored format.

Note 1: close() needs to be called on an ArmoredOutputStream to write the final checksum. flush() will not do this as other classes assume it is always fine to call flush() - it is not though if the checksum gets output. Note 2: as multiple PGP blobs are often written to the same stream, close() does not close the underlying stream.

  • Field Details

  • Constructor Details

    • ArmoredOutputStream

      public ArmoredOutputStream(OutputStream out)
      Constructs an armored output stream with default headers.
      Parameters:
      out - the OutputStream to wrap.
    • ArmoredOutputStream

      public ArmoredOutputStream(OutputStream out, Hashtable<String,String> headers)
      Constructs an armored output stream with default and custom headers.
      Parameters:
      out - the OutputStream to wrap.
      headers - additional headers that add to or override the default headers.
  • Method Details

    • setHeader

      @Deprecated public void setHeader(String name, String value)
      Deprecated.
      use appropriate methods in ArmoredOutputStream.Builder instead.
      Set an additional header entry. Any current value(s) under the same name will be replaced by the new one. A null value will clear the entry for name.
      Parameters:
      name - the name of the header entry.
      value - the value of the header entry.
    • clearHeaders

      @Deprecated public void clearHeaders()
      Deprecated.
      use appropriate methods in ArmoredOutputStream.Builder instead.
      Remove all headers.
    • addHeader

      @Deprecated public void addHeader(String name, String value)
      Deprecated.
      use appropriate methods in ArmoredOutputStream.Builder instead
      Set an additional header entry. The current value(s) will continue to exist together with the new one. Adding a null value has no effect.
      Parameters:
      name - the name of the header entry.
      value - the value of the header entry.
    • resetHeaders

      @Deprecated public void resetHeaders()
      Reset the headers to only contain a Version string (if one is present)
    • beginClearText

      public void beginClearText(int hashAlgorithm) throws IOException
      Start a clear text signed message - backwards compatibility.
      Parameters:
      hashAlgorithm - hash algorithm
      Throws:
      IOException
    • beginClearText

      public void beginClearText(int... hashAlgorithms) throws IOException
      Start a clear text signed message.

      The text written afterwards is dash-escaped line by line. Only a LF (or CR LF) ends a line; a CR not followed by a LF is part of the text, as in RFC 9580 sec. 7, so when calculating the signature split the text into lines on LF only and strip trailing white space, CR included.

      Parameters:
      hashAlgorithms - hash algorithms
      Throws:
      IOException
    • endClearText

      public void endClearText()
      End the clear text of a cleartext signed message. If the text written did not end with a LF, a line ending is written before the signature armor that follows, as the signature armor must start a line (a CR not followed by a LF does not end one, see beginClearText).
    • write

      public void write(int b) throws IOException
      Specified by:
      write in class OutputStream
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Note: close() does not close the underlying stream. So it is possible to write multiple objects using armoring to a single stream.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
    • builder

      public static ArmoredOutputStream.Builder builder()