Class WadFile.Adder

java.lang.Object
net.mtrop.doom.WadFile.Adder
All Implemented Interfaces:
AutoCloseable
Enclosing class:
WadFile

public class WadFile.Adder extends Object implements AutoCloseable
Bulk add mechanism for WadFile. All methods on this object manipulate the WadFile it is created from, and defers the final writing of the entry list until it is closed. This object is meant to be created via a try-with-resources block, like so:
 try (WadFile.Adder adder = wad.createAdder())
 {
     adder.addData(....);
     ...
 }
 
...upon which the entries are committed on close. This will still commit the list even on an error occurring during add, unless the the writing of the list results in an error as well.
Since:
2.7.0
  • Method Details

    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException
    • addEntry

      public WadEntry addEntry(String entryName, int offset, int length) throws IOException
      Adds a new entry to the Wad, but with an explicit offset and size. Exercise caution with this method, since you can reference anywhere in the Wad!

      NOTE: The entry is not written until close() is called (or the WadFile.Adder is closed automatically).

      Parameters:
      entryName - the name of the entry.
      offset - the entry's content start byte.
      length - the entry's length in bytes.
      Returns:
      the entry that was created.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name, or the offset/size is negative.
      IOException - if the entry cannot be written.
      NullPointerException - if name is null.
    • addMarker

      public WadEntry addMarker(String entryName) throws IOException
      Adds an entry marker to the Wad (entry with 0 size, arbitrary offset).
      Parameters:
      entryName - the name of the entry.
      Returns:
      the entry that was added.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IOException - if the entry cannot be written.
      NullPointerException - if name is null.
    • addMarkerAt

      public WadEntry addMarkerAt(int index, String entryName) throws IOException
      Adds an entry marker to the Wad (entry with 0 size, arbitrary offset).
      Parameters:
      index - the index at which to add the marker.
      entryName - the name of the entry.
      Returns:
      the entry that was added.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IOException - if the entry cannot be written.
      NullPointerException - if name is null.
    • addData

      public WadEntry addData(String entryName, byte[] data) throws IOException
      Adds data to this Wad, using entryName as the name of the new entry. The overhead for multiple additions may be expensive I/O-wise depending on the Wad implementation.
      Parameters:
      entryName - the name of the entry to add this as.
      data - the bytes of data to add as this wad's data.
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IOException - if the data cannot be written.
      NullPointerException - if entryName or data is null.
    • addData

      public <BO extends BinaryObject> WadEntry addData(String entryName, BO data) throws IOException
      Adds data to this Wad, using entryName as the name of the new entry. The overhead for multiple additions may be expensive I/O-wise depending on the Wad implementation.
      Type Parameters:
      BO - a BinaryObject type.
      Parameters:
      entryName - the name of the entry to add this as.
      data - the BinaryObject to add as this wad's data (converted via BinaryObject.toBytes()).
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IOException - if the data cannot be written.
      NullPointerException - if entryName or data is null.
    • addData

      public <BO extends BinaryObject> WadEntry addData(String entryName, BO[] data) throws IOException
      Adds data to this Wad, using entryName as the name of the new entry. The BinaryObjects provided have all of their converted data concatenated together as one blob of contiguous data. The overhead for multiple additions may be expensive I/O-wise depending on the Wad implementation.
      Type Parameters:
      BO - a BinaryObject type.
      Parameters:
      entryName - the name of the entry to add this as.
      data - the BinaryObjects to add as this wad's data (converted via BinaryObject.toBytes()).
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IOException - if the data cannot be written.
      NullPointerException - if entryName or data is null.
    • addData

      public <TO extends TextObject> WadEntry addData(String entryName, TO data, Charset encoding) throws IOException
      Adds data to this Wad, using entryName as the name of the new entry. The overhead for multiple additions may be expensive I/O-wise depending on the Wad implementation.
      Type Parameters:
      TO - a TextObject type.
      Parameters:
      entryName - the name of the entry to add this as.
      data - the TextObject to add as this wad's data (converted via TextObject.toText(), then String.getBytes(Charset)).
      encoding - the encoding type for the data written to the Wad.
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IOException - if the data cannot be written.
      NullPointerException - if entryName or data or encoding is null.
    • addData

      public WadEntry addData(String entryName, File fileToAdd) throws IOException
      Adds data to this Wad, using entryName as the name of the new entry. The provided File is read until the end of the file is reached. The overhead for multiple individual additions may be expensive I/O-wise depending on the Wad implementation.
      Parameters:
      entryName - the name of the entry to add this as.
      fileToAdd - the file to add the contents of.
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      FileNotFoundException - if the file path refers to a file that is a directory or doesn't exist.
      IOException - if the data cannot be written or the stream could not be read.
      NullPointerException - if entryName or data or encoding is null.
      Since:
      2.7.0
    • addData

      public WadEntry addData(String entryName, InputStream in) throws IOException
      Adds data to this Wad, using entryName as the name of the new entry. The provided input stream is read until the end of the stream is reached. The overhead for multiple additions may be expensive I/O-wise depending on the Wad implementation.
      Parameters:
      entryName - the name of the entry to add this as.
      in - the input stream to read.
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IOException - if the data cannot be written or the stream could not be read.
      NullPointerException - if entryName or data or encoding is null.
    • addData

      public WadEntry addData(String entryName, InputStream in, int maxLength) throws IOException
      Adds data to this Wad, using entryName as the name of the new entry. The provided input stream is read until the end of the stream is reached or maxLength bytes are read. The overhead for multiple additions may be expensive I/O-wise depending on the Wad implementation.
      Parameters:
      entryName - the name of the entry to add this as.
      in - the input stream to read.
      maxLength - the maximum amount of bytes to read from the InputStream, or a value < 0 to keep reading until end-of-stream.
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IOException - if the data cannot be written or the stream could not be read.
      NullPointerException - if entryName or data or encoding is null.
    • addDataAt

      public WadEntry addDataAt(int index, String entryName, byte[] data) throws IOException
      Adds data to this Wad at a particular entry offset, using entryName as the name of the entry. The rest of the entries in the wad are shifted down one index. The overhead for multiple additions may be expensive I/O-wise depending on the Wad implementation.
      Parameters:
      index - the index at which to add the entry.
      entryName - the name of the entry to add this as.
      data - the bytes of data to add as this wad's data.
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IndexOutOfBoundsException - if the provided index < 0 or > getEntryCount().
      IOException - if the data cannot be written.
      NullPointerException - if entryName or data is null.
    • addDataAt

      public <BO extends BinaryObject> WadEntry addDataAt(int index, String entryName, BO data) throws IOException
      Adds data to this Wad at a particular entry offset, using entryName as the name of the entry. The rest of the entries in the wad are shifted down one index. The overhead for multiple additions may be expensive I/O-wise depending on the Wad implementation.
      Type Parameters:
      BO - a BinaryObject type.
      Parameters:
      index - the index at which to add the entry.
      entryName - the name of the entry to add this as.
      data - the BinaryObject to add as this wad's data (converted via BinaryObject.toBytes()).
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IndexOutOfBoundsException - if the provided index < 0 or > getEntryCount().
      IOException - if the data cannot be written.
      NullPointerException - if entryName or data is null.
    • addDataAt

      public <BO extends BinaryObject> WadEntry addDataAt(int index, String entryName, BO[] data) throws IOException
      Adds data to this Wad at a particular entry offset, using entryName as the name of the entry. The rest of the entries in the wad are shifted down one index. The BinaryObjects provided have all of their converted data concatenated together as one blob of contiguous data. The overhead for multiple additions may be expensive I/O-wise depending on the Wad implementation.
      Type Parameters:
      BO - a BinaryObject type.
      Parameters:
      index - the index at which to add the entry.
      entryName - the name of the entry to add this as.
      data - the BinaryObjects to add as this wad's data (converted via BinaryObject.toBytes()).
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IndexOutOfBoundsException - if the provided index < 0 or > getEntryCount().
      IOException - if the data cannot be written.
      NullPointerException - if entryName or data is null.
    • addDataAt

      public <TO extends TextObject> WadEntry addDataAt(int index, String entryName, TO data, Charset encoding) throws IOException
      Adds data to this Wad at a particular entry offset, using entryName as the name of the entry. The rest of the entries in the wad are shifted down one index. The overhead for multiple additions may be expensive I/O-wise depending on the Wad implementation.
      Type Parameters:
      TO - a TextObject type.
      Parameters:
      index - the index at which to add the entry.
      entryName - the name of the entry to add this as.
      data - the TextObject to add as this wad's data (converted via TextObject.toText(), then String.getBytes(Charset)).
      encoding - the encoding type for the data written to the Wad.
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IndexOutOfBoundsException - if the provided index < 0 or > getEntryCount().
      IOException - if the data cannot be written.
      NullPointerException - if entryName or data is null.
    • addDataAt

      public WadEntry addDataAt(int index, String entryName, File fileToAdd) throws IOException
      Adds data to this Wad, using entryName as the name of the new entry. The provided File is read until the end of the file is reached. The overhead for multiple individual additions may be expensive I/O-wise depending on the Wad implementation.
      Parameters:
      index - the index at which to add the entry.
      entryName - the name of the entry to add this as.
      fileToAdd - the file to add the contents of.
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      FileNotFoundException - if the file path refers to a file that is a directory or doesn't exist.
      IOException - if the data cannot be written or the stream could not be read.
      NullPointerException - if entryName or data or encoding is null.
      Since:
      2.7.0
    • addDataAt

      public WadEntry addDataAt(int index, String entryName, InputStream in) throws IOException
      Adds data to this Wad at a particular entry offset, using entryName as the name of the entry. The provided input stream is read until the end of the stream is reached. The rest of the entries in the wad are shifted down one index. The overhead for multiple additions may be expensive I/O-wise depending on the Wad implementation.
      Parameters:
      index - the index at which to add the entry.
      entryName - the name of the entry to add this as.
      in - the input stream to read.
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IndexOutOfBoundsException - if the provided index < 0 or > getEntryCount().
      IOException - if the data cannot be written or the stream could not be read.
      NullPointerException - if entryName or data is null.
    • addDataAt

      public WadEntry addDataAt(int index, String entryName, InputStream in, int maxLength) throws IOException
      Adds data to this Wad at a particular entry offset, using entryName as the name of the entry. The provided input stream is read until the end of the stream is reached or maxLength bytes are read. The rest of the entries in the wad are shifted down one index. The overhead for multiple additions may be expensive I/O-wise depending on the Wad implementation.
      Parameters:
      index - the index at which to add the entry.
      entryName - the name of the entry to add this as.
      in - the input stream to read.
      maxLength - the maximum amount of bytes to read from the InputStream, or a value < 0 to keep reading until end-of-stream.
      Returns:
      a WadEntry that describes the added data.
      Throws:
      IllegalArgumentException - if the provided name is not a valid name.
      IndexOutOfBoundsException - if the provided index < 0 or > getEntryCount().
      IOException - if the data cannot be written or the stream could not be read.
      NullPointerException - if entryName or data is null.