Ejemplo n.º 1
0
        /// <summary>
        /// Extracts the first line, i.e characters until the first newline or end of stream.
        /// Reads one character at a time to be sure not to read too far ahead. The stream is left
        /// in a state of either exhausted or at the beginning of the next line of data.
        /// </summary>
        /// <param name="source"> <seealso cref="CharReadable"/> to read from. </param>
        /// <returns> char[] containing characters until the first newline character or end of stream. </returns>
        /// <exception cref="IOException"> on I/O reading error. </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: public static char[] extractFirstLineFrom(CharReadable source) throws java.io.IOException
        public static char[] ExtractFirstLineFrom(CharReadable source)
        {
            char[] result = new char[100];
            int    cursor = 0;
            int    read;
            bool   foundEol = false;

            do
            {
                // Grow on demand
                if (cursor >= result.Length)
                {
                    result = Arrays.copyOf(result, cursor * 2);
                }

                // Read one character
                read = source.Read(result, cursor, 1);
                if (read > 0)
                {
                    foundEol = BufferedCharSeeker.IsEolChar(result[cursor]);
                    if (!foundEol)
                    {
                        cursor++;
                    }
                }
            } while (read > 0 && !foundEol);
            return(Arrays.copyOf(result, cursor));
        }
Ejemplo n.º 2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void shouldReadAhead() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void ShouldReadAhead()
        {
            // GIVEN
            TrackingReader      actual      = new TrackingReader(23);
            int                 bufferSize  = 5;
            CharReadable        aheadReader = ThreadAheadReadable.ThreadAhead(actual, bufferSize);
            SectionedCharBuffer buffer      = new SectionedCharBuffer(bufferSize);

            // WHEN starting it up it should read and fill the buffer to the brim
            assertEquals(bufferSize, actual.AwaitCompletedReadAttempts(1));

            // WHEN we read one buffer
            int read = 0;

            buffer = aheadReader.Read(buffer, buffer.Front());
            AssertBuffer(Chars(read, bufferSize), buffer, 0, bufferSize);
            read += buffer.Available();

            // and simulate reading all characters, i.e. back section will be empty in the new buffer
            buffer = aheadReader.Read(buffer, buffer.Front());
            AssertBuffer(Chars(read, bufferSize), buffer, 0, bufferSize);
            read += buffer.Available();

            // then simulate reading some characters, i.e. back section will contain some characters
            int keep = 2;

            buffer = aheadReader.Read(buffer, buffer.Front() - keep);
            AssertBuffer(Chars(read - keep, bufferSize + keep), buffer, keep, bufferSize);
            read += buffer.Available();

            keep   = 3;
            buffer = aheadReader.Read(buffer, buffer.Front() - keep);
            AssertBuffer(Chars(read - keep, bufferSize + keep), buffer, keep, bufferSize);
            read += buffer.Available();

            keep   = 1;
            buffer = aheadReader.Read(buffer, buffer.Front() - keep);
            assertEquals(3, buffer.Available());
            AssertBuffer(Chars(read - keep, buffer.Available() + keep), buffer, keep, 3);
            read += buffer.Available();
            assertEquals(23, read);
        }
Ejemplo n.º 3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldExtractOnlyLine() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldExtractOnlyLine()
        {
            // given
            string       firstLine = "characters of only line";
            CharReadable reader    = Readables.Wrap(firstLine);

            // when
            char[] firstLineCharacters = Readables.ExtractFirstLineFrom(reader);
            int    readAfterwards      = reader.Read(new char[1], 0, 1);

            // then
            assertArrayEquals(firstLine.ToCharArray(), firstLineCharacters);
            assertEquals(-1, readAfterwards);
        }
Ejemplo n.º 4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldExtractFirstLine() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldExtractFirstLine()
        {
            // given
            string       firstLine  = "characters of first line";
            string       secondLine = "here on the second line";
            CharReadable reader     = Readables.Wrap(firstLine + "\n" + secondLine);

            // when
            char[] firstLineCharacters  = Readables.ExtractFirstLineFrom(reader);
            char[] secondLineCharacters = new char[secondLine.Length];
            reader.Read(secondLineCharacters, 0, secondLineCharacters.Length);

            // then
            assertArrayEquals(firstLine.ToCharArray(), firstLineCharacters);
            assertArrayEquals(secondLine.ToCharArray(), secondLineCharacters);
        }
Ejemplo n.º 5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test void shouldHandleReadAheadEmptyData() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        internal virtual void ShouldHandleReadAheadEmptyData()
        {
            // GIVEN
            TrackingReader actual        = new TrackingReader(0);
            int            bufferSize    = 10;
            CharReadable   aheadReadable = ThreadAheadReadable.ThreadAhead(actual, bufferSize);

            // WHEN
            actual.AwaitCompletedReadAttempts(1);

            // THEN
            SectionedCharBuffer buffer = new SectionedCharBuffer(bufferSize);

            buffer = aheadReadable.Read(buffer, buffer.Front());
            assertEquals(buffer.Pivot(), buffer.Back());
            assertEquals(buffer.Pivot(), buffer.Front());
        }
Ejemplo n.º 6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void shouldComplyWithSpecifiedCharset(java.nio.charset.Charset charset) throws Exception
        private void ShouldComplyWithSpecifiedCharset(Charset charset)
        {
            // GIVEN
            string data = "abcåäö[]{}";
            File   file = WriteToFile(data, charset);

            // WHEN
            CharReadable        reader = Readables.Files(charset, file);
            SectionedCharBuffer buffer = new SectionedCharBuffer(100);

            buffer = reader.Read(buffer, buffer.Front());

            // THEN
            char[] expected = data.ToCharArray();
            char[] array    = buffer.Array();
            assertEquals(expected.Length, buffer.Available());
            for (int i = 0; i < expected.Length; i++)
            {
                assertEquals(expected[i], array[buffer.Pivot() + i]);
            }
        }
Ejemplo n.º 7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldTrackPosition() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldTrackPosition()
        {
            // GIVEN
            string data = "1234567890";
            //                 ^   ^
            CharReadable        reader = Readables.Wrap(data);
            SectionedCharBuffer buffer = new SectionedCharBuffer(4);

            // WHEN
            int expected = 0;

            do
            {
                buffer    = reader.Read(buffer, buffer.Front());
                expected += buffer.Available();

                // THEN
                assertEquals(expected, reader.Position());
            } while (buffer.HasAvailable());

            // and THEN
            assertEquals(data.ToCharArray().length, expected);
        }