(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / ICSharpCode.SharpZipLib / ICSharpCode.SharpZipLib / Tar / TarBuffer.cs
blobb53b713e26d2976d2f4a9b6bc6f6e0a053fecb34
1 // TarBuffer.cs
2 // Copyright (C) 2001 Mike Krueger
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 // Linking this library statically or dynamically with other modules is
19 // making a combined work based on this library. Thus, the terms and
20 // conditions of the GNU General Public License cover the whole
21 // combination.
23 // As a special exception, the copyright holders of this library give you
24 // permission to link this library with independent modules to produce an
25 // executable, regardless of the license terms of these independent
26 // modules, and to copy and distribute the resulting executable under
27 // terms of your choice, provided that you also meet, for each linked
28 // independent module, the terms and conditions of the license of that
29 // module. An independent module is a module which is not derived from
30 // or based on this library. If you modify this library, you may extend
31 // this exception to your version of the library, but you are not
32 // obligated to do so. If you do not wish to do so, delete this
33 // exception statement from your version.
35 using System;
36 using System.IO;
37 using System.Text;
39 namespace ICSharpCode.SharpZipLib.Tar
42 /// <summary>
43 /// The TarBuffer class implements the tar archive concept
44 /// of a buffered input stream. This concept goes back to the
45 /// days of blocked tape drives and special io devices. In the
46 /// C# universe, the only real function that this class
47 /// performs is to ensure that files have the correct "record"
48 /// size, or other tars will complain.
49 /// <p>
50 /// You should never have a need to access this class directly.
51 /// TarBuffers are created by Tar IO Streams.
52 /// </p>
53 /// </summary>
54 public class TarBuffer
57 /* A quote from GNU tar man file on blocking and records
58 A `tar' archive file contains a series of blocks. Each block
59 contains `BLOCKSIZE' bytes. Although this format may be thought of as
60 being on magnetic tape, other media are often used.
62 Each file archived is represented by a header block which describes
63 the file, followed by zero or more blocks which give the contents of
64 the file. At the end of the archive file there may be a block filled
65 with binary zeros as an end-of-file marker. A reasonable system should
66 write a block of zeros at the end, but must not assume that such a
67 block exists when reading an archive.
69 The blocks may be "blocked" for physical I/O operations. Each
70 record of N blocks (where N is set by the `--blocking-factor=512-SIZE'
71 (`-b 512-SIZE') option to `tar') is written with a single `write ()'
72 operation. On magnetic tapes, the result of such a write is a single
73 record. When writing an archive, the last record of blocks should be
74 written at the full size, with blocks after the zero block containing
75 all zeros. When reading an archive, a reasonable system should
76 properly handle an archive whose last record is shorter than the rest,
77 or which contains garbage records after a zero block.
80 // public static readonly int DEFAULT_RCDSIZE = 512;
81 // public const int DEFAULT_BLOCKFACTOR = 20;
82 // public static readonly int DEFAULT_BLKSIZE = DEFAULT_RCDSIZE * DEFAULT_BLOCKFACTOR;
84 public static readonly int BlockSize = 512;
85 public static readonly int DefaultBlockFactor = 20;
86 public static readonly int DefaultRecordSize = BlockSize * DefaultBlockFactor;
88 Stream inputStream;
89 Stream outputStream;
91 byte[] recordBuffer;
92 int currentBlockIndex;
93 int currentRecordIndex;
95 int recordSize = DefaultRecordSize;
96 public int RecordSize
98 get { return recordSize; }
101 int blockFactor = DefaultBlockFactor;
103 public int BlockFactor
105 get { return blockFactor; }
108 bool debug = false;
110 /// <summary>
111 /// Set the debugging flag for the buffer.
112 /// </summary>
113 public void SetDebug(bool debug)
115 this.debug = debug;
119 protected TarBuffer()
123 public static TarBuffer CreateInputTarBuffer(Stream inputStream)
125 return CreateInputTarBuffer(inputStream, TarBuffer.DefaultBlockFactor);
128 public static TarBuffer CreateInputTarBuffer(Stream inputStream, int blockFactor)
130 TarBuffer tarBuffer = new TarBuffer();
131 tarBuffer.inputStream = inputStream;
132 tarBuffer.outputStream = null;
133 tarBuffer.Initialize(blockFactor);
135 return tarBuffer;
138 public static TarBuffer CreateOutputTarBuffer(Stream outputStream)
140 return CreateOutputTarBuffer(outputStream, TarBuffer.DefaultBlockFactor);
143 public static TarBuffer CreateOutputTarBuffer(Stream outputStream, int blockFactor)
145 TarBuffer tarBuffer = new TarBuffer();
146 tarBuffer.inputStream = null;
147 tarBuffer.outputStream = outputStream;
148 tarBuffer.Initialize(blockFactor);
150 return tarBuffer;
153 /// <summary>
154 /// Initialization common to all constructors.
155 /// </summary>
156 void Initialize(int blockFactor)
158 this.debug = false;
159 this.blockFactor = blockFactor;
160 this.recordSize = blockFactor * BlockSize;
162 this.recordBuffer = new byte[RecordSize];
164 if (inputStream != null)
166 this.currentRecordIndex = -1;
167 this.currentBlockIndex = BlockFactor;
169 else
171 this.currentRecordIndex = 0;
172 this.currentBlockIndex = 0;
176 /// <summary>
177 /// Get the TAR Buffer's block factor
178 /// </summary>
179 public int GetBlockFactor()
181 return this.blockFactor;
184 /// <summary>
185 /// Get the TAR Buffer's record size.
186 /// </summary>
187 public int GetRecordSize()
189 return this.recordSize;
192 /// <summary>
193 /// Determine if an archive block indicates End of Archive. End of
194 /// archive is indicated by a block that consists entirely of null bytes.
195 /// All remaining blocks for the record should also be null's
196 /// However some older tars only do a couple of null blocks (Old GNU tar for one)
197 /// and also partial records
198 /// </summary>
199 /// <param name = "block">
200 /// The block data to check.
201 /// </param>
202 public bool IsEOFBlock(byte[] block)
204 for (int i = 0, sz = BlockSize; i < sz; ++i)
206 if (block[i] != 0)
208 return false;
212 return true;
215 /// <summary>
216 /// Skip over a block on the input stream.
217 /// </summary>
218 public void SkipBlock()
220 if (this.debug)
222 //Console.WriteLine.WriteLine("SkipBlock: recIdx = " + this.currentRecordIndex + " blkIdx = " + this.currentBlockIndex);
225 if (this.inputStream == null)
227 throw new System.IO.IOException("no input stream defined");
230 if (this.currentBlockIndex >= this.BlockFactor)
232 if (!this.ReadRecord())
234 return; // UNDONE
238 this.currentBlockIndex++;
241 /// <summary>
242 /// Read a block from the input stream and return the data.
243 /// </summary>
244 /// <returns>
245 /// The block data.
246 /// </returns>
247 public byte[] ReadBlock()
249 if (this.debug)
251 //Console.WriteLine.WriteLine( "ReadBlock: blockIndex = " + this.currentBlockIndex + " recordIndex = " + this.currentRecordIndex );
254 if (this.inputStream == null)
256 throw new ApplicationException("TarBuffer.ReadBlock - no input stream defined");
259 if (this.currentBlockIndex >= this.BlockFactor)
261 if (!this.ReadRecord())
263 return null;
267 byte[] result = new byte[BlockSize];
269 Array.Copy(this.recordBuffer, (this.currentBlockIndex * BlockSize), result, 0, BlockSize );
270 this.currentBlockIndex++;
271 return result;
274 /// <returns>
275 /// false if End-Of-File, else true
276 /// </returns>
277 bool ReadRecord()
279 if (this.debug)
281 //Console.WriteLine.WriteLine("ReadRecord: recordIndex = " + this.currentRecordIndex);
284 if (this.inputStream == null)
286 throw new System.IO.IOException("no input stream stream defined");
289 this.currentBlockIndex = 0;
291 int offset = 0;
292 int bytesNeeded = RecordSize;
294 while (bytesNeeded > 0)
296 long numBytes = this.inputStream.Read(this.recordBuffer, offset, bytesNeeded);
299 // NOTE
300 // We have found EOF, and the record is not full!
302 // This is a broken archive. It does not follow the standard
303 // blocking algorithm. However, because we are generous, and
304 // it requires little effort, we will simply ignore the error
305 // and continue as if the entire record were read. This does
306 // not appear to break anything upstream. We used to return
307 // false in this case.
309 // Thanks to 'Yohann.Roussel@alcatel.fr' for this fix.
311 if (numBytes <= 0)
313 break;
316 offset += (int)numBytes;
317 bytesNeeded -= (int)numBytes;
318 if (numBytes != RecordSize)
320 if (this.debug)
322 //Console.WriteLine.WriteLine("ReadRecord: INCOMPLETE READ " + numBytes + " of " + this.blockSize + " bytes read.");
327 this.currentRecordIndex++;
328 return true;
331 /// <summary>
332 /// Get the current block number, within the current record, zero based.
333 /// </summary>
334 /// <returns>
335 /// The current zero based block number.
336 /// </returns>
337 public int GetCurrentBlockNum()
339 return this.currentBlockIndex;
342 /// <summary>
343 /// Get the current record number
344 /// Absolute block number in file = (currentRecordNum * block factor) + currentBlockNum.
345 /// </summary>
346 /// <returns>
347 /// The current zero based record number.
348 /// </returns>
349 public int GetCurrentRecordNum()
351 return this.currentRecordIndex;
354 /// <summary>
355 /// Write an archive block to the archive.
356 /// </summary>
357 /// <param name="block">
358 /// The data to write to the archive.
359 /// </param>
360 ///
361 public void WriteBlock(byte[] block)
363 if (this.debug)
365 //Console.WriteLine.WriteLine("WriteRecord: recIdx = " + this.currentRecordIndex + " blkIdx = " + this.currentBlockIndex );
368 if (this.outputStream == null)
370 throw new ApplicationException("TarBuffer.WriteBlock - no output stream defined");
373 if (block.Length != BlockSize)
375 throw new ApplicationException("TarBuffer.WriteBlock - block to write has length '" + block.Length + "' which is not the block size of '" + BlockSize + "'" );
378 if (this.currentBlockIndex >= BlockFactor)
380 this.WriteRecord();
383 Array.Copy(block, 0, this.recordBuffer, (this.currentBlockIndex * BlockSize), BlockSize);
384 this.currentBlockIndex++;
387 /// <summary>
388 /// Write an archive record to the archive, where the record may be
389 /// inside of a larger array buffer. The buffer must be "offset plus
390 /// record size" long.
391 /// </summary>
392 /// <param name="buf">
393 /// The buffer containing the record data to write.
394 /// </param>
395 /// <param name="offset">
396 /// The offset of the record data within buf.
397 /// </param>
398 public void WriteBlock(byte[] buf, int offset)
400 if (this.debug)
402 //Console.WriteLine.WriteLine("WriteBlock: recIdx = " + this.currentRecordIndex + " blkIdx = " + this.currentBlockIndex );
405 if (this.outputStream == null)
407 throw new ApplicationException("TarBuffer.WriteBlock - no output stream stream defined");
410 if ((offset + BlockSize) > buf.Length)
412 throw new ApplicationException("TarBuffer.WriteBlock - record has length '" + buf.Length + "' with offset '" + offset + "' which is less than the record size of '" + this.recordSize + "'" );
415 if (this.currentBlockIndex >= this.BlockFactor)
417 this.WriteRecord();
420 Array.Copy(buf, offset, this.recordBuffer, (this.currentBlockIndex * BlockSize), BlockSize);
422 this.currentBlockIndex++;
425 /// <summary>
426 /// Write a TarBuffer record to the archive.
427 /// </summary>
428 void WriteRecord()
430 if (this.debug)
432 //Console.WriteLine.WriteLine("Writerecord: record index = " + this.currentRecordIndex);
435 if (this.outputStream == null)
437 throw new ApplicationException("TarBuffer.WriteRecord no output stream defined");
440 this.outputStream.Write(this.recordBuffer, 0, RecordSize);
441 this.outputStream.Flush();
443 this.currentBlockIndex = 0;
444 this.currentRecordIndex++;
447 /// <summary>
448 /// Flush the current data block if it has any data in it.
449 /// </summary>
450 void Flush()
452 if (this.debug)
454 //Console.WriteLine.WriteLine("TarBuffer.FlushBlock() called.");
457 if (this.outputStream == null)
459 throw new ApplicationException("TarBuffer.Flush no output stream defined");
462 if (this.currentBlockIndex > 0)
464 this.WriteRecord();
466 outputStream.Flush();
469 /// <summary>
470 /// Close the TarBuffer. If this is an output buffer, also flush the
471 /// current block before closing.
472 /// </summary>
473 public void Close()
475 if (this.debug)
477 //Console.WriteLine.WriteLine("TarBuffer.Close().");
480 if (outputStream != null)
482 Flush();
484 outputStream.Close();
485 outputStream = null;
487 else if (inputStream != null)
489 inputStream.Close();
490 inputStream = null;
496 /* The original Java file had this header:
498 ** Authored by Timothy Gerard Endres
499 ** <mailto:time@gjt.org> <http://www.trustice.com>
501 ** This work has been placed into the public domain.
502 ** You may use this work in any way and for any purpose you wish.
504 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
505 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
506 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
507 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
508 ** REDISTRIBUTION OF THIS SOFTWARE.