Class CpioArchiveInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.commons.compress.archivers.ArchiveInputStream
-
- org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.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 Summary
Fields Modifier and Type Field Description private intblockSizeprivate booleanclosedprivate longcrc(package private) java.lang.Stringencodingprivate CpioArchiveEntryentryprivate longentryBytesReadprivate booleanentryEOFprivate byte[]fourBytesBufprivate java.io.InputStreaminprivate byte[]sixBytesBufprivate byte[]tmpbufprivate byte[]twoBytesBufprivate ZipEncodingzipEncodingThe encoding to use for file names and labels.-
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
Constructors Constructor Description CpioArchiveInputStream(java.io.InputStream in)Construct the cpio input stream with a blocksize ofBLOCK_SIZEand expecting ASCII file names.CpioArchiveInputStream(java.io.InputStream in, int blockSize)Construct the cpio input stream with a blocksize ofBLOCK_SIZEexpecting ASCII file names.CpioArchiveInputStream(java.io.InputStream in, int blockSize, java.lang.String encoding)Construct the cpio input stream with a blocksize ofBLOCK_SIZE.CpioArchiveInputStream(java.io.InputStream in, java.lang.String encoding)Construct the cpio input stream with a blocksize ofBLOCK_SIZE.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Returns 0 after EOF has reached for the current entry data, otherwise always return 1.voidclose()Closes the CPIO input stream.private voidcloseEntry()Closes the current CPIO entry and positions the stream for reading the next entry.private voidensureOpen()Check to make sure that this stream has not been closedCpioArchiveEntrygetNextCPIOEntry()Reads the next CPIO file entry and positions stream at the beginning of the entry data.ArchiveEntrygetNextEntry()Returns the next Archive Entry in this Stream.static booleanmatches(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 0xC771intread(byte[] b, int off, int len)Reads from the current CPIO entry into an array of bytes.private longreadAsciiLong(int length, int radix)private longreadBinaryLong(int length, boolean swapHalfWord)private java.lang.StringreadCString(int length)private intreadFully(byte[] b, int off, int len)private CpioArchiveEntryreadNewEntry(boolean hasCrc)private CpioArchiveEntryreadOldAsciiEntry()private CpioArchiveEntryreadOldBinaryEntry(boolean swapHalfWord)private byte[]readRange(int len)private voidskip(int bytes)longskip(long n)Skips specified number of bytes in the current CPIO entry.private voidskipRemainderOfLastBlock()Skips the padding zeros written after the TRAILER!!! entry.-
Methods inherited from class org.apache.commons.compress.archivers.ArchiveInputStream
canReadEntryData, count, count, getBytesRead, getCount, pushedBackBytes, read
-
-
-
-
Field Detail
-
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 java.io.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 java.lang.String encoding
-
-
Constructor Detail
-
CpioArchiveInputStream
public CpioArchiveInputStream(java.io.InputStream in)
Construct the cpio input stream with a blocksize ofBLOCK_SIZEand expecting ASCII file names.- Parameters:
in- The cpio stream
-
CpioArchiveInputStream
public CpioArchiveInputStream(java.io.InputStream in, java.lang.String encoding)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
-
CpioArchiveInputStream
public CpioArchiveInputStream(java.io.InputStream in, int blockSize)Construct the cpio input stream with a blocksize ofBLOCK_SIZEexpecting ASCII file names.- Parameters:
in- The cpio streamblockSize- The block size of the archive.- Since:
- 1.5
-
CpioArchiveInputStream
public CpioArchiveInputStream(java.io.InputStream in, int blockSize, java.lang.String encoding)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:
java.lang.IllegalArgumentException- ifblockSizeis not bigger than 0- Since:
- 1.6
-
-
Method Detail
-
available
public int available() throws java.io.IOExceptionReturns 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:
availablein classjava.io.InputStream- Returns:
- 1 before EOF and 0 after EOF has reached for current entry.
- Throws:
java.io.IOException- if an I/O error has occurred or if a CPIO file error has occurred
-
close
public void close() throws java.io.IOExceptionCloses the CPIO input stream.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException- if an I/O error has occurred
-
closeEntry
private void closeEntry() throws java.io.IOExceptionCloses the current CPIO entry and positions the stream for reading the next entry.- Throws:
java.io.IOException- if an I/O error has occurred or if a CPIO file error has occurred
-
ensureOpen
private void ensureOpen() throws java.io.IOExceptionCheck to make sure that this stream has not been closed- Throws:
java.io.IOException- if the stream is already closed
-
getNextCPIOEntry
public CpioArchiveEntry getNextCPIOEntry() throws java.io.IOException
Reads the next CPIO file entry and positions stream at the beginning of the entry data.- Returns:
- the CpioArchiveEntry just read
- Throws:
java.io.IOException- if an I/O error has occurred or if a CPIO file error has occurred
-
skip
private void skip(int bytes) throws java.io.IOException- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOExceptionReads from the current CPIO entry into an array of bytes. Blocks until some input is available.- Overrides:
readin classjava.io.InputStream- 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:
java.io.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 java.io.IOException- Throws:
java.io.IOException
-
readRange
private final byte[] readRange(int len) throws java.io.IOException- Throws:
java.io.IOException
-
readBinaryLong
private long readBinaryLong(int length, boolean swapHalfWord) throws java.io.IOException- Throws:
java.io.IOException
-
readAsciiLong
private long readAsciiLong(int length, int radix) throws java.io.IOException- Throws:
java.io.IOException
-
readNewEntry
private CpioArchiveEntry readNewEntry(boolean hasCrc) throws java.io.IOException
- Throws:
java.io.IOException
-
readOldAsciiEntry
private CpioArchiveEntry readOldAsciiEntry() throws java.io.IOException
- Throws:
java.io.IOException
-
readOldBinaryEntry
private CpioArchiveEntry readOldBinaryEntry(boolean swapHalfWord) throws java.io.IOException
- Throws:
java.io.IOException
-
readCString
private java.lang.String readCString(int length) throws java.io.IOException- Throws:
java.io.IOException
-
skip
public long skip(long n) throws java.io.IOExceptionSkips specified number of bytes in the current CPIO entry.- Overrides:
skipin classjava.io.InputStream- Parameters:
n- the number of bytes to skip- Returns:
- the actual number of bytes skipped
- Throws:
java.io.IOException- if an I/O error has occurredjava.lang.IllegalArgumentException- if n < 0
-
getNextEntry
public ArchiveEntry getNextEntry() throws java.io.IOException
Description copied from class:ArchiveInputStreamReturns the next Archive Entry in this Stream.- Specified by:
getNextEntryin classArchiveInputStream- Returns:
- the next entry,
or
nullif there are no more entries - Throws:
java.io.IOException- if the next entry could not be read
-
skipRemainderOfLastBlock
private void skipRemainderOfLastBlock() throws java.io.IOExceptionSkips the padding zeros written after the TRAILER!!! entry.- Throws:
java.io.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 matchlength- length of data- Returns:
- whether the buffer seems to contain CPIO data
-
-