本文整理匯總了Java中java.nio.channels.Pipe.SinkChannel方法的典型用法代碼示例。如果您正苦於以下問題:Java Pipe.SinkChannel方法的具體用法?Java Pipe.SinkChannel怎麽用?Java Pipe.SinkChannel使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.channels.Pipe
的用法示例。
在下文中一共展示了Pipe.SinkChannel方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: SingleInputExpect
import java.nio.channels.Pipe; //導入方法依賴的package包/類
protected SingleInputExpect(
final Pipe.SourceChannel source,
final Pipe.SinkChannel sink,
final InputStream input,
final Charset charset,
final Appendable echoInput,
final Filter filter,
final int bufferSize,
final boolean autoFlushEcho) throws IOException {
this.input = input;
this.charset = charset;
this.echoInput = echoInput;
this.filter = filter;
this.bufferSize = bufferSize;
this.autoFlushEcho = autoFlushEcho;
this.source = source;
this.sink = sink;
source.configureBlocking(false);
buffer = new StringBuilder();
}
示例3: main
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public static void main(String[] args){
Pipe pipe;
Pipe.SinkChannel sinkChannel;
Pipe.SourceChannel sourceChannel;
try {
pipe = Pipe.open();
new Thread(new ReadWork(pipe.source())).start();
new Thread(new WriteWork(pipe.sink())).start();
} catch (IOException e) {
e.printStackTrace();
}
}
示例4: 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();
}
}
示例5: sink
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public Pipe.SinkChannel sink()
{
return sink;
}
示例6: WriteWork
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public WriteWork(Pipe.SinkChannel sinkChannel){
this.sinkChannel = sinkChannel;
}
示例7: 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();
}
}
}
示例8: main
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public static void main(String[] args) {
try {
// Creamos una instancia de Pipe
Pipe pipe = Pipe.open();
// Obtenemos el sink channel, el cual usamos para la escritura
Pipe.SinkChannel sinkChannel = pipe.sink();
String message = "Data sent through Java NIO Channels Pipe";
ByteBuffer buffer = ByteBuffer.allocate(512);
buffer.clear();
buffer.put(message.getBytes());
buffer.flip();
// Escribimos el mensaje en el channel
while (buffer.hasRemaining()) {
sinkChannel.write(buffer);
}
// Obtenemos el source channel el cual usamos para leer
Pipe.SourceChannel sourceChannel = pipe.source();
buffer = ByteBuffer.allocate(512);
// Vamos leyendo los datos y escribiendo en la consola
while (sourceChannel.read(buffer) > 0) {
buffer.flip();
while (buffer.hasRemaining()) {
System.out.print((char) buffer.get());
}
buffer.clear();
}
} catch (IOException e) {
// Log and Handle exception
e.printStackTrace();
}
}
示例9: mai
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public mai(Context paramContext, File paramFile, String paramString, long paramLong1, long paramLong2, Map<String, String> paramMap)
{
if ((paramLong1 < 0L) || ((paramLong2 != -1L) && (paramLong1 > paramLong2))) {
throw new IllegalArgumentException("Invalid stream limits");
}
this.f = paramLong1;
this.d = paramLong2;
long l1;
if (paramFile == null)
{
l1 = 0L;
this.e = l1;
if (this.f >= this.e) {
break label216;
}
this.b = new RandomAccessFile(paramFile, "r");
this.b.seek(this.f);
}
for (;;)
{
if (paramString == null) {
break label224;
}
Pipe localPipe = Pipe.open();
this.c = Channels.newInputStream(localPipe.source());
Pipe.SinkChannel localSinkChannel = localPipe.sink();
this.a = ixd.a(paramContext).a(paramString, 4, paramMap, localSinkChannel, this);
long l2 = Math.max(this.e, this.f);
if (l2 != 0L)
{
new StringBuilder(41).append("Starting request at: ").append(l2);
this.a.a(l2);
}
this.a.f();
return;
l1 = paramFile.length();
break;
label216:
this.b = null;
}
label224:
this.a = null;
this.c = null;
}
示例10: getWriteChannel
import java.nio.channels.Pipe; //導入方法依賴的package包/類
Pipe.SinkChannel getWriteChannel() {
return inboundSink;
}
示例11: WritePipe
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public WritePipe(Pipe.SinkChannel selectable) throws IOException{
super(selectable, null);
}
示例12: PipeSinkWritableSelectableChannel
import java.nio.channels.Pipe; //導入方法依賴的package包/類
public PipeSinkWritableSelectableChannel(Pipe.SinkChannel wrapped) {
this.wrapped = wrapped;
}