Search in sources :

Example 91 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project j2objc by google.

the class ChannelsTest method testNewChannelOutputStream.

/*
     * Test method for 'java.nio.channels.Channels.NewChannel(OutputStream)'
     */
public void testNewChannelOutputStream() throws IOException {
    int writeNum = 0;
    ByteBuffer writebuf = ByteBuffer.allocateDirect(this.writebufSize);
    for (int val = 0; val < this.writebufSize / 2; val++) {
        writebuf.putChar((char) (val + 64));
    }
    this.fouts = new FileOutputStream(tmpFile);
    WritableByteChannel testChannel = this.fouts.getChannel();
    WritableByteChannel rbChannel = Channels.newChannel(this.fouts);
    assertTrue(testChannel.isOpen());
    assertTrue(rbChannel.isOpen());
    byte[] bit = new byte[1];
    bit[0] = 80;
    this.fouts.write(bit);
    this.fouts.flush();
    this.fins = new FileInputStream(tmpFile);
    assertEquals(this.fins.available(), 1);
    this.fins.close();
    writeNum = rbChannel.write(writebuf);
    // write success ,but output null
    assertEquals(0, writeNum);
    this.fouts.close();
    try {
        writeNum = testChannel.write(writebuf);
        fail();
    } catch (ClosedChannelException e) {
    // correct
    }
    assertEquals(0, writeNum);
    // close of rbchannel does affect on testchannel(same channel)
    rbChannel.close();
    try {
        writeNum = testChannel.write(writebuf);
        fail();
    } catch (ClosedChannelException e) {
    // correct
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) FileOutputStream(java.io.FileOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

Example 92 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project j2objc by google.

the class ChannelsTest method testNewOutputStreamWritableByteChannel_InputNull.

public void testNewOutputStreamWritableByteChannel_InputNull() throws Exception {
    byte[] writebuf = new byte[this.testNum];
    try {
        OutputStream testouts = Channels.newOutputStream(null);
        assertNotNull(testouts);
        testouts.write(writebuf);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        WritableByteChannel writebc = Channels.newChannel((OutputStream) null);
        assertTrue(writebc.isOpen());
        OutputStream testoutputS = Channels.newOutputStream(writebc);
        testoutputS.write(writebuf);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel)

Example 93 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project j2objc by google.

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 94 with WritableByteChannel

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

the class ByteBufferIO method write.

public static void write(OutputStream output, ByteBuffer buffer) throws IOException {
    DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(output));
    dataOutput.writeInt(buffer.position());
    buffer.flip();
    WritableByteChannel channel = Channels.newChannel(dataOutput);
    channel.write(buffer);
    // TODO: Do we need this?
    dataOutput.flush();
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel)

Example 95 with WritableByteChannel

use of java.nio.channels.WritableByteChannel in project jstorm by alibaba.

the class Utils method downloadFromMaster.

public static void downloadFromMaster(Map conf, String file, String localFile) throws IOException, TException {
    WritableByteChannel out = null;
    NimbusClient client = null;
    try {
        client = NimbusClient.getConfiguredClient(conf, 10 * 1000);
        String id = client.getClient().beginFileDownload(file);
        out = Channels.newChannel(new FileOutputStream(localFile));
        while (true) {
            ByteBuffer chunk = client.getClient().downloadChunk(id);
            int written = out.write(chunk);
            if (written == 0) {
                client.getClient().finishFileDownload(id);
                break;
            }
        }
    } finally {
        if (out != null)
            out.close();
        if (client != null)
            client.close();
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Aggregations

WritableByteChannel (java.nio.channels.WritableByteChannel)118 ByteBuffer (java.nio.ByteBuffer)35 ReadableByteChannel (java.nio.channels.ReadableByteChannel)30 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 FileOutputStream (java.io.FileOutputStream)24 Test (org.testng.annotations.Test)19 ByteArrayInputStream (java.io.ByteArrayInputStream)16 IOException (java.io.IOException)16 Test (org.junit.Test)15 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)7 Writer (java.io.Writer)6 FileChannel (java.nio.channels.FileChannel)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