Class CpioArchiveInputStream

java.lang.Object
java.io.InputStream
org.apache.commons.compress.archivers.ArchiveInputStream
org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream
All Implemented Interfaces:
Closeable, AutoCloseable, CpioConstants

public class CpioArchiveInputStream extends ArchiveInputStream implements CpioConstants
CpioArchiveInputStream is a stream for reading cpio streams. All formats of cpio are supported (old ascii, old binary, new portable format and the new portable format with crc).

The stream can be read by extracting a cpio entry (containing all informations about a entry) and afterwards reading from the stream the file specified by the entry.

 CpioArchiveInputStream cpioIn = new CpioArchiveInputStream(
         Files.newInputStream(Paths.get("test.cpio")));
 CpioArchiveEntry cpioEntry;

 while ((cpioEntry = cpioIn.getNextEntry()) != null) {
     System.out.println(cpioEntry.getName());
     int tmp;
     StringBuilder buf = new StringBuilder();
     while ((tmp = cpIn.read()) != -1) {
         buf.append((char) tmp);
     }
     System.out.println(buf.toString());
 }
 cpioIn.close();
 

Note: This implementation should be compatible to cpio 2.5

This class uses mutable fields and is not considered to be threadsafe.

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

  • Field Details

    • closed

      private boolean closed
    • entry

      private CpioArchiveEntry entry
    • entryBytesRead

      private long entryBytesRead
    • entryEOF

      private boolean entryEOF
    • tmpbuf

      private final byte[] tmpbuf
    • crc

      private long crc
    • in

      private final InputStream in
    • twoBytesBuf

      private final byte[] twoBytesBuf
    • fourBytesBuf

      private final byte[] fourBytesBuf
    • sixBytesBuf

      private final byte[] sixBytesBuf
    • blockSize

      private final int blockSize
    • zipEncoding

      private final ZipEncoding zipEncoding
      The encoding to use for file names and labels.
    • encoding

      final String encoding
  • Constructor Details

    • CpioArchiveInputStream

      public CpioArchiveInputStream(InputStream in)
      Construct the cpio input stream with a blocksize of BLOCK_SIZE and expecting ASCII file names.
      Parameters:
      in - The cpio stream
    • CpioArchiveInputStream

      public CpioArchiveInputStream(InputStream in, String encoding)
      Construct the cpio input stream with a blocksize of BLOCK_SIZE.
      Parameters:
      in - The cpio stream
      encoding - The encoding of file names to expect - use null for the platform's default.
      Since:
      1.6
    • CpioArchiveInputStream

      public CpioArchiveInputStream(InputStream in, int blockSize)
      Construct the cpio input stream with a blocksize of BLOCK_SIZE expecting ASCII file names.
      Parameters:
      in - The cpio stream
      blockSize - The block size of the archive.
      Since:
      1.5
    • CpioArchiveInputStream

      public CpioArchiveInputStream(InputStream in, int blockSize, String encoding)
      Construct the cpio input stream with a blocksize of BLOCK_SIZE.
      Parameters:
      in - The cpio stream
      blockSize - The block size of the archive.
      encoding - The encoding of file names to expect - use null for the platform's default.
      Throws:
      IllegalArgumentException - if blockSize is not bigger than 0
      Since:
      1.6
  • Method Details

    • available

      public int available() throws IOException
      Returns 0 after EOF has reached for the current entry data, otherwise always return 1.

      Programs should not count on this method to return the actual number of bytes that could be read without blocking.

      Overrides:
      available in class InputStream
      Returns:
      1 before EOF and 0 after EOF has reached for current entry.
      Throws:
      IOException - if an I/O error has occurred or if a CPIO file error has occurred
    • close

      public void close() throws IOException
      Closes the CPIO input stream.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException - if an I/O error has occurred
    • closeEntry

      private void closeEntry() throws IOException
      Closes the current CPIO entry and positions the stream for reading the next entry.
      Throws:
      IOException - if an I/O error has occurred or if a CPIO file error has occurred
    • ensureOpen

      private void ensureOpen() throws IOException
      Check to make sure that this stream has not been closed
      Throws:
      IOException - if the stream is already closed
    • getNextCPIOEntry

      public CpioArchiveEntry getNextCPIOEntry() throws IOException
      Reads the next CPIO file entry and positions stream at the beginning of the entry data.
      Returns:
      the CpioArchiveEntry just read
      Throws:
      IOException - if an I/O error has occurred or if a CPIO file error has occurred
    • skip

      private void skip(int bytes) throws IOException
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Reads from the current CPIO entry into an array of bytes. Blocks until some input is available.
      Overrides:
      read in class InputStream
      Parameters:
      b - the buffer into which the data is read
      off - the start offset of the data
      len - the maximum number of bytes read
      Returns:
      the actual number of bytes read, or -1 if the end of the entry is reached
      Throws:
      IOException - if an I/O error has occurred or if a CPIO file error has occurred
    • readFully

      private final int readFully(byte[] b, int off, int len) throws IOException
      Throws:
      IOException
    • readRange

      private final byte[] readRange(int len) throws IOException
      Throws:
      IOException
    • readBinaryLong

      private long readBinaryLong(int length, boolean swapHalfWord) throws IOException
      Throws:
      IOException
    • readAsciiLong

      private long readAsciiLong(int length, int radix) throws IOException
      Throws:
      IOException
    • readNewEntry

      private CpioArchiveEntry readNewEntry(boolean hasCrc) throws IOException
      Throws:
      IOException
    • readOldAsciiEntry

      private CpioArchiveEntry readOldAsciiEntry() throws IOException
      Throws:
      IOException
    • readOldBinaryEntry

      private CpioArchiveEntry readOldBinaryEntry(boolean swapHalfWord) throws IOException
      Throws:
      IOException
    • readCString

      private String readCString(int length) throws IOException
      Throws:
      IOException
    • skip

      public long skip(long n) throws IOException
      Skips specified number of bytes in the current CPIO entry.
      Overrides:
      skip in class InputStream
      Parameters:
      n - the number of bytes to skip
      Returns:
      the actual number of bytes skipped
      Throws:
      IOException - if an I/O error has occurred
      IllegalArgumentException - if n < 0
    • getNextEntry

      public ArchiveEntry getNextEntry() throws IOException
      Description copied from class: ArchiveInputStream
      Returns the next Archive Entry in this Stream.
      Specified by:
      getNextEntry in class ArchiveInputStream
      Returns:
      the next entry, or null if there are no more entries
      Throws:
      IOException - if the next entry could not be read
    • skipRemainderOfLastBlock

      private void skipRemainderOfLastBlock() throws IOException
      Skips the padding zeros written after the TRAILER!!! entry.
      Throws:
      IOException
    • matches

      public static boolean matches(byte[] signature, int length)
      Checks if the signature matches one of the following magic values: Strings: "070701" - MAGIC_NEW "070702" - MAGIC_NEW_CRC "070707" - MAGIC_OLD_ASCII Octal Binary value: 070707 - MAGIC_OLD_BINARY (held as a short) = 0x71C7 or 0xC771
      Parameters:
      signature - data to match
      length - length of data
      Returns:
      whether the buffer seems to contain CPIO data