Class CpioArchiveOutputStream

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, CpioConstants

public class CpioArchiveOutputStream extends ArchiveOutputStream<CpioArchiveEntry> implements CpioConstants
CpioArchiveOutputStream is a stream for writing CPIO streams. All formats of CPIO are supported (old ASCII, old binary, new portable format and the new portable format with CRC).

An entry can be written by creating an instance of CpioArchiveEntry and fill it with the necessary values and put it into the CPIO stream. Afterwards write the contents of the file into the CPIO stream. Either close the stream by calling finish() or put a next entry into the cpio stream.

 CpioArchiveOutputStream out = new CpioArchiveOutputStream(
         new FileOutputStream(new File("test.cpio")));
 CpioArchiveEntry entry = new CpioArchiveEntry();
 entry.setName("testfile");
 String contents = "12345";
 entry.setFileSize(contents.length());
 entry.setMode(CpioConstants.C_ISREG); // regular file
 ... set other attributes, e.g. time, number of links
 out.putArchiveEntry(entry);
 out.write(testContents.getBytes());
 out.close();
 

Note: This implementation should be compatible to cpio 2.5

This class uses mutable fields and is not considered threadsafe.

based on code from the jRPM project (jrpm.sourceforge.net)

  • Field Details

    • entry

      private CpioArchiveEntry entry
    • entryFormat

      private final short entryFormat
      See CpioArchiveEntry(short) for possible values.
    • names

      private final HashMap<String,CpioArchiveEntry> names
    • crc

      private long crc
    • written

      private long written
    • blockSize

      private final int blockSize
    • nextArtificalDeviceAndInode

      private long nextArtificalDeviceAndInode
    • encoding

      private final Charset encoding
      The encoding to use for file names and labels.
  • Constructor Details

    • CpioArchiveOutputStream

      public CpioArchiveOutputStream(OutputStream out)
      Constructs the cpio output stream. The format for this CPIO stream is the "new" format using ASCII encoding for file names
      Parameters:
      out - The cpio stream
    • CpioArchiveOutputStream

      public CpioArchiveOutputStream(OutputStream out, short format)
      Constructs the cpio output stream with a specified format, a blocksize of BLOCK_SIZE and using ASCII as the file name encoding.
      Parameters:
      out - The cpio stream
      format - The format of the stream
    • CpioArchiveOutputStream

      public CpioArchiveOutputStream(OutputStream out, short format, int blockSize)
      Constructs the cpio output stream with a specified format using ASCII as the file name encoding.
      Parameters:
      out - The cpio stream
      format - The format of the stream
      blockSize - The block size of the archive.
      Since:
      1.1
    • CpioArchiveOutputStream

      public CpioArchiveOutputStream(OutputStream out, short format, int blockSize, Charset encoding)
      Constructs the cpio output stream with a specified format using ASCII as the file name encoding.
      Parameters:
      out - The cpio stream
      format - The format of the stream
      blockSize - The block size of the archive.
      encoding - The encoding of file names to write - use null for the ASCII.
      Since:
      1.27.1-0
    • CpioArchiveOutputStream

      public CpioArchiveOutputStream(OutputStream out, Charset encoding)
      Constructs the cpio output stream. The format for this CPIO stream is the "new" format.
      Parameters:
      out - The cpio stream
      encoding - The encoding of file names to write - use null for the ASCII.
      Since:
      1.27.1-0
  • Method Details

    • close

      public void close() throws IOException
      Closes the CPIO output stream as well as the stream being filtered.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class ArchiveOutputStream<CpioArchiveEntry>
      Throws:
      IOException - if an I/O error has occurred or if a CPIO file error has occurred
    • closeArchiveEntry

      public void closeArchiveEntry() throws IOException
      Description copied from class: ArchiveOutputStream
      Closes the archive entry, writing any trailer information that may be required.
      Specified by:
      closeArchiveEntry in class ArchiveOutputStream<CpioArchiveEntry>
      Throws:
      IOException - if an I/O error occurs
    • createArchiveEntry

      public CpioArchiveEntry createArchiveEntry(Path inputPath, String entryName, LinkOption... options) throws IOException
      Creates a new CpioArchiveEntry. The entryName must be an ASCII encoded string.
      Specified by:
      createArchiveEntry in class ArchiveOutputStream<CpioArchiveEntry>
      Parameters:
      inputPath - the file to create the entry from
      entryName - name to use for the entry
      options - options indicating how symbolic links are handled.
      Returns:
      the ArchiveEntry set up with details from the file
      Throws:
      IOException - if an I/O error occurs
      See Also:
      • invalid reference
        org.apache.commons.compress.archivers.ArchiveOutputStream#createArchiveEntry(Path, String, LinkOption...)
    • encode

      private byte[] encode(String str)
      Encodes the given string using the configured encoding.
      Parameters:
      str - the String to write
      Returns:
      result of encoding the string
      Throws:
      IOException - if the string couldn't be written
    • finish

      public void finish() throws IOException
      Finishes writing the contents of the CPIO output stream without closing the underlying stream. Use this method when applying multiple filters in succession to the same output stream.
      Overrides:
      finish in class ArchiveOutputStream<CpioArchiveEntry>
      Throws:
      IOException - if an I/O exception has occurred or if a CPIO file error has occurred
    • pad

      private void pad(int count) throws IOException
      Throws:
      IOException
    • putArchiveEntry

      public void putArchiveEntry(CpioArchiveEntry entry) throws IOException
      Begins writing a new CPIO file entry and positions the stream to the start of the entry data. Closes the current entry if still active. The current time will be used if the entry has no set modification time and the default header format will be used if no other format is specified in the entry.
      Specified by:
      putArchiveEntry in class ArchiveOutputStream<CpioArchiveEntry>
      Parameters:
      entry - the CPIO cpioEntry to be written
      Throws:
      IOException - if an I/O error has occurred or if a CPIO file error has occurred
      ClassCastException - if entry is not an instance of CpioArchiveEntry
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Writes an array of bytes to the current CPIO entry data. This method will block until all the bytes are written.
      Overrides:
      write in class FilterOutputStream
      Parameters:
      b - the data to be written
      off - the start offset in the data
      len - the number of bytes that are written
      Throws:
      IOException - if an I/O error has occurred or if a CPIO file error has occurred
    • writeAsciiLong

      private void writeAsciiLong(long number, int length, int radix) throws IOException
      Throws:
      IOException
    • writeBinaryLong

      private void writeBinaryLong(long number, int length, boolean swapHalfWord) throws IOException
      Throws:
      IOException
    • writeCString

      private void writeCString(byte[] str) throws IOException
      Writes an encoded string to the stream followed by \0
      Parameters:
      str - the String to write
      Throws:
      IOException - if the string couldn't be written
    • writeHeader

      private void writeHeader(CpioArchiveEntry e) throws IOException
      Throws:
      IOException
    • writeNewEntry

      private void writeNewEntry(CpioArchiveEntry entry) throws IOException
      Throws:
      IOException
    • writeOldAsciiEntry

      private void writeOldAsciiEntry(CpioArchiveEntry entry) throws IOException
      Throws:
      IOException
    • writeOldBinaryEntry

      private void writeOldBinaryEntry(CpioArchiveEntry entry, boolean swapHalfWord) throws IOException
      Throws:
      IOException