Class ThreatWriter

java.lang.Object
java.io.Writer
be.kuleuven.cs.distrinet.sparta.io.ThreatWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable
Direct Known Subclasses:
ThreatCSVWriter, ThreatTextWriter

public abstract class ThreatWriter extends Writer
Writes Threats to a character-output stream. A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system property line.separator. Not all platforms use the newline character ('\n') to terminate lines. Calling this method to terminate each output line is therefore preferred to writing a newline character directly.
  • Field Details

    • writer

      protected final Writer writer
  • Constructor Details

    • ThreatWriter

      public ThreatWriter(Writer writer)
      Create a new ThreatWriter to provided support for writing out a default set of threat properties to text-based files.
      Parameters:
      writer - a writer
    • ThreatWriter

      public ThreatWriter(Writer writer, Map<String,Function<Threat,? extends Object>> properties)
      Create a new ThreatWriter to provided support for writing out a specified set of threat properties to text-based files. If any of the provided functions return a ModelElement, the ThreatWriter will attempt to retrieve the name of the element when writing it to the output writer.
      Parameters:
      writer - a writer
      properties - a set of functions, which applied to a threat provide the property to write out
  • Method Details

    • close

      public void close() throws IOException
      Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Writer
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Flushes the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams. If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class Writer
      Throws:
      IOException
    • write

      public void write(char[] cbuf, int off, int len) throws IOException
      Writes a portion of an array of characters.
      Specified by:
      write in class Writer
      Parameters:
      cbuf - Array of characters
      off - Offset from which to start writing characters
      len - Number of characters to write
      Throws:
      IndexOutOfBoundsException - Implementations should throw this exception if off is negative, or len is negative, or off + len is negative or greater than the length of the given array
      IOException - If an I/O error occurs
    • getProperties

      protected Map<String,Function<Threat,? extends Object>> getProperties()
      Retrieve the set of properties that are being exported by this ThreatWriter.
      Returns:
      a Map containing the property name and the function to apply on a threat to retrieve that property
    • write

      public abstract void write(Threat threat) throws IOException
      Write-out a threat
      Parameters:
      threat - the Threat to write out
      Throws:
      IOException - If an I/O error occurs.
    • write

      public void write(Threat... threats) throws IOException
      Write-out multiple threats
      Parameters:
      threats - the Threats to write out
      Throws:
      IOException - If an I/O error occurs.
    • newLine

      public void newLine() throws IOException
      Writes a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.
      Throws:
      IOException - If an I/O error occurs.