本文整理汇总了Java中java.nio.channels.Channel类的典型用法代码示例。如果您正苦于以下问题:Java Channel类的具体用法?Java Channel怎么用?Java Channel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Channel类属于java.nio.channels包,在下文中一共展示了Channel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleException
import java.nio.channels.Channel; //导入依赖的package包/类
@Override
public void handleException(Channel channel, IOException exception) {
IoUtils.safeClose(channel);
if (exchange.isResponseStarted()) {
IoUtils.safeClose(clientConnection);
UndertowLogger.REQUEST_IO_LOGGER.debug("Exception reading from target server", exception);
if (!exchange.isResponseStarted()) {
exchange.setResponseCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
} else {
IoUtils.safeClose(exchange.getConnection());
}
} else {
exchange.setResponseCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.endExchange();
}
}
示例2: getChannel
import java.nio.channels.Channel; //导入依赖的package包/类
public static synchronized Channel getChannel() throws IOException {
if (devnull < 0) {
devnull = open0("/dev/null", O_RDWR);
}
// If we don't have the channel try to create it
if (!haveChannel) {
channel = createChannel();
haveChannel = true;
}
// if there is a channel then do the security check before
// returning it.
if (channel != null) {
checkAccess(channel);
}
return channel;
}
示例3: inheritedChannel
import java.nio.channels.Channel; //导入依赖的package包/类
public synchronized Channel inheritedChannel() throws IOException {
System.err.println("SP.inheritedChannel");
if (channel == null) {
channel = SocketChannel.open();
Socket socket = channel.socket();
System.err.println("socket = " + socket);
/*
* Notify test that inherited channel was created.
*/
try {
System.err.println("notify test...");
Registry registry =
LocateRegistry.getRegistry(TestLibrary.INHERITEDCHANNELNOTSERVERSOCKET_REGISTRY_PORT);
Callback obj = (Callback) registry.lookup("Callback");
obj.notifyTest();
} catch (NotBoundException nbe) {
throw (IOException)
new IOException("callback object not bound").
initCause(nbe);
}
}
return channel;
}
示例4: main
import java.nio.channels.Channel; //导入依赖的package包/类
public static void main(String args[]) {
// test the assertion that SelectorProvider.inheritedChannel()
// and System.inheritedChannel return null when standard input
// is not connected to a socket
Channel c1, c2;
try {
c1 = SelectorProvider.provider().inheritedChannel();
c2 = System.inheritedChannel();
} catch (IOException ioe) {
throw new RuntimeException("Unexpected IOException: " + ioe);
}
if (c1 != null || c2 != null) {
throw new RuntimeException("Channel returned - unexpected");
}
}
示例5: releaseFileLock
import java.nio.channels.Channel; //导入依赖的package包/类
public void releaseFileLock() {
if (fileLock != null) {
if (log.isTraceEnable()) {
log.info(this, "Releasing the file lock of " + this.filePath.getFileName());
}
Channel fc = fileLock.acquiredBy();
try {
fileLock.release();
fileLock = null;
if (fc != null) {
fc.close();
}
}
catch (IOException e) {
}
}
}
示例6: inheritedChannel
import java.nio.channels.Channel; //导入依赖的package包/类
public synchronized Channel inheritedChannel() throws IOException {
System.err.println("SP.inheritedChannel");
if (channel == null) {
channel = SocketChannel.open();
Socket socket = channel.socket();
System.err.println("socket = " + socket);
/*
* Notify test that inherited channel was created.
*/
try {
System.err.println("notify test...");
int registryPort = Integer.getInteger(
"test.java.rmi.rmidViaInheritedChannel.registry.port", 0);
Registry registry = LocateRegistry.getRegistry(registryPort);
Callback obj = (Callback) registry.lookup("Callback");
obj.notifyTest();
} catch (NotBoundException nbe) {
throw (IOException)
new IOException("callback object not bound").
initCause(nbe);
}
}
return channel;
}
示例7: completed
import java.nio.channels.Channel; //导入依赖的package包/类
@Override
public void completed(final ClientConnection connection) {
final ServerConnection serverConnection = exchange.getConnection();
//we attach to the connection so it can be re-used
serverConnection.putAttachment(clientAttachmentKey, connection);
serverConnection.addCloseListener(serverConnection1 -> IoUtils.safeClose(connection));
connection.getCloseSetter().set((ChannelListener<Channel>) channel -> serverConnection.removeAttachment(clientAttachmentKey));
exchange.setRelativePath("/");
Realm realm = realmCache.matches(exchange.getRequestPath());
Application application = realmCache.getApplication(realm);
String path = exchange.getRequestURI();
if (path.startsWith(application.getVirtualPath())) {
String passTo = calculatePathTo(path, application);
exchange.setRequestPath(passTo);
exchange.setRequestURI(passTo);
}
callback.completed(exchange, new ProxyConnection(connection, "/"));
}
示例8: fsync
import java.nio.channels.Channel; //导入依赖的package包/类
/**
* Internal fsync implementation.
*/
private static void fsync(PyObject fd, boolean metadata) {
RawIOBase rawIO = FileDescriptors.get(fd);
rawIO.checkClosed();
Channel channel = rawIO.getChannel();
if (!(channel instanceof FileChannel)) {
throw Py.OSError(Errno.EINVAL);
}
try {
((FileChannel)channel).force(metadata);
} catch (ClosedChannelException cce) {
// In the rare case it's closed but the rawIO wasn't
throw Py.ValueError("I/O operation on closed file");
} catch (IOException ioe) {
throw Py.OSError(ioe);
}
}
示例9: doClose
import java.nio.channels.Channel; //导入依赖的package包/类
private void doClose() {
connLock.lock();
try {
Channel channel = this.channel;
if (channel != null) {
if (channel.isOpen()) {
IOUtils.close(channel);
listener.onDisConnected(client);
logger.w("channel closed !!!");
}
this.channel = null;
}
} finally {
state.set(disconnected);
connLock.unlock();
}
}
示例10: startListening
import java.nio.channels.Channel; //导入依赖的package包/类
public void startListening() {
while (isAlive) {
Channel channel;
channel = network.choosingConnection(); // first stop here!
if (channel instanceof ServerSocketChannel) {
//System.out.println("TCP received!");
// TODO: Read TCP received message, convert to enum and reply in another class.
TcpReceiver tcpReceiver = new TcpReceiver(channel);
new Thread(tcpReceiver).start();
}
else if (channel instanceof DatagramChannel) {
System.out.println("UDP received!");
// TODO: Read UDP received message, convert to enum and reply in another class.
} else {
System.out.println("meh");
}
}
}
示例11: choosingConnection
import java.nio.channels.Channel; //导入依赖的package包/类
@Override
public Channel choosingConnection() {
try {
selector.select(); // stop here!
//System.out.println("Selector chose something!");
Set<SelectionKey> keys = selector.selectedKeys();
for (Iterator<SelectionKey> i = keys.iterator(); i.hasNext();) {
SelectionKey key = (SelectionKey) i.next();
i.remove();
Channel c = (Channel) key.channel();
if (key.isAcceptable() && c == tcpChannel)
return tcpChannel;
else if (key.isReadable() && c == udpChannel)
return udpChannel;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例12: close
import java.nio.channels.Channel; //导入依赖的package包/类
/**
* Closes a channel. Channel can be null and any IOException's will be swallowed.
*
* @param channel The stream to close.
*/
public static void close( Channel channel )
{
if ( channel == null )
{
return;
}
try
{
channel.close();
}
catch ( IOException ex )
{
// ignore
}
}
示例13: doReleaseExclusiveReadLock
import java.nio.channels.Channel; //导入依赖的package包/类
@Override
protected void doReleaseExclusiveReadLock(GenericFileOperations<File> operations,
GenericFile<File> file, Exchange exchange) throws Exception {
// must call super
super.doReleaseExclusiveReadLock(operations, file, exchange);
FileLock lock = exchange.getProperty(asReadLockKey(file, Exchange.FILE_LOCK_EXCLUSIVE_LOCK), FileLock.class);
RandomAccessFile rac = exchange.getProperty(asReadLockKey(file, Exchange.FILE_LOCK_EXCLUSIVE_LOCK), RandomAccessFile.class);
String target = file.getFileName();
if (lock != null) {
Channel channel = lock.acquiredBy();
try {
lock.release();
} finally {
// close channel as well
IOHelper.close(channel, "while releasing exclusive read lock for file: " + target, LOG);
IOHelper.close(rac, "while releasing exclusive read lock for file: " + target, LOG);
}
}
}
示例14: closeHard
import java.nio.channels.Channel; //导入依赖的package包/类
/**
* This should be followed by closing a selector.
*
* @param channel the channel to close.
*/
public static void closeHard(final Channel channel) {
if (channel != null) {
try {
// Close socket
if (channel instanceof SocketChannel) {
closeHard(((SocketChannel) channel).socket());
}
// Close channel
channel.close();
} catch (final Exception e) {
ignoreException(e, "closing hard");
}
}
}
示例15: test
import java.nio.channels.Channel; //导入依赖的package包/类
@Test
public void test(){
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
byteBuffer.put((byte) 'a');
byteBuffer.put((byte) 'b');
byteBuffer.put((byte) 'c');
Channel channel = Channels.newChannel(new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
});
CharBuffer charBuffer = CharBuffer.allocate(10);
charBuffer.put('a');
charBuffer.put('b');
charBuffer.put('c');
System.out.println(charBuffer.get());
}