Search in sources :

Example 41 with Support_OutputStream

use of tests.support.Support_OutputStream in project j2objc by google.

the class OldObjectOutputStreamTest method test_reset.

public void test_reset() throws Exception {
    String o = "HelloWorld";
    sos = new Support_OutputStream(200);
    oos.close();
    oos = new ObjectOutputStream(sos);
    oos.writeObject(o);
    oos.writeObject(o);
    oos.reset();
    oos.writeObject(o);
    sos.setThrowsException(true);
    try {
        oos.reset();
        fail("Test 1: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
    sos.setThrowsException(false);
    ois = new ObjectInputStream(new ByteArrayInputStream(sos.toByteArray()));
    assertEquals("Test 2: Incorrect object read.", o, ois.readObject());
    assertEquals("Test 3: Incorrect object read.", o, ois.readObject());
    assertEquals("Test 4: Incorrect object read.", o, ois.readObject());
    ois.close();
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 42 with Support_OutputStream

use of tests.support.Support_OutputStream in project j2objc by google.

the class OldBufferedOutputStreamTest method test_writeI.

public void test_writeI() throws IOException {
    baos = new java.io.ByteArrayOutputStream();
    os = new java.io.BufferedOutputStream(baos);
    os.write('t');
    bais = new java.io.ByteArrayInputStream(baos.toByteArray());
    assertEquals("Test 1: Byte written, not buffered;", 0, bais.available());
    os.flush();
    bais = new java.io.ByteArrayInputStream(baos.toByteArray());
    assertEquals("Test 2: Byte not written after flush;", 1, bais.available());
    byte[] wbytes = new byte[1];
    bais.read(wbytes, 0, 1);
    assertEquals("Test 3: Incorrect byte written or read;", 't', wbytes[0]);
    os.close();
    sos = new Support_OutputStream(true);
    os = new BufferedOutputStream(sos, 1);
    os.write('t');
    try {
        // Exception is only thrown when the buffer is flushed.
        os.write('e');
        fail("Test 4: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
    // To avoid exception during tearDown().
    sos.setThrowsException(false);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Support_OutputStream(tests.support.Support_OutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 43 with Support_OutputStream

use of tests.support.Support_OutputStream in project j2objc by google.

the class OldBufferedOutputStreamTest method test_write$BII.

public void test_write$BII() throws IOException {
    os = new java.io.BufferedOutputStream(baos = new java.io.ByteArrayOutputStream(), 512);
    os.write(fileString.getBytes(), 0, 500);
    bais = new java.io.ByteArrayInputStream(baos.toByteArray());
    assertEquals("Test 1: Bytes written, not buffered;", 0, bais.available());
    os.flush();
    bais = new java.io.ByteArrayInputStream(baos.toByteArray());
    assertEquals("Test 2: Bytes not written after flush;", 500, bais.available());
    os.write(fileString.getBytes(), 500, 513);
    bais = new java.io.ByteArrayInputStream(baos.toByteArray());
    assertTrue("Test 3: Bytes not written when buffer full.", bais.available() >= 1000);
    byte[] wbytes = new byte[1013];
    bais.read(wbytes, 0, 1013);
    assertTrue("Test 4: Incorrect bytes written or read.", fileString.substring(0, 1013).equals(new String(wbytes, 0, wbytes.length)));
    os.close();
    sos = new Support_OutputStream(true);
    os = new BufferedOutputStream(sos, 10);
    try {
        os.write(fileString.getBytes(), 0, 500);
        fail("Test 5: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
    // To avoid exception during tearDown().
    sos.setThrowsException(false);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Support_OutputStream(tests.support.Support_OutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 44 with Support_OutputStream

use of tests.support.Support_OutputStream in project j2objc by google.

the class OldBufferedOutputStreamTest method test_flush.

public void test_flush() throws IOException {
    baos = new ByteArrayOutputStream();
    os = new java.io.BufferedOutputStream(baos, 600);
    os.write(fileString.getBytes(), 0, 500);
    os.flush();
    assertEquals("Test 1: Bytes not written after flush;", 500, ((ByteArrayOutputStream) baos).size());
    os.close();
    sos = new Support_OutputStream(true);
    os = new BufferedOutputStream(sos, 10);
    try {
        os.flush();
        fail("Test 2: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
    // To avoid exception during tearDown().
    sos.setThrowsException(false);
}
Also used : Support_OutputStream(tests.support.Support_OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 45 with Support_OutputStream

use of tests.support.Support_OutputStream in project j2objc by google.

the class OldByteArrayOutputStreamTest method test_writeToLjava_io_OutputStream.

public void test_writeToLjava_io_OutputStream() throws Exception {
    Support_OutputStream sos = new Support_OutputStream();
    bos = new java.io.ByteArrayOutputStream();
    bos.write(fileString.getBytes(), 0, 10);
    bos.writeTo(sos);
    assertTrue("Test 1: Incorrect string written.", sos.toString().equals(fileString.substring(0, 10)));
    sos.setThrowsException(true);
    try {
        bos.writeTo(sos);
        fail("Test 2: IOException expected.");
    } catch (IOException e) {
    // Expected.
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Support_OutputStream(tests.support.Support_OutputStream) IOException(java.io.IOException)

Aggregations

Support_OutputStream (tests.support.Support_OutputStream)48 IOException (java.io.IOException)30 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 OutputStreamWriter (java.io.OutputStreamWriter)16 FilterOutputStream (java.io.FilterOutputStream)12 ByteArrayInputStream (java.io.ByteArrayInputStream)8 BufferedOutputStream (java.io.BufferedOutputStream)6 ObjectOutputStream (java.io.ObjectOutputStream)6 DataOutputStream (java.io.DataOutputStream)4 Charset (java.nio.charset.Charset)4 ObjectInputStream (java.io.ObjectInputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 CharsetEncoder (java.nio.charset.CharsetEncoder)2 DigestOutputStream (java.security.DigestOutputStream)2 MessageDigest (java.security.MessageDigest)2