Search in sources :

Example 71 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project mapdb by jankotek.

the class FileChannelVol method writeFully.

protected void writeFully(long offset, ByteBuffer buf) {
    int remaining = buf.limit() - buf.position();
    if (CC.VOLUME_PRINT_STACK_AT_OFFSET != 0 && CC.VOLUME_PRINT_STACK_AT_OFFSET >= offset && CC.VOLUME_PRINT_STACK_AT_OFFSET <= offset + remaining) {
        new IOException("VOL STACK:").printStackTrace();
    }
    try {
        while (remaining > 0) {
            int write = channel.write(buf, offset);
            if (write < 0)
                throw new EOFException();
            remaining -= write;
        }
    } catch (ClosedByInterruptException e) {
        throw new DBException.VolumeClosedByInterrupt(e);
    } catch (ClosedChannelException e) {
        throw new DBException.VolumeClosed(e);
    } catch (IOException e) {
        throw new DBException.VolumeIOException(e);
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) DBException(org.mapdb.DBException) ClosedChannelException(java.nio.channels.ClosedChannelException) EOFException(java.io.EOFException) IOException(java.io.IOException)

Example 72 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project evosuite by EvoSuite.

the class EvoFileChannel method write.

private int write(ByteBuffer[] srcs, int offset, int length, AtomicInteger posToUpdate) throws IOException {
    if (!isOpenForWrite) {
        throw new NonWritableChannelException();
    }
    if ((offset < 0) || (offset > srcs.length) || (length < 0) || (length > srcs.length - offset)) {
        throw new IndexOutOfBoundsException();
    }
    throwExceptionIfClosed();
    int counter = 0;
    byte[] buffer = new byte[1];
    synchronized (readWriteMonitor) {
        for (int j = offset; j < length; j++) {
            ByteBuffer src = srcs[j];
            int r = src.remaining();
            for (int i = 0; i < r; i++) {
                byte b = src.get();
                buffer[0] = b;
                NativeMockedIO.writeBytes(path, posToUpdate, buffer, 0, 1);
                counter++;
                if (closed) {
                    throw new AsynchronousCloseException();
                }
                if (Thread.currentThread().isInterrupted()) {
                    close();
                    throw new ClosedByInterruptException();
                }
            }
        }
    }
    return counter;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) NonWritableChannelException(java.nio.channels.NonWritableChannelException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 73 with ClosedByInterruptException

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

the class SystemOperation method exec.

public static String exec(String cmd) throws IOException {
    LOG.debug("Shell cmd: {}", cmd);
    Process process = new ProcessBuilder(new String[] { "/bin/bash", "-c", cmd }).start();
    try {
        process.waitFor();
        String output = IOUtils.toString(process.getInputStream());
        String errorOutput = IOUtils.toString(process.getErrorStream());
        LOG.debug("Shell Output: {}", output);
        if (errorOutput.length() != 0) {
            LOG.error("Shell Error Output: {}", errorOutput);
            throw new IOException(errorOutput);
        }
        return output;
    } catch (InterruptedException | ClosedByInterruptException ie) {
        throw new IOException(ie);
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) IOException(java.io.IOException)

Example 74 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project canal by alibaba.

the class BioSocketChannel method read.

public byte[] read(int readSize, int timeout) throws IOException {
    InputStream input = this.input;
    byte[] data = new byte[readSize];
    int remain = readSize;
    int accTimeout = 0;
    if (input == null) {
        throw new SocketException("Socket already closed.");
    }
    while (remain > 0 && accTimeout < timeout) {
        try {
            int read = input.read(data, readSize - remain, remain);
            if (read > -1) {
                remain -= read;
            } else {
                throw new IOException("EOF encountered.");
            }
        } catch (SocketTimeoutException te) {
            if (Thread.interrupted()) {
                throw new ClosedByInterruptException();
            }
            accTimeout += SO_TIMEOUT;
        }
    }
    if (remain > 0 && accTimeout >= timeout) {
        throw new SocketTimeoutException("Timeout occurred, failed to read total " + readSize + " bytes in " + timeout + " milliseconds, actual read only " + (readSize - remain) + " bytes");
    }
    return data;
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 75 with ClosedByInterruptException

use of java.nio.channels.ClosedByInterruptException in project canal by alibaba.

the class DirectLogFetcher method fetch.

/**
 * {@inheritDoc}
 *
 * @see com.taobao.tddl.dbsync.binlog.LogFetcher#fetch()
 */
public boolean fetch() throws IOException {
    try {
        // Fetching packet header from input.
        if (!fetch0(0, NET_HEADER_SIZE)) {
            logger.warn("Reached end of input stream while fetching header");
            return false;
        }
        // Fetching the first packet(may a multi-packet).
        int netlen = getUint24(PACKET_LEN_OFFSET);
        int netnum = getUint8(PACKET_SEQ_OFFSET);
        if (!fetch0(NET_HEADER_SIZE, netlen)) {
            logger.warn("Reached end of input stream: packet #" + netnum + ", len = " + netlen);
            return false;
        }
        // Detecting error code.
        final int mark = getUint8(NET_HEADER_SIZE);
        if (mark != 0) {
            if (// error from master
            mark == 255) {
                // Indicates an error, for example trying to fetch from
                // wrong
                // binlog position.
                position = NET_HEADER_SIZE + 1;
                final int errno = getInt16();
                String sqlstate = forward(1).getFixString(SQLSTATE_LENGTH);
                String errmsg = getFixString(limit - position);
                throw new IOException("Received error packet:" + " errno = " + errno + ", sqlstate = " + sqlstate + " errmsg = " + errmsg);
            } else if (mark == 254) {
                // Indicates end of stream. It's not clear when this would
                // be sent.
                logger.warn("Received EOF packet from server, apparent" + " master disconnected. It's may be duplicate slaveId , check instance config");
                return false;
            } else {
                // Should not happen.
                throw new IOException("Unexpected response " + mark + " while fetching binlog: packet #" + netnum + ", len = " + netlen);
            }
        }
        // if mysql is in semi mode
        if (issemi) {
            // parse semi mark
            int semimark = getUint8(NET_HEADER_SIZE + 1);
            int semival = getUint8(NET_HEADER_SIZE + 2);
            this.semival = semival;
        }
        // The first packet is a multi-packet, concatenate the packets.
        while (netlen == MAX_PACKET_LENGTH) {
            if (!fetch0(0, NET_HEADER_SIZE)) {
                logger.warn("Reached end of input stream while fetching header");
                return false;
            }
            netlen = getUint24(PACKET_LEN_OFFSET);
            netnum = getUint8(PACKET_SEQ_OFFSET);
            if (!fetch0(limit, netlen)) {
                logger.warn("Reached end of input stream: packet #" + netnum + ", len = " + netlen);
                return false;
            }
        }
        // Preparing buffer variables to decoding.
        if (issemi) {
            origin = NET_HEADER_SIZE + 3;
        } else {
            origin = NET_HEADER_SIZE + 1;
        }
        position = origin;
        limit -= origin;
        return true;
    } catch (SocketTimeoutException e) {
        close();
        /* Do cleanup */
        logger.error("Socket timeout expired, closing connection", e);
        throw e;
    } catch (InterruptedIOException | ClosedByInterruptException e) {
        close();
        /* Do cleanup */
        logger.info("I/O interrupted while reading from client socket", e);
        throw e;
    } catch (IOException e) {
        close();
        /* Do cleanup */
        logger.error("I/O error while reading from client socket", e);
        throw e;
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) InterruptedIOException(java.io.InterruptedIOException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException)

Aggregations

ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)81 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