Search in sources :

Example 51 with CharArrayReader

use of java.io.CharArrayReader in project robovm by robovm.

the class BufferedReaderTest method test_read$CII.

/**
	 * @tests java.io.BufferedReader#read(char[], int, int)
	 */
public void test_read$CII() throws Exception {
    char[] ca = new char[2];
    BufferedReader toRet = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(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");
    } catch (NullPointerException e) {
    //expected
    }
    try {
        toRet.close();
    } 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");
    } catch (IOException e) {
    //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");
    } catch (IOException e) {
    // 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");
    } catch (IOException e) {
    // expected
    }
    // Test to ensure that a drained stream returns 0 at EOF
    toRet = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(new byte[2])));
    try {
        assertEquals("Emptying the reader should return two bytes", 2, 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.getLocalizedMessage());
    }
    // Test for method int java.io.BufferedReader.read(char [], int, int)
    try {
        char[] buf = new char[testString.length()];
        br = new BufferedReader(new Support_StringReader(testString));
        br.read(buf, 50, 500);
        assertTrue("Chars read improperly", new String(buf, 50, 500).equals(testString.substring(0, 500)));
    } catch (java.io.IOException e) {
        fail("Exception during read test");
    }
    BufferedReader bufin = new BufferedReader(new Reader() {

        int size = 2, pos = 0;

        char[] contents = new char[size];

        public int read() throws IOException {
            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() throws IOException {
        }
    });
    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 BufferedReader(new PipedReader(), 9).read(new char[] {}, 7, 0);
        fail("should throw IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException e) {
    }
    // Regression for HARMONY-54
    char[] ch = {};
    BufferedReader reader = new BufferedReader(new CharArrayReader(ch));
    try {
        // Check exception thrown when the reader is open.
        reader.read(null, 1, 0);
        fail("Assert 0: NullPointerException expected");
    } catch (NullPointerException e) {
    // Expected
    }
    // Now check IOException is thrown in preference to
    // NullPointerexception when the reader is closed.
    reader.close();
    try {
        reader.read(null, 1, 0);
        fail("Assert 1: IOException expected");
    } catch (IOException e) {
    // Expected
    }
    try {
        // And check that the IOException is thrown before
        // ArrayIndexOutOfBoundException
        reader.read(ch, 0, 42);
        fail("Assert 2: IOException expected");
    } catch (IOException e) {
    // expected
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) PipedReader(java.io.PipedReader) Support_StringReader(tests.support.Support_StringReader) CharArrayReader(java.io.CharArrayReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) IOException(java.io.IOException) PipedReader(java.io.PipedReader) CharArrayReader(java.io.CharArrayReader) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) Support_StringReader(tests.support.Support_StringReader)

Example 52 with CharArrayReader

use of java.io.CharArrayReader in project robovm by robovm.

the class BufferedReaderTest method test_read.

/**
	 * @tests java.io.BufferedReader#read()
	 */
public void test_read() throws IOException {
    // Test for method int java.io.BufferedReader.read()
    try {
        br = new BufferedReader(new Support_StringReader(testString));
        int r = br.read();
        assertTrue("Char read improperly", testString.charAt(0) == r);
        br = new BufferedReader(new Support_StringReader(new String(new char[] { '蝥' })));
        assertTrue("Wrong double byte character", br.read() == '蝥');
    } catch (java.io.IOException e) {
        fail("Exception during read test");
    }
    char[] chars = new char[256];
    for (int i = 0; i < 256; i++) chars[i] = (char) i;
    Reader in = new BufferedReader(new Support_StringReader(new String(chars)), 12);
    try {
        // Fill the
        assertEquals("Wrong initial char", 0, in.read());
        // buffer
        char[] buf = new char[14];
        // Read greater than the buffer
        in.read(buf, 0, 14);
        assertTrue("Wrong block read data", new String(buf).equals(new String(chars, 1, 14)));
        // Check next byte
        assertEquals("Wrong chars", 15, in.read());
    } catch (IOException e) {
        fail("Exception during read test 2:" + e);
    }
    // regression test for HARMONY-841
    assertTrue(new BufferedReader(new CharArrayReader(new char[5], 1, 0), 2).read() == -1);
}
Also used : CharArrayReader(java.io.CharArrayReader) BufferedReader(java.io.BufferedReader) PipedReader(java.io.PipedReader) Support_StringReader(tests.support.Support_StringReader) CharArrayReader(java.io.CharArrayReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) IOException(java.io.IOException) Support_StringReader(tests.support.Support_StringReader)

Example 53 with CharArrayReader

use of java.io.CharArrayReader in project robovm by robovm.

the class OldBufferedReaderTest method test_read.

public void test_read() throws IOException {
    Support_ASimpleReader ssr = new Support_ASimpleReader(true);
    try {
        br = new BufferedReader(new Support_StringReader(testString));
        int r = br.read();
        assertTrue("Char read improperly", testString.charAt(0) == r);
        br = new BufferedReader(new Support_StringReader(new String(new char[] { '蝥' })));
        assertTrue("Wrong double byte character", br.read() == '蝥');
    } catch (java.io.IOException e) {
        fail("Exception during read test");
    }
    char[] chars = new char[256];
    for (int i = 0; i < 256; i++) chars[i] = (char) i;
    Reader in = new BufferedReader(new Support_StringReader(new String(chars)), 12);
    try {
        // Fill the
        assertEquals("Wrong initial char", 0, in.read());
        // buffer
        char[] buf = new char[14];
        // Read greater than the buffer
        in.read(buf, 0, 14);
        assertTrue("Wrong block read data", new String(buf).equals(new String(chars, 1, 14)));
        // Check next byte
        assertEquals("Wrong chars", 15, in.read());
    } catch (IOException e) {
        fail("Exception during read test 2:" + e);
    }
    // regression test for HARMONY-841
    assertTrue(new BufferedReader(new CharArrayReader(new char[5], 1, 0), 2).read() == -1);
    br.close();
    br = new BufferedReader(ssr);
    try {
        br.read();
        fail("IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
    // Avoid IOException in tearDown().
    ssr.throwExceptionOnNextUse = false;
}
Also used : CharArrayReader(java.io.CharArrayReader) Support_ASimpleReader(tests.support.Support_ASimpleReader) BufferedReader(java.io.BufferedReader) PipedReader(java.io.PipedReader) Support_ASimpleReader(tests.support.Support_ASimpleReader) Support_StringReader(tests.support.Support_StringReader) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ThrowingReader(tests.support.ThrowingReader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IOException(java.io.IOException) Support_StringReader(tests.support.Support_StringReader)

Example 54 with CharArrayReader

use of java.io.CharArrayReader in project robovm by robovm.

the class OldCharArrayReaderTest method test_ready.

public void test_ready() {
    // Test for method boolean java.io.CharArrayReader.ready()
    cr = new CharArrayReader(hw);
    boolean expectException = false;
    try {
        assertTrue("ready returned false", cr.ready());
        cr.skip(1000);
        assertTrue("ready returned true", !cr.ready());
        cr.close();
        expectException = true;
        cr.ready();
        fail("No exception 1");
    } catch (IOException e) {
        if (!expectException)
            fail("Unexpected: " + e);
    }
    try {
        cr = new CharArrayReader(hw);
        cr.close();
        cr.ready();
        fail("No exception 2");
    } catch (IOException e) {
    }
}
Also used : CharArrayReader(java.io.CharArrayReader) IOException(java.io.IOException)

Example 55 with CharArrayReader

use of java.io.CharArrayReader in project robovm by robovm.

the class OldCharArrayReaderTest method test_read.

/**
     * java.io.CharArrayReader#read()
     */
public void test_read() throws IOException {
    cr = new CharArrayReader(hw);
    assertEquals("Test 1: Read returned incorrect char;", 'H', cr.read());
    cr = new CharArrayReader(new char[] { '蝥' });
    assertTrue("Test 2: Incorrect double byte char;", cr.read() == '蝥');
    cr.close();
    try {
        cr.read();
        fail("Test 3: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : CharArrayReader(java.io.CharArrayReader) IOException(java.io.IOException)

Aggregations

CharArrayReader (java.io.CharArrayReader)102 IOException (java.io.IOException)45 BufferedReader (java.io.BufferedReader)22 Reader (java.io.Reader)19 File (java.io.File)11 InputStreamReader (java.io.InputStreamReader)10 Test (org.junit.Test)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 StringReader (java.io.StringReader)8 FileReader (java.io.FileReader)7 PreparedStatement (java.sql.PreparedStatement)7 BufferedWriter (java.io.BufferedWriter)5 CharArrayWriter (java.io.CharArrayWriter)5 FileWriter (java.io.FileWriter)5 InputStream (java.io.InputStream)5 ResultSet (java.sql.ResultSet)5 InputSource (org.xml.sax.InputSource)5 PipedReader (java.io.PipedReader)4 CharBuffer (java.nio.CharBuffer)4 Signature (java.security.Signature)4