public class CpioArchiveInputStream extends ArchiveInputStream implements CpioConstants
CPIOArchiveInputStream cpioIn = new CPIOArchiveInputStream(
new FileInputStream(new File("test.cpio")));
CPIOArchiveEntry cpioEntry;
while ((cpioEntry = cpioIn.getNextEntry()) != null) {
System.out.println(cpioEntry.getName());
int tmp;
StringBuffer buf = new StringBuffer();
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 boolean |
closed |
private long |
crc |
private CpioArchiveEntry |
entry |
private long |
entryBytesRead |
private boolean |
entryEOF |
private java.io.InputStream |
in |
private byte[] |
tmpbuf |
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 and Description |
---|
CpioArchiveInputStream(java.io.InputStream in)
Construct the cpio input stream
|
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns 0 after EOF has reached for the current entry data, otherwise
always return 1.
|
void |
close()
Closes the CPIO input stream.
|
private void |
closeEntry()
Closes the current CPIO entry and positions the stream for reading the
next entry.
|
private void |
ensureOpen()
Check to make sure that this stream has not been closed
|
CpioArchiveEntry |
getNextCPIOEntry()
Reads the next CPIO file entry and positions stream at the beginning of
the entry data.
|
ArchiveEntry |
getNextEntry()
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 0xC771
|
int |
read(byte[] b,
int off,
int len)
Reads from the current CPIO entry into an array of bytes.
|
private long |
readAsciiLong(int length,
int radix) |
private long |
readBinaryLong(int length,
boolean swapHalfWord) |
private java.lang.String |
readCString(int length) |
private int |
readFully(byte[] b,
int off,
int len) |
private CpioArchiveEntry |
readNewEntry(boolean hasCrc) |
private CpioArchiveEntry |
readOldAsciiEntry() |
private CpioArchiveEntry |
readOldBinaryEntry(boolean swapHalfWord) |
private void |
skip(int bytes) |
long |
skip(long n)
Skips specified number of bytes in the current CPIO entry.
|
canReadEntryData, count, count, getBytesRead, getCount, pushedBackBytes, read
private boolean closed
private CpioArchiveEntry entry
private long entryBytesRead
private boolean entryEOF
private final byte[] tmpbuf
private long crc
private final java.io.InputStream in
public CpioArchiveInputStream(java.io.InputStream in)
in
- The cpio streampublic int available() throws java.io.IOException
available
in class java.io.InputStream
java.io.IOException
- if an I/O error has occurred or if a CPIO file error has
occurredpublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
- if an I/O error has occurredprivate void closeEntry() throws java.io.IOException
java.io.IOException
- if an I/O error has occurred or if a CPIO file error has
occurredprivate void ensureOpen() throws java.io.IOException
java.io.IOException
- if the stream is already closedpublic CpioArchiveEntry getNextCPIOEntry() throws java.io.IOException
java.io.IOException
- if an I/O error has occurred or if a CPIO file error has
occurredprivate void skip(int bytes) throws java.io.IOException
java.io.IOException
public int read(byte[] b, int off, int len) throws java.io.IOException
read
in class java.io.InputStream
b
- the buffer into which the data is readoff
- the start offset of the datalen
- the maximum number of bytes readjava.io.IOException
- if an I/O error has occurred or if a CPIO file error has
occurredprivate final int readFully(byte[] b, int off, int len) throws java.io.IOException
java.io.IOException
private long readBinaryLong(int length, boolean swapHalfWord) throws java.io.IOException
java.io.IOException
private long readAsciiLong(int length, int radix) throws java.io.IOException
java.io.IOException
private CpioArchiveEntry readNewEntry(boolean hasCrc) throws java.io.IOException
java.io.IOException
private CpioArchiveEntry readOldAsciiEntry() throws java.io.IOException
java.io.IOException
private CpioArchiveEntry readOldBinaryEntry(boolean swapHalfWord) throws java.io.IOException
java.io.IOException
private java.lang.String readCString(int length) throws java.io.IOException
java.io.IOException
public long skip(long n) throws java.io.IOException
skip
in class java.io.InputStream
n
- the number of bytes to skipjava.io.IOException
- if an I/O error has occurredjava.lang.IllegalArgumentException
- if n < 0public ArchiveEntry getNextEntry() throws java.io.IOException
getNextEntry
in class ArchiveInputStream
null
if there are no more entriesjava.io.IOException
- if the next entry could not be readpublic static boolean matches(byte[] signature, int length)