tarbuffer.cs

来自「全功能c#编译器」· CS 代码 · 共 527 行 · 第 1/2 页

CS
527
字号
// TarBuffer.cs
// Copyright (C) 2001 Mike Krueger
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// Linking this library statically or dynamically with other modules is
// making a combined work based on this library.  Thus, the terms and
// conditions of the GNU General Public License cover the whole
// combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent
// modules, and to copy and distribute the resulting executable under
// terms of your choice, provided that you also meet, for each linked
// independent module, the terms and conditions of the license of that
// module.  An independent module is a module which is not derived from
// or based on this library.  If you modify this library, you may extend
// this exception to your version of the library, but you are not
// obligated to do so.  If you do not wish to do so, delete this
// exception statement from your version.

using System;
using System.IO;
using System.Text;

namespace ICSharpCode.SharpZipLib.Tar 
{
	
	/// <summary>
	/// The TarBuffer class implements the tar archive concept
	/// of a buffered input stream. This concept goes back to the
	/// days of blocked tape drives and special io devices. In the
	/// C# universe, the only real function that this class
	/// performs is to ensure that files have the correct "record"
	/// size, or other tars will complain.
	/// <p>
	/// You should never have a need to access this class directly.
	/// TarBuffers are created by Tar IO Streams.
	/// </p>
	/// </summary>
	public class TarBuffer
	{

/* A quote from GNU tar man file on blocking and records
   A `tar' archive file contains a series of blocks.  Each block
contains `BLOCKSIZE' bytes.  Although this format may be thought of as
being on magnetic tape, other media are often used.

   Each file archived is represented by a header block which describes
the file, followed by zero or more blocks which give the contents of
the file.  At the end of the archive file there may be a block filled
with binary zeros as an end-of-file marker.  A reasonable system should
write a block of zeros at the end, but must not assume that such a
block exists when reading an archive.

   The blocks may be "blocked" for physical I/O operations.  Each
record of N blocks (where N is set by the `--blocking-factor=512-SIZE'
(`-b 512-SIZE') option to `tar') is written with a single `write ()'
operation.  On magnetic tapes, the result of such a write is a single
record.  When writing an archive, the last record of blocks should be
written at the full size, with blocks after the zero block containing
all zeros.  When reading an archive, a reasonable system should
properly handle an archive whose last record is shorter than the rest,
or which contains garbage records after a zero block.
*/

		/// <summary>
		/// The size of a block in a tar archive
		/// </summary>
		public static readonly int BlockSize = 512;
		
		/// <summary>
		/// Number of blocks in a record by default
		/// </summary>
		public static readonly int DefaultBlockFactor = 20;
		
		/// <summary>
		/// Size in bytes of a record by default
		/// </summary>
		public static readonly int DefaultRecordSize = BlockSize * DefaultBlockFactor;
		
		Stream inputStream;
		Stream outputStream;
		
		byte[] recordBuffer;
		int    currentBlockIndex;
		int    currentRecordIndex;

		int    recordSize = DefaultRecordSize;
		
		/// <summary>
		/// Get the recordsize for this buffer
		/// </summary>
		public int RecordSize {
			get { 
				return recordSize; 
			}
		}

		int    blockFactor = DefaultBlockFactor;

		/// <summary>
		/// Get the Blocking factor for the buffer
		/// </summary>
		public int BlockFactor {
			get { 
				return blockFactor; 
			}
		}

		bool   debug = false;

		/// <summary>
		/// Set the debugging flag for the buffer.
		/// </summary>
		public void SetDebug(bool debug)
		{
			this.debug = debug;
		}
		
		
		/// <summary>
		/// Construct a default TarBuffer
		/// </summary>
		protected TarBuffer()
		{
		}
		
		/// <summary>
		/// Create TarBuffer for reading with default BlockFactor
		/// </summary>
		/// <param name="inputStream">Stream to buffer</param>
		/// <returns>TarBuffer</returns>
		public static TarBuffer CreateInputTarBuffer(Stream inputStream)
		{
			return CreateInputTarBuffer(inputStream, TarBuffer.DefaultBlockFactor);
		}

		/// <summary>
		/// Construct TarBuffer for reading inputStream setting BlockFactor
		/// </summary>
		/// <param name="inputStream">Stream to buffer</param>
		/// <param name="blockFactor">Blocking factor to apply</param>
		/// <returns>TarBuffer</returns>
		public static TarBuffer CreateInputTarBuffer(Stream inputStream, int blockFactor)
		{
			TarBuffer tarBuffer = new TarBuffer();
			tarBuffer.inputStream  = inputStream;
			tarBuffer.outputStream = null;
			tarBuffer.Initialize(blockFactor);
			
			return tarBuffer;
		}

		/// <summary>
		/// Construct TarBuffer for writing with default BlockFactor
		/// </summary>
		/// <param name="outputStream">output stream for buffer</param>
		/// <returns>TarBuffer</returns>
		public static TarBuffer CreateOutputTarBuffer(Stream outputStream)
		{
			return CreateOutputTarBuffer(outputStream, TarBuffer.DefaultBlockFactor);
		}

		/// <summary>
		/// Construct TarBuffer for writing setting BlockFactor
		/// </summary>
		/// <param name="outputStream">output stream to buffer</param>
		/// <param name="blockFactor">Blocking factor to apply</param>
		/// <returns>TarBuffer</returns>
		public static TarBuffer CreateOutputTarBuffer(Stream outputStream, int blockFactor)
		{
			TarBuffer tarBuffer = new TarBuffer();
			tarBuffer.inputStream  = null;
			tarBuffer.outputStream = outputStream;
			tarBuffer.Initialize(blockFactor);
			
			return tarBuffer;
		}
		
		/// <summary>
		/// Initialization common to all constructors.
		/// </summary>
		void Initialize(int blockFactor)
		{
			this.debug        = false;
			this.blockFactor  = blockFactor;
			this.recordSize   = blockFactor * BlockSize;

			this.recordBuffer  = new byte[RecordSize];
			
			if (inputStream != null) {
				this.currentRecordIndex = -1;
				this.currentBlockIndex = BlockFactor;
			} else {
				this.currentRecordIndex = 0;
				this.currentBlockIndex = 0;
			}
		}
		
		/// <summary>
		/// Get the TAR Buffer's block factor
		/// </summary>
		public int GetBlockFactor()
		{
			return this.blockFactor;
		}
		
		/// <summary>
		/// Get the TAR Buffer's record size.
		/// </summary>
		public int GetRecordSize()
		{
			return this.recordSize;
		}
		
		/// <summary>
		/// Determine if an archive block indicates End of Archive. End of
		/// archive is indicated by a block that consists entirely of null bytes.
		/// All remaining blocks for the record should also be null's
		/// However some older tars only do a couple of null blocks (Old GNU tar for one)
		/// and also partial records
		/// </summary>
		/// <param name = "block">
		/// The block data to check.
		/// </param>
		public bool IsEOFBlock(byte[] block)
		{
			for (int i = 0, sz = BlockSize; i < sz; ++i) {
				if (block[i] != 0) {
					return false;
				}
			}
			
			return true;
		}
		
		/// <summary>
		/// Skip over a block on the input stream.
		/// </summary>
		public void SkipBlock()
		{
			if (this.debug) {
				//Console.WriteLine.WriteLine("SkipBlock: recIdx = " + this.currentRecordIndex + " blkIdx = " + this.currentBlockIndex);
			}
			
			if (this.inputStream == null) {
				throw new TarException("no input stream defined");
			}
			
			if (this.currentBlockIndex >= this.BlockFactor) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?