快速业务通道

彻底转变流,第2部分:优化Java内部I/O - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-16
管道被破坏(也就是说,读程序线程或写程序线程已死亡),异常被抛出。

清单 14. 检查流状态

private void exceptionCheck () throws IOException {    // throw any pending exception    if (exception != null) {     IOException ex = exception;     exception = null;     throw ex; // could wrap ex in a local exception    }   }   private void closedCheck () throws IOException {    // throw an exception if the pipe is closed    if (closed)     throw new IOException ("Stream closed");   }   private void brokenCheck (boolean rw) throws IOException {    // get a reference to the peer thread    Thread thread = (rw == WRITER) ? reader : writer;    // throw an exception if the peer thread has died    if ((thread != null) && !thread.isAlive ())     throw new IOException ("Broken pipe");   }

彻底转变流,第2部分:优化Java内部I/O(10)

时间:2011-06-21 Merlin Hughes

当数据被写入这个管道时,清单 15 中的方法被调用。总的来说,它类似于 读方法:我们先取得写程序线程的副本,然后检查流是否被关闭,接着进入把数 据复制到管道的循环。和前面一样,该方法使用 checkedAvailable() 方法, checkedAvailable() 自动阻塞,直到管道中有可用的容量。

清单 15. 写数据

private synchronized void writeImpl (byte[] data, int offset, int length)     throws IOException {    // take a reference to the writer thread    if (writer == null)     writer = Thread.currentThread ();    // throw an exception if the stream is closed    if (eof || closed) {     throw new IOException ("Stream closed");    } else {     int written = 0;     try {      // loop to write all the data      do {       // wait for space to become available for writing       int available = checkedAvailable (WRITER);       // calculate amount of contiguous space in pipe buffer       int contiguous = capacity - (writex % capacity);       // calculate how much we will write this time       int amount = (length > available) ? available : length;       if (amount > contiguous) {       // two array copies needed if space wraps around the buffer end        System.arraycopy (data, offset, buffer, writex % capacity,         contiguous);        System.arraycopy (data, offset + contiguous, buffer, 0,         amount - contiguous);       } else {        // otherwise, one array copy needed        System.arraycopy (data, offset, buffer, writex % capacity,         amount);       }       // update indices with amount of data written       processed (WRITER, amount);       // update amount written by this method       written += amount;      } while (written < length);      // data successfully written     } catch (InterruptedIOException ex) {      // write operation was interrupted; set the bytesTransferred      // exception field to reflect the amount of data written      ex.bytesTransferred = wr

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号