Search in sources :

Example 11 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project nifi by apache.

the class SocketChannelOutputStream method write.

@Override
public void write(final byte[] b, final int off, final int len) throws IOException {
    final ByteBuffer buffer = ByteBuffer.wrap(b, off, len);
    final int timeoutMillis = this.timeout;
    long maxTime = System.currentTimeMillis() + timeoutMillis;
    int bytesWritten;
    long sleepNanos = 1L;
    while (buffer.hasRemaining()) {
        bytesWritten = channel.write(buffer);
        if (bytesWritten == 0) {
            if (System.currentTimeMillis() > maxTime) {
                throw new SocketTimeoutException("Timed out writing to socket");
            }
            try {
                TimeUnit.NANOSECONDS.sleep(sleepNanos);
            } catch (InterruptedException e) {
                close();
                // set the interrupt status
                Thread.currentThread().interrupt();
                // simulate an interrupted blocked write operation
                throw new ClosedByInterruptException();
            }
            sleepNanos = Math.min(sleepNanos * 2, CHANNEL_FULL_WAIT_NANOS);
        } else {
            maxTime = System.currentTimeMillis() + timeoutMillis;
        }
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) SocketTimeoutException(java.net.SocketTimeoutException) ByteBuffer(java.nio.ByteBuffer)

Example 12 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project nifi by apache.

the class SocketChannelOutputStream method write.

@Override
public void write(final int b) throws IOException {
    oneByteBuffer.flip();
    oneByteBuffer.clear();
    oneByteBuffer.put((byte) b);
    oneByteBuffer.flip();
    final int timeoutMillis = this.timeout;
    long maxTime = System.currentTimeMillis() + timeoutMillis;
    int bytesWritten;
    long sleepNanos = 1L;
    while (oneByteBuffer.hasRemaining()) {
        bytesWritten = channel.write(oneByteBuffer);
        if (bytesWritten == 0) {
            if (System.currentTimeMillis() > maxTime) {
                throw new SocketTimeoutException("Timed out writing to socket");
            }
            try {
                TimeUnit.NANOSECONDS.sleep(sleepNanos);
            } catch (InterruptedException e) {
                close();
                // set the interrupt status
                Thread.currentThread().interrupt();
                // simulate an interrupted blocked write operation
                throw new ClosedByInterruptException();
            }
            sleepNanos = Math.min(sleepNanos * 2, CHANNEL_FULL_WAIT_NANOS);
        } else {
            return;
        }
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 13 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project jdk8u_jdk by JetBrains.

the class FileChannelImpl method lock.

public FileLock lock(long position, long size, boolean shared) throws IOException {
    ensureOpen();
    if (shared && !readable)
        throw new NonReadableChannelException();
    if (!shared && !writable)
        throw new NonWritableChannelException();
    FileLockImpl fli = new FileLockImpl(this, position, size, shared);
    FileLockTable flt = fileLockTable();
    flt.add(fli);
    boolean completed = false;
    int ti = -1;
    try {
        begin();
        ti = threads.add();
        if (!isOpen())
            return null;
        int n;
        do {
            n = nd.lock(fd, true, position, size, shared);
        } while ((n == FileDispatcher.INTERRUPTED) && isOpen());
        if (isOpen()) {
            if (n == FileDispatcher.RET_EX_LOCK) {
                assert shared;
                FileLockImpl fli2 = new FileLockImpl(this, position, size, false);
                flt.replace(fli, fli2);
                fli = fli2;
            }
            completed = true;
        }
    } finally {
        if (!completed)
            flt.remove(fli);
        threads.remove(ti);
        try {
            end(completed);
        } catch (ClosedByInterruptException e) {
            throw new FileLockInterruptionException();
        }
    }
    return fli;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) NonReadableChannelException(java.nio.channels.NonReadableChannelException) FileLockInterruptionException(java.nio.channels.FileLockInterruptionException) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Example 14 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project jdk8u_jdk by JetBrains.

the class FileChannelImpl method transferToTrustedChannel.

private long transferToTrustedChannel(long position, long count, WritableByteChannel target) throws IOException {
    boolean isSelChImpl = (target instanceof SelChImpl);
    if (!((target instanceof FileChannelImpl) || isSelChImpl))
        return IOStatus.UNSUPPORTED;
    // Trusted target: Use a mapped buffer
    long remaining = count;
    while (remaining > 0L) {
        long size = Math.min(remaining, MAPPED_TRANSFER_SIZE);
        try {
            MappedByteBuffer dbb = map(MapMode.READ_ONLY, position, size);
            try {
                // ## Bug: Closing this channel will not terminate the write
                int n = target.write(dbb);
                assert n >= 0;
                remaining -= n;
                if (isSelChImpl) {
                    // one attempt to write to selectable channel
                    break;
                }
                assert n > 0;
                position += n;
            } finally {
                unmap(dbb);
            }
        } catch (ClosedByInterruptException e) {
            // to be thrown after closing this channel.
            assert !target.isOpen();
            try {
                close();
            } catch (Throwable suppressed) {
                e.addSuppressed(suppressed);
            }
            throw e;
        } catch (IOException ioe) {
            // Only throw exception if no bytes have been written
            if (remaining == count)
                throw ioe;
            break;
        }
    }
    return count - remaining;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) MappedByteBuffer(java.nio.MappedByteBuffer) IOException(java.io.IOException)

Example 15 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project geode by apache.

the class TCPConduit method run.

/**
   * this is the server socket listener thread's run loop
   */
public void run() {
    ConnectionTable.threadWantsSharedResources();
    if (logger.isTraceEnabled(LogMarker.DM)) {
        logger.trace(LogMarker.DM, "Starting P2P Listener on  {}", id);
    }
    for (; ; ) {
        SystemFailure.checkFailure();
        if (stopper.isCancelInProgress()) {
            break;
        }
        if (stopped) {
            break;
        }
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        if (stopper.isCancelInProgress()) {
            // part of bug 37271
            break;
        }
        Socket othersock = null;
        try {
            if (this.useNIO) {
                SocketChannel otherChannel = channel.accept();
                othersock = otherChannel.socket();
            } else {
                try {
                    othersock = socket.accept();
                } catch (SSLException ex) {
                    // SW: This is the case when there is a problem in P2P
                    // SSL configuration, so need to exit otherwise goes into an
                    // infinite loop just filling the logs
                    logger.warn(LocalizedMessage.create(LocalizedStrings.TCPConduit_STOPPING_P2P_LISTENER_DUE_TO_SSL_CONFIGURATION_PROBLEM), ex);
                    break;
                }
                socketCreator.configureServerSSLSocket(othersock);
            }
            if (stopped) {
                try {
                    if (othersock != null) {
                        othersock.close();
                    }
                } catch (Exception e) {
                }
                continue;
            }
            acceptConnection(othersock);
        } catch (ClosedByInterruptException cbie) {
        // safe to ignore
        } catch (ClosedChannelException e) {
            // we're dead
            break;
        } catch (CancelException e) {
            break;
        } catch (Exception e) {
            if (!stopped) {
                if (e instanceof SocketException && "Socket closed".equalsIgnoreCase(e.getMessage())) {
                    // safe to ignore; see bug 31156
                    if (!socket.isClosed()) {
                        logger.warn(LocalizedMessage.create(LocalizedStrings.TCPConduit_SERVERSOCKET_THREW_SOCKET_CLOSED_EXCEPTION_BUT_SAYS_IT_IS_NOT_CLOSED), e);
                        try {
                            socket.close();
                            createServerSocket();
                        } catch (IOException ioe) {
                            logger.fatal(LocalizedMessage.create(LocalizedStrings.TCPConduit_UNABLE_TO_CLOSE_AND_RECREATE_SERVER_SOCKET), ioe);
                            // post 5.1.0x, this should force shutdown
                            try {
                                Thread.sleep(5000);
                            } catch (InterruptedException ie) {
                                // Don't reset; we're just exiting the thread
                                logger.info(LocalizedMessage.create(LocalizedStrings.TCPConduit_INTERRUPTED_AND_EXITING_WHILE_TRYING_TO_RECREATE_LISTENER_SOCKETS));
                                return;
                            }
                        }
                    }
                } else {
                    this.stats.incFailedAccept();
                    if (e instanceof IOException && "Too many open files".equals(e.getMessage())) {
                        getConTable().fileDescriptorsExhausted();
                    } else {
                        logger.warn(e.getMessage(), e);
                    }
                }
            }
        // connections.cleanupLowWater();
        }
        if (!stopped && socket.isClosed()) {
            // NOTE: do not check for distributed system closing here. Messaging
            // may need to occur during the closing of the DS or cache
            logger.warn(LocalizedMessage.create(LocalizedStrings.TCPConduit_SERVERSOCKET_CLOSED_REOPENING));
            try {
                createServerSocket();
            } catch (ConnectionException ex) {
                logger.warn(ex.getMessage(), ex);
            }
        }
    }
    if (logger.isTraceEnabled(LogMarker.DM)) {
        logger.debug("Stopped P2P Listener on  {}", id);
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) SocketException(java.net.SocketException) CancelException(org.apache.geode.CancelException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) SocketException(java.net.SocketException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) CancelException(org.apache.geode.CancelException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) SSLException(javax.net.ssl.SSLException)

Aggregations

ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)79 IOException (java.io.IOException)48 ByteBuffer (java.nio.ByteBuffer)15 ClosedChannelException (java.nio.channels.ClosedChannelException)11 SocketTimeoutException (java.net.SocketTimeoutException)9 InetSocketAddress (java.net.InetSocketAddress)7 MappedByteBuffer (java.nio.MappedByteBuffer)7 SocketChannel (java.nio.channels.SocketChannel)7 File (java.io.File)6 ServerSocketChannel (java.nio.channels.ServerSocketChannel)6 ServerSocket (java.net.ServerSocket)5 FileChannel (java.nio.channels.FileChannel)5 FileLockInterruptionException (java.nio.channels.FileLockInterruptionException)5 InterruptedIOException (java.io.InterruptedIOException)4 Path (java.nio.file.Path)4 Test (org.junit.Test)4 BuildId (com.facebook.buck.model.BuildId)3 FileNotFoundException (java.io.FileNotFoundException)3 InputStream (java.io.InputStream)3 SocketException (java.net.SocketException)3