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
}
}
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) {
}
}
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);
}
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();
}
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();
}
}
Aggregations