Class CpioArchiveOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- org.apache.commons.compress.archivers.ArchiveOutputStream<CpioArchiveEntry>
-
- org.apache.commons.compress.archivers.cpio.CpioArchiveOutputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable,CpioConstants
public class CpioArchiveOutputStream extends ArchiveOutputStream<CpioArchiveEntry> implements CpioConstants
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
Fields Modifier and Type Field Description private intblockSize(package private) java.lang.StringcharsetNameprivate longcrcprivate CpioArchiveEntryentryprivate shortentryFormatSeeCpioArchiveEntry(short)for possible values.private java.util.HashMap<java.lang.String,CpioArchiveEntry>namesprivate longnextArtificalDeviceAndInodeprivate longwrittenprivate 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 CpioArchiveOutputStream(java.io.OutputStream out)Constructs the cpio output stream.CpioArchiveOutputStream(java.io.OutputStream out, short format)Constructs the cpio output stream with a specified format, a blocksize ofBLOCK_SIZEand using ASCII as the file name encoding.CpioArchiveOutputStream(java.io.OutputStream out, short format, int blockSize)Constructs the cpio output stream with a specified format using ASCII as the file name encoding.CpioArchiveOutputStream(java.io.OutputStream out, short format, int blockSize, java.lang.String encoding)Constructs the cpio output stream with a specified format using ASCII as the file name encoding.CpioArchiveOutputStream(java.io.OutputStream out, java.lang.String encoding)Constructs the cpio output stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the CPIO output stream as well as the stream being filtered.voidcloseArchiveEntry()Closes the archive entry, writing any trailer information that may be required.CpioArchiveEntrycreateArchiveEntry(java.io.File inputFile, java.lang.String entryName)Creates a new CpioArchiveEntry.CpioArchiveEntrycreateArchiveEntry(java.nio.file.Path inputPath, java.lang.String entryName, java.nio.file.LinkOption... options)Creates a new CpioArchiveEntry.private byte[]encode(java.lang.String str)Encodes the given string using the configured encoding.voidfinish()Finishes writing the contents of the CPIO output stream without closing the underlying stream.private voidpad(int count)voidputArchiveEntry(CpioArchiveEntry entry)Begins writing a new CPIO file entry and positions the stream to the start of the entry data.voidwrite(byte[] b, int off, int len)Writes an array of bytes to the current CPIO entry data.private voidwriteAsciiLong(long number, int length, int radix)private voidwriteBinaryLong(long number, int length, boolean swapHalfWord)private voidwriteCString(byte[] str)Writes an encoded string to the stream followed by \0private voidwriteHeader(CpioArchiveEntry e)private voidwriteNewEntry(CpioArchiveEntry entry)private voidwriteOldAsciiEntry(CpioArchiveEntry entry)private voidwriteOldBinaryEntry(CpioArchiveEntry entry, boolean swapHalfWord)-
Methods inherited from class org.apache.commons.compress.archivers.ArchiveOutputStream
canWriteEntryData, checkFinished, checkOpen, count, count, getBytesWritten, getCount, isClosed, isFinished, write
-
-
-
-
Field Detail
-
entry
private CpioArchiveEntry entry
-
entryFormat
private final short entryFormat
SeeCpioArchiveEntry(short)for possible values.
-
names
private final java.util.HashMap<java.lang.String,CpioArchiveEntry> names
-
crc
private long crc
-
written
private long written
-
blockSize
private final int blockSize
-
nextArtificalDeviceAndInode
private long nextArtificalDeviceAndInode
-
zipEncoding
private final ZipEncoding zipEncoding
The encoding to use for file names and labels.
-
charsetName
final java.lang.String charsetName
-
-
Constructor Detail
-
CpioArchiveOutputStream
public CpioArchiveOutputStream(java.io.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- The cpio stream
-
CpioArchiveOutputStream
public CpioArchiveOutputStream(java.io.OutputStream out, short format)Constructs the cpio output stream with a specified format, a blocksize ofBLOCK_SIZEand using ASCII as the file name encoding.- Parameters:
out- The cpio streamformat- The format of the stream
-
CpioArchiveOutputStream
public CpioArchiveOutputStream(java.io.OutputStream out, short format, int blockSize)Constructs the cpio output stream with a specified format using ASCII as the file name encoding.- Parameters:
out- The cpio streamformat- The format of the streamblockSize- The block size of the archive.- Since:
- 1.1
-
CpioArchiveOutputStream
public CpioArchiveOutputStream(java.io.OutputStream out, short format, int blockSize, java.lang.String encoding)Constructs the cpio output stream with a specified format using ASCII as the file name encoding.- Parameters:
out- The cpio streamformat- The format of the streamblockSize- The block size of the archive.encoding- The encoding of file names to write - use null for the platform's default.- Since:
- 1.6
-
CpioArchiveOutputStream
public CpioArchiveOutputStream(java.io.OutputStream out, java.lang.String encoding)Constructs the cpio output stream. The format for this CPIO stream is the "new" format.- Parameters:
out- The cpio streamencoding- The encoding of file names to write - use null for the platform's default.- Since:
- 1.6
-
-
Method Detail
-
close
public void close() throws java.io.IOExceptionCloses the CPIO output stream as well as the stream being filtered.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classArchiveOutputStream<CpioArchiveEntry>- Throws:
java.io.IOException- if an I/O error has occurred or if a CPIO file error has occurred
-
closeArchiveEntry
public void closeArchiveEntry() throws java.io.IOExceptionDescription copied from class:ArchiveOutputStreamCloses the archive entry, writing any trailer information that may be required.- Specified by:
closeArchiveEntryin classArchiveOutputStream<CpioArchiveEntry>- Throws:
java.io.IOException- if an I/O error occurs
-
createArchiveEntry
public CpioArchiveEntry createArchiveEntry(java.io.File inputFile, java.lang.String entryName) throws java.io.IOException
Creates a new CpioArchiveEntry. The entryName must be an ASCII encoded string.- Specified by:
createArchiveEntryin classArchiveOutputStream<CpioArchiveEntry>- Parameters:
inputFile- the file to create the entry fromentryName- name to use for the entry- Returns:
- the ArchiveEntry set up with details from the file
- Throws:
java.io.IOException- if an I/O error occurs- See Also:
ArchiveOutputStream.createArchiveEntry(java.io.File, String)
-
createArchiveEntry
public CpioArchiveEntry createArchiveEntry(java.nio.file.Path inputPath, java.lang.String entryName, java.nio.file.LinkOption... options) throws java.io.IOException
Creates a new CpioArchiveEntry. The entryName must be an ASCII encoded string.- Overrides:
createArchiveEntryin classArchiveOutputStream<CpioArchiveEntry>- Parameters:
inputPath- the file to create the entry fromentryName- name to use for the entryoptions- options indicating how symbolic links are handled.- Returns:
- the ArchiveEntry set up with details from the file
- Throws:
java.io.IOException- if an I/O error occurs- See Also:
ArchiveOutputStream.createArchiveEntry(java.io.File, String)
-
encode
private byte[] encode(java.lang.String str) throws java.io.IOExceptionEncodes the given string using the configured encoding.- Parameters:
str- the String to write- Returns:
- result of encoding the string
- Throws:
java.io.IOException- if the string couldn't be written
-
finish
public void finish() throws java.io.IOExceptionFinishes 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.- Overrides:
finishin classArchiveOutputStream<CpioArchiveEntry>- Throws:
java.io.IOException- if an I/O exception has occurred or if a CPIO file error has occurred
-
pad
private void pad(int count) throws java.io.IOException- Throws:
java.io.IOException
-
putArchiveEntry
public void putArchiveEntry(CpioArchiveEntry entry) throws java.io.IOException
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.- Specified by:
putArchiveEntryin classArchiveOutputStream<CpioArchiveEntry>- Parameters:
entry- the CPIO cpioEntry to be written- Throws:
java.io.IOException- if an I/O error has occurred or if a CPIO file error has occurredjava.lang.ClassCastException- if entry is not an instance of CpioArchiveEntry
-
write
public void write(byte[] b, int off, int len) throws java.io.IOExceptionWrites an array of bytes to the current CPIO entry data. This method will block until all the bytes are written.- Overrides:
writein classjava.io.FilterOutputStream- Parameters:
b- the data to be writtenoff- the start offset in the datalen- the number of bytes that are written- Throws:
java.io.IOException- if an I/O error has occurred or if a CPIO file error has occurred
-
writeAsciiLong
private void writeAsciiLong(long number, int length, int radix) throws java.io.IOException- Throws:
java.io.IOException
-
writeBinaryLong
private void writeBinaryLong(long number, int length, boolean swapHalfWord) throws java.io.IOException- Throws:
java.io.IOException
-
writeCString
private void writeCString(byte[] str) throws java.io.IOExceptionWrites an encoded string to the stream followed by \0- Parameters:
str- the String to write- Throws:
java.io.IOException- if the string couldn't be written
-
writeHeader
private void writeHeader(CpioArchiveEntry e) throws java.io.IOException
- Throws:
java.io.IOException
-
writeNewEntry
private void writeNewEntry(CpioArchiveEntry entry) throws java.io.IOException
- Throws:
java.io.IOException
-
writeOldAsciiEntry
private void writeOldAsciiEntry(CpioArchiveEntry entry) throws java.io.IOException
- Throws:
java.io.IOException
-
writeOldBinaryEntry
private void writeOldBinaryEntry(CpioArchiveEntry entry, boolean swapHalfWord) throws java.io.IOException
- Throws:
java.io.IOException
-
-