tarbuffer.cs
来自「全功能c#编译器」· CS 代码 · 共 527 行 · 第 1/2 页
CS
527 行
if (!this.ReadRecord()) {
return;
}
}
this.currentBlockIndex++;
}
/// <summary>
/// Read a block from the input stream and return the data.
/// </summary>
/// <returns>
/// The block of data read
/// </returns>
public byte[] ReadBlock()
{
if (this.debug) {
//Console.WriteLine.WriteLine( "ReadBlock: blockIndex = " + this.currentBlockIndex + " recordIndex = " + this.currentRecordIndex );
}
if (this.inputStream == null) {
throw new TarException("TarBuffer.ReadBlock - no input stream defined");
}
if (this.currentBlockIndex >= this.BlockFactor) {
if (!this.ReadRecord()) {
return null;
}
}
byte[] result = new byte[BlockSize];
Array.Copy(this.recordBuffer, (this.currentBlockIndex * BlockSize), result, 0, BlockSize );
this.currentBlockIndex++;
return result;
}
/// <summary>
/// Read a record from data stream.
/// </summary>
/// <returns>
/// alse if End-Of-File, else true
/// </returns>
bool ReadRecord()
{
if (this.debug) {
//Console.WriteLine.WriteLine("ReadRecord: recordIndex = " + this.currentRecordIndex);
}
if (this.inputStream == null) {
throw new TarException("no input stream stream defined");
}
this.currentBlockIndex = 0;
int offset = 0;
int bytesNeeded = RecordSize;
while (bytesNeeded > 0) {
long numBytes = this.inputStream.Read(this.recordBuffer, offset, bytesNeeded);
//
// NOTE
// We have found EOF, and the record is not full!
//
// This is a broken archive. It does not follow the standard
// blocking algorithm. However, because we are generous, and
// it requires little effort, we will simply ignore the error
// and continue as if the entire record were read. This does
// not appear to break anything upstream. We used to return
// false in this case.
//
// Thanks to 'Yohann.Roussel@alcatel.fr' for this fix.
//
if (numBytes <= 0) {
break;
}
offset += (int)numBytes;
bytesNeeded -= (int)numBytes;
if (numBytes != RecordSize) {
if (this.debug) {
//Console.WriteLine.WriteLine("ReadRecord: INCOMPLETE READ " + numBytes + " of " + this.blockSize + " bytes read.");
}
}
}
this.currentRecordIndex++;
return true;
}
/// <summary>
/// Get the current block number, within the current record, zero based.
/// </summary>
/// <returns>
/// The current zero based block number.
/// </returns>
public int GetCurrentBlockNum()
{
return this.currentBlockIndex;
}
/// <summary>
/// Get the current record number
/// Absolute block number in file = (currentRecordNum * block factor) + currentBlockNum.
/// </summary>
/// <returns>
/// The current zero based record number.
/// </returns>
public int GetCurrentRecordNum()
{
return this.currentRecordIndex;
}
/// <summary>
/// Write a block of data to the archive.
/// </summary>
/// <param name="block">
/// The data to write to the archive.
/// </param>
///
public void WriteBlock(byte[] block)
{
if (this.debug) {
//Console.WriteLine.WriteLine("WriteRecord: recIdx = " + this.currentRecordIndex + " blkIdx = " + this.currentBlockIndex );
}
if (this.outputStream == null) {
throw new TarException("TarBuffer.WriteBlock - no output stream defined");
}
if (block.Length != BlockSize) {
throw new TarException("TarBuffer.WriteBlock - block to write has length '" + block.Length + "' which is not the block size of '" + BlockSize + "'" );
}
if (this.currentBlockIndex >= BlockFactor) {
this.WriteRecord();
}
Array.Copy(block, 0, this.recordBuffer, (this.currentBlockIndex * BlockSize), BlockSize);
this.currentBlockIndex++;
}
/// <summary>
/// Write an archive record to the archive, where the record may be
/// inside of a larger array buffer. The buffer must be "offset plus
/// record size" long.
/// </summary>
/// <param name="buf">
/// The buffer containing the record data to write.
/// </param>
/// <param name="offset">
/// The offset of the record data within buf.
/// </param>
public void WriteBlock(byte[] buf, int offset)
{
if (this.debug) {
//Console.WriteLine.WriteLine("WriteBlock: recIdx = " + this.currentRecordIndex + " blkIdx = " + this.currentBlockIndex );
}
if (this.outputStream == null) {
throw new TarException("TarBuffer.WriteBlock - no output stream stream defined");
}
if ((offset + BlockSize) > buf.Length) {
throw new TarException("TarBuffer.WriteBlock - record has length '" + buf.Length + "' with offset '" + offset + "' which is less than the record size of '" + this.recordSize + "'" );
}
if (this.currentBlockIndex >= this.BlockFactor) {
this.WriteRecord();
}
Array.Copy(buf, offset, this.recordBuffer, (this.currentBlockIndex * BlockSize), BlockSize);
this.currentBlockIndex++;
}
/// <summary>
/// Write a TarBuffer record to the archive.
/// </summary>
void WriteRecord()
{
if (this.debug) {
//Console.WriteLine.WriteLine("Writerecord: record index = " + this.currentRecordIndex);
}
if (this.outputStream == null)
{
throw new TarException("TarBuffer.WriteRecord no output stream defined");
}
this.outputStream.Write(this.recordBuffer, 0, RecordSize);
this.outputStream.Flush();
this.currentBlockIndex = 0;
this.currentRecordIndex++;
}
/// <summary>
/// Flush the current data block if it has any data in it.
/// </summary>
void Flush()
{
if (this.debug)
{
//Console.WriteLine.WriteLine("TarBuffer.FlushBlock() called.");
}
if (this.outputStream == null)
{
throw new TarException("TarBuffer.Flush no output stream defined");
}
if (this.currentBlockIndex > 0)
{
this.WriteRecord();
}
outputStream.Flush();
}
/// <summary>
/// Close the TarBuffer. If this is an output buffer, also flush the
/// current block before closing.
/// </summary>
public void Close()
{
if (this.debug)
{
//Console.WriteLine.WriteLine("TarBuffer.Close().");
}
if (outputStream != null)
{
Flush();
outputStream.Close();
outputStream = null;
}
else if (inputStream != null)
{
inputStream.Close();
inputStream = null;
}
}
}
}
/* The original Java file had this header:
*
** Authored by Timothy Gerard Endres
** <mailto:time@gjt.org> <http://www.trustice.com>
**
** This work has been placed into the public domain.
** You may use this work in any way and for any purpose you wish.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?