Top Description Fields Constructors Methods
org.apache.commons.compress.archivers.cpio

public Class CpioArchiveInputStream

extends ArchiveInputStream<CpioArchiveEntry>
implements CpioConstants
Class Inheritance
All Implemented Interfaces
org.apache.commons.compress.archivers.cpio.CpioConstants
Imports
java.io.EOFException, .IOException, .InputStream, org.apache.commons.compress.archivers.ArchiveInputStream, org.apache.commons.compress.archivers.zip.ZipEncoding, .ZipEncodingHelper, org.apache.commons.compress.utils.ArchiveUtils, .CharsetNames, .IOUtils, .ParsingUtils

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

Modifier and TypeField 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.

Constructor Summary

AccessConstructor and Description
public
CpioArchiveInputStream(final InputStream
The cpio stream
in
)

Constructs the cpio input stream with a blocksize of BLOCK_SIZE and expecting ASCII file names.

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 BLOCK_SIZE expecting ASCII file names.

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 BLOCK_SIZE.

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 BLOCK_SIZE.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

1 before EOF and 0 after EOF has reached for current entry.
available
()

Overrides java.io.FilterInputStream.available.

Returns 0 after EOF has reached for the current entry data, otherwise always return 1.
public void
close()

Overrides java.io.FilterInputStream.close.

Implements java.io.Closeable.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

public CpioArchiveEntry

Returns:

the CpioArchiveEntry just read
getNextCPIOEntry
()

Deprecated Use getNextEntry().
Reads the next CPIO file entry and positions stream at the beginning of the entry data.
public CpioArchiveEntry
getNextEntry()

Implements abstract org.apache.commons.compress.archivers.ArchiveInputStream.getNextEntry.

Gets the next Archive Entry in this Stream.
public static boolean

Returns:

whether the buffer seems to contain CPIO data
matches
(final byte[]
data 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 reached
read
(final byte[]
the 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.io.FilterInputStream.read.

Reads from the current CPIO entry into an array of bytes.
private long
readAsciiLong(final int length, final int radix)

private long
readBinaryLong(final int length, final boolean swapHalfWord)

private String
readCString(final int length)

private int
readFully(final byte[] b, final int off, final int len)

private CpioArchiveEntry
readNewEntry(final boolean hasCrc)

private CpioArchiveEntry
private CpioArchiveEntry
readOldBinaryEntry(final boolean swapHalfWord)

private byte[]
readRange(final int len)

private void
skip(final int bytes)

public long

Returns:

the actual number of bytes skipped
skip
(final long
the number of bytes to skip
n
)

Overrides java.io.FilterInputStream.skip.

Skips specified number of bytes in the current CPIO entry.
private void
skipRemainderOfLastBlock()

Skips the padding zeros written after the TRAILER!!!

Inherited from org.apache.commons.compress.archivers.ArchiveInputStream:
canReadEntryDatacountcountgetBytesReadgetCharsetgetCountpushedBackBytesread

Field Detail

blockSizeback to summary
private final int blockSize
closedback to summary
private boolean closed
crcback to summary
private long crc
entryback to summary
private CpioArchiveEntry entry
entryBytesReadback to summary
private long entryBytesRead
entryEOFback to summary
private boolean entryEOF
fourBytesBufback to summary
private final byte[] fourBytesBuf

Cached buffer - must only be used locally in the class (COMPRESS-172 - reduce garbage collection).

sixBytesBufback to summary
private final byte[] sixBytesBuf
tmpbufback to summary
private final byte[] tmpbuf
twoBytesBufback to summary
private final byte[] twoBytesBuf

Cached buffer - must only be used locally in the class (COMPRESS-172 - reduce garbage collection).

zipEncodingback to summary
private final ZipEncoding zipEncoding

The encoding to use for file names and labels.

Constructor Detail

CpioArchiveInputStreamback to summary
public CpioArchiveInputStream(final InputStream in)

Constructs the cpio input stream with a blocksize of BLOCK_SIZE and expecting ASCII file names.

Parameters
in:InputStream

The cpio stream

CpioArchiveInputStreamback to summary
public CpioArchiveInputStream(final InputStream in, final int blockSize)

Constructs the cpio input stream with a blocksize of BLOCK_SIZE expecting ASCII file names.

Parameters
in:InputStream

The cpio stream

blockSize:int

The block size of the archive.

Since
1.5
CpioArchiveInputStreamback to summary
public CpioArchiveInputStream(final InputStream in, final int blockSize, final String encoding)

Constructs the cpio input stream with a blocksize of BLOCK_SIZE.

Parameters
in:InputStream

The cpio stream

blockSize:int

The block size of the archive.

encoding:String

The encoding of file names to expect - use null for the platform's default.

Exceptions
IllegalArgumentException:
if blockSize is not bigger than 0
Since
1.6
CpioArchiveInputStreamback to summary
public CpioArchiveInputStream(final InputStream in, final String encoding)

Constructs the cpio input stream with a blocksize of BLOCK_SIZE.

Parameters
in:InputStream

The cpio stream

encoding:String

The encoding of file names to expect - use null for the platform's default.

Since
1.6

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.FilterInputStream.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.

Returns:int

1 before EOF and 0 after EOF has reached for current entry.

Annotations
@Override
Exceptions
IOException:
if an I/O error has occurred or if a CPIO file error has occurred
closeback to summary
public void close() throws IOException

Overrides java.io.FilterInputStream.close.

Implements java.io.Closeable.close.

Closes the CPIO input stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error has occurred
closeEntryback to summary
private void closeEntry() throws IOException

Closes the current CPIO entry and positions the stream for reading the next entry.

Exceptions
IOException:
if an I/O error has occurred or if a CPIO file error has occurred
ensureOpenback to summary
private void ensureOpen() throws IOException

Check to make sure that this stream has not been closed

Exceptions
IOException:
if the stream is already closed
getNextCPIOEntryback to summary
public CpioArchiveEntry getNextCPIOEntry() throws IOException

Deprecated

Use getNextEntry().

Reads the next CPIO file entry and positions stream at the beginning of the entry data.

Returns:CpioArchiveEntry

the CpioArchiveEntry just read

Annotations
@Deprecated
Exceptions
IOException:
if an I/O error has occurred or if a CPIO file error has occurred
getNextEntryback to summary
public CpioArchiveEntry getNextEntry() throws IOException

Implements abstract org.apache.commons.compress.archivers.ArchiveInputStream.getNextEntry.

Doc from org.apache.commons.compress.archivers.ArchiveInputStream.getNextEntry.

Gets the next Archive Entry in this Stream.

Returns:CpioArchiveEntry

the next entry, or null if there are no more entries

Annotations
@Override
Exceptions
IOException:
if the next entry could not be read
matchesback 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

Parameters
signature:byte[]

data to match

length:int

length of data

Returns:boolean

whether the buffer seems to contain CPIO data

readback to summary
public int read(final byte[] b, final int off, final int len) throws IOException

Overrides java.io.FilterInputStream.read.

Reads from the current CPIO entry into an array of bytes. Blocks until some input is available.

Parameters
b:byte[]

the buffer into which the data is read

off:int

the start offset of the data

len:int

the maximum number of bytes read

Returns:int

the actual number of bytes read, or -1 if the end of the entry is reached

Annotations
@Override
Exceptions
IOException:
if an I/O error has occurred or if a CPIO file error has occurred
readAsciiLongback to summary
private long readAsciiLong(final int length, final int radix) throws IOException
readBinaryLongback to summary
private long readBinaryLong(final int length, final boolean swapHalfWord) throws IOException
readCStringback to summary
private String readCString(final int length) throws IOException
readFullyback to summary
private int readFully(final byte[] b, final int off, final int len) throws IOException
readNewEntryback to summary
private CpioArchiveEntry readNewEntry(final boolean hasCrc) throws IOException
readOldAsciiEntryback to summary
private CpioArchiveEntry readOldAsciiEntry() throws IOException
readOldBinaryEntryback to summary
private CpioArchiveEntry readOldBinaryEntry(final boolean swapHalfWord) throws IOException
readRangeback to summary
private byte[] readRange(final int len) throws IOException
skipback to summary
private void skip(final int bytes) throws IOException
skipback to summary
public long skip(final long n) throws IOException

Overrides java.io.FilterInputStream.skip.

Skips specified number of bytes in the current CPIO entry.

Parameters
n:long

the number of bytes to skip

Returns:long

the actual number of bytes skipped

Annotations
@Override
Exceptions
IOException:
if an I/O error has occurred
IllegalArgumentException:
if n < 0
skipRemainderOfLastBlockback to summary
private void skipRemainderOfLastBlock() throws IOException

Skips the padding zeros written after the TRAILER!!! entry.