lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [6/6] lucenenet git commit: BUG: Lucene.Net.Tests.Support.TestDataInputStream: Calculation of the array length was done on Windows, but due to line endings the size of the file may be different on other OS's, so changed the test to measure the file lengt
Date Sun, 09 Apr 2017 18:25:53 GMT
BUG: Lucene.Net.Tests.Support.TestDataInputStream: Calculation of the array length was done
on Windows, but due to line endings the size of the file may be different on other OS's, so
changed the test to measure the file length before declaring the array for the tests.


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

Branch: refs/heads/api-work
Commit: 1b50f1dd9f68544a0028b318c63ab490422aeaad
Parents: a850302
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Mon Apr 10 01:18:42 2017 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Mon Apr 10 01:18:42 2017 +0700

----------------------------------------------------------------------
 .../Support/TestDataInputStream.cs              | 29 +++++++++++++++-----
 src/Lucene.Net/Support/DataInputStream.cs       | 26 ++++++++++++++++++
 2 files changed, 48 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1b50f1dd/src/Lucene.Net.Tests/Support/TestDataInputStream.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/Support/TestDataInputStream.cs b/src/Lucene.Net.Tests/Support/TestDataInputStream.cs
index cbb480a..5bc3816 100644
--- a/src/Lucene.Net.Tests/Support/TestDataInputStream.cs
+++ b/src/Lucene.Net.Tests/Support/TestDataInputStream.cs
@@ -14,22 +14,38 @@ namespace Lucene.Net.Support
         public void TestReadFully()
         {
             const string READFULLY_TEST_FILE = "ReadFully.txt";
-            byte[] buffer = new byte[1367];
+            int fileLength;
+            Stream @in;
 
-            Stream @in = GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(),
READFULLY_TEST_FILE);
+            // Read one time to measure the length of the file (it may be different 
+            // on different operating systems because of line endings)
+            using (@in = GetType().getResourceAsStream(READFULLY_TEST_FILE))
+            {
+                using (var ms = new MemoryStream())
+                {
+                    @in.CopyTo(ms);
+                    fileLength = ms.ToArray().Length;
+                }
+            }
+
+            // Declare the buffer one byte too large
+            byte[] buffer = new byte[fileLength + 1];
+            @in = GetType().getResourceAsStream(READFULLY_TEST_FILE);
             DataInputStream dis;
             using (dis = new DataInputStream(@in))
             { 
-                // Read once for real
-                dis.ReadFully(buffer, 0, 1366);
+                // Read once for real (to the exact length)
+                dis.ReadFully(buffer, 0, fileLength);
             }
 
             // Read past the end of the stream
-            @in = GetType().GetTypeInfo().Assembly.FindAndGetManifestResourceStream(GetType(),
READFULLY_TEST_FILE);
+            @in = GetType().getResourceAsStream(READFULLY_TEST_FILE);
             dis = new DataInputStream(@in);
             bool caughtException = false;
             try
             {
+                // Using the buffer length (that is 1 byte too many)
+                // should generate EndOfStreamException.
                 dis.ReadFully(buffer, 0, buffer.Length);
             }
 #pragma warning disable 168
@@ -46,8 +62,7 @@ namespace Lucene.Net.Support
             }
 
             // Ensure we get an IndexOutOfRangeException exception when length is negative
-            @in = GetType().GetTypeInfo().
-                Assembly.FindAndGetManifestResourceStream(GetType(), READFULLY_TEST_FILE);
+            @in = GetType().getResourceAsStream(READFULLY_TEST_FILE);
             dis = new DataInputStream(@in);
             caughtException = false;
             try

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/1b50f1dd/src/Lucene.Net/Support/DataInputStream.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Support/DataInputStream.cs b/src/Lucene.Net/Support/DataInputStream.cs
index 8265735..c8d3894 100644
--- a/src/Lucene.Net/Support/DataInputStream.cs
+++ b/src/Lucene.Net/Support/DataInputStream.cs
@@ -50,13 +50,17 @@ namespace Lucene.Net.Support
         public void ReadFully(byte[] b, int off, int len)
         {
             if (len < 0)
+            {
                 throw new IndexOutOfRangeException();
+            }
             int n = 0;
             while (n < len)
             {
                 int count = @in.Read(b, off + n, len - n);
                 if (count == 0)
+                {
                     throw new EndOfStreamException();
+                }
                 n += count;
             }
         }
@@ -92,7 +96,9 @@ namespace Lucene.Net.Support
         {
             int ch = @in.ReadByte();
             if (ch < 0)
+            {
                 throw new EndOfStreamException();
+            }
             return (ch != 0);
         }
 
@@ -103,7 +109,9 @@ namespace Lucene.Net.Support
         {
             int ch = @in.ReadByte();
             if (ch < 0)
+            {
                 throw new EndOfStreamException();
+            }
             return ch;
         }
 
@@ -114,7 +122,9 @@ namespace Lucene.Net.Support
         {
             int ch = @in.ReadByte();
             if (ch < 0)
+            {
                 throw new EndOfStreamException();
+            }
             return (byte)ch;
         }
 
@@ -126,7 +136,9 @@ namespace Lucene.Net.Support
             int ch1 = @in.ReadByte();
             int ch2 = @in.ReadByte();
             if ((ch1 | ch2) < 0)
+            {
                 throw new EndOfStreamException();
+            }
             return (short)((ch1 << 8) + (ch2 << 0));
         }
 
@@ -138,7 +150,9 @@ namespace Lucene.Net.Support
             int ch1 = @in.ReadByte();
             int ch2 = @in.ReadByte();
             if ((ch1 | ch2) < 0)
+            {
                 throw new EndOfStreamException();
+            }
             return (ch1 << 8) + (ch2 << 0);
         }
 
@@ -147,7 +161,9 @@ namespace Lucene.Net.Support
             int ch1 = @in.ReadByte();
             int ch2 = @in.ReadByte();
             if ((ch1 | ch2) < 0)
+            {
                 throw new EndOfStreamException();
+            }
             return (char)((ch1 << 8) + (ch2 << 0));
         }
 
@@ -161,7 +177,9 @@ namespace Lucene.Net.Support
             int ch3 = @in.ReadByte();
             int ch4 = @in.ReadByte();
             if ((ch1 | ch2 | ch3 | ch4) < 0)
+            {
                 throw new EndOfStreamException();
+            }
             return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 <<
0));
         }
 
@@ -320,12 +338,16 @@ namespace Lucene.Net.Support
                         /* 110x xxxx   10xx xxxx*/
                         count += 2;
                         if (count > utflen)
+                        {
                             throw new FormatException(
                                 "malformed input: partial character at end");
+                        }
                         char2 = (int)bytearr[count - 1];
                         if ((char2 & 0xC0) != 0x80)
+                        {
                             throw new FormatException(
                                 "malformed input around byte " + count);
+                        }
                         chararr[chararr_count++] = (char)(((c & 0x1F) << 6) |
                                                         (char2 & 0x3F));
                         break;
@@ -333,13 +355,17 @@ namespace Lucene.Net.Support
                         /* 1110 xxxx  10xx xxxx  10xx xxxx */
                         count += 3;
                         if (count > utflen)
+                        {
                             throw new FormatException(
                                 "malformed input: partial character at end");
+                        }
                         char2 = (int)bytearr[count - 2];
                         char3 = (int)bytearr[count - 1];
                         if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
+                        {
                             throw new FormatException(
                                 "malformed input around byte " + (count - 1));
+                        }
                         chararr[chararr_count++] = (char)(((c & 0x0F) << 12) |
                                                         ((char2 & 0x3F) << 6) |
                                                         ((char3 & 0x3F) << 0));


Mime
View raw message