Skip to content

Commit 9a9de82

Browse files
sauravk58PR Writer
andauthored
Fix #1175: Change IndexInputStream to throw NotSupportedException for writable members (#1231)
Changed Flush(), SetLength(), and Write() methods to throw NotSupportedException instead of IllegalStateException/InvalidCastException to be consistent with .NET conventions for read-only streams. Added unit tests to verify the correct exception types are thrown. Co-authored-by: PR Writer <prwriter@reveloexperts.com>
1 parent aa62606 commit 9a9de82

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

src/Lucene.Net.Replicator/IndexInputInputStream.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public IndexInputStream(IndexInput input)
3838

3939
public override void Flush()
4040
{
41-
throw IllegalStateException.Create("Cannot flush a readonly stream."); // LUCENENET TODO: Change to NotSupportedException ?
41+
throw new NotSupportedException("Cannot flush a readonly stream.");
4242
}
4343

4444
public override long Seek(long offset, SeekOrigin origin)
@@ -60,7 +60,7 @@ public override long Seek(long offset, SeekOrigin origin)
6060

6161
public override void SetLength(long value)
6262
{
63-
throw IllegalStateException.Create("Cannot change length of a readonly stream."); // LUCENENET TODO: Change to NotSupportedException ?
63+
throw new NotSupportedException("Cannot change length of a readonly stream.");
6464
}
6565

6666
public override int Read(byte[] buffer, int offset, int count)
@@ -74,7 +74,7 @@ public override int Read(byte[] buffer, int offset, int count)
7474

7575
public override void Write(byte[] buffer, int offset, int count)
7676
{
77-
throw new InvalidCastException("Cannot write to a readonly stream."); // LUCENENET TODO: Change to NotSupportedException ?
77+
throw new NotSupportedException("Cannot write to a readonly stream.");
7878
}
7979

8080
public override bool CanRead => true;

src/Lucene.Net.Tests.Replicator/IndexInputStreamTest.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,51 @@ public void Read_RemainingIndexInputLargerThanReadCount_ReturnsExpectedSection([
6464
Assert.AreEqual(readBuffer, buffer.Skip((section - 1) * readBytes).Take(readBytes).ToArray());
6565
}
6666

67+
/// <summary>
68+
/// Test for GitHub issue #1175: IndexInputStream should throw NotSupportedException for writable members
69+
/// https://github.com/apache/lucenenet/issues/1175
70+
/// </summary>
71+
[Test]
72+
[LuceneNetSpecific]
73+
public void TestGitHubIssue1175_Flush_ThrowsNotSupportedException()
74+
{
75+
byte[] buffer = new byte[1.KiloBytes()];
76+
Random.NextBytes(buffer);
77+
using IndexInputStream stream = new IndexInputStream(new MockIndexInput(buffer));
78+
79+
Assert.Throws<NotSupportedException>(() => stream.Flush());
80+
}
81+
82+
/// <summary>
83+
/// Test for GitHub issue #1175: IndexInputStream should throw NotSupportedException for writable members
84+
/// https://github.com/apache/lucenenet/issues/1175
85+
/// </summary>
86+
[Test]
87+
[LuceneNetSpecific]
88+
public void TestGitHubIssue1175_SetLength_ThrowsNotSupportedException()
89+
{
90+
byte[] buffer = new byte[1.KiloBytes()];
91+
Random.NextBytes(buffer);
92+
using IndexInputStream stream = new IndexInputStream(new MockIndexInput(buffer));
93+
94+
Assert.Throws<NotSupportedException>(() => stream.SetLength(100));
95+
}
96+
97+
/// <summary>
98+
/// Test for GitHub issue #1175: IndexInputStream should throw NotSupportedException for writable members
99+
/// https://github.com/apache/lucenenet/issues/1175
100+
/// </summary>
101+
[Test]
102+
[LuceneNetSpecific]
103+
public void TestGitHubIssue1175_Write_ThrowsNotSupportedException()
104+
{
105+
byte[] buffer = new byte[1.KiloBytes()];
106+
Random.NextBytes(buffer);
107+
using IndexInputStream stream = new IndexInputStream(new MockIndexInput(buffer));
108+
109+
Assert.Throws<NotSupportedException>(() => stream.Write(new byte[10], 0, 10));
110+
}
111+
67112
/// <summary>
68113
/// Test for GitHub issue #1158: Integer overflow in IndexInputStream.Read()
69114
/// https://github.com/apache/lucenenet/issues/1158

0 commit comments

Comments
 (0)