Skip to content

Commit 5a22072

Browse files
committed
As IsStreamOwner conect to Tar handling and tests.
1 parent e4a209b commit 5a22072

File tree

4 files changed

+117
-16
lines changed

4 files changed

+117
-16
lines changed

src/Tar/TarBuffer.cs

+18-3
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,16 @@ public int CurrentBlock
418418
get { return currentBlockIndex; }
419419
}
420420

421+
/// <summary>
422+
/// Get/set flag indicating ownership of the underlying stream.
423+
/// When the flag is true <see cref="Close"></see> will close the underlying stream also.
424+
/// </summary>
425+
public bool IsStreamOwner
426+
{
427+
get { return isStreamOwner_; }
428+
set { isStreamOwner_ = value; }
429+
}
430+
421431
/// <summary>
422432
/// Get the current block number, within the current record, zero based.
423433
/// </summary>
@@ -571,12 +581,16 @@ public void Close()
571581
{
572582
if (outputStream != null) {
573583
WriteFinalRecord();
574-
575-
outputStream.Close();
584+
585+
if (isStreamOwner_) {
586+
outputStream.Close();
587+
}
576588
outputStream = null;
577589
}
578590
else if (inputStream != null) {
579-
inputStream.Close();
591+
if (isStreamOwner_) {
592+
inputStream.Close();
593+
}
580594
inputStream = null;
581595
}
582596
}
@@ -591,6 +605,7 @@ public void Close()
591605

592606
int recordSize = DefaultRecordSize;
593607
int blockFactor = DefaultBlockFactor;
608+
bool isStreamOwner_ = true;
594609
#endregion
595610
}
596611
}

src/Tar/TarInputStream.cs

+10
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ public TarInputStream(Stream inputStream, int blockFactor)
7171

7272
#endregion
7373

74+
/// <summary>
75+
/// Get/set flag indicating ownership of the underlying stream.
76+
/// When the flag is true <see cref="Close"></see> will close the underlying stream also.
77+
/// </summary>
78+
public bool IsStreamOwner
79+
{
80+
get { return buffer.IsStreamOwner; }
81+
set { buffer.IsStreamOwner = value; }
82+
}
83+
7484
#region Stream Overrides
7585
/// <summary>
7686
/// Gets a value indicating whether the current stream supports reading

src/Tar/TarOutputStream.cs

+10
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ public TarOutputStream(Stream outputStream, int blockFactor)
7979
}
8080
#endregion
8181

82+
/// <summary>
83+
/// Get/set flag indicating ownership of the underlying stream.
84+
/// When the flag is true <see cref="Close"></see> will close the underlying stream also.
85+
/// </summary>
86+
public bool IsStreamOwner
87+
{
88+
get { return buffer.IsStreamOwner; }
89+
set { buffer.IsStreamOwner = value; }
90+
}
91+
8292
/// <summary>
8393
/// true if the stream supports reading; otherwise, false.
8494
/// </summary>

tests/Tar/TarTests.cs

+79-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using NUnit.Framework;
66

7+
using ICSharpCode.SharpZipLib.Tests.TestSupport;
78
using ICSharpCode.SharpZipLib.Tar;
89

910
namespace ICSharpCode.SharpZipLib.Tests.Tar {
@@ -50,11 +51,13 @@ public void EmptyTar()
5051
Assert.AreEqual(0, entryCount, "Expected 0 tar entries");
5152
}
5253
}
54+
5355
/// <summary>
5456
/// Check that the tar block factor can be varied successfully.
5557
/// </summary>
5658
[Test]
57-
public void BlockFactorHandling()
59+
[Category("Tar")]
60+
public void BlockFactorHandling()
5861
{
5962
const int MinimumBlockFactor = 1;
6063
const int MaximumBlockFactor = 64;
@@ -111,13 +114,13 @@ public void BlockFactorHandling()
111114
}
112115
}
113116
}
114-
115117

116118
/// <summary>
117119
/// Check that the tar trailer only contains nulls.
118120
/// </summary>
119121
[Test]
120-
public void TrailerContainsNulls()
122+
[Category("Tar")]
123+
public void TrailerContainsNulls()
121124
{
122125
const int TestBlockFactor = 3;
123126

@@ -438,19 +441,20 @@ public void ValuesPreserved()
438441
/// Check invalid mod times are detected
439442
/// </summary>
440443
[Test]
441-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
444+
[Category("Tar")]
445+
[ExpectedException(typeof(ArgumentOutOfRangeException))]
442446
public void InvalidModTime()
443447
{
444448
TarEntry e = TarEntry.CreateTarEntry("test");
445449
e.ModTime = DateTime.MinValue;
446450
}
447451

448-
449452
/// <summary>
450453
/// Check invalid sizes are detected
451454
/// </summary>
452455
[Test]
453-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
456+
[Category("Tar")]
457+
[ExpectedException(typeof(ArgumentOutOfRangeException))]
454458
public void InvalidSize()
455459
{
456460
TarEntry e = TarEntry.CreateTarEntry("test");
@@ -461,7 +465,8 @@ public void InvalidSize()
461465
/// Check invalid names are detected
462466
/// </summary>
463467
[Test]
464-
[ExpectedException(typeof(ArgumentNullException))]
468+
[Category("Tar")]
469+
[ExpectedException(typeof(ArgumentNullException))]
465470
public void InvalidName()
466471
{
467472
TarEntry e = TarEntry.CreateTarEntry("test");
@@ -472,7 +477,8 @@ public void InvalidName()
472477
/// Check setting user and group names.
473478
/// </summary>
474479
[Test]
475-
public void UserAndGroupNames()
480+
[Category("Tar")]
481+
public void UserAndGroupNames()
476482
{
477483
TarEntry e = TarEntry.CreateTarEntry("test");
478484
e.UserName = null;
@@ -487,7 +493,8 @@ public void UserAndGroupNames()
487493
/// Check invalid magic values are detected
488494
/// </summary>
489495
[Test]
490-
[ExpectedException(typeof(ArgumentNullException))]
496+
[Category("Tar")]
497+
[ExpectedException(typeof(ArgumentNullException))]
491498
public void InvalidMagic()
492499
{
493500
TarEntry e = TarEntry.CreateTarEntry("test");
@@ -498,7 +505,8 @@ public void InvalidMagic()
498505
/// Check invalid link names are detected
499506
/// </summary>
500507
[Test]
501-
[ExpectedException(typeof(ArgumentNullException))]
508+
[Category("Tar")]
509+
[ExpectedException(typeof(ArgumentNullException))]
502510
public void InvalidLinkName()
503511
{
504512
TarEntry e = TarEntry.CreateTarEntry("test");
@@ -509,15 +517,17 @@ public void InvalidLinkName()
509517
/// Check invalid version names are detected
510518
/// </summary>
511519
[Test]
512-
[ExpectedException(typeof(ArgumentNullException))]
520+
[Category("Tar")]
521+
[ExpectedException(typeof(ArgumentNullException))]
513522
public void InvalidVersionName()
514523
{
515524
TarEntry e = TarEntry.CreateTarEntry("test");
516525
e.TarHeader.Version = null;
517526
}
518527

519528
[Test]
520-
public void CloningAndUniqueness()
529+
[Category("Tar")]
530+
public void CloningAndUniqueness()
521531
{
522532
// Partial test of cloning for TarHeader and TarEntry
523533
TarEntry e = TarEntry.CreateTarEntry("ohsogood");
@@ -557,5 +567,61 @@ public void CloningAndUniqueness()
557567

558568
Assert.AreNotEqual(headerD.LinkName, entryf.TarHeader.LinkName, "Entry headers should be unique");
559569
}
560-
}
570+
571+
[Test]
572+
[Category("Tar")]
573+
public void OutputStreamOwnership()
574+
{
575+
TrackedMemoryStream memStream = new TrackedMemoryStream();
576+
TarOutputStream s = new TarOutputStream(memStream);
577+
578+
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
579+
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
580+
581+
s.Close();
582+
583+
Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close");
584+
Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close");
585+
586+
memStream = new TrackedMemoryStream();
587+
s = new TarOutputStream(memStream);
588+
589+
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
590+
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
591+
592+
s.IsStreamOwner = false;
593+
s.Close();
594+
595+
Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close");
596+
Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close");
597+
}
598+
599+
[Test]
600+
[Category("Tar")]
601+
public void InputStreamOwnership()
602+
{
603+
TrackedMemoryStream memStream = new TrackedMemoryStream();
604+
TarInputStream s = new TarInputStream(memStream);
605+
606+
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
607+
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
608+
609+
s.Close();
610+
611+
Assert.IsTrue(memStream.IsClosed, "Should be closed after parent owner close");
612+
Assert.IsTrue(memStream.IsDisposed, "Should be disposed after parent owner close");
613+
614+
memStream = new TrackedMemoryStream();
615+
s = new TarInputStream(memStream);
616+
617+
Assert.IsFalse(memStream.IsClosed, "Shouldnt be closed initially");
618+
Assert.IsFalse(memStream.IsDisposed, "Shouldnt be disposed initially");
619+
620+
s.IsStreamOwner = false;
621+
s.Close();
622+
623+
Assert.IsFalse(memStream.IsClosed, "Should not be closed after parent owner close");
624+
Assert.IsFalse(memStream.IsDisposed, "Should not be disposed after parent owner close");
625+
}
626+
}
561627
}

0 commit comments

Comments
 (0)