|
|
|
||
|
The number of Windows will eventually give you the known value of The code used is as follows:
// create a Pipe and retrieve its sink and source
// NOTE: the sink and source are SelectableChannels
final Pipe pipe = Pipe.open();
final WritableByteChannel sink = pipe.sink();
final ReadableByteChannel source = pipe.source();
// set the sink to non-blocking and create and register a write
// Selector on it. The Selector is used to determine when the sink
// is "full".
// NOTE: the cast is required since there is no common super-type
// for selectable + readable / writable
((SelectableChannel)sink).configureBlocking(false/*non-blocking*/);
final Selector writeSelector = Selector.open();
((SelectableChannel)sink).register(writeSelector, SelectionKey.OP_WRITE);
// continue to write to the sink until it is "full"
// NOTE: the sanity upper-bound is used to ensure that, in a remote
// case, a sink is not infinite
final ByteBuffer writeBuffer = ByteBuffer.allocate(BUFFER_SIZE);
for(int i=0; i<BUFFER_SIZE; i++)
writeBuffer.put((byte)(i & 0xFF)); // arbitrary
writeBuffer.flip();
boolean isInfinite = true; // set to false if limit found on write
int numberOfBytesWritten = 0;
for(int i=0; i<UPPER_BOUND/*sanity*/; i++)
{
// ensure that data can be written
// NOTE: selectNow() is used so that it does not block
if(writeSelector.selectNow() > 0)
{
// clear the selected keys (required)
writeSelector.selectedKeys().clear();
// write the data
// NOTE: the actual data written is arbitrary
numberOfBytesWritten += sink.write(writeBuffer);
writeBuffer.rewind();
} else
{
// the sink is full. Flag that a limit was found and break
// out of loop.
isInfinite = false;
break;
}
}
And, no, changing I should mention that An interesting Linux tidbit: if the following code is added after the code listed above with a // attempt to write more to the sink even though we shouldn't be // able to writeBuffer.limit(1/*writes one byte*/); numberOfBytesWritten = sink.write(writeBuffer); writeBuffer.rewind(); It seems that the Linux write selector is lying to us. This is not the case on Windows. Link-back to main entry: NIO and SSL. |
| Comments | ||||
|
| Post a comment |
|
|
Unless otherwise expressly stated, all original material of whatever nature created by Rob Grzywinski and included in this weblog and any related pages, including the weblog's archives, is licensed under a Creative Commons License. |