Read() public method

Reads a single character.
If an I/O error occurs
public Read ( ) : int
return int
Esempio n. 1
0
        /**
         * @tests java.io.BufferedReader#reset()
         */
        public void Test_Reset()
        {
            // Test for method void java.io.BufferedReader.reset()
            try
            {
                br = new BufferedCharFilter(new StringReader(testString));
                br.Skip(500);
                br.Mark(900);
                br.Skip(500);
                br.Reset();
                char[] buf = new char[testString.Length];
                br.Read(buf, 0, 500);
                assertTrue("Failed to reset properly", testString.Substring(500,
                                                                            1000 - 500).Equals(new string(buf, 0, 500), StringComparison.Ordinal));
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                fail("Exception during reset test");
            }
            try
            {
                br = new BufferedCharFilter(new StringReader(testString));
                br.Skip(500);
                br.Reset();
                fail("Reset succeeded on unmarked stream");
            }
#pragma warning disable 168
            catch (IOException x)
#pragma warning restore 168
            {
                return;
            }
        }
Esempio n. 2
0
        public void Test_Read()
        {
            // Test for method int java.io.BufferedReader.read()
            try
            {
                br = new BufferedCharFilter(new StringReader(testString));
                int r = br.Read();
                assertTrue("Char read improperly", testString[0] == r);
                br = new BufferedCharFilter(new StringReader(new String(
                                                                 new char[] { '\u8765' })));
                assertTrue("Wrong double byte character", br.Read() == '\u8765');
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                fail("Exception during read test");
            }

            char[] chars = new char[256];
            for (int i = 0; i < 256; i++)
            {
                chars[i] = (char)i;
            }
            BufferedCharFilter @in = new BufferedCharFilter(new StringReader(new String(
                                                                                 chars)), 12);
            try
            {
                assertEquals("Wrong initial char", 0, @in.Read()); // Fill the
                                                                   // buffer
                char[] buf = new char[14];
                @in.Read(buf, 0, 14);                              // Read greater than the buffer

                assertTrue("Wrong block read data", new string(buf)
                           .Equals(new string(chars, 1, 14), StringComparison.Ordinal));

                assertEquals("Wrong chars", 15, @in.Read()); // Check next byte
            }
            catch (IOException e)
            {
                fail("Exception during read test 2:" + e);
            }

            // regression test for HARMONY-841
            assertTrue(new BufferedCharFilter(new StringReader(new string(new char[5], 1, 0)), 2).Read() == -1);
        }
Esempio n. 3
0
        public void Test_Reset_IOException()
        {
            int[]
            expected = new int[] { '1', '2', '3', '4', '5', '6', '7', '8',
                                   '9', '0', -1 };
            br = new BufferedCharFilter(new StringReader("1234567890"), 9);
            br.Mark(9);
            for (int i = 0; i < 11; i++)
            {
                assertEquals(expected[i], br.Read());
            }
            try
            {
                br.Reset();
                fail("should throw IOException");
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                // Expected
            }
            for (int i = 0; i < 11; i++)
            {
                assertEquals(-1, br.Read());
            }

            br = new BufferedCharFilter(new StringReader("1234567890"));
            br.Mark(10);
            for (int i = 0; i < 10; i++)
            {
                assertEquals(expected[i], br.Read());
            }
            br.Reset();
            for (int i = 0; i < 11; i++)
            {
                assertEquals(expected[i], br.Read());
            }
        }
Esempio n. 4
0
 public void Test_Close()
 {
     // Test for method void java.io.BufferedReader.close()
     try
     {
         br = new BufferedCharFilter(new StringReader(testString));
         br.Dispose();
         br.Read();
         fail("Read on closed stream");
     }
     catch (Exception x) when(x.IsIOException())
     {
         return;
     }
 }
Esempio n. 5
0
        public void Test_Close()
        {
            // Test for method void java.io.BufferedReader.close()
            try
            {
                br = new BufferedCharFilter(new StringReader(testString));
                br.Dispose();
                br.Read();
                fail("Read on closed stream");
            }
#pragma warning disable 168
            catch (IOException x)
#pragma warning restore 168
            {
                return;
            }
        }
Esempio n. 6
0
 public void Test_SkipJ()
 {
     // Test for method long java.io.BufferedReader.skip(long)
     try
     {
         br = new BufferedCharFilter(new StringReader(testString));
         br.Skip(500);
         char[] buf = new char[testString.Length];
         br.Read(buf, 0, 500);
         assertTrue("Failed to set skip properly", testString.Substring(500,
                                                                        1000 - 500).Equals(new string(buf, 0, 500), StringComparison.Ordinal));
     }
     catch (Exception e) when(e.IsIOException())
     {
         fail("Exception during skip test");
     }
 }
        public void Test_SkipJ()
        {
            // Test for method long java.io.BufferedReader.skip(long)
            try
            {
                br = new BufferedCharFilter(new StringReader(testString));
                br.Skip(500);
                char[] buf = new char[testString.Length];
                br.Read(buf, 0, 500);
                assertTrue("Failed to set skip properly", testString.Substring(500,
                                                                               1000 - 500).equals(new String(buf, 0, 500)));
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                fail("Exception during skip test");
            }
        }
Esempio n. 8
0
        public void Test_Read_CII_Exception()
        {
            br = new BufferedCharFilter(new StringReader(testString));
            char[] nullCharArray = null;
            char[] charArray     = testString.toCharArray();

            try
            {
                br.Read(nullCharArray, -1, -1);

                fail("should throw IndexOutOfBoundsException");
            }
#pragma warning disable 168
            catch (ArgumentOutOfRangeException e)
#pragma warning restore 168
            {
                // expected
            }

            try
            {
                br.Read(nullCharArray, -1, 0);

                fail("should throw IndexOutOfBoundsException");
            }
#pragma warning disable 168
            catch (ArgumentOutOfRangeException e)
#pragma warning restore 168
            {
                // expected
            }

            try
            {
                br.Read(nullCharArray, 0, -1);

                fail("should throw NullPointerException");
            }
#pragma warning disable 168
            catch (NullReferenceException e)
#pragma warning restore 168
            {
                // expected
            }

            try
            {
                br.Read(nullCharArray, 0, 0);

                fail("should throw NullPointerException");
            }
#pragma warning disable 168
            catch (NullReferenceException e)
#pragma warning restore 168
            {
                // expected
            }

            try
            {
                br.Read(nullCharArray, 0, 1);

                fail("should throw NullPointerException");
            }
#pragma warning disable 168
            catch (NullReferenceException e)
#pragma warning restore 168
            {
                // expected
            }

            try
            {
                br.Read(charArray, -1, -1);

                fail("should throw IndexOutOfBoundsException");
            }
#pragma warning disable 168
            catch (ArgumentOutOfRangeException e)
#pragma warning restore 168
            {
                // expected
            }

            try
            {
                br.Read(charArray, -1, 0);

                fail("should throw IndexOutOfBoundsException");
            }
#pragma warning disable 168
            catch (ArgumentOutOfRangeException e)
#pragma warning restore 168
            {
                // expected
            }

            br.Read(charArray, 0, 0);
            br.Read(charArray, 0, charArray.Length);
            br.Read(charArray, charArray.Length, 0);

            try
            {
                br.Read(charArray, charArray.Length + 1, 0);

                fail("should throw IndexOutOfBoundsException");
            }
#pragma warning disable 168
            catch (ArgumentOutOfRangeException e)
#pragma warning restore 168
            {
                //expected
            }

            try
            {
                br.Read(charArray, charArray.Length + 1, 1);

                fail("should throw IndexOutOfBoundsException");
            }
#pragma warning disable 168
            catch (ArgumentOutOfRangeException e)
#pragma warning restore 168
            {
                //expected
            }

            br.Dispose();

            try
            {
                br.Read(nullCharArray, -1, -1);

                fail("should throw IOException");
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                // expected
            }

            try
            {
                br.Read(charArray, -1, 0);

                fail("should throw IOException");
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                // expected
            }

            try
            {
                br.Read(charArray, 0, -1);

                fail("should throw IOException");
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                // expected
            }
        }
Esempio n. 9
0
        public void Test_ReadCII()
        {
            char[]             ca    = new char[2];
            BufferedCharFilter toRet = new BufferedCharFilter(new StreamReader(
                                                                  new MemoryStream(new byte[0])));

            /* Null buffer should throw NPE even when len == 0 */
            try
            {
                toRet.Read(null, 1, 0);

                fail("null buffer reading zero bytes should throw NPE");
            }
#pragma warning disable 168
            catch (NullReferenceException e)
#pragma warning restore 168
            {
                //expected
            }

            try
            {
                toRet.Dispose();
            }
            catch (IOException e)
            {
                fail("unexpected 1: " + e);
            }

            try
            {
                toRet.Read(null, 1, 0);

                fail("null buffer reading zero bytes on closed stream should throw IOException");
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                //expected
            }

            /* Closed reader should throw IOException reading zero bytes */
            try
            {
                toRet.Read(ca, 0, 0);

                fail("Reading zero bytes on a closed reader should not work");
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                // expected
            }

            /*
             * Closed reader should throw IOException in preference to index out of
             * bounds
             */
            try
            {
                // Read should throw IOException before
                // ArrayIndexOutOfBoundException
                toRet.Read(ca, 1, 5);

                fail("IOException should have been thrown");
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                // expected
            }

            // Test to ensure that a drained stream returns 0 at EOF
            toRet = new BufferedCharFilter(new StreamReader(

                                               new MemoryStream(new byte[2])));
            try
            {
                assertEquals("Emptying the reader should return two bytes", 2,
                             toRet.Read(ca, 0, 2));

                // LUCENENET specific: end of stream should be 0 in .NET
                assertEquals("EOF on a reader should be 0", 0, toRet.Read(ca, 0,
                                                                          2));
                //assertEquals("EOF on a reader should be -1", -1, toRet.Read(ca, 0,
                //        2));

                assertEquals("Reading zero bytes at EOF should work", 0, toRet
                             .Read(ca, 0, 0));
            }
            catch (IOException ex)
            {
                fail("Unexpected IOException : " + ex.ToString());
            }

            // Test for method int java.io.BufferedReader.read(char [], int, int)
            try
            {
                char[] buf = new char[testString.Length];
                br = new BufferedCharFilter(new StringReader(testString));
                br.Read(buf, 50, 500);

                assertTrue("Chars read improperly", new string(buf, 50, 500)
                           .Equals(testString.Substring(0, 500 - 0), StringComparison.Ordinal));
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                fail("Exception during read test");
            }

            BufferedCharFilter bufin = new BufferedCharFilter(new ReaderAnonymousInnerClassHelper());

            //BufferedCharFilter bufin = new BufferedCharFilter(new Reader() {
            //            int size = 2, pos = 0;

            //char[] contents = new char[size];

            //public int read()
            //{
            //				if (pos >= size)
            //					throw new IOException("Read past end of data");
            //				return contents[pos++];
            //			}

            //			public int read(char[] buf, int off, int len) throws IOException
            //{
            //				if (pos >= size)
            //					throw new IOException("Read past end of data");
            //int toRead = len;
            //				if (toRead > (size - pos))
            //					toRead = size - pos;
            //				System.arraycopy(contents, pos, buf, off, toRead);
            //				pos += toRead;
            //				return toRead;
            //			}

            //			public boolean ready() throws IOException
            //{
            //				return size - pos > 0;
            //}

            //public void close()
            //{
            //}
            //		});
            try
            {
                bufin.Read();
                int result = bufin.Read(new char[2], 0, 2);

                assertTrue("Incorrect result: " + result, result == 1);
            }
            catch (IOException e)
            {
                fail("Unexpected: " + e);
            }

            //regression for HARMONY-831
            try
            {
                new BufferedCharFilter(new StringReader(""), 9).Read(new char[] { }, 7, 0);
                fail("should throw IndexOutOfBoundsException");
            }
#pragma warning disable 168
            catch (ArgumentOutOfRangeException e)
#pragma warning restore 168
            {
            }

            // Regression for HARMONY-54
            char[]             ch     = { };
            BufferedCharFilter reader = new BufferedCharFilter(new StringReader(new string(ch)));
            try
            {
                // Check exception thrown when the reader is open.
                reader.Read(null, 1, 0);
                fail("Assert 0: NullPointerException expected");
            }
#pragma warning disable 168
            catch (NullReferenceException e)
#pragma warning restore 168
            {
                // Expected
            }

            // Now check IOException is thrown in preference to
            // NullPointerexception when the reader is closed.
            reader.Dispose();
            try
            {
                reader.Read(null, 1, 0);
                fail("Assert 1: IOException expected");
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                // Expected
            }

            try
            {
                // And check that the IOException is thrown before
                // ArrayIndexOutOfBoundException
                reader.Read(ch, 0, 42);
                fail("Assert 2: IOException expected");
            }
#pragma warning disable 168
            catch (IOException e)
#pragma warning restore 168
            {
                // expected
            }
        }
Esempio n. 10
0
        public void Test_MarkI()
        {
            // Test for method void java.io.BufferedReader.mark(int)
            char[] buf = null;
            br = new BufferedCharFilter(new StringReader(testString));
            br.Skip(500);
            br.Mark(1000);
            br.Skip(250);
            br.Reset();
            buf = new char[testString.Length];
            br.Read(buf, 0, 500);

            assertTrue("Failed to set mark properly", testString.Substring(500,
                                                                           1000 - 500).Equals(new string(buf, 0, 500), StringComparison.Ordinal));

            try
            {
                br = new BufferedCharFilter(new StringReader(testString), 800);
                br.Skip(500);
                br.Mark(250);
                br.Read(buf, 0, 1000);
                br.Reset();

                fail("Failed to invalidate mark properly");
            }
#pragma warning disable 168
            catch (IOException x)
#pragma warning restore 168
            {
                // Expected
            }

            char[] chars = new char[256];
            for (int i = 0; i < 256; i++)
            {
                chars[i] = (char)i;
            }
            BufferedCharFilter @in = new BufferedCharFilter(new StringReader(new String(
                                                                                 chars)), 12);

            @in.Skip(6);
            @in.Mark(14);
            @in.Read(new char[14], 0, 14);
            @in.Reset();

            assertTrue("Wrong chars", @in.Read() == (char)6 &&
                       @in.Read() == (char)7);

            @in = new BufferedCharFilter(new StringReader(new String(chars)), 12);
            @in.Skip(6);
            @in.Mark(8);
            @in.Skip(7);
            @in.Reset();

            assertTrue("Wrong chars 2", @in.Read() == (char)6 &&
                       @in.Read() == (char)7);

            BufferedCharFilter br2 = new BufferedCharFilter(new StringReader("01234"), 2);
            br2.Mark(3);
            char[] carray = new char[3];
            int    result = br2.read(carray);
            assertEquals(3, result);
            assertEquals("Assert 0:", '0', carray[0]);
            assertEquals("Assert 1:", '1', carray[1]);
            assertEquals("Assert 2:", '2', carray[2]);
            assertEquals("Assert 3:", '3', br2.Read());

            br2 = new BufferedCharFilter(new StringReader("01234"), 2);
            br2.Mark(3);
            carray = new char[4];
            result = br2.read(carray);
            assertEquals("Assert 4:", 4, result);
            assertEquals("Assert 5:", '0', carray[0]);
            assertEquals("Assert 6:", '1', carray[1]);
            assertEquals("Assert 7:", '2', carray[2]);
            assertEquals("Assert 8:", '3', carray[3]);
            assertEquals("Assert 9:", '4', br2.Read());
            assertEquals("Assert 10:", -1, br2.Read());

            BufferedCharFilter reader = new BufferedCharFilter(new StringReader("01234"));
            reader.Mark(int.MaxValue);
            reader.Read();
            reader.Dispose();
        }