org.apache.commons.compress.archivers.cpio
Class CpioArchiveOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by org.apache.commons.compress.archivers.ArchiveOutputStream
          extended by org.apache.commons.compress.archivers.cpio.CpioArchiveOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, CpioConstants

public class CpioArchiveOutputStream
extends ArchiveOutputStream
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
private  int blockSize
           
private  boolean closed
           
private  long crc
           
private  CpioArchiveEntry entry
           
private  short entryFormat
          See CpioArchiveEntry#setFormat(short) for possible values.
private  boolean finished
          indicates if this archive is finished
private  java.util.HashMap names
           
private  long nextArtificalDeviceAndInode
           
private  java.io.OutputStream out
           
private  long written
           
 
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
CpioArchiveOutputStream(java.io.OutputStream out)
          Construct the cpio output stream.
CpioArchiveOutputStream(java.io.OutputStream out, short format)
          Construct the cpio output stream with a specified format and a blocksize of BLOCK_SIZE.
CpioArchiveOutputStream(java.io.OutputStream out, short format, int blockSize)
          Construct the cpio output stream with a specified format
 
Method Summary
 void close()
          Closes the CPIO output stream as well as the stream being filtered.
 void closeArchiveEntry()
          Closes the archive entry, writing any trailer information that may be required.
 ArchiveEntry createArchiveEntry(java.io.File inputFile, java.lang.String entryName)
          Creates a new ArchiveEntry.
private  void ensureOpen()
          Check to make sure that this stream has not been closed
 void finish()
          Finishes writing the contents of the CPIO output stream without closing the underlying stream.
private  void pad(int count)
           
 void putArchiveEntry(ArchiveEntry entry)
          Begins writing a new CPIO file entry and positions the stream to the start of the entry data.
 void write(byte[] b, int off, int len)
          Writes an array of bytes to the current CPIO entry data.
private  void writeAsciiLong(long number, int length, int radix)
           
private  void writeBinaryLong(long number, int length, boolean swapHalfWord)
           
private  void writeCString(java.lang.String str)
          Writes an ASCII string to the stream followed by \0
private  void writeHeader(CpioArchiveEntry e)
           
private  void writeNewEntry(CpioArchiveEntry entry)
           
private  void writeOldAsciiEntry(CpioArchiveEntry entry)
           
private  void writeOldBinaryEntry(CpioArchiveEntry entry, boolean swapHalfWord)
           
 
Methods inherited from class org.apache.commons.compress.archivers.ArchiveOutputStream
canWriteEntryData, count, count, getBytesWritten, getCount, write
 
Methods inherited from class java.io.OutputStream
flush, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

entry

private CpioArchiveEntry entry

closed

private boolean closed

finished

private boolean finished
indicates if this archive is finished


entryFormat

private final short entryFormat
See CpioArchiveEntry#setFormat(short) for possible values.


names

private final java.util.HashMap names

crc

private long crc

written

private long written

out

private final java.io.OutputStream out

blockSize

private final int blockSize

nextArtificalDeviceAndInode

private long nextArtificalDeviceAndInode
Constructor Detail

CpioArchiveOutputStream

public CpioArchiveOutputStream(java.io.OutputStream out,
                               short format)
Construct the cpio output stream with a specified format and a blocksize of BLOCK_SIZE.

Parameters:
out - The cpio stream
format - The format of the stream

CpioArchiveOutputStream

public CpioArchiveOutputStream(java.io.OutputStream out,
                               short format,
                               int blockSize)
Construct the cpio output stream with a specified format

Parameters:
out - The cpio stream
format - The format of the stream
blockSize - The block size of the archive.
Since:
Apache Commons Compress 1.1

CpioArchiveOutputStream

public CpioArchiveOutputStream(java.io.OutputStream out)
Construct the cpio output stream. The format for this CPIO stream is the "new" format

Parameters:
out - The cpio stream
Method Detail

ensureOpen

private void ensureOpen()
                 throws java.io.IOException
Check to make sure that this stream has not been closed

Throws:
java.io.IOException - if the stream is already closed

putArchiveEntry

public void putArchiveEntry(ArchiveEntry 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:
putArchiveEntry in class ArchiveOutputStream
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 occurred
java.lang.ClassCastException - if entry is not an instance of CpioArchiveEntry

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

closeArchiveEntry

public void closeArchiveEntry()
                       throws java.io.IOException
Description copied from class: ArchiveOutputStream
Closes the archive entry, writing any trailer information that may be required.

Specified by:
closeArchiveEntry in class ArchiveOutputStream
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Writes an array of bytes to the current CPIO entry data. This method will block until all the bytes are written.

Overrides:
write in class java.io.OutputStream
Parameters:
b - the data to be written
off - the start offset in the data
len - 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

finish

public void finish()
            throws java.io.IOException
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.

Specified by:
finish in class ArchiveOutputStream
Throws:
java.io.IOException - if an I/O exception has occurred or if a CPIO file error has occurred

close

public void close()
           throws java.io.IOException
Closes the CPIO output stream as well as the stream being filtered.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - if an I/O error has occurred or if a CPIO file error has occurred

pad

private void pad(int count)
          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

writeAsciiLong

private void writeAsciiLong(long number,
                            int length,
                            int radix)
                     throws java.io.IOException
Throws:
java.io.IOException

writeCString

private void writeCString(java.lang.String str)
                   throws java.io.IOException
Writes an ASCII string to the stream followed by \0

Parameters:
str - the String to write
Throws:
java.io.IOException - if the string couldn't be written

createArchiveEntry

public ArchiveEntry createArchiveEntry(java.io.File inputFile,
                                       java.lang.String entryName)
                                throws java.io.IOException
Creates a new ArchiveEntry. The entryName must be an ASCII encoded string.

Specified by:
createArchiveEntry in class ArchiveOutputStream
Returns:
the ArchiveEntry set up with details from the file
Throws:
java.io.IOException
See Also:
ArchiveOutputStream.createArchiveEntry(java.io.File, java.lang.String)