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

public Class CpioArchiveOutputStream

extends ArchiveOutputStream<CpioArchiveEntry>
implements CpioConstants
Class Inheritance
All Implemented Interfaces
org.apache.commons.compress.archivers.cpio.CpioConstants
Imports
java.io.File, .IOException, .OutputStream, java.nio.ByteBuffer, java.nio.file.LinkOption, .Path, java.util.Arrays, .HashMap, org.apache.commons.compress.archivers.ArchiveOutputStream, org.apache.commons.compress.archivers.zip.ZipEncoding, .ZipEncodingHelper, org.apache.commons.compress.utils.ArchiveUtils, .CharsetNames

CpioArchiveOutputStream is a stream for writing CPIO streams. All formats of CPIO are supported (old ASCII, old binary, new portable format and the new portable format with CRC).

An entry can be written by creating an instance of CpioArchiveEntry and fill it with the necessary values and put it into the CPIO stream. Afterwards write the contents of the file into the CPIO stream. Either close the stream by calling finish() or put a next entry into the cpio stream.

CpioArchiveOutputStream out = new CpioArchiveOutputStream(
        new FileOutputStream(new File("test.cpio")));
CpioArchiveEntry entry = new CpioArchiveEntry();
entry.setName("testfile");
String contents = "12345";
entry.setFileSize(contents.length());
entry.setMode(CpioConstants.C_ISREG); // regular file
... set other attributes, e.g. time, number of links
out.putArchiveEntry(entry);
out.write(testContents.getBytes());
out.close();

Note

This implementation should be compatible to cpio 2.5

This class uses mutable fields and is not considered threadsafe.

based on code from the jRPM project (jrpm.sourceforge.net)

Field Summary

Modifier and TypeField and Description
private final int
pack-priv final String
private boolean
private long
private CpioArchiveEntry
private final short
private boolean
finished

Indicates if this archive is finished

private final HashMap<String, CpioArchiveEntry>
private long
private final OutputStream
private long
private final ZipEncoding
zipEncoding

The encoding to use for file names and labels.

Constructor Summary

AccessConstructor and Description
public
CpioArchiveOutputStream(final OutputStream
The cpio stream
out
)

Constructs the cpio output stream.

public
CpioArchiveOutputStream(final OutputStream
The cpio stream
out
,
final short
The format of the stream
format
)

Constructs the cpio output stream with a specified format, a blocksize of BLOCK_SIZE and using ASCII as the file name encoding.

public
CpioArchiveOutputStream(final OutputStream
The cpio stream
out
,
final short
The format of the stream
format
,
final int
The block size of the archive.
blockSize
)

Constructs the cpio output stream with a specified format using ASCII as the file name encoding.

public
CpioArchiveOutputStream(final OutputStream
The cpio stream
out
,
final short
The format of the stream
format
,
final int
The block size of the archive.
blockSize
,
final String
The encoding of file names to write - use null for the platform's default.
encoding
)

Constructs the cpio output stream with a specified format using ASCII as the file name encoding.

public
CpioArchiveOutputStream(final OutputStream
The cpio stream
out
,
final String
The encoding of file names to write - use null for the platform's default.
encoding
)

Constructs the cpio output stream.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closes the CPIO output stream as well as the stream being filtered.
public void
closeArchiveEntry()

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.closeArchiveEntry.

Closes the archive entry, writing any trailer information that may be required.
public CpioArchiveEntry
createArchiveEntry(final File
the file to create the entry from
inputFile
,
final String
name to use for the entry
entryName
)

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

Creates a new CpioArchiveEntry.
public CpioArchiveEntry
createArchiveEntry(final Path
the file to create the entry from
inputPath
,
final String
name to use for the entry
entryName
,
final LinkOption...
options indicating how symbolic links are handled.
options
)

Overrides org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

Creates a new CpioArchiveEntry.
private byte[]

Returns:

result of encoding the string
encode
(final String
the String to write
str
)

Encodes the given string using the configured encoding.

private void
ensureOpen()

Check to make sure that this stream has not been closed

public void
finish()

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.finish.

Finishes writing the contents of the CPIO output stream without closing the underlying stream.
private void
pad(final int count)

public void
putArchiveEntry(final CpioArchiveEntry
the CPIO cpioEntry to be written
entry
)

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.putArchiveEntry.

Begins writing a new CPIO file entry and positions the stream to the start of the entry data.
public void
write(final byte[]
the data to be written
b
,
final int
the start offset in the data
off
,
final int
the number of bytes that are written
len
)

Overrides java.io.OutputStream.write.

Writes an array of bytes to the current CPIO entry data.
private void
writeAsciiLong(final long number, final int length, final int radix)

private void
writeBinaryLong(final long number, final int length, final boolean swapHalfWord)

private void
writeCString(final byte[]
the String to write
str
)

Writes an encoded string to the stream followed by \0

private void
private void
private void
private void
writeOldBinaryEntry(final CpioArchiveEntry entry, final boolean swapHalfWord)

Inherited from org.apache.commons.compress.archivers.ArchiveOutputStream:
canWriteEntryDatacountcountgetBytesWrittengetCountwrite

Field Detail

blockSizeback to summary
private final int blockSize
charsetNameback to summary
pack-priv final String charsetName
closedback to summary
private boolean closed
crcback to summary
private long crc
entryback to summary
private CpioArchiveEntry entry
entryFormatback to summary
private final short entryFormat

See CpioArchiveEntry#CpioArchiveEntry(short) for possible values.

finishedback to summary
private boolean finished

Indicates if this archive is finished

namesback to summary
private final HashMap<String, CpioArchiveEntry> names
nextArtificalDeviceAndInodeback to summary
private long nextArtificalDeviceAndInode
outback to summary
private final OutputStream out
writtenback to summary
private long written
zipEncodingback to summary
private final ZipEncoding zipEncoding

The encoding to use for file names and labels.

Constructor Detail

CpioArchiveOutputStreamback to summary
public CpioArchiveOutputStream(final OutputStream out)

Constructs the cpio output stream. The format for this CPIO stream is the "new" format using ASCII encoding for file names

Parameters
out:OutputStream

The cpio stream

CpioArchiveOutputStreamback to summary
public CpioArchiveOutputStream(final OutputStream out, final short format)

Constructs the cpio output stream with a specified format, a blocksize of BLOCK_SIZE and using ASCII as the file name encoding.

Parameters
out:OutputStream

The cpio stream

format:short

The format of the stream

CpioArchiveOutputStreamback to summary
public CpioArchiveOutputStream(final OutputStream out, final short format, final int blockSize)

Constructs the cpio output stream with a specified format using ASCII as the file name encoding.

Parameters
out:OutputStream

The cpio stream

format:short

The format of the stream

blockSize:int

The block size of the archive.

Since
1.1
CpioArchiveOutputStreamback to summary
public CpioArchiveOutputStream(final OutputStream out, final short format, final int blockSize, final String encoding)

Constructs the cpio output stream with a specified format using ASCII as the file name encoding.

Parameters
out:OutputStream

The cpio stream

format:short

The format of the stream

blockSize:int

The block size of the archive.

encoding:String

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

Since
1.6
CpioArchiveOutputStreamback to summary
public CpioArchiveOutputStream(final OutputStream out, final String encoding)

Constructs the cpio output stream. The format for this CPIO stream is the "new" format.

Parameters
out:OutputStream

The cpio stream

encoding:String

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

Since
1.6

Method Detail

closeback to summary
public void close() throws IOException

Overrides java.io.OutputStream.close.

Implements java.io.Closeable.close.

Closes the CPIO output stream as well as the stream being filtered.

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

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.closeArchiveEntry.

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.closeArchiveEntry.

Closes the archive entry, writing any trailer information that may be required.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
createArchiveEntryback to summary
public CpioArchiveEntry createArchiveEntry(final File inputFile, final String entryName) throws IOException

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

Creates a new CpioArchiveEntry. The entryName must be an ASCII encoded string.

Parameters
inputFile:File

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

the file to create the entry from

entryName:String

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

name to use for the entry

Returns:CpioArchiveEntry

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

the ArchiveEntry set up with details from the file

Annotations
@Override
Exceptions
IOException:

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

if an I/O error occurs

See Also
org.apache.commons.compress.archivers.ArchiveOutputStream#createArchiveEntry(java.io.File, String)
createArchiveEntryback to summary
public CpioArchiveEntry createArchiveEntry(final Path inputPath, final String entryName, final LinkOption... options) throws IOException

Overrides org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

Creates a new CpioArchiveEntry. The entryName must be an ASCII encoded string.

Parameters
inputPath:Path

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

the file to create the entry from

entryName:String

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

name to use for the entry

options:LinkOption[]

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

options indicating how symbolic links are handled.

Returns:CpioArchiveEntry

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

the ArchiveEntry set up with details from the file

Annotations
@Override
Exceptions
IOException:

Doc from org.apache.commons.compress.archivers.ArchiveOutputStream.createArchiveEntry.

if an I/O error occurs

See Also
org.apache.commons.compress.archivers.ArchiveOutputStream#createArchiveEntry(java.io.File, String)
encodeback to summary
private byte[] encode(final String str) throws IOException

Encodes the given string using the configured encoding.

Parameters
str:String

the String to write

Returns:byte[]

result of encoding the string

Exceptions
IOException:
if the string couldn't be written
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
finishback to summary
public void finish() throws IOException

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.finish.

Finishes writing the contents of the CPIO output stream without closing the underlying stream. Use this method when applying multiple filters in succession to the same output stream.

Annotations
@Override
Exceptions
IOException:
if an I/O exception has occurred or if a CPIO file error has occurred
padback to summary
private void pad(final int count) throws IOException
putArchiveEntryback to summary
public void putArchiveEntry(final CpioArchiveEntry entry) throws IOException

Implements abstract org.apache.commons.compress.archivers.ArchiveOutputStream.putArchiveEntry.

Begins writing a new CPIO file entry and positions the stream to the start of the entry data. Closes the current entry if still active. The current time will be used if the entry has no set modification time and the default header format will be used if no other format is specified in the entry.

Parameters
entry:CpioArchiveEntry

the CPIO cpioEntry to be written

Annotations
@Override
Exceptions
IOException:
if an I/O error has occurred or if a CPIO file error has occurred
ClassCastException:
if entry is not an instance of CpioArchiveEntry
writeback to summary
public void write(final byte[] b, final int off, final int len) throws IOException

Overrides java.io.OutputStream.write.

Writes an array of bytes to the current CPIO entry data. This method will block until all the bytes are written.

Parameters
b:byte[]

the data to be written

off:int

the start offset in the data

len:int

the number of bytes that are written

Annotations
@Override
Exceptions
IOException:
if an I/O error has occurred or if a CPIO file error has occurred
writeAsciiLongback to summary
private void writeAsciiLong(final long number, final int length, final int radix) throws IOException
writeBinaryLongback to summary
private void writeBinaryLong(final long number, final int length, final boolean swapHalfWord) throws IOException
writeCStringback to summary
private void writeCString(final byte[] str) throws IOException

Writes an encoded string to the stream followed by \0

Parameters
str:byte[]

the String to write

Exceptions
IOException:
if the string couldn't be written
writeHeaderback to summary
private void writeHeader(final CpioArchiveEntry e) throws IOException
writeNewEntryback to summary
private void writeNewEntry(final CpioArchiveEntry entry) throws IOException
writeOldAsciiEntryback to summary
private void writeOldAsciiEntry(final CpioArchiveEntry entry) throws IOException
writeOldBinaryEntryback to summary
private void writeOldBinaryEntry(final CpioArchiveEntry entry, final boolean swapHalfWord) throws IOException