当前位置: 首页>>代码示例>>Java>>正文


Java IoUtils.throwInterruptedIoException方法代码示例

本文整理汇总了Java中libcore.io.IoUtils.throwInterruptedIoException方法的典型用法代码示例。如果您正苦于以下问题:Java IoUtils.throwInterruptedIoException方法的具体用法?Java IoUtils.throwInterruptedIoException怎么用?Java IoUtils.throwInterruptedIoException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在libcore.io.IoUtils的用法示例。


在下文中一共展示了IoUtils.throwInterruptedIoException方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: receive

import libcore.io.IoUtils; //导入方法依赖的package包/类
/**
 * Receives a char and stores it into the PipedReader. This called by
 * PipedWriter.write() when writes occur.
 * <P>
 * If the buffer is full and the thread sending #receive is interrupted, the
 * InterruptedIOException will be thrown.
 *
 * @param oneChar
 *            the char to store into the pipe.
 *
 * @throws IOException
 *             If the stream is already closed or another IOException
 *             occurs.
 */
synchronized void receive(char oneChar) throws IOException {
    if (buffer == null) {
        throw new IOException("Pipe is closed");
    }
    if (lastReader != null && !lastReader.isAlive()) {
        throw new IOException("Pipe broken");
    }
    /*
    * Set the last thread to be writing on this PipedWriter. If
    * lastWriter dies while someone is waiting to read an IOException
    * of "Pipe broken" will be thrown in read()
    */
    lastWriter = Thread.currentThread();
    try {
        while (buffer != null && out == in) {
            notifyAll();
            wait(1000);
            if (lastReader != null && !lastReader.isAlive()) {
                throw new IOException("Pipe broken");
            }
        }
    } catch (InterruptedException e) {
        IoUtils.throwInterruptedIoException();
    }
    if (buffer == null) {
        throw new IOException("Pipe is closed");
    }
    if (in == -1) {
        in = 0;
    }
    buffer[in++] = oneChar;
    if (in == buffer.length) {
        in = 0;
    }
}
 
开发者ID:Sellegit,项目名称:j2objc,代码行数:50,代码来源:PipedReader.java

示例2: receive

import libcore.io.IoUtils; //导入方法依赖的package包/类
/**
 * Receives a byte and stores it in this stream's {@code buffer}. This
 * method is called by {@link PipedOutputStream#write(int)}. The least
 * significant byte of the integer {@code oneByte} is stored at index
 * {@code in} in the {@code buffer}.
 * <p>
 * This method blocks as long as {@code buffer} is full.
 *
 * @param oneByte
 *            the byte to store in this pipe.
 * @throws InterruptedIOException
 *             if the {@code buffer} is full and the thread that has called
 *             this method is interrupted.
 * @throws IOException
 *             if this stream is closed or the thread that has last read
 *             from this stream is no longer alive.
 */
protected synchronized void receive(int oneByte) throws IOException {
    if (buffer == null || isClosed) {
        throw new IOException("Pipe is closed");
    }
    if (lastReader != null && !lastReader.isAlive()) {
        throw new IOException("Pipe broken");
    }

    /*
     * Set the last thread to be writing on this PipedInputStream. If
     * lastWriter dies while someone is waiting to read an IOException of
     * "Pipe broken" will be thrown in read()
     */
    lastWriter = Thread.currentThread();
    try {
        while (buffer != null && out == in) {
            if (lastReader != null && !lastReader.isAlive()) {
                throw new IOException("Pipe broken");
            }
            notifyAll();
            wait(1000);
        }
    } catch (InterruptedException e) {
        IoUtils.throwInterruptedIoException();
    }
    if (buffer == null) {
        throw new IOException("Pipe is closed");
    }
    if (in == -1) {
        in = 0;
    }
    buffer[in++] = (byte) oneByte;
    if (in == buffer.length) {
        in = 0;
    }

    // let blocked readers read the newly available data
    notifyAll();
}
 
开发者ID:Sellegit,项目名称:j2objc,代码行数:57,代码来源:PipedInputStream.java

示例3: read

import libcore.io.IoUtils; //导入方法依赖的package包/类
/**
 * Reads a single byte from this stream and returns it as an integer in the
 * range from 0 to 255. Returns -1 if the end of this stream has been
 * reached. If there is no data in the pipe, this method blocks until data
 * is available, the end of the stream is detected or an exception is
 * thrown.
 * <p>
 * Separate threads should be used to read from a {@code PipedInputStream}
 * and to write to the connected {@link PipedOutputStream}. If the same
 * thread is used, a deadlock may occur.
 *
 * @return the byte read or -1 if the end of the source stream has been
 *         reached.
 * @throws IOException
 *             if this stream is closed or not connected to an output
 *             stream, or if the thread writing to the connected output
 *             stream is no longer alive.
 */
@Override
public synchronized int read() throws IOException {
    if (!isConnected) {
        throw new IOException("Not connected");
    }
    if (buffer == null) {
        throw new IOException("InputStream is closed");
    }

    if (isClosed && in == -1) {
        // write end closed and no more need to read
        return -1;
    }

    /**
     * Set the last thread to be reading on this PipedInputStream. If
     * lastReader dies while someone is waiting to write an IOException of
     * "Pipe broken" will be thrown in receive()
     */
    lastReader = Thread.currentThread();
    try {
        int attempts = 3;
        while (in == -1) {
            // Are we at end of stream?
            if (isClosed) {
                return -1;
            }
            if ((attempts-- <= 0) && lastWriter != null && !lastWriter.isAlive()) {
                throw new IOException("Pipe broken");
            }
            // Notify callers of receive()
            notifyAll();
            wait(1000);
        }
    } catch (InterruptedException e) {
        IoUtils.throwInterruptedIoException();
    }

    int result = buffer[out++] & 0xff;
    if (out == buffer.length) {
        out = 0;
    }
    if (out == in) {
        // empty buffer
        in = -1;
        out = 0;
    }

    // let blocked writers write to the newly available buffer space
    notifyAll();

    return result;
}
 
开发者ID:Sellegit,项目名称:j2objc,代码行数:72,代码来源:PipedInputStream.java


注:本文中的libcore.io.IoUtils.throwInterruptedIoException方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。