diff --git a/src/Lucene.Net.Analysis.Kuromoji/Dict/BinaryDictionary.cs b/src/Lucene.Net.Analysis.Kuromoji/Dict/BinaryDictionary.cs index 9ca9d5d381..8ce02fd1de 100644 --- a/src/Lucene.Net.Analysis.Kuromoji/Dict/BinaryDictionary.cs +++ b/src/Lucene.Net.Analysis.Kuromoji/Dict/BinaryDictionary.cs @@ -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(); diff --git a/src/Lucene.Net/Codecs/Compressing/CompressingStoredFieldsReader.cs b/src/Lucene.Net/Codecs/Compressing/CompressingStoredFieldsReader.cs index 4b3ffee24f..f3de8c585f 100644 --- a/src/Lucene.Net/Codecs/Compressing/CompressingStoredFieldsReader.cs +++ b/src/Lucene.Net/Codecs/Compressing/CompressingStoredFieldsReader.cs @@ -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); diff --git a/src/Lucene.Net/Store/BufferedIndexInput.cs b/src/Lucene.Net/Store/BufferedIndexInput.cs index b2b0831b68..720af9d6e6 100644 --- a/src/Lucene.Net/Store/BufferedIndexInput.cs +++ b/src/Lucene.Net/Store/BufferedIndexInput.cs @@ -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 { @@ -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; @@ -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) diff --git a/src/Lucene.Net/Store/ByteBufferIndexInput.cs b/src/Lucene.Net/Store/ByteBufferIndexInput.cs index 874069a81f..bdcff227c2 100644 --- a/src/Lucene.Net/Store/ByteBufferIndexInput.cs +++ b/src/Lucene.Net/Store/ByteBufferIndexInput.cs @@ -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; @@ -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; @@ -232,7 +232,7 @@ 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); @@ -240,7 +240,7 @@ public override sealed void Seek(long pos) // 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 diff --git a/src/Lucene.Net/Store/Directory.cs b/src/Lucene.Net/Store/Directory.cs index debd09ace9..51726d20b3 100644 --- a/src/Lucene.Net/Store/Directory.cs +++ b/src/Lucene.Net/Store/Directory.cs @@ -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); diff --git a/src/Lucene.Net/Store/InputStreamDataInput.cs b/src/Lucene.Net/Store/InputStreamDataInput.cs index fab212a7bd..d3b804019a 100644 --- a/src/Lucene.Net/Store/InputStreamDataInput.cs +++ b/src/Lucene.Net/Store/InputStreamDataInput.cs @@ -37,7 +37,7 @@ public override byte ReadByte() int v = _reader.ReadByte(); if (v == -1) { - throw new EndOfStreamException(); + throw EOFException.Create(); } return (byte)v; } @@ -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; diff --git a/src/Lucene.Net/Store/NIOFSDirectory.cs b/src/Lucene.Net/Store/NIOFSDirectory.cs index 7549011a1d..3ffb457c28 100644 --- a/src/Lucene.Net/Store/NIOFSDirectory.cs +++ b/src/Lucene.Net/Store/NIOFSDirectory.cs @@ -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 @@ -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; diff --git a/src/Lucene.Net/Store/RAMInputStream.cs b/src/Lucene.Net/Store/RAMInputStream.cs index 3d72de06b1..e845d631b1 100644 --- a/src/Lucene.Net/Store/RAMInputStream.cs +++ b/src/Lucene.Net/Store/RAMInputStream.cs @@ -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 { diff --git a/src/Lucene.Net/Store/SimpleFSDirectory.cs b/src/Lucene.Net/Store/SimpleFSDirectory.cs index 57e10eb955..3ffb38fc04 100644 --- a/src/Lucene.Net/Store/SimpleFSDirectory.cs +++ b/src/Lucene.Net/Store/SimpleFSDirectory.cs @@ -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 @@ -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; diff --git a/src/Lucene.Net/Support/ExceptionHandling/Exceptions/IOExceptions/EOFException.cs b/src/Lucene.Net/Support/ExceptionHandling/Exceptions/IOExceptions/EOFException.cs new file mode 100644 index 0000000000..c4de0c5008 --- /dev/null +++ b/src/Lucene.Net/Support/ExceptionHandling/Exceptions/IOExceptions/EOFException.cs @@ -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. + */ + + /// + /// Signals that an end of file or end of stream has been reached unexpectedly during input. + /// + /// 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. + /// + /// This is a Java compatibility exception, and should be thrown in + /// Lucene.NET everywhere Lucene throws it, however catch blocks should + /// always use the method. + /// + /// catch (Exception ex) when (ex.IsEOFException()) + /// + /// + // 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 + /// + /// Initializes a new instance of this class with serialized data. + /// + /// The that holds the serialized object data about the exception being thrown. + /// The that contains contextual information about the source or destination. + 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); + } +} diff --git a/src/Lucene.Net/Util/Packed/BlockPackedReaderIterator.cs b/src/Lucene.Net/Util/Packed/BlockPackedReaderIterator.cs index b35825d4ec..12f16fd6fa 100644 --- a/src/Lucene.Net/Util/Packed/BlockPackedReaderIterator.cs +++ b/src/Lucene.Net/Util/Packed/BlockPackedReaderIterator.cs @@ -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 @@ -219,7 +219,7 @@ public long Next() { if (ord == valueCount) { - throw new EndOfStreamException(); + throw EOFException.Create(); } if (off == blockSize) { @@ -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) { diff --git a/src/Lucene.Net/Util/Packed/PackedReaderIterator.cs b/src/Lucene.Net/Util/Packed/PackedReaderIterator.cs index e87c020cf5..9425a17caa 100644 --- a/src/Lucene.Net/Util/Packed/PackedReaderIterator.cs +++ b/src/Lucene.Net/Util/Packed/PackedReaderIterator.cs @@ -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); diff --git a/src/Lucene.Net/Util/Packed/PackedWriter.cs b/src/Lucene.Net/Util/Packed/PackedWriter.cs index bafb449a5a..4ff193dd33 100644 --- a/src/Lucene.Net/Util/Packed/PackedWriter.cs +++ b/src/Lucene.Net/Util/Packed/PackedWriter.cs @@ -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)