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
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 information about an 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 Summary
Fields inherited from interface org.apache.commons.compress.archivers.cpio.CpioConstants
BLOCK_SIZE, C_IRGRP, C_IROTH, C_IRUSR, C_ISBLK, C_ISCHR, C_ISDIR, C_ISFIFO, C_ISGID, C_ISLNK, C_ISNWK, C_ISREG, C_ISSOCK, C_ISUID, C_ISVTX, C_IWGRP, C_IWOTH, C_IWUSR, C_IXGRP, C_IXOTH, C_IXUSR, CPIO_TRAILER, FORMAT_NEW, FORMAT_NEW_CRC, FORMAT_NEW_MASK, FORMAT_OLD_ASCII, FORMAT_OLD_BINARY, FORMAT_OLD_MASK, MAGIC_NEW, MAGIC_NEW_CRC, MAGIC_OLD_ASCII, MAGIC_OLD_BINARY, S_IFMT
-
Constructor Summary
ConstructorDescriptionConstruct the cpio input stream with a blocksize ofBLOCK_SIZE
and expecting ASCII file names.CpioArchiveInputStream
(InputStream in, int blockSize) Construct the cpio input stream with a blocksize ofBLOCK_SIZE
expecting ASCII file names.CpioArchiveInputStream
(InputStream in, int blockSize, String encoding) Construct the cpio input stream with a blocksize ofBLOCK_SIZE
.CpioArchiveInputStream
(InputStream in, String encoding) Construct the cpio input stream with a blocksize ofBLOCK_SIZE
. -
Method Summary
Modifier and TypeMethodDescriptionint
Returns 0 after EOF has reached for the current entry data, otherwise always return 1.void
close()
Closes the CPIO input stream.Reads the next CPIO file entry and positions stream at the beginning of the entry data.Returns the next Archive Entry in this Stream.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 0xC771int
read
(byte[] b, int off, int len) Reads from the current CPIO entry into an array of bytes.long
skip
(long n) Skips specified number of bytes in the current CPIO entry.Methods inherited from class org.apache.commons.compress.archivers.ArchiveInputStream
canReadEntryData, count, count, getBytesRead, getCount, pushedBackBytes, read
Methods inherited from class java.io.InputStream
mark, markSupported, read, reset
-
Constructor Details
-
CpioArchiveInputStream
Construct the cpio input stream with a blocksize ofBLOCK_SIZE
and expecting ASCII file names.- Parameters:
in
- The cpio stream
-
CpioArchiveInputStream
Construct the cpio input stream with a blocksize ofBLOCK_SIZE
expecting ASCII file names.- Parameters:
in
- The cpio streamblockSize
- The block size of the archive.- Since:
- 1.5
-
CpioArchiveInputStream
Construct the cpio input stream with a blocksize ofBLOCK_SIZE
.- Parameters:
in
- The cpio streamblockSize
- The block size of the archive.encoding
- The encoding of file names to expect - use null for the platform's default.- Throws:
IllegalArgumentException
- ifblockSize
is not bigger than 0- Since:
- 1.6
-
CpioArchiveInputStream
Construct the cpio input stream with a blocksize ofBLOCK_SIZE
.- Parameters:
in
- The cpio streamencoding
- The encoding of file names to expect - use null for the platform's default.- Since:
- 1.6
-
-
Method Details
-
matches
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 matchlength
- length of data- Returns:
- whether the buffer seems to contain CPIO data
-
available
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 classInputStream
- 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
Closes the CPIO input stream.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
- if an I/O error has occurred
-
getNextCPIOEntry
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
-
getNextEntry
Description copied from class:ArchiveInputStream
Returns the next Archive Entry in this Stream.- Specified by:
getNextEntry
in classArchiveInputStream
- Returns:
- the next entry,
or
null
if there are no more entries - Throws:
IOException
- if the next entry could not be read
-
read
Reads from the current CPIO entry into an array of bytes. Blocks until some input is available.- Overrides:
read
in classInputStream
- Parameters:
b
- the buffer into which the data is readoff
- the start offset of the datalen
- 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
-
skip
Skips specified number of bytes in the current CPIO entry.- Overrides:
skip
in classInputStream
- Parameters:
n
- the number of bytes to skip- Returns:
- the actual number of bytes skipped
- Throws:
IOException
- if an I/O error has occurredIllegalArgumentException
- if n < 0
-