快速业务通道

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

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-16
ecome available for reading      int available = checkedAvailable (READER);      // return -1 on EOF      if (available < 0)       return -1;      // calculate amount of contiguous data in pipe buffer      int contiguous = capacity - (readx % capacity);      // calculate how much we will read this time      int amount = (length > available) ? available : length;      if (amount > contiguous) {       // two array copies needed if data wrap around the buffer end       System.arraycopy (buffer, readx % capacity, data, offset,        contiguous);       System.arraycopy (buffer, 0, data, offset + contiguous,        amount - contiguous);      } else {       // otherwise, one array copy needed       System.arraycopy (buffer, readx % capacity, data, offset,        amount);      }      // update indices with amount of data read      processed (READER, amount);      // return amount read      return amount;     }    }   }   public synchronized long skip (long amount) throws IOException {    // take a reference to the reader thread    if (reader == null)     reader = Thread.currentThread ();    // throw an exception if the stream is closed    closedCheck ();    // throw any pending exception    exceptionCheck ();    if (amount <= 0) {     return 0;    } else {     // wait for some data to become available for skipping     int available = checkedAvailable (READER);     // return 0 on EOF     if (available < 0)      return 0;     // calculate how much we will skip this time     if (amount > available)      amount = available;     // update indices with amount of data skipped     processed (READER, (int) amount);     // return amount skipped     return amount;    }   }

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

时间:2011-06-21 Merlin Hughes

当数据从这个管道被读取或数据被写到这个管道时,清单 11 中的方法被调 用。该方法更新有关的索引,如果管道达到它的滞后级别,该方法自动地唤醒阻 塞的线程。

清单 11. 更新索引

private void processed (boolean rw, int amount) {    if (rw == READER) {     // update read index with amount read     readx = (readx + amount) % (capacity * 2);    } else {     // update write index with amount written     writex = (writex + amount) % (capacity * 2);    }    // check whether a thread is sleeping and we have reached the    // hysteresis threshold    if (sleeping && (available (!rw) >= level)) {     // wake sleeping thread     notify ();     sleeping = false;    }   }

在管道有可用空间或可用数据(取决于 rw 参数)前,清单 12 中的 checkedAvailable() 方法一直等待,然后把空间的大小或数据的多少返回给调 用程序。在这个方法内还核对流未被关闭、管道未被破坏等。

清单 12. 检查可用性

public synchronized int available () throws IOException {    // throw an exception if the stream is closed    closedCheck ();

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站: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号