ByteBufferIndexInput: Refactored to avoid calls on invalid conditions rather 
than catching and re-throwing exceptions


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/26e080ef
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/26e080ef
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/26e080ef

Branch: refs/heads/master
Commit: 26e080efb05ac3012da4e8a034746d66f3b2e570
Parents: ae5075c
Author: Shad Storhaug <[email protected]>
Authored: Sun Apr 30 23:39:27 2017 +0700
Committer: Shad Storhaug <[email protected]>
Committed: Mon May 1 04:48:38 2017 +0700

----------------------------------------------------------------------
 src/Lucene.Net/Store/ByteBufferIndexInput.cs | 109 ++++++++++------------
 1 file changed, 49 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/26e080ef/src/Lucene.Net/Store/ByteBufferIndexInput.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Store/ByteBufferIndexInput.cs 
b/src/Lucene.Net/Store/ByteBufferIndexInput.cs
index bda469c..629b5d0 100644
--- a/src/Lucene.Net/Store/ByteBufferIndexInput.cs
+++ b/src/Lucene.Net/Store/ByteBufferIndexInput.cs
@@ -97,42 +97,41 @@ namespace Lucene.Net.Store
             Seek(0L);
         }
 
-
         public override sealed byte ReadByte()
         {
-            try
+            // LUCENENET: Refactored to avoid calls on invalid conditions 
instead of
+            // catching and re-throwing exceptions in the normal workflow.
+            EnsureOpen();
+            if (curBuf.HasRemaining)
             {
                 return curBuf.Get();
             }
-            catch (BufferUnderflowException)
+
+            do
             {
-                do
+                curBufIndex++;
+                if (curBufIndex >= buffers.Length)
                 {
-                    curBufIndex++;
-                    if (curBufIndex >= buffers.Length)
-                    {
-                        throw new EndOfStreamException("read past EOF: " + 
this);
-                    }
-                    curBuf = buffers[curBufIndex];
-                    curBuf.Position = 0;
-                } while (!curBuf.HasRemaining);
-                return curBuf.Get();
-            }
-            catch (NullReferenceException)
-            {
-                throw new 
ObjectDisposedException(this.GetType().GetTypeInfo().FullName, "Already closed: 
" + this);
-            }
+                    throw new EndOfStreamException("read past EOF: " + this);
+                }
+                curBuf = buffers[curBufIndex];
+                curBuf.Position = 0;
+            } while (!curBuf.HasRemaining);
+            return curBuf.Get();
         }
 
         public override sealed void ReadBytes(byte[] b, int offset, int len)
         {
-            try
+            // LUCENENET: Refactored to avoid calls on invalid conditions 
instead of
+            // catching and re-throwing exceptions in the normal workflow.
+            EnsureOpen();
+            int curAvail = curBuf.Remaining;
+            if (len <= curAvail)
             {
                 curBuf.Get(b, offset, len);
             }
-            catch (BufferUnderflowException)
+            else
             {
-                int curAvail = curBuf.Remaining;
                 while (len > curAvail)
                 {
                     curBuf.Get(b, offset, curAvail);
@@ -149,10 +148,6 @@ namespace Lucene.Net.Store
                 }
                 curBuf.Get(b, offset, len);
             }
-            catch (NullReferenceException)
-            {
-                throw new 
ObjectDisposedException(this.GetType().GetTypeInfo().FullName, "Already closed: 
" + this);
-            }
         }
 
         /// <summary>
@@ -160,18 +155,14 @@ namespace Lucene.Net.Store
         /// </summary>
         public override sealed short ReadInt16()
         {
-            try
+            // LUCENENET: Refactored to avoid calls on invalid conditions 
instead of
+            // catching and re-throwing exceptions in the normal workflow.
+            EnsureOpen();
+            if (curBuf.Remaining >= 2)
             {
                 return curBuf.GetInt16();
             }
-            catch (BufferUnderflowException)
-            {
-                return base.ReadInt16();
-            }
-            catch (NullReferenceException)
-            {
-                throw new 
ObjectDisposedException(this.GetType().GetTypeInfo().FullName, "Already closed: 
" + this);
-            }
+            return base.ReadInt16();
         }
 
         /// <summary>
@@ -179,18 +170,14 @@ namespace Lucene.Net.Store
         /// </summary>
         public override sealed int ReadInt32()
         {
-            try
+            // LUCENENET: Refactored to avoid calls on invalid conditions 
instead of
+            // catching and re-throwing exceptions in the normal workflow.
+            EnsureOpen();
+            if (curBuf.Remaining >= 4)
             {
                 return curBuf.GetInt32();
             }
-            catch (BufferUnderflowException)
-            {
-                return base.ReadInt32();
-            }
-            catch (NullReferenceException)
-            {
-                throw new 
ObjectDisposedException(this.GetType().GetTypeInfo().FullName, "Already closed: 
" + this);
-            }
+            return base.ReadInt32();
         }
 
         /// <summary>
@@ -198,30 +185,22 @@ namespace Lucene.Net.Store
         /// </summary>
         public override sealed long ReadInt64()
         {
-            try
+            // LUCENENET: Refactored to avoid calls on invalid conditions 
instead of
+            // catching and re-throwing exceptions in the normal workflow.
+            EnsureOpen();
+            if (curBuf.Remaining >= 8)
             {
                 return curBuf.GetInt64();
             }
-            catch (BufferUnderflowException)
-            {
-                return base.ReadInt64();
-            }
-            catch (NullReferenceException)
-            {
-                throw new 
ObjectDisposedException(this.GetType().GetTypeInfo().FullName, "Already closed: 
" + this);
-            }
+            return base.ReadInt64();
         }
 
         public override sealed long GetFilePointer()
         {
-            try
-            {
-                return (((long)curBufIndex) << chunkSizePower) + 
curBuf.Position - offset;
-            }
-            catch (NullReferenceException)
-            {
-                throw new 
ObjectDisposedException(this.GetType().GetTypeInfo().FullName, "Already closed: 
" + this);
-            }
+            // LUCENENET: Refactored to avoid calls on invalid conditions 
instead of
+            // catching and re-throwing exceptions in the normal workflow.
+            EnsureOpen();
+            return (((long)curBufIndex) << chunkSizePower) + curBuf.Position - 
offset;
         }
 
         public override sealed void Seek(long pos)
@@ -364,6 +343,16 @@ namespace Lucene.Net.Store
             curBufIndex = 0;
         }
 
+        // LUCENENET specific - rather than using all of this exception 
catching nonsense 
+        // for control flow, we check whether we are disposed first.
+        private void EnsureOpen()
+        {
+            if (buffers == null || curBuf == null)
+            {
+                throw new 
ObjectDisposedException(this.GetType().GetTypeInfo().FullName, "Already closed: 
" + this);
+            }
+        }
+
         protected override void Dispose(bool disposing)
         {
             if (disposing)

Reply via email to