Java's DataInputStream is similar to .NET's BinaryReader. However, it reads using a modified UTF-8 format that cannot be read using BinaryReader. This is a port of DataInputStream that is fully compatible with Java's DataOutputStream.

Usage Note: Always favor BinaryReader over DataInputStream unless you specifically need the modified UTF-8 format and/or the ReadUTF(IDataInput) method.

Inheritance: IDataInput, IDisposable
コード例 #1
0
        public void TestReadFully()
        {
            const string READFULLY_TEST_FILE = "Lucene.Net.Tests.core.Support.ReadFully.txt";
            byte[] buffer = new byte[1367];

            Stream @in = GetType().Assembly.GetManifestResourceStream(READFULLY_TEST_FILE);
            DataInputStream dis;
            using (dis = new DataInputStream(@in))
            {
                // Read once for real
                dis.ReadFully(buffer, 0, 1366);
            }

            // Read past the end of the stream
            @in = GetType().Assembly.GetManifestResourceStream(READFULLY_TEST_FILE);
            dis = new DataInputStream(@in);
            bool caughtException = false;
            try
            {
                dis.ReadFully(buffer, 0, buffer.Length);
            }
            #pragma warning disable 168
            catch (EndOfStreamException ie)
            #pragma warning restore 168
            {
                caughtException = true;
            }
            finally
            {
                dis.Dispose();
                if (!caughtException)
                    fail("Test failed");
            }

            // Ensure we get an IndexOutOfRangeException exception when length is negative
            @in = GetType().Assembly.GetManifestResourceStream(READFULLY_TEST_FILE);
            dis = new DataInputStream(@in);
            caughtException = false;
            try
            {
                dis.ReadFully(buffer, 0, -20);
            }
            #pragma warning disable 168
            catch (IndexOutOfRangeException ie)
            #pragma warning restore 168
            {
                caughtException = true;
            }
            finally
            {
                dis.Dispose();
                if (!caughtException)
                    fail("Test failed");
            }
        }
コード例 #2
0
 /// <summary>
 /// Load a stemmer table from an inputstream.
 /// </summary>
 public static Trie Load(Stream stemmerTable)
 {
     DataInputStream @in = null;
     try
     {
         @in = new DataInputStream(stemmerTable);
         string method = @in.ReadUTF().ToUpperInvariant();
         if (method.IndexOf('M') < 0)
         {
             return new Trie(@in);
         }
         else
         {
             return new MultiTrie2(@in);
         }
     }
     finally
     {
         @in.Dispose();
     }
 }
コード例 #3
0
        public void TestReadLinePushback()
        {
            using (MemoryStream pis = new MemoryStream("\r".GetBytes(Encoding.UTF8)))
            {
                DataInputStream dis = new DataInputStream(pis);

            #pragma warning disable 612, 618
                string line = dis.ReadLine();
            #pragma warning restore 612, 618
                if (line == null)
                {
                    fail("Got null, should return empty line");
                }

                long count = pis.Length - (line.Length + 1 /*account for the newline*/);

                if (count != 0)
                {
                    fail("Test failed: available() returns "
                                         + count + " when the file is empty");
                }
            }
        }
コード例 #4
0
        private static void WriteAndReadAString()
        {
            // Write out a string whose UTF-8 encoding is quite possibly
            // longer than 65535 bytes
            int length = Random().nextInt(A_NUMBER_NEAR_65535) + 1;
            MemoryStream baos = new MemoryStream();
            StringBuilder testBuffer = new StringBuilder();
            for (int i = 0; i < length; i++)
                testBuffer.append((char)Random().Next());
            string testString = testBuffer.toString();
            DataOutputStream dos = new DataOutputStream(baos);
            dos.WriteUTF(testString);

            // Corrupt the data to produce malformed characters
            byte[] testBytes = baos.ToArray();
            int dataLength = testBytes.Length;
            int corruptions = Random().nextInt(MAX_CORRUPTIONS_PER_CYCLE);
            for (int i = 0; i < corruptions; i++)
            {
                int index = Random().nextInt(dataLength);
                testBytes[index] = (byte)Random().Next();
            }

            // Pay special attention to mangling the end to produce
            // partial characters at end
            testBytes[dataLength - 1] = (byte)Random().Next();
            testBytes[dataLength - 2] = (byte)Random().Next();

            // Attempt to decode the bytes back into a String
            MemoryStream bais = new MemoryStream(testBytes);
            DataInputStream dis = new DataInputStream(bais);
            dis.ReadUTF();
        }
コード例 #5
0
 private static void dotest(DataInputStream dis, int pos, int total,
     int toskip, int expected)
 {
     try
     {
         if (VERBOSE)
         {
             Console.WriteLine("\n\nTotal bytes in the stream = " + total);
             Console.WriteLine("Currently at position = " + pos);
             Console.WriteLine("Bytes to skip = " + toskip);
             Console.WriteLine("Expected result = " + expected);
         }
         int skipped = dis.SkipBytes(toskip);
         if (VERBOSE)
         {
             Console.WriteLine("Actual skipped = " + skipped);
         }
         if (skipped != expected)
         {
             fail("DataInputStream.skipBytes does not return expected value");
         }
     }
     catch (EndOfStreamException e)
     {
         fail("DataInputStream.skipBytes throws unexpected EOFException");
     }
     catch (IOException e)
     {
         Console.WriteLine("IOException is thrown - possible result");
     }
 }
コード例 #6
0
 public void TestSkipBytes()
 {
     DataInputStream dis = new DataInputStream(new MyInputStream());
     dotest(dis, 0, 11, -1, 0);
     dotest(dis, 0, 11, 5, 5);
     Console.WriteLine("\n***CAUTION**** - may go into an infinite loop");
     dotest(dis, 5, 11, 20, 6);
 }
コード例 #7
0
 internal static Trie LoadTrie(string path)
 {
     Trie trie;
     using (DataInputStream @is = new DataInputStream(
         new FileStream(path, FileMode.Open, FileAccess.Read)))
     {
         string method = @is.ReadUTF().ToUpperInvariant();
         if (method.IndexOf('M') < 0)
         {
             trie = new Trie(@is);
         }
         else
         {
             trie = new MultiTrie(@is);
         }
     }
     return trie;
 }
コード例 #8
0
        public void TestReadFully()
        {
            const string READFULLY_TEST_FILE = "Lucene.Net.Tests.core.Support.ReadFully.txt";

            byte[] buffer = new byte[1367];

            Stream          @in = GetType().Assembly.GetManifestResourceStream(READFULLY_TEST_FILE);
            DataInputStream dis;

            using (dis = new DataInputStream(@in))
            {
                // Read once for real
                dis.ReadFully(buffer, 0, 1366);
            }

            // Read past the end of the stream
            @in = GetType().Assembly.GetManifestResourceStream(READFULLY_TEST_FILE);
            dis = new DataInputStream(@in);
            bool caughtException = false;

            try
            {
                dis.ReadFully(buffer, 0, buffer.Length);
            }
#pragma warning disable 168
            catch (EndOfStreamException ie)
#pragma warning restore 168
            {
                caughtException = true;
            }
            finally
            {
                dis.Dispose();
                if (!caughtException)
                {
                    fail("Test failed");
                }
            }

            // Ensure we get an IndexOutOfRangeException exception when length is negative
            @in             = GetType().Assembly.GetManifestResourceStream(READFULLY_TEST_FILE);
            dis             = new DataInputStream(@in);
            caughtException = false;
            try
            {
                dis.ReadFully(buffer, 0, -20);
            }
#pragma warning disable 168
            catch (IndexOutOfRangeException ie)
#pragma warning restore 168
            {
                caughtException = true;
            }
            finally
            {
                dis.Dispose();
                if (!caughtException)
                {
                    fail("Test failed");
                }
            }
        }