Search in sources :

Example 21 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project robovm by robovm.

the class ChannelsTest method testNewWriterWritableByteChannelString_InputNull.

/*
     * this test cannot be passed when buffer set to 0!
     */
public void testNewWriterWritableByteChannelString_InputNull() throws IOException {
    this.fouts = new FileOutputStream(tmpFile);
    WritableByteChannel wbChannel = Channels.newChannel(this.fouts);
    Writer testWriter = Channels.newWriter(wbChannel, Charset.forName(CODE_SET).newEncoder(), 1);
    //$NON-NLS-1$
    String writebuf = "";
    for (int val = 0; val < this.writebufSize / 2; val++) {
        writebuf = writebuf + ((char) (val + 64));
    }
    // can write to buffer
    testWriter.write(writebuf);
    testWriter.flush();
    testWriter.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) Writer(java.io.Writer)

Example 22 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project robovm by robovm.

the class ChannelsTest method testNewOutputStreamWritableByteChannel.

public void testNewOutputStreamWritableByteChannel() throws Exception {
    byte[] writebuf = new byte[this.testNum];
    ByteBuffer writebcbuf = ByteBuffer.allocateDirect(this.testNum);
    this.fouts = new FileOutputStream(tmpFile);
    WritableByteChannel writebc = this.fouts.getChannel();
    assertTrue(writebc.isOpen());
    OutputStream testouts = Channels.newOutputStream(writebc);
    // read in testins and fins use the same pointer
    testouts.write(writebuf);
    this.assertFileSizeSame(tmpFile, this.testNum);
    writebc.write(writebcbuf);
    this.assertFileSizeSame(tmpFile, this.testNum * 2);
    testouts.write(writebuf);
    this.assertFileSizeSame(tmpFile, this.testNum * 3);
    // readbc.close() affect testins
    writebc.close();
    assertFalse(writebc.isOpen());
    try {
        testouts.write(writebuf);
        fail();
    } catch (ClosedChannelException e) {
    // correct
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) FileOutputStream(java.io.FileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 23 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project robovm by robovm.

the class ChannelsTest method testNewChannelOutputStream_inputNull.

// test if fout to change is null
public void testNewChannelOutputStream_inputNull() throws IOException {
    int writeres = this.testNum;
    ByteBuffer writebuf = ByteBuffer.allocate(this.writebufSize);
    for (int val = 0; val < this.writebufSize / 2; val++) {
        writebuf.putChar((char) (val + 64));
    }
    this.fouts = null;
    try {
        WritableByteChannel rbChannel = Channels.newChannel(this.fouts);
        writeres = rbChannel.write(writebuf);
        assertEquals(0, writeres);
        writebuf.flip();
        writeres = rbChannel.write(writebuf);
        fail("Should throw NPE.");
    } catch (NullPointerException expected) {
    }
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 24 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project robovm by robovm.

the class ChannelsTest method testNewChannelOutputStream_BufNull.

// test if write buf is null
public void testNewChannelOutputStream_BufNull() throws IOException {
    int writeres = this.testNum;
    ByteBuffer writebuf = null;
    try {
        this.fouts = new FileOutputStream(tmpFile);
    } catch (FileNotFoundException e) {
        fail();
    }
    WritableByteChannel rbChannel = Channels.newChannel(this.fouts);
    try {
        writeres = rbChannel.write(writebuf);
        fail();
    } catch (NullPointerException e) {
    // correct
    }
    assertEquals(this.testNum, writeres);
}
Also used : FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 25 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project robovm by robovm.

the class ChannelsTest method testnewWriterCharsetError.

public void testnewWriterCharsetError() throws Exception {
    this.fouts = new FileOutputStream(tmpFile);
    WritableByteChannel wbChannel = Channels.newChannel(this.fouts);
    try {
        Channels.newWriter(wbChannel, Charset.forName(BAD_CODE_SET).newEncoder(), -1);
        fail();
    } catch (UnsupportedCharsetException e) {
    // correct
    }
}
Also used : UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) FileOutputStream(java.io.FileOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel)

Aggregations

WritableByteChannel (java.nio.channels.WritableByteChannel)111 ByteBuffer (java.nio.ByteBuffer)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 ReadableByteChannel (java.nio.channels.ReadableByteChannel)27 FileOutputStream (java.io.FileOutputStream)23 Test (org.testng.annotations.Test)19 ByteArrayInputStream (java.io.ByteArrayInputStream)16 Test (org.junit.Test)15 IOException (java.io.IOException)14 File (java.io.File)13 OutputStream (java.io.OutputStream)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)10 Vector (java.util.Vector)10 FileInputStream (java.io.FileInputStream)6 Writer (java.io.Writer)6 DbusEventIterator (com.linkedin.databus.core.DbusEventBuffer.DbusEventIterator)4 PhysicalPartition (com.linkedin.databus.core.data_model.PhysicalPartition)4 DbusEventsStatisticsCollector (com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector)4 ClosedChannelException (java.nio.channels.ClosedChannelException)4 Map (java.util.Map)4