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