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)
| Modifier and Type | Field and Description |
|---|---|
| private final int | |
| private boolean | |
| private long | |
| private CpioArchiveEntry | |
| private long | |
| private boolean | |
| private final byte[] | fourBytesBuf
Cached buffer - must only be used locally in the class (COMPRESS-172 - reduce garbage collection). |
| private final byte[] | |
| private final byte[] | |
| private final byte[] | twoBytesBuf
Cached buffer - must only be used locally in the class (COMPRESS-172 - reduce garbage collection). |
| private final ZipEncoding | zipEncoding
The encoding to use for file names and labels. |
| Access | Constructor and Description |
|---|---|
| public | CpioArchiveInputStream(final InputStream
The cpio stream in)Constructs the cpio input stream with a blocksize of |
| public | CpioArchiveInputStream(final InputStream
The cpio stream in, final int The block size of the archive. blockSize)Constructs the cpio input stream with a blocksize of |
| public | CpioArchiveInputStream(final InputStream
The cpio stream in, final int The block size of the archive. blockSize, final String The encoding of file names to expect - use null for the platform's default. encoding)Constructs the cpio input stream with a blocksize of |
| public | CpioArchiveInputStream(final InputStream
The cpio stream in, final String The encoding of file names to expect - use null for the platform's default. encoding)Constructs the cpio input stream with a blocksize of |
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: 1 before EOF and 0 after EOF has reached for current entry.Overrides java. |
| public void | close()
Overrides java. Implements java. |
| private void | |
| private void | |
| public CpioArchiveEntry | Returns: the CpioArchiveEntry just read
Deprecated
Use
Reads the next CPIO file entry and positions stream at the beginning of the entry data.
getNextEntry().
|
| public CpioArchiveEntry | getNextEntry()
Implements abstract org. |
| public static boolean | Returns: whether the buffer seems to contain CPIO datadata to match signature, final int length of data 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 |
| public int | Returns: the actual number of bytes read, or -1 if the end of the entry is reachedthe buffer into which the data is read b, final int the start offset of the data off, final int the maximum number of bytes read len)Overrides java. |
| private long | |
| private long | |
| private String | |
| private int | |
| private CpioArchiveEntry | |
| private CpioArchiveEntry | |
| private CpioArchiveEntry | |
| private byte[] | |
| private void | |
| public long | Returns: the actual number of bytes skippedthe number of bytes to skip n)Overrides java. |
| private void |
| blockSize | back to summary |
|---|---|
| private final int blockSize | |
| closed | back to summary |
|---|---|
| private boolean closed | |
| crc | back to summary |
|---|---|
| private long crc | |
| entry | back to summary |
|---|---|
| private CpioArchiveEntry entry | |
| entryBytesRead | back to summary |
|---|---|
| private long entryBytesRead | |
| entryEOF | back to summary |
|---|---|
| private boolean entryEOF | |
| fourBytesBuf | back to summary |
|---|---|
| private final byte[] fourBytesBuf Cached buffer - must only be used locally in the class (COMPRESS-172 - reduce garbage collection). | |
| sixBytesBuf | back to summary |
|---|---|
| private final byte[] sixBytesBuf | |
| tmpbuf | back to summary |
|---|---|
| private final byte[] tmpbuf | |
| twoBytesBuf | back to summary |
|---|---|
| private final byte[] twoBytesBuf Cached buffer - must only be used locally in the class (COMPRESS-172 - reduce garbage collection). | |
| zipEncoding | back to summary |
|---|---|
| private final ZipEncoding zipEncoding The encoding to use for file names and labels. | |
| CpioArchiveInputStream | back to summary |
|---|---|
| public CpioArchiveInputStream(final InputStream in) Constructs the cpio input stream with a blocksize of
| |
| CpioArchiveInputStream | back to summary |
|---|---|
| public CpioArchiveInputStream(final InputStream in, final int blockSize) Constructs the cpio input stream with a blocksize of
| |
| CpioArchiveInputStream | back to summary |
|---|---|
| public CpioArchiveInputStream(final InputStream in, final int blockSize, final String encoding) Constructs the cpio input stream with a blocksize of
| |
| CpioArchiveInputStream | back to summary |
|---|---|
| public CpioArchiveInputStream(final InputStream in, final String encoding) Constructs the cpio input stream with a blocksize of
| |
| available | back to summary |
|---|---|
| public int available() throws IOException Overrides java. 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.
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. Closes the CPIO input stream.
| |
| closeEntry | back to summary |
|---|---|
| private void closeEntry() throws IOException Closes the current CPIO entry and positions the stream for reading the next entry.
| |
| ensureOpen | back to summary |
|---|---|
| private void ensureOpen() throws IOException Check to make sure that this stream has not been closed
| |
| getNextCPIOEntry | back to summary |
|---|---|
| public CpioArchiveEntry getNextCPIOEntry() throws IOException
Deprecated Use Reads the next CPIO file entry and positions stream at the beginning of the entry data.
| |
| getNextEntry | back to summary |
|---|---|
| public CpioArchiveEntry getNextEntry() throws IOException Implements abstract org. Doc from org. Gets the next Archive Entry in this Stream.
| |
| matches | back to summary |
|---|---|
| public static boolean matches(final byte[] signature, final 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
| |
| read | back to summary |
|---|---|
| public int read(final byte[] b, final int off, final int len) throws IOException Overrides java. Reads from the current CPIO entry into an array of bytes. Blocks until some input is available.
| |
| readAsciiLong | back to summary |
|---|---|
| private long readAsciiLong(final int length, final int radix) throws IOException | |
| readBinaryLong | back to summary |
|---|---|
| private long readBinaryLong(final int length, final boolean swapHalfWord) throws IOException | |
| readCString | back to summary |
|---|---|
| private String readCString(final int length) throws IOException | |
| readFully | back to summary |
|---|---|
| private int readFully(final byte[] b, final int off, final int len) throws IOException | |
| readNewEntry | back to summary |
|---|---|
| private CpioArchiveEntry readNewEntry(final boolean hasCrc) throws IOException | |
| readOldAsciiEntry | back to summary |
|---|---|
| private CpioArchiveEntry readOldAsciiEntry() throws IOException | |
| readOldBinaryEntry | back to summary |
|---|---|
| private CpioArchiveEntry readOldBinaryEntry(final boolean swapHalfWord) throws IOException | |
| readRange | back to summary |
|---|---|
| private byte[] readRange(final int len) throws IOException | |
| skip | back to summary |
|---|---|
| private void skip(final int bytes) throws IOException | |
| skip | back to summary |
|---|---|
| public long skip(final long n) throws IOException Overrides java. Skips specified number of bytes in the current CPIO entry.
| |
| skipRemainderOfLastBlock | back to summary |
|---|---|
| private void skipRemainderOfLastBlock() throws IOException Skips the padding zeros written after the TRAILER!!! entry. | |