TarOutputStreampublic class TarOutputStream extends FilterOutputStream The TarOutputStream writes a UNIX tar archive as an OutputStream.
Methods are provided to put entries, and then write their contents
by writing to this stream using write(). |
Fields Summary |
---|
public static final int | LONGFILE_ERRORFail if a long file name is required in the archive. | public static final int | LONGFILE_TRUNCATELong paths will be truncated in the archive. | public static final int | LONGFILE_GNUGNU tar extensions are used to store long file names in the archive. | protected boolean | debug | protected long | currSize | protected String | currName | protected long | currBytes | protected byte[] | oneBuf | protected byte[] | recordBuf | protected int | assemLen | protected byte[] | assemBuf | protected TarBuffer | buffer | protected int | longFileMode | private boolean | closed |
Constructors Summary |
---|
public TarOutputStream(OutputStream os)Constructor for TarInputStream.
this(os, TarBuffer.DEFAULT_BLKSIZE, TarBuffer.DEFAULT_RCDSIZE);
| public TarOutputStream(OutputStream os, int blockSize)Constructor for TarInputStream.
this(os, blockSize, TarBuffer.DEFAULT_RCDSIZE);
| public TarOutputStream(OutputStream os, int blockSize, int recordSize)Constructor for TarInputStream.
super(os);
this.buffer = new TarBuffer(os, blockSize, recordSize);
this.debug = false;
this.assemLen = 0;
this.assemBuf = new byte[recordSize];
this.recordBuf = new byte[recordSize];
this.oneBuf = new byte[1];
|
Methods Summary |
---|
public void | close()Ends the TAR archive and closes the underlying OutputStream.
This means that finish() is called followed by calling the
TarBuffer's close().
if (!closed) {
this.finish();
this.buffer.close();
out.close();
closed = true;
}
| public void | closeEntry()Close an entry. This method MUST be called for all file
entries that contain data. The reason is that we must
buffer data written to the stream in order to satisfy
the buffer's record based writes. Thus, there may be
data fragments still being assembled that must be written
to the output stream before this entry is closed and the
next entry written.
if (this.assemLen > 0) {
for (int i = this.assemLen; i < this.assemBuf.length; ++i) {
this.assemBuf[i] = 0;
}
this.buffer.writeRecord(this.assemBuf);
this.currBytes += this.assemLen;
this.assemLen = 0;
}
if (this.currBytes < this.currSize) {
throw new IOException("entry '" + currName + "' closed at '"
+ this.currBytes
+ "' before the '" + this.currSize
+ "' bytes specified in the header were written");
}
| public void | finish()Ends the TAR archive without closing the underlying OutputStream.
The result is that the two EOF records of nulls are written.
// See Bugzilla 28776 for a discussion on this
// http://issues.apache.org/bugzilla/show_bug.cgi?id=28776
this.writeEOFRecord();
this.writeEOFRecord();
| public int | getRecordSize()Get the record size being used by this stream's TarBuffer.
return this.buffer.getRecordSize();
| public void | putNextEntry(TarEntry entry)Put an entry on the output stream. This writes the entry's
header record and positions the output stream for writing
the contents of the entry. Once this method is called, the
stream is ready for calls to write() to write the entry's
contents. Once the contents are written, closeEntry()
MUST be called to ensure that all buffered data
is completely written to the output stream.
if (entry.getName().length() >= TarConstants.NAMELEN) {
if (longFileMode == LONGFILE_GNU) {
// create a TarEntry for the LongLink, the contents
// of which are the entry's name
TarEntry longLinkEntry = new TarEntry(TarConstants.GNU_LONGLINK,
TarConstants.LF_GNUTYPE_LONGNAME);
longLinkEntry.setSize(entry.getName().length() + 1);
putNextEntry(longLinkEntry);
write(entry.getName().getBytes());
write(0);
closeEntry();
} else if (longFileMode != LONGFILE_TRUNCATE) {
throw new RuntimeException("file name '" + entry.getName()
+ "' is too long ( > "
+ TarConstants.NAMELEN + " bytes)");
}
}
entry.writeEntryHeader(this.recordBuf);
this.buffer.writeRecord(this.recordBuf);
this.currBytes = 0;
if (entry.isDirectory()) {
this.currSize = 0;
} else {
this.currSize = entry.getSize();
}
currName = entry.getName();
| public void | setBufferDebug(boolean debug)Sets the debugging flag in this stream's TarBuffer.
this.buffer.setDebug(debug);
| public void | setDebug(boolean debugF)Sets the debugging flag.
this.debug = debugF;
| public void | setLongFileMode(int longFileMode)Set the long file mode.
This can be LONGFILE_ERROR(0), LONGFILE_TRUNCATE(1) or LONGFILE_GNU(2).
This specifies the treatment of long file names (names >= TarConstants.NAMELEN).
Default is LONGFILE_ERROR.
this.longFileMode = longFileMode;
| public void | write(int b)Writes a byte to the current tar archive entry.
This method simply calls read( byte[], int, int ).
this.oneBuf[0] = (byte) b;
this.write(this.oneBuf, 0, 1);
| public void | write(byte[] wBuf)Writes bytes to the current tar archive entry.
This method simply calls write( byte[], int, int ).
this.write(wBuf, 0, wBuf.length);
| public void | write(byte[] wBuf, int wOffset, int numToWrite)Writes bytes to the current tar archive entry. This method
is aware of the current entry and will throw an exception if
you attempt to write bytes past the length specified for the
current entry. The method is also (painfully) aware of the
record buffering required by TarBuffer, and manages buffers
that are not a multiple of recordsize in length, including
assembling records from small buffers.
if ((this.currBytes + numToWrite) > this.currSize) {
throw new IOException("request to write '" + numToWrite
+ "' bytes exceeds size in header of '"
+ this.currSize + "' bytes for entry '"
+ currName + "'");
//
// We have to deal with assembly!!!
// The programmer can be writing little 32 byte chunks for all
// we know, and we must assemble complete records for writing.
// REVIEW Maybe this should be in TarBuffer? Could that help to
// eliminate some of the buffer copying.
//
}
if (this.assemLen > 0) {
if ((this.assemLen + numToWrite) >= this.recordBuf.length) {
int aLen = this.recordBuf.length - this.assemLen;
System.arraycopy(this.assemBuf, 0, this.recordBuf, 0,
this.assemLen);
System.arraycopy(wBuf, wOffset, this.recordBuf,
this.assemLen, aLen);
this.buffer.writeRecord(this.recordBuf);
this.currBytes += this.recordBuf.length;
wOffset += aLen;
numToWrite -= aLen;
this.assemLen = 0;
} else {
System.arraycopy(wBuf, wOffset, this.assemBuf, this.assemLen,
numToWrite);
wOffset += numToWrite;
this.assemLen += numToWrite;
numToWrite -= numToWrite;
}
}
//
// When we get here we have EITHER:
// o An empty "assemble" buffer.
// o No bytes to write (numToWrite == 0)
//
while (numToWrite > 0) {
if (numToWrite < this.recordBuf.length) {
System.arraycopy(wBuf, wOffset, this.assemBuf, this.assemLen,
numToWrite);
this.assemLen += numToWrite;
break;
}
this.buffer.writeRecord(wBuf, wOffset);
int num = this.recordBuf.length;
this.currBytes += num;
numToWrite -= num;
wOffset += num;
}
| private void | writeEOFRecord()Write an EOF (end of archive) record to the tar archive.
An EOF record consists of a record of all zeros.
for (int i = 0; i < this.recordBuf.length; ++i) {
this.recordBuf[i] = 0;
}
this.buffer.writeRecord(this.recordBuf);
|
|