本文整理汇总了Java中java.nio.channels.Pipe.open方法的典型用法代码示例。如果您正苦于以下问题:Java Pipe.open方法的具体用法?Java Pipe.open怎么用?Java Pipe.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.Pipe
的用法示例。
在下文中一共展示了Pipe.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getImpl
import java.nio.channels.Pipe; //导入方法依赖的package包/类
@Override
public <T> T getImpl(Class<? extends T> cl)
{
if (InputStream.class.isAssignableFrom(cl))
{
try
{
Pipe pipe = Pipe.open();
DownloadTask dltask = new DownloadTask(pipe);
Thread download_thread = new Thread(dltask, "Product Download");
download_thread.start();
InputStream is = Channels.newInputStream(pipe.source());
return cl.cast(is);
}
catch (IOException ex)
{
LOGGER.error("could not create pipe", ex);
}
}
return null;
}
示例2: write
import java.nio.channels.Pipe; //导入方法依赖的package包/类
/**
* {@inheritDoc}
* @see io.netty.channel.ChannelOutboundInvoker#write(java.lang.Object)
*/
@Override
public ChannelFuture write(Object message) {
if(message!=null) {
if(message instanceof FileRegion) {
try {
Pipe pipe = Pipe.open();
FileRegion fr = (FileRegion)message;
long bytesToRead = fr.count();
fr.transferTo(pipe.sink(), 0L);
byte[] content = new byte[(int)bytesToRead];
pipe.source().read(ByteBuffer.wrap(content));
channelWrites.add(content);
} catch (Exception ex) {
log.error("Failed to read content from pipe", ex);
channelWrites.add(ex);
}
} else {
channelWrites.add(message);
}
log.info("Received Channel Write [{}] type:[{}]", message, message.getClass().getName());
}
return null;
}
示例3: write
import java.nio.channels.Pipe; //导入方法依赖的package包/类
/**
* {@inheritDoc}
* @see org.jboss.netty.channel.Channel#write(java.lang.Object)
*/
@Override
public ChannelFuture write(Object message) {
if(message!=null) {
if(message instanceof FileRegion) {
try {
Pipe pipe = Pipe.open();
FileRegion fr = (FileRegion)message;
long bytesToRead = fr.getCount();
fr.transferTo(pipe.sink(), 0L);
byte[] content = new byte[(int)bytesToRead];
pipe.source().read(ByteBuffer.wrap(content));
channelWrites.add(content);
} catch (Exception ex) {
log.error("Failed to read content from pipe", ex);
channelWrites.add(ex);
}
} else {
channelWrites.add(message);
}
log.info("Received Channel Write [{}] type:[{}]", message, message.getClass().getName());
}
return Channels.succeededFuture(this);
}
示例4: testPackageUploadWithFileSucceeds
import java.nio.channels.Pipe; //导入方法依赖的package包/类
@Test
public void testPackageUploadWithFileSucceeds() throws Exception {
Pipe pipe = Pipe.open();
String contents = "This is a test!";
File tmpFile = makeFileWithContents("file.txt", contents);
when(mockGcsUtil.getObjects(anyListOf(GcsPath.class)))
.thenReturn(ImmutableList.of(StorageObjectOrIOException.create(
new FileNotFoundException("some/path"))));
when(mockGcsUtil.create(any(GcsPath.class), anyString())).thenReturn(pipe.sink());
List<DataflowPackage> targets =
defaultPackageUtil.stageClasspathElements(
ImmutableList.of(tmpFile.getAbsolutePath()), STAGING_PATH, createOptions);
DataflowPackage target = Iterables.getOnlyElement(targets);
verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
verify(mockGcsUtil).create(any(GcsPath.class), anyString());
verifyNoMoreInteractions(mockGcsUtil);
assertThat(target.getName(), RegexMatcher.matches("file-" + HASH_PATTERN + ".txt"));
assertThat(target.getLocation(), equalTo(STAGING_PATH + target.getName()));
assertThat(new LineReader(Channels.newReader(pipe.source(), "UTF-8")).readLine(),
equalTo(contents));
}
示例5: testPackageUploadWithEmptyDirectorySucceeds
import java.nio.channels.Pipe; //导入方法依赖的package包/类
@Test
public void testPackageUploadWithEmptyDirectorySucceeds() throws Exception {
Pipe pipe = Pipe.open();
File tmpDirectory = tmpFolder.newFolder("folder");
when(mockGcsUtil.getObjects(anyListOf(GcsPath.class)))
.thenReturn(ImmutableList.of(StorageObjectOrIOException.create(
new FileNotFoundException("some/path"))));
when(mockGcsUtil.create(any(GcsPath.class), anyString())).thenReturn(pipe.sink());
List<DataflowPackage> targets =
defaultPackageUtil.stageClasspathElements(
ImmutableList.of(tmpDirectory.getAbsolutePath()), STAGING_PATH, createOptions);
DataflowPackage target = Iterables.getOnlyElement(targets);
verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
verify(mockGcsUtil).create(any(GcsPath.class), anyString());
verifyNoMoreInteractions(mockGcsUtil);
assertThat(target.getName(), RegexMatcher.matches("folder-" + HASH_PATTERN + ".jar"));
assertThat(target.getLocation(), equalTo(STAGING_PATH + target.getName()));
assertNull(new ZipInputStream(Channels.newInputStream(pipe.source())).getNextEntry());
}
示例6: testPackageUploadEventuallySucceeds
import java.nio.channels.Pipe; //导入方法依赖的package包/类
@Test
public void testPackageUploadEventuallySucceeds() throws Exception {
Pipe pipe = Pipe.open();
File tmpFile = makeFileWithContents("file.txt", "This is a test!");
when(mockGcsUtil.getObjects(anyListOf(GcsPath.class)))
.thenReturn(ImmutableList.of(StorageObjectOrIOException.create(
new FileNotFoundException("some/path"))));
when(mockGcsUtil.create(any(GcsPath.class), anyString()))
.thenThrow(new IOException("Fake Exception: 410 Gone")) // First attempt fails
.thenReturn(pipe.sink()); // second attempt succeeds
try (PackageUtil directPackageUtil =
PackageUtil.withExecutorService(MoreExecutors.newDirectExecutorService())) {
directPackageUtil.stageClasspathElements(
ImmutableList.of(tmpFile.getAbsolutePath()),
STAGING_PATH,
fastNanoClockAndSleeper,
createOptions);
} finally {
verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
verify(mockGcsUtil, times(2)).create(any(GcsPath.class), anyString());
verifyNoMoreInteractions(mockGcsUtil);
}
}
示例7: testPackageUploadIsNotSkippedWhenSizesAreDifferent
import java.nio.channels.Pipe; //导入方法依赖的package包/类
@Test
public void testPackageUploadIsNotSkippedWhenSizesAreDifferent() throws Exception {
Pipe pipe = Pipe.open();
File tmpDirectory = tmpFolder.newFolder("folder");
tmpFolder.newFolder("folder", "empty_directory");
tmpFolder.newFolder("folder", "directory");
makeFileWithContents("folder/file.txt", "This is a test!");
makeFileWithContents("folder/directory/file.txt", "This is also a test!");
when(mockGcsUtil.getObjects(anyListOf(GcsPath.class)))
.thenReturn(ImmutableList.of(StorageObjectOrIOException.create(
createStorageObject(STAGING_PATH, Long.MAX_VALUE))));
when(mockGcsUtil.create(any(GcsPath.class), anyString())).thenReturn(pipe.sink());
defaultPackageUtil.stageClasspathElements(
ImmutableList.of(tmpDirectory.getAbsolutePath()), STAGING_PATH, createOptions);
verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
verify(mockGcsUtil).create(any(GcsPath.class), anyString());
verifyNoMoreInteractions(mockGcsUtil);
}
示例8: 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);
}
}
示例9: getSoundData
import java.nio.channels.Pipe; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public ReadableByteChannel getSoundData() {
Pipe pipe = null;
try {
pipe = Pipe.open();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
decoderthreadpool.execute(new SoundDataDecoder(
new DuplicateReadOnlyByteChannel(bytechannel), pipe.sink()));
return pipe.source();
}
示例10: WindowsSelectorImpl
import java.nio.channels.Pipe; //导入方法依赖的package包/类
WindowsSelectorImpl(SelectorProvider sp) throws IOException {
super(sp);
pollWrapper = new PollArrayWrapper(INIT_CAP);
wakeupPipe = Pipe.open();
wakeupSourceFd = ((SelChImpl)wakeupPipe.source()).getFDVal();
// Disable the Nagle algorithm so that the wakeup is more immediate
SinkChannelImpl sink = (SinkChannelImpl)wakeupPipe.sink();
(sink.sc).socket().setTcpNoDelay(true);
wakeupSinkFd = ((SelChImpl)sink).getFDVal();
pollWrapper.addWakeupSocket(wakeupSourceFd, 0);
}
示例11: DotNetSelectorImpl
import java.nio.channels.Pipe; //导入方法依赖的package包/类
DotNetSelectorImpl(SelectorProvider sp) throws IOException
{
super(sp);
wakeupPipe = Pipe.open();
wakeupSourceFd = ((SelChImpl)wakeupPipe.source()).getFD().getSocket();
// Disable the Nagle algorithm so that the wakeup is more immediate
SinkChannelImpl sink = (SinkChannelImpl)wakeupPipe.sink();
(sink.sc).socket().setTcpNoDelay(true);
wakeupSinkFd = ((SelChImpl)sink).getFD().getSocket();
}
示例12: test
import java.nio.channels.Pipe; //导入方法依赖的package包/类
public void test() throws Exception {
Thread tester = new Thread("PipeTester") {
private Pipe testPipe = null;
@Override
public void run() {
for (;;) {
boolean interrupted = this.isInterrupted();
try {
testPipe = Pipe.open();
close();
if (interrupted) {
if (!this.isInterrupted())
exc = new RuntimeException("interrupt status reset");
break;
}
} catch (IOException ioe) {
exc = ioe;
}
}
}
private void close() throws IOException {
if (testPipe != null) {
testPipe.sink().close();
testPipe.source().close();
}
}
};
tester.start();
Thread.sleep(200);
tester.interrupt();
tester.join();
if (exc != null)
throw exc;
}
示例13: createInput
import java.nio.channels.Pipe; //导入方法依赖的package包/类
private InputStream createInput(Map<String, List<String>> config) {
try {
Pipe pipe = Pipe.open();
OutputStream rawOutput = Channels.newOutputStream(pipe.sink());
ObjectOutputStream output = new ObjectOutputStream(rawOutput);
output.writeObject(config);
return Channels.newInputStream(pipe.source());
} catch (IOException e) {
throw new UncheckedIOException("Could not open pipe.", e);
}
}
示例14: testPackageUploadWithDirectorySucceeds
import java.nio.channels.Pipe; //导入方法依赖的package包/类
@Test
public void testPackageUploadWithDirectorySucceeds() throws Exception {
Pipe pipe = Pipe.open();
File tmpDirectory = tmpFolder.newFolder("folder");
tmpFolder.newFolder("folder", "empty_directory");
tmpFolder.newFolder("folder", "directory");
makeFileWithContents("folder/file.txt", "This is a test!");
makeFileWithContents("folder/directory/file.txt", "This is also a test!");
when(mockGcsUtil.getObjects(anyListOf(GcsPath.class)))
.thenReturn(ImmutableList.of(StorageObjectOrIOException.create(
new FileNotFoundException("some/path"))));
when(mockGcsUtil.create(any(GcsPath.class), anyString())).thenReturn(pipe.sink());
defaultPackageUtil.stageClasspathElements(
ImmutableList.of(tmpDirectory.getAbsolutePath()), STAGING_PATH, createOptions);
verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
verify(mockGcsUtil).create(any(GcsPath.class), anyString());
verifyNoMoreInteractions(mockGcsUtil);
ZipInputStream inputStream = new ZipInputStream(Channels.newInputStream(pipe.source()));
List<String> zipEntryNames = new ArrayList<>();
for (ZipEntry entry = inputStream.getNextEntry(); entry != null;
entry = inputStream.getNextEntry()) {
zipEntryNames.add(entry.getName());
}
assertThat(zipEntryNames,
containsInAnyOrder("directory/file.txt", "empty_directory/", "file.txt"));
}
示例15: 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();
}
}