Skip to content

Commit

Permalink
SWEEP: Changed all applicable EndOfStream creation statements to EOFE…
Browse files Browse the repository at this point in the history
…xception.Create() (see #446)
  • Loading branch information
NightOwl888 committed Apr 26, 2021
1 parent 0257003 commit 68c3f8f
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Lucene.Net.Analysis.Kuromoji/Dict/BinaryDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected BinaryDictionary()
int read = dictIS.Read(tmpBuffer.Array, 0, size);
if (read != size)
{
throw new EndOfStreamException("Cannot read whole dictionary");
throw EOFException.Create("Cannot read whole dictionary");
}
}
buffer = tmpBuffer.AsReadOnlyBuffer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ internal virtual void FillBuffer()
if (Debugging.AssertsEnabled) Debugging.Assert(decompressed <= length);
if (decompressed == length)
{
throw new EndOfStreamException();
throw EOFException.Create();
}
int toDecompress = Math.Min(length - decompressed, outerInstance.chunkSize);
outerInstance.decompressor.Decompress(outerInstance.fieldsStream, toDecompress, 0, toDecompress, outerInstance.bytes);
Expand Down
6 changes: 3 additions & 3 deletions src/Lucene.Net/Store/BufferedIndexInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public override sealed void ReadBytes(byte[] b, int offset, int len, bool useBuf
{
// Throw an exception when refill() could not read len bytes:
Buffer.BlockCopy(m_buffer, 0, b, offset, bufferLength);
throw new EndOfStreamException("read past EOF: " + this);
throw EOFException.Create("read past EOF: " + this);
}
else
{
Expand All @@ -192,7 +192,7 @@ public override sealed void ReadBytes(byte[] b, int offset, int len, bool useBuf
long after = bufferStart + bufferPosition + len;
if (after > Length)
{
throw new EndOfStreamException("read past EOF: " + this);
throw EOFException.Create("read past EOF: " + this);
}
ReadInternal(b, offset, len);
bufferStart = after;
Expand Down Expand Up @@ -378,7 +378,7 @@ private void Refill()
int newLength = (int)(end - start);
if (newLength <= 0)
{
throw new EndOfStreamException("read past EOF: " + this);
throw EOFException.Create("read past EOF: " + this);
}

if (m_buffer == null)
Expand Down
8 changes: 4 additions & 4 deletions src/Lucene.Net/Store/ByteBufferIndexInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public override sealed byte ReadByte()
curBufIndex++;
if (curBufIndex >= buffers.Length)
{
throw new EndOfStreamException("read past EOF: " + this);
throw EOFException.Create("read past EOF: " + this);
}
curBuf = buffers[curBufIndex];
curBuf.Position = 0;
Expand Down Expand Up @@ -151,7 +151,7 @@ public override sealed void ReadBytes(byte[] b, int offset, int len)
curBufIndex++;
if (curBufIndex >= buffers.Length)
{
throw new EndOfStreamException("read past EOF: " + this);
throw EOFException.Create("read past EOF: " + this);
}
curBuf = buffers[curBufIndex];
curBuf.Position = 0;
Expand Down Expand Up @@ -232,15 +232,15 @@ public override sealed void Seek(long pos)
// LUCENENET: Defensive programming so we don't get an IndexOutOfRangeException
// when reading from buffers.
if (bi < 0 || bi >= buffers.Length)
throw new EndOfStreamException("seek past EOF: " + this);
throw EOFException.Create("seek past EOF: " + this);

ByteBuffer b = buffers[bi];
int newPosition = (int)(pos & chunkSizeMask);

// LUCENENET: Defensive programming so we don't get an ArgumentOutOfRangeException
// when setting b.Position.
if (newPosition < 0 || newPosition > b.Limit)
throw new EndOfStreamException("seek past EOF: " + this);
throw EOFException.Create("seek past EOF: " + this);

b.Position = newPosition;
// write values, on exception all is unchanged
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Store/Directory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ protected override void ReadInternal(byte[] b, int offset, int len)
long start = GetFilePointer();
if (start + len > length)
{
throw new EndOfStreamException("read past EOF: " + this);
throw EOFException.Create("read past EOF: " + this);
}
@base.Seek(fileOffset + start);
@base.ReadBytes(b, offset, len, false);
Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net/Store/InputStreamDataInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override byte ReadByte()
int v = _reader.ReadByte();
if (v == -1)
{
throw new EndOfStreamException();
throw EOFException.Create();
}
return (byte)v;
}
Expand All @@ -50,7 +50,7 @@ public override void ReadBytes(byte[] b, int offset, int len)
if (cnt < 0)
{
// Partially read the input, but no more data available in the stream.
throw new EndOfStreamException();
throw EOFException.Create();
}
len -= cnt;
offset += cnt;
Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net/Store/NIOFSDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ protected override void ReadInternal(byte[] b, int offset, int len)

if (pos + len > m_end)
{
throw new EndOfStreamException("read past EOF: " + this);
throw EOFException.Create("read past EOF: " + this);
}

try
Expand All @@ -263,7 +263,7 @@ protected override void ReadInternal(byte[] b, int offset, int len)
int i = m_channel.Read(bb, pos);
if (i <= 0) // be defensive here, even though we checked before hand, something could have changed
{
throw new EndOfStreamException("read past EOF: " + this + " off: " + offset + " len: " + len + " pos: " + pos + " chunkLen: " + readLength + " end: " + m_end);
throw EOFException.Create("read past EOF: " + this + " off: " + offset + " len: " + len + " pos: " + pos + " chunkLen: " + readLength + " end: " + m_end);
}
pos += i;
readOffset += i;
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Store/RAMInputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void SwitchCurrentBuffer(bool enforceEOF)
// end of file reached, no more buffers left
if (enforceEOF)
{
throw new EndOfStreamException("read past EOF: " + this);
throw EOFException.Create("read past EOF: " + this);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net/Store/SimpleFSDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ protected override void ReadInternal(byte[] b, int offset, int len)

if (position + len > m_end)
{
throw new EndOfStreamException("read past EOF: " + this);
throw EOFException.Create("read past EOF: " + this);
}

try
Expand All @@ -224,7 +224,7 @@ protected override void ReadInternal(byte[] b, int offset, int len)
// int i = m_file.Read(b, offset + total, toRead);
// if (i < 0) // be defensive here, even though we checked before hand, something could have changed
// {
// throw new EndOfStreamException("read past EOF: " + this + " off: " + offset + " len: " + len + " total: " + total + " chunkLen: " + toRead + " end: " + m_end);
// throw EOFException.Create("read past EOF: " + this + " off: " + offset + " len: " + len + " total: " + total + " chunkLen: " + toRead + " end: " + m_end);
// }
// if (Debugging.AssertsEnabled) Debugging.Assert(i > 0, "RandomAccessFile.read with non zero-length toRead must always read at least one byte");
// total += i;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Lucene.Net.Diagnostics;
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;

namespace Lucene
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/// <summary>
/// Signals that an end of file or end of stream has been reached unexpectedly during input.
/// <para/>
/// This exception is mainly used by data input streams to signal end of stream. Note that
/// many other input operations return a special value on end of stream rather than throwing
/// an exception.
/// <para/>
/// This is a Java compatibility exception, and should be thrown in
/// Lucene.NET everywhere Lucene throws it, however catch blocks should
/// always use the <see cref="ExceptionExtensions.IsEOFException(Exception)"/> method.
/// <code>
/// catch (Exception ex) when (ex.IsEOFException())
/// </code>
/// </summary>
// LUCENENET: It is no longer good practice to use binary serialization.
// See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568
#if FEATURE_SERIALIZABLE_EXCEPTIONS
[Serializable]
#endif
internal class EOFException : EndOfStreamException
{
[Obsolete("Use EOFException.Create() instead.", error: true)]
public EOFException()
{
}

[Obsolete("Use EOFException.Create() instead.", error: true)]
public EOFException(string message) : base(message)
{
}

[Obsolete("Use EOFException.Create() instead.", error: true)]
public EOFException(string message, Exception innerException) : base(message, innerException)
{
}

[Obsolete("Use EOFException.Create() instead.", error: true)]
public EOFException(Exception cause)
: base(cause?.ToString(), cause)
{
}

#if FEATURE_SERIALIZABLE_EXCEPTIONS
/// <summary>
/// Initializes a new instance of this class with serialized data.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
protected EOFException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif

// Static factory methods

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Exception Create() => new EndOfStreamException();


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Exception Create(string message) => new EndOfStreamException(message);


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Exception Create(string message, Exception innerException) => new EndOfStreamException(message, innerException);


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Exception Create(Exception cause) => new EndOfStreamException(cause.Message, cause);
}
}
6 changes: 3 additions & 3 deletions src/Lucene.Net/Util/Packed/BlockPackedReaderIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void Skip(long count)
if (Debugging.AssertsEnabled) Debugging.Assert(count >= 0);
if (ord + count > valueCount || ord + count < 0)
{
throw new EndOfStreamException();
throw EOFException.Create();
}

// 1. skip buffered values
Expand Down Expand Up @@ -219,7 +219,7 @@ public long Next()
{
if (ord == valueCount)
{
throw new EndOfStreamException();
throw EOFException.Create();
}
if (off == blockSize)
{
Expand All @@ -237,7 +237,7 @@ public Int64sRef Next(int count)
if (Debugging.AssertsEnabled) Debugging.Assert(count > 0);
if (ord == valueCount)
{
throw new EndOfStreamException();
throw EOFException.Create();
}
if (off == blockSize)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Util/Packed/PackedReaderIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override Int64sRef Next(int count)
int remaining = m_valueCount - position - 1;
if (remaining <= 0)
{
throw new EndOfStreamException();
throw EOFException.Create();
}
count = Math.Min(remaining, count);

Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Util/Packed/PackedWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public override void Add(long v)
}
if (m_valueCount != -1 && written >= m_valueCount)
{
throw new EndOfStreamException("Writing past end of stream");
throw EOFException.Create("Writing past end of stream");
}
nextValues[off++] = v;
if (off == nextValues.Length)
Expand Down

0 comments on commit 68c3f8f

Please sign in to comment.