Example usage for java.io InterruptedIOException InterruptedIOException

List of usage examples for java.io InterruptedIOException InterruptedIOException

Introduction

In this page you can find the example usage for java.io InterruptedIOException InterruptedIOException.

Prototype

public InterruptedIOException(String s) 

Source Link

Document

Constructs an InterruptedIOException with the specified detail message.

Usage

From source file:TimedSocket.java

/**
 * Attempts to connect to a service at the specified address
 * and port, for a specified maximum amount of time.
 *
 * @param addr  Address of host//w ww . java 2 s.c o m
 * @param port  Port of service
 * @param delay Delay in milliseconds
 */
public static Socket getSocket(InetAddress addr, int port, int delay)
        throws InterruptedIOException, IOException {
    // Create a new socket thread, and start it running
    SocketThread st = new SocketThread(addr, port);
    st.start();

    int timer = 0;
    Socket sock = null;

    for (;;) {
        // Check to see if a connection is established

        if (st.isConnected()) {
            // Yes ...  assign to sock variable, and break out of loop
            sock = st.getSocket();
            break;
        } else {
            // Check to see if an error occurred
            if (st.isError()) {
                // No connection could be established
                throw (st.getException());
            }

            try {
                // Sleep for a short period of time
                Thread.sleep(POLL_DELAY);
            } catch (InterruptedException ie) {
            }

            // Increment timer
            timer += POLL_DELAY;

            // Check to see if time limit exceeded
            if (timer > delay) {
                // Can't connect to server
                throw new InterruptedIOException("Could not connect for " + delay + " milliseconds");
            }
        }
    }

    return sock;
}

From source file:com.creditease.utilframe.http.client.entity.FileUploadEntity.java

@SuppressWarnings("resource")
@Override/*ww w  .  ja v  a2  s  .c  o m*/
public void writeTo(OutputStream outStream) throws IOException {
    if (outStream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }
    BufferedInputStream inStream = null;
    try {
        inStream = new BufferedInputStream(new FileInputStream(this.file));
        byte[] tmp = new byte[4096];
        int len;
        while ((len = inStream.read(tmp)) != -1) {
            outStream.write(tmp, 0, len);
            uploadedSize += len;
            if (callBackHandler != null) {
                if (!callBackHandler.updateProgress(fileSize, uploadedSize, false)) {
                    IOUtils.closeQuietly(inStream);
                    throw new InterruptedIOException("cancel");
                }
            }
        }
        outStream.flush();
        if (callBackHandler != null) {
            callBackHandler.updateProgress(fileSize, uploadedSize, true);
        }
    } finally {
        IOUtils.closeQuietly(inStream);
    }
}

From source file:com.dongfang.net.http.client.entity.FileUploadEntity.java

@Override
public void writeTo(OutputStream outStream) throws IOException {
    if (outStream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }//from   w  ww  .j  av  a2 s  . c om
    InputStream inStream = null;
    try {
        inStream = new FileInputStream(this.file);
        byte[] tmp = new byte[4096];
        int len;
        while ((len = inStream.read(tmp)) != -1) {
            outStream.write(tmp, 0, len);
            uploadedSize += len;
            if (callBackHandler != null) {
                if (!callBackHandler.updateProgress(fileSize, uploadedSize, false)) {
                    throw new InterruptedIOException("stop");
                }
            }
        }
        outStream.flush();
        if (callBackHandler != null) {
            callBackHandler.updateProgress(fileSize, uploadedSize, true);
        }
    } finally {
        IOUtils.closeQuietly(inStream);
    }
}

From source file:cn.isif.util_plus.http.client.entity.FileUploadEntity.java

@Override
public void writeTo(OutputStream outStream) throws IOException {
    if (outStream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }//ww  w  .  j  a  v  a 2 s . com
    BufferedInputStream inStream = null;
    try {
        inStream = new BufferedInputStream(new FileInputStream(this.file));
        byte[] tmp = new byte[4096];
        int len;
        while ((len = inStream.read(tmp)) != -1) {
            outStream.write(tmp, 0, len);
            uploadedSize += len;
            if (callBackHandler != null) {
                if (!callBackHandler.updateProgress(fileSize, uploadedSize, false)) {
                    throw new InterruptedIOException("cancel");
                }
            }
        }
        outStream.flush();
        if (callBackHandler != null) {
            callBackHandler.updateProgress(fileSize, uploadedSize, true);
        }
    } finally {
        IOUtils.closeQuietly(inStream);
    }
}

From source file:com.dongfang.net.http.client.entity.InputStreamUploadEntity.java

public void writeTo(final OutputStream outStream) throws IOException {
    if (outStream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }// w  w w .  ja va  2s.c  o  m
    InputStream inStream = this.content;
    try {
        byte[] buffer = new byte[BUFFER_SIZE];
        int l;
        if (this.length < 0) {
            // consume until EOF
            while ((l = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, l);
                uploadedSize += l;
                if (callBackHandler != null) {
                    if (!callBackHandler.updateProgress(uploadedSize + 1, uploadedSize, false)) {
                        throw new InterruptedIOException("stop");
                    }
                }
            }
        } else {
            // consume no more than length
            long remaining = this.length;
            while (remaining > 0) {
                l = inStream.read(buffer, 0, (int) Math.min(BUFFER_SIZE, remaining));
                if (l == -1) {
                    break;
                }
                outStream.write(buffer, 0, l);
                remaining -= l;
                uploadedSize += l;
                if (callBackHandler != null) {
                    if (!callBackHandler.updateProgress(length, uploadedSize, false)) {
                        throw new InterruptedIOException("stop");
                    }
                }
            }
        }
        outStream.flush();
        if (callBackHandler != null) {
            callBackHandler.updateProgress(length, uploadedSize, true);
        }
    } finally {
        IOUtils.closeQuietly(inStream);
    }
}

From source file:cn.isif.util_plus.http.client.entity.InputStreamUploadEntity.java

public void writeTo(final OutputStream outStream) throws IOException {
    if (outStream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }/*from w w  w.java 2 s.c  o  m*/
    InputStream inStream = this.content;
    try {
        byte[] buffer = new byte[BUFFER_SIZE];
        int l;
        if (this.length < 0) {
            // consume until EOF
            while ((l = inStream.read(buffer)) != -1) {
                outStream.write(buffer, 0, l);
                uploadedSize += l;
                if (callBackHandler != null) {
                    if (!callBackHandler.updateProgress(uploadedSize + 1, uploadedSize, false)) {
                        throw new InterruptedIOException("cancel");
                    }
                }
            }
        } else {
            // consume no more than length
            long remaining = this.length;
            while (remaining > 0) {
                l = inStream.read(buffer, 0, (int) Math.min(BUFFER_SIZE, remaining));
                if (l == -1) {
                    break;
                }
                outStream.write(buffer, 0, l);
                remaining -= l;
                uploadedSize += l;
                if (callBackHandler != null) {
                    if (!callBackHandler.updateProgress(length, uploadedSize, false)) {
                        throw new InterruptedIOException("cancel");
                    }
                }
            }
        }
        outStream.flush();
        if (callBackHandler != null) {
            callBackHandler.updateProgress(length, uploadedSize, true);
        }
    } finally {
        IOUtils.closeQuietly(inStream);
    }
}

From source file:org.apache.hadoop.hbase.util.DrainableQueue.java

/**
 * Enqueue an event if we are still in the "deferred processing" mode for this queue. Even if
 * we have already started draining the queue, we still enqueue events if the draining process
 * has not completed./*from  w w  w. j av a2  s  .  c  o  m*/
 */
public synchronized boolean enqueue(T event) throws InterruptedIOException {
    if (canEnqueue()) {
        try {
            queue.put(event);
        } catch (InterruptedException ex) {
            String msg = "Could not add event to queue " + name;
            LOG.error(msg, ex);
            throw new InterruptedIOException(msg + ": " + ex.getMessage());
        }
        // Enqueued the event.
        return true;
    }
    // Event not accepted, tell the caller to process it in real time.
    return false;
}

From source file:gridool.util.net.SocketUtils.java

/**
 * @param connectTimeout A timeout of zero is interpreted as an infinite timeout.
 * @param pollDelay sleep in mills before retry.
 * @param maxRetry No-retry if the value is 1.
 *//*from ww w .j a v  a 2  s.co  m*/
public static Socket openSocket(Socket socket, SocketAddress sockAddr, int connectTimeout, long pollDelay,
        int maxRetry) throws IOException {
    assert (sockAddr != null);
    assert (pollDelay >= 0) : pollDelay;
    assert (maxRetry > 0) : maxRetry;
    for (int i = 0; i < maxRetry; i++) {
        try {
            socket.connect(sockAddr, connectTimeout);
        } catch (SocketTimeoutException ste) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Socket.connect to " + sockAddr + " timeout #" + (i + 1));
            }
        } catch (IOException e) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Socket.connect to " + sockAddr + " failed #" + (i + 1), e);
            }
        } catch (Throwable e) {
            LOG.fatal("failed to connect: " + sockAddr, e);
            throw new IOException(e);
        }
        if (socket.isConnected()) {
            return socket;
        }
        if (pollDelay > 0) {
            try {
                Thread.sleep(pollDelay);
            } catch (InterruptedException ie) {
                ;
            }
        }
    }
    throw new InterruptedIOException("Could not connect to " + sockAddr);
}

From source file:cn.isif.util_plus.http.client.entity.DecompressingEntity.java

/**
 * {@inheritDoc}//from   w w  w . ja va2  s.  c om
 */
@Override
public void writeTo(OutputStream outStream) throws IOException {
    if (outStream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }
    InputStream inStream = null;
    try {
        inStream = getContent();

        byte[] tmp = new byte[4096];
        int len;
        while ((len = inStream.read(tmp)) != -1) {
            outStream.write(tmp, 0, len);
            uploadedSize += len;
            if (callBackHandler != null) {
                if (!callBackHandler.updateProgress(uncompressedLength, uploadedSize, false)) {
                    throw new InterruptedIOException("cancel");
                }
            }
        }
        outStream.flush();
        if (callBackHandler != null) {
            callBackHandler.updateProgress(uncompressedLength, uploadedSize, true);
        }
    } finally {
        IOUtils.closeQuietly(inStream);
    }
}

From source file:com.example.base.http.DecompressingEntity.java

/**
 * {@inheritDoc}//from  w w w  .  j a va2s .co m
 */
@Override
public void writeTo(OutputStream outStream) throws IOException {
    if (outStream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }
    InputStream inStream = null;
    try {
        inStream = getContent();

        byte[] tmp = new byte[4096];
        int len;
        while ((len = inStream.read(tmp)) != -1) {
            outStream.write(tmp, 0, len);
            uploadedSize += len;
            if (callBackHandler != null) {
                if (!callBackHandler.updateProgress(uncompressedLength, uploadedSize, false)) {
                    throw new InterruptedIOException("stop");
                }
            }
        }
        outStream.flush();
        if (callBackHandler != null) {
            callBackHandler.updateProgress(uncompressedLength, uploadedSize, true);
        }
    } finally {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (Throwable e) {
            }
        }
    }
}