本文整理汇总了Java中java.nio.channels.Pipe类的典型用法代码示例。如果您正苦于以下问题:Java Pipe类的具体用法?Java Pipe怎么用?Java Pipe使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Pipe类属于java.nio.channels包,在下文中一共展示了Pipe类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getImagesData
import java.nio.channels.Pipe; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public ReadableByteChannel getImagesData() {
// Leverage the raw frame decoder as input to the colour decoder
Pipe pipe = null;
try {
pipe = Pipe.open();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
decoderthreadpool.execute(new ColourFrameDecoder(getRawImageData(), pipe.sink()));
return pipe.source();
}
示例2: 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;
}
示例3: main
import java.nio.channels.Pipe; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
// Load necessary classes ahead of time
DatagramChannel dc = DatagramChannel.open();
Exception se = new SocketException();
SelectorProvider sp = SelectorProvider.provider();
Pipe p = sp.openPipe();
ServerSocketChannel ssc = ServerSocketChannel.open();
test1();
test2();
test3();
test4();
}
示例4: 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;
}
示例5: 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);
}
示例6: 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));
}
示例7: testStagingPreservesClasspath
import java.nio.channels.Pipe; //导入依赖的package包/类
@Test
public void testStagingPreservesClasspath() throws Exception {
File smallFile = makeFileWithContents("small.txt", "small");
File largeFile = makeFileWithContents("large.txt", "large contents");
when(mockGcsUtil.getObjects(anyListOf(GcsPath.class)))
.thenReturn(ImmutableList.of(StorageObjectOrIOException.create(
new FileNotFoundException("some/path"))));
when(mockGcsUtil.create(any(GcsPath.class), anyString()))
.thenAnswer(new Answer<SinkChannel>() {
@Override
public SinkChannel answer(InvocationOnMock invocation) throws Throwable {
return Pipe.open().sink();
}
});
List<DataflowPackage> targets =
defaultPackageUtil.stageClasspathElements(
ImmutableList.of(smallFile.getAbsolutePath(), largeFile.getAbsolutePath()),
STAGING_PATH,
createOptions);
// Verify that the packages are returned small, then large, matching input order even though
// the large file would be uploaded first.
assertThat(targets.get(0).getName(), startsWith("small"));
assertThat(targets.get(1).getName(), startsWith("large"));
}
示例8: 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());
}
示例9: 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);
}
}
示例10: 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);
}
示例11: testPackageUploadWithExplicitPackageName
import java.nio.channels.Pipe; //导入依赖的package包/类
@Test
public void testPackageUploadWithExplicitPackageName() throws Exception {
Pipe pipe = Pipe.open();
File tmpFile = makeFileWithContents("file.txt", "This is a test!");
final String overriddenName = "alias.txt";
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(overriddenName + "=" + 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(), equalTo(overriddenName));
assertThat(target.getLocation(),
RegexMatcher.matches(STAGING_PATH + "file-" + HASH_PATTERN + ".txt"));
}
示例12: buildConstructors
import java.nio.channels.Pipe; //导入依赖的package包/类
@Override
protected void buildConstructors(Appendable target, String className) throws IOException {
target.append("public ").append(className).append("(");
if (generateRunnable) {
target.append(") { \n");
FieldReferenceOffsetManager from = MessageSchema.from(schema);
if (!from.hasSimpleMessagesOnly) {
target.append(tab).append("startup();\n");
}
} else {
target.append(GraphManager.class.getCanonicalName()).append(" gm, ");
Appendables.appendClass(target, Pipe.class, schema.getClass()).append(" ").append(pipeVarName).append(") {\n");
target.append("super(gm,NONE,").append(pipeVarName).append(");\n");
target.append("this.").append(pipeVarName).append(" = ").append(pipeVarName).append(";\n");
Appendables.appendStaticCall(target, Pipe.class, "from").append(pipeVarName).append(").validateGUID(FROM_GUID);\n");
}
target.append("}\n\n");
}
示例13: 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);
}
}
示例14: ChannelQueue
import java.nio.channels.Pipe; //导入依赖的package包/类
public ChannelQueue(Selector readSelector) throws IOException {
this.pipe = Pipe.open();
this.sinkChannel = this.pipe.sink();
this.sourceChannel = this.pipe.source();
this.sinkChannel.configureBlocking(false);
this.sourceChannel.configureBlocking(false);
this.readSelector = readSelector;
this.queue = new LinkedBlockingQueue();
this.notificationQueue = 0;
this.readBuffer = ByteBuffer.allocate(1);
this.writeBuffer = ByteBuffer.allocate(1);
this.writeBuffer.put((byte)0x1);
this.writeBuffer.flip();
this.sourceChannel.register(this.readSelector, SelectionKey.OP_READ);
}
示例15: 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();
}