本文整理匯總了Java中java.nio.channels.ClosedChannelException類的典型用法代碼示例。如果您正苦於以下問題:Java ClosedChannelException類的具體用法?Java ClosedChannelException怎麽用?Java ClosedChannelException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ClosedChannelException類屬於java.nio.channels包,在下文中一共展示了ClosedChannelException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: Client
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
public Client(Selector selector, SocketChannel clientChannel, CloseListener<Client> closeListener) throws ClosedChannelException {
id = nextId++;
this.clientChannel = clientChannel;
router = new Router(this, selector);
pendingIdBuffer = createIntBuffer(id);
SelectionHandler selectionHandler = (selectionKey) -> {
if (selectionKey.isValid() && selectionKey.isWritable()) {
processSend();
}
if (selectionKey.isValid() && selectionKey.isReadable()) {
processReceive();
}
if (selectionKey.isValid()) {
updateInterests();
}
};
// on start, we are interested only in writing (we must first send the client id)
interests = SelectionKey.OP_WRITE;
selectionKey = clientChannel.register(selector, interests, selectionHandler);
this.closeListener = closeListener;
}
示例2: setOption
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
@Override
public <T> SctpServerChannel setOption(SctpSocketOption<T> name, T value)
throws IOException {
if (name == null)
throw new NullPointerException();
if (!supportedOptions().contains(name))
throw new UnsupportedOperationException("'" + name + "' not supported");
synchronized (stateLock) {
if (!isOpen())
throw new ClosedChannelException();
SctpNet.setSocketOption(fdVal, name, value, 0 /*oneToOne*/);
return this;
}
}
示例3: getRemoteAddresses
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
@Override
public Set<SocketAddress> getRemoteAddresses(Association association)
throws IOException {
synchronized (stateLock) {
checkAssociation(association);
if (!isOpen())
throw new ClosedChannelException();
try {
return SctpNet.getRemoteAddresses(fdVal, association.associationID());
} catch (SocketException se) {
/* a valid association should always have remote addresses */
Set<SocketAddress> addrs = associationMap.get(association);
return addrs != null ? addrs : Collections.<SocketAddress>emptySet();
}
}
}
示例4: setBytes
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
ByteBuffer buf = ByteBuffer.wrap(array, index, length);
int readBytes = 0;
do {
int localReadBytes;
try {
localReadBytes = in.read(buf);
} catch (ClosedChannelException e) {
localReadBytes = -1;
}
if (localReadBytes < 0) {
if (readBytes == 0) {
return -1;
} else {
break;
}
} else if (localReadBytes == 0) {
break;
}
readBytes += localReadBytes;
} while (readBytes < length);
return readBytes;
}
示例5: transferTo
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
public long transferTo(long position, long count,
WritableByteChannel target)
throws IOException
{
ensureOpen();
if (!target.isOpen())
throw new ClosedChannelException();
if (!readable)
throw new NonReadableChannelException();
if (target instanceof FileChannelImpl &&
!((FileChannelImpl)target).writable)
throw new NonWritableChannelException();
if ((position < 0) || (count < 0))
throw new IllegalArgumentException();
long sz = size();
if (position > sz)
return 0;
int icount = (int)Math.min(count, Integer.MAX_VALUE);
if ((sz - position) < icount)
icount = (int)(sz - position);
// Slow path for untrusted targets
return transferToArbitraryChannel(position, icount, target);
}
示例6: exceptionCaught
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
Channel ch = e.getChannel();
Throwable cause = e.getCause();
if (cause instanceof TooLongFrameException) {
sendError(ctx, BAD_REQUEST);
return;
} else if (cause instanceof IOException) {
if (cause instanceof ClosedChannelException) {
LOG.debug("Ignoring closed channel error", cause);
return;
}
String message = String.valueOf(cause.getMessage());
if (IGNORABLE_ERROR_MESSAGE.matcher(message).matches()) {
LOG.debug("Ignoring client socket close", cause);
return;
}
}
LOG.error("Shuffle error: ", cause);
if (ch.isConnected()) {
LOG.error("Shuffle error " + e);
sendError(ctx, INTERNAL_SERVER_ERROR);
}
}
示例7: setOption
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
@Override
public <T> SctpChannel setOption(SctpSocketOption<T> name, T value)
throws IOException {
if (name == null)
throw new NullPointerException();
if (!supportedOptions().contains(name))
throw new UnsupportedOperationException("'" + name + "' not supported");
synchronized (stateLock) {
if (!isOpen())
throw new ClosedChannelException();
SctpNet.setSocketOption(fdVal, name, value, 0 /*oneToOne*/);
}
return this;
}
示例8: branch
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
@Override
public SctpChannel branch(Association association)
throws IOException {
synchronized (stateLock) {
checkAssociation(association);
if (!isOpen())
throw new ClosedChannelException();
FileDescriptor bFd = SctpNet.branch(fdVal,
association.associationID());
/* successfully branched, we can now remove it from assoc list */
removeAssociation(association);
return new SctpChannelImpl(provider(), bFd, association);
}
}
示例9: handleWriteTimeout
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
private void handleWriteTimeout(final long ret) throws IOException {
if (!connection.isOpen()) {
return;
}
if (ret == 0 && handle != null) {
return;
}
Integer timeout = getTimeout();
if (timeout == null || timeout <= 0) {
return;
}
long currentTime = System.currentTimeMillis();
long expireTimeVar = expireTime;
if (expireTimeVar != -1 && currentTime > expireTimeVar) {
IoUtils.safeClose(connection);
throw new ClosedChannelException();
}
expireTime = currentTime + timeout;
XnioExecutor.Key key = handle;
if (key == null) {
handle = connection.getIoThread().executeAfter(timeoutCommand, timeout, TimeUnit.MILLISECONDS);
}
}
示例10: exceptionCaught
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
{
// Stop the epic channel closed spam at close
if (!(cause instanceof ClosedChannelException))
{
// Mute the reset by peer exception - it's disconnection noise
if (cause.getMessage().contains("Connection reset by peer"))
{
FMLLog.log(Level.DEBUG, cause, "Muted NetworkDispatcher exception");
}
else
{
FMLLog.log(Level.ERROR, cause, "NetworkDispatcher exception");
}
}
super.exceptionCaught(ctx, cause);
}
示例11: handleIdleTimeout
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
private void handleIdleTimeout() throws ClosedChannelException {
if(timedOut) {
return;
}
long idleTimeout = this.idleTimeout;
if(idleTimeout <= 0) {
return;
}
long currentTime = System.currentTimeMillis();
long expireTimeVar = expireTime;
if(expireTimeVar != -1 && currentTime > expireTimeVar) {
timedOut = true;
doClose();
throw new ClosedChannelException();
}
expireTime = currentTime + idleTimeout;
XnioExecutor.Key key = handle;
if (key == null) {
handle = sink.getWriteThread().executeAfter(timeoutCommand, idleTimeout, TimeUnit.MILLISECONDS);
}
}
示例12: write
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
@Override
public long write(final ByteBuffer[] srcs, final int offset, final int length) throws IOException {
if (anyAreSet(state, SHUTDOWN | CLOSED) || currentBuffer == null) {
throw new ClosedChannelException();
}
try {
int total = 0;
for (int i = offset; i < offset + length; ++i) {
if (srcs[i].hasRemaining()) {
int ret = write(srcs[i]);
total += ret;
if (ret == 0) {
return total;
}
}
}
return total;
} catch (IOException e) {
freeBuffer();
throw e;
}
}
示例13: getOption
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
@Override
@SuppressWarnings("unchecked")
public <T> T getOption(SctpSocketOption<T> name, Association association)
throws IOException {
if (name == null)
throw new NullPointerException();
if (!supportedOptions().contains(name))
throw new UnsupportedOperationException("'" + name + "' not supported");
synchronized (stateLock) {
if (association != null && (name.equals(SCTP_PRIMARY_ADDR) ||
name.equals(SCTP_SET_PEER_PRIMARY_ADDR))) {
checkAssociation(association);
}
if (!isOpen())
throw new ClosedChannelException();
int assocId = association == null ? 0 : association.associationID();
return (T)SctpNet.getSocketOption(fdVal, name, assocId);
}
}
示例14: transferFrom
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
public long transferFrom(final FileChannel src, final long position, final long count) throws IOException {
if (count == 0L) return 0L;
long val = state;
if (allAreSet(val, FLAG_CLOSE_REQUESTED)) {
throw new ClosedChannelException();
}
if (allAreClear(val, MASK_COUNT)) {
throw new FixedLengthOverflowException();
}
long res = 0L;
try {
return res = next.transferFrom(src, position, min(count, (val & MASK_COUNT)));
} catch (IOException e) {
broken = true;
throw e;
} finally {
exitWrite(val, res);
}
}
示例15: transferFrom
import java.nio.channels.ClosedChannelException; //導入依賴的package包/類
public long transferFrom(ReadableByteChannel src,
long position, long count)
throws IOException
{
ensureOpen();
if (!src.isOpen())
throw new ClosedChannelException();
if (!writable)
throw new NonWritableChannelException();
if ((position < 0) || (count < 0))
throw new IllegalArgumentException();
if (position > size())
return 0;
if (src instanceof FileChannelImpl)
return transferFromFileChannel((FileChannelImpl)src,
position, count);
return transferFromArbitraryChannel(src, position, count);
}