本文整理匯總了Java中java.nio.channels.Pipe.sink方法的典型用法代碼示例。如果您正苦於以下問題:Java Pipe.sink方法的具體用法?Java Pipe.sink怎麽用?Java Pipe.sink使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.channels.Pipe
的用法示例。
在下文中一共展示了Pipe.sink方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
Pipe pipe = Pipe.open();
Pipe.SinkChannel sinkChannel = pipe.sink();
Pipe.SourceChannel sourceChannel = pipe.source();
String newData = "New String to write to file..." + System.currentTimeMillis();
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();
ByteBuffer bufread = ByteBuffer.allocate(48);
while (buf.hasRemaining()) {
sinkChannel.write(buf);
int bytesRead = sourceChannel.read(bufread);
System.out.println(bytesRead);
}
}
示例2: setup
import java.nio.channels.Pipe; //導入方法依賴的package包/類
/**
* Creates a mock input stream which send some data every SMALL_TIMEOUT ms.
*/
@Before
public void setup() throws Exception {
mock = TestUtils.mockInputStream(text);
final Pipe pipe = Pipe.open();
input = new SingleInputExpect(
pipe.source(),
pipe.sink(),
mock.getStream(),
Charset.defaultCharset(),
null,
null,
DEFAULT_BUFFER_SIZE,
false);
executor = Executors.newSingleThreadExecutor();
input.start(executor);
mock.waitUntilReady();
}
示例3: open
import java.nio.channels.Pipe; //導入方法依賴的package包/類
private synchronized void open() throws IOException {
if (inboundSource == null) {
SelectorProvider provider = SelectorProvider.provider();
Pipe inboundPipe = provider.openPipe();
inboundSource = inboundPipe.source();
inboundSink = inboundPipe.sink();
Pipe outboundPipe = provider.openPipe();
outboundSource = outboundPipe.source();
outboundSink = outboundPipe.sink();
}
}
示例4: createNonBlockingChannel
import java.nio.channels.Pipe; //導入方法依賴的package包/類
private Pipe.SourceChannel createNonBlockingChannel(byte[] content) throws IOException {
Pipe pipe = Pipe.open();
WritableByteChannel sinkChannel = pipe.sink();
sinkChannel.write(ByteBuffer.wrap(content));
Pipe.SourceChannel sourceChannel = pipe.source();
sourceChannel.configureBlocking(false);
return sourceChannel;
}
示例5: test_sink
import java.nio.channels.Pipe; //導入方法依賴的package包/類
/**
* @tests java.nio.channels.Pipe#sink()
*/
@TestTargetNew(
level = TestLevel.COMPLETE,
notes = "",
method = "sink",
args = {}
)
public void test_sink() throws IOException {
Pipe pipe = Pipe.open();
SinkChannel sink = pipe.sink();
assertTrue(sink.isBlocking());
}
示例6: prepareEngines
import java.nio.channels.Pipe; //導入方法依賴的package包/類
void prepareEngines() throws IOException {
Pipe clientSendPipe = Pipe.open();
Pipe serverSendPipe = Pipe.open();
SinkChannel clientSink = clientSendPipe.sink();
SourceChannel serverSource = clientSendPipe.source();
SinkChannel serverSink = serverSendPipe.sink();
SourceChannel clientSource = serverSendPipe.source();
clientEngine = new HandshakeHandler(true, clientSource, clientSink);
serverEngine = new HandshakeHandler(false, serverSource, serverSink);
}
示例7: startWorker
import java.nio.channels.Pipe; //導入方法依賴的package包/類
private static ReadableByteChannel startWorker (int reps)
throws Exception
{
Pipe pipe = Pipe.open();
Worker worker = new Worker (pipe.sink(), reps);
worker.start();
return (pipe.source());
}
示例8: EpollSelectorImpl
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public EpollSelectorImpl(SelectorProvider selectorProvider) {
super(selectorProvider);
try {
Pipe mockSelector = selectorProvider.openPipe();
sink = mockSelector.sink();
source = mockSelector.source();
sourcefd = ((FileDescriptorHandler) source).getFD();
source.configureBlocking(false);
fileDescriptorClass = sourcefd.getClass();
keyFDs = new int[1];
readyFDs = new int[1];
readyOps = new int[1];
// register sink channel
keyFDs[0] = resolveFD(fileDescriptorClass, sourcefd);
keys[0] = source.keyFor(this);
epollFD = prepare();
keysCount = 1;
quickMap.put(keyFDs[0], (EpollSelectionKeyImpl) keys[0]);
addFileDescriptor(epollFD, 1, keyFDs[0]);
} catch (IOException e) {
// do nothing
}
}
示例9: SelectorImpl
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public SelectorImpl(SelectorProvider selectorProvider) {
super(selectorProvider);
try {
Pipe mockSelector = selectorProvider.openPipe();
sink = mockSelector.sink();
source = mockSelector.source();
sourcefd = ((FileDescriptorHandler) source).getFD();
source.configureBlocking(false);
readableFDs = new FileDescriptor[1];
writableFDs = new FileDescriptor[0];
keysToReadableFDs = new int[1];
keysToWritableFDs = new int[0];
readableFDsToKeys = new int[1];
writableFDsToKeys = new int[0];
// register sink channel
readableFDs[0] = sourcefd;
keys[0] = (SelectionKeyImpl) source.keyFor(this);
// index it
keysToReadableFDs[0] = 0;
readableFDsToKeys[0] = 0;
lastKeyIndex = 0;
readableKeysCount = 1;
} catch (IOException e) {
// do nothing
}
}
示例10: NBCircularIOStream
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public NBCircularIOStream() throws IOException {
final Pipe pipe = Pipe.open();
sink = new BufferedWritableSelectableChannel(new PipeSinkWritableSelectableChannel(pipe.sink()));
final Pipe.SourceChannel source = pipe.source();
sink.configureBlocking(false);
source.configureBlocking(true);
in = Channels.newInputStream(source);
}
示例11: SelectorImpl
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public SelectorImpl(SelectorProvider selectorProvider) {
super(selectorProvider);
try {
Pipe mockSelector = selectorProvider.openPipe();
sink = mockSelector.sink();
source = mockSelector.source();
sourcefd = ((FileDescriptorHandler)source).getFD();
source.configureBlocking(false);
} catch (IOException e) {
// do nothing
}
}
示例12: make_fdpair
import java.nio.channels.Pipe; //導入方法依賴的package包/類
private void make_fdpair() {
Pipe pipe;
try {
pipe = Pipe.open();
} catch (IOException e) {
throw new ZError.IOException(e);
}
r = pipe.source();
w = pipe.sink();
}
示例13: test
import java.nio.channels.Pipe; //導入方法依賴的package包/類
@Test
public void test() throws IOException, InterruptedException {
Pipe pipe = Pipe.open();
WritableByteChannel out = pipe.sink();
ReadableByteChannel in = pipe.source();
FibonacciProducer producer = new FibonacciProducer(out, 200);
FibonacciConsumer consumer = new FibonacciConsumer(in);
producer.start();
consumer.start();
TimeUnit.SECONDS.sleep(5);
}
示例14: testReadableByteChannel
import java.nio.channels.Pipe; //導入方法依賴的package包/類
@Test
public void testReadableByteChannel() throws Exception {
int[] testSizes = { 0, 10, 1023, 1024, 1025, 2047, 2048, 2049 };
for (int size : testSizes) {
SelectorProvider sp = SelectorProvider.provider();
Pipe p = sp.openPipe();
Pipe.SinkChannel sink = p.sink();
Pipe.SourceChannel source = p.source();
sink.configureBlocking(false);
ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10);
byte[] someBytes = new byte[size + 10];
generator.nextBytes(someBytes);
outgoingdata.put(someBytes);
outgoingdata.flip();
int totalWritten = 0;
while (totalWritten < size + 10) {
int written = sink.write(outgoingdata);
if (written < 0)
throw new Exception("Write failed");
totalWritten += written;
}
File f = File.createTempFile("blah"+size, null);
f.deleteOnExit();
RandomAccessFile raf = new RandomAccessFile(f, "rw");
FileChannel fc = raf.getChannel();
long oldPosition = fc.position();
long bytesWritten = fc.transferFrom(source, 0, size);
fc.force(true);
if (bytesWritten != size)
throw new RuntimeException("Transfer failed");
if (fc.position() != oldPosition)
throw new RuntimeException("Position changed");
if (fc.size() != size)
throw new RuntimeException("Unexpected sink size "+ fc.size());
fc.close();
sink.close();
source.close();
f.delete();
}
}
示例15: testSocketIOWithTimeout
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public void testSocketIOWithTimeout() throws IOException {
// first open pipe:
Pipe pipe = Pipe.open();
Pipe.SourceChannel source = pipe.source();
Pipe.SinkChannel sink = pipe.sink();
try {
InputStream in = new SocketInputStream(source, TIMEOUT);
OutputStream out = new SocketOutputStream(sink, TIMEOUT);
byte[] writeBytes = TEST_STRING.getBytes();
byte[] readBytes = new byte[writeBytes.length];
out.write(writeBytes);
doIO(null, out);
in.read(readBytes);
assertTrue(Arrays.equals(writeBytes, readBytes));
doIO(in, null);
/*
* Verify that it handles interrupted threads properly.
* Use a large timeout and expect the thread to return quickly.
*/
in = new SocketInputStream(source, 0);
Thread thread = new Thread(new ReadRunnable(in));
thread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {}
thread.interrupt();
try {
thread.join();
} catch (InterruptedException e) {
throw new IOException("Unexpected InterruptedException : " + e);
}
//make sure the channels are still open
assertTrue(source.isOpen());
assertTrue(sink.isOpen());
out.close();
assertFalse(sink.isOpen());
// close sink and expect -1 from source.read()
assertEquals(-1, in.read());
// make sure close() closes the underlying channel.
in.close();
assertFalse(source.isOpen());
} finally {
if (source != null) {
source.close();
}
if (sink != null) {
sink.close();
}
}
}