麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > Java > 正文

遠程控制順暢無阻礙-java來實現

2024-07-21 02:04:31
字體:
來源:轉載
供稿:網友
 

我平時比較喜歡從網上聽歌,有些鏈接下載速度太慢了。如果用httpurlconnection類的方法打開連接,然后用inputstream類獲得輸入流,再用bufferedinputstream構造出帶緩沖區的輸入流,如果網速太慢的話,無論緩沖區設置多大,聽起來都是斷斷續續的,達不到真正緩沖的目的。于是嘗試編寫代碼實現用緩沖方式讀取遠程文件,以下貼出的代碼是我寫的mp3解碼器的一部分。我是不怎么贊同使用多線程下載的,加之有的鏈接下載速度本身就比較快,所以在下載速度足夠的情況下,就讓下載線程退出,直到只剩下一個下載線程。當然,多線程中令人頭痛的死鎖問題、httpurlconnection的超時阻塞問題都會使代碼看起來異常復雜。

 

簡要介紹一下實現多線程環形緩沖的方法。將緩沖區buf[]分為16塊,每塊32k,下載線程負責向緩沖區寫數據,每次寫一塊;讀線程(buffrandacceurl類)每次讀小于32k的任意字節。同步描述:寫/寫互斥等待空閑塊;寫/寫并發填寫buf[];讀/寫并發使用buf[]。

 

經過我很長一段時間使用,我認為比較滿意地實現了我的目標,同其它mp3播放器對比,我的這種方法能夠比較流暢、穩定地下載并播放。我把實現多線程下載緩沖的方法寫出來,不足之處懇請批評指正。

 

一、httpreader類功能:http協議從指定url讀取數據

 

/** *//*** author by http://www.bt285.cn http://www.5a520.cn*/package instream;     import java.io.ioexception;   import java.io.inputstream;   import java.net.httpurlconnection;   import java.net.url;     public final class httpreader {       public static final int max_retry = 10;       private static long content_length;       private url url;       private httpurlconnection httpconnection;       private inputstream in_stream;       private long cur_pos;           //用于決定seek方法中是否執行文件定位       private int connect_timeout;       private int read_timeout;              public httpreader(url u) {           this(u, 5000, 5000);       }              public httpreader(url u, int connect_timeout, int read_timeout) {           this.connect_timeout = connect_timeout;           this.read_timeout = read_timeout;           url = u;           if (content_length == 0) {               int retry = 0;               while (retry < httpreader.max_retry)                   try {                       this.seek(0);                       content_length = httpconnection.getcontentlength();                       break;                   } catch (exception e) {                       retry++;                   }           }       }              public static long getcontentlength() {           return content_length;       }              public int read(byte[] b, int off, int len) throws ioexception {           int r = in_stream.read(b, off, len);           cur_pos += r;           return r;       }              public int getdata(byte[] b, int off, int len) throws ioexception {           int r, rema = len;           while (rema > 0) {               if ((r = in_stream.read(b, off, rema)) == -1) {                   return -1;               }               rema -= r;               off += r;               cur_pos += r;           }           return len;       }              public void close() {           if (httpconnection != null) {               httpconnection.disconnect();               httpconnection = null;           }           if (in_stream != null) {               try {                   in_stream.close();               } catch (ioexception e) {}               in_stream = null;           }           url = null;       }              /**//*       * 拋出異常通知再試       * 響應碼503可能是由某種暫時的原因引起的,例如同一ip頻繁的連接請求可能遭服務器拒絕       */      public void seek(long start_pos) throws ioexception {           if (start_pos == cur_pos && in_stream != null)               return;           if (httpconnection != null) {               httpconnection.disconnect();               httpconnection = null;           }           if (in_stream != null) {               in_stream.close();               in_stream = null;           }           httpconnection = (httpurlconnection) url.openconnection();           httpconnection.setconnecttimeout(connect_timeout);           httpconnection.setreadtimeout(read_timeout);           string sproperty = "bytes=" + start_pos + "-";           httpconnection.setrequestproperty("range", sproperty);           //httpconnection.setrequestproperty("connection", "keep-alive");           int responsecode = httpconnection.getresponsecode();           if (responsecode < 200 || responsecode >= 300) {               try {                   thread.sleep(500);               } catch (interruptedexception e) {                   e.printstacktrace();               }               throw new ioexception("http responsecode="+responsecode);           }             in_stream = httpconnection.getinputstream();           cur_pos = start_pos;       }     }

 

二、iwritercallback接口功能:實現讀/寫通信。

 

package instream;     public interface iwritercallback {       public boolean trywriting(writer w) throws interruptedexception;       public void updatebuffer(int i, int len);       public void updatewritercount();       public void terminatewriters();   }

|||

 

三、writer類:下載線程,負責向buf[]寫數據。

 

/** *//*** http://www.bt285.cn http://www.5a520.cn */package instream;   import java.io.ioexception;   import java.net.url;     public final class writer implements runnable {       private static boolean isalive = true;       private byte[] buf;       private iwritercallback icb;       protected int index;            //buf[]內"塊"索引號       protected long start_pos;       //index對應的文件位置(相對于文件首的偏移量)       protected int await_count;      //用于判斷:下載速度足夠就退出一個"寫"線程       private httpreader hr;              public writer(iwritercallback call_back, url u, byte[] b, int i) {           hr = new httpreader(u);           if(httpreader.getcontentlength() == 0)  //實例化httpreader對象都不成功               return;           icb = call_back;           buf = b;           thread t = new thread(this,"dt_"+i);           t.setpriority(thread.norm_priority + 1);           t.start();       }              public void run() {           int write_bytes=0, write_pos=0, rema = 0, retry = 0;           boolean cont = true;           while (cont) {               try {                   // 1.等待空閑塊                   if(retry == 0) {                       if (icb.trywriting(this) == false)                           break;                       write_bytes = 0;                       rema = buffrandacceurl.unit_length;                       write_pos = index << buffrandacceurl.unit_length_bits;                   }                                      // 2.定位                   hr.seek(start_pos);                     // 3.下載"一塊"                   int w;                   while (rema > 0 && isalive) {                       w = (rema < 2048) ? rema : 2048; //每次讀幾k合適?                       if ((w = hr.read(buf, write_pos, w)) == -1) {                           cont = false;                           break;                       }                       rema -= w;                       write_pos += w;                       start_pos += w;                       write_bytes += w;                   }                                      //4.通知"讀"線程                   retry = 0;                   icb.updatebuffer(index, write_bytes);               } catch (interruptedexception e) {                   isalive = false;                   icb.terminatewriters();                   break;               } catch (ioexception e) {                   if(++retry == httpreader.max_retry) {                       isalive = false;                       icb.terminatewriters();                       break;                   }               }           }           icb.updatewritercount();           try {               hr.close();           } catch (exception e) {}           hr = null;           buf = null;           icb = null;       }     }

 

四、irandomaccess接口:

 

隨機讀取文件接口,buffrandacceurl類和buffrandaccefile類實現接口方法。buffrandaccefile類實現讀取本地磁盤文件,這兒就不給出其源碼了。

 

package instream;     public interface irandomaccess {       public int read() throws exception;       public int read(byte b[]) throws exception;       public int read(byte b[], int off, int len) throws exception;       public int dump(int src_off, byte b[], int dst_off, int len) throws exception;       public void seek(long pos) throws exception;       public long length();       public long getfilepointer();       public void close();   }

 

五、buffrandacceurl類功能:創建下載線程;read方法從buf[]讀數據。

 

關鍵是如何簡單有效防止死鎖?以下只是我的一次嘗試,請指正。

 

/** *//*** http://www.5a520.cn  http://www.bt285.cn*/ package instream;     import java.net.url;   import java.net.urldecoder;   import decode.header;   import tag.mp3tag;   import tag.tagthread;     public final class buffrandacceurl implements irandomaccess, iwritercallback {       public static final int unit_length_bits = 15;                  //32k       public static final int unit_length = 1 << unit_length_bits;       public static final int buf_length = unit_length << 4;            //16塊       public static final int unit_count = buf_length >> unit_length_bits;       public static final int buf_length_mask = (buf_length - 1);       private static final int max_writer = 8;       private static long file_pointer;       private static int read_pos;       private static int fill_bytes;       private static byte[] buf;      //同時也作讀寫同步鎖:buf.wait()/buf.notify()       private static int[] buf_bytes;       private static int buf_index;       private static int alloc_pos;       private static url url = null;       private static boolean isalive = true;       private static int writer_count;       private static int await_count;       private long file_length;       private long frame_bytes;              public buffrandacceurl(string surl) throws exception {           this(surl,max_writer);       }              public buffrandacceurl(string surl, int download_threads) throws exception {           buf = new byte[buf_length];           buf_bytes = new int[unit_count];           url = new url(surl);                      //創建線程以異步方式解析id3           new tagthread(url);                      //打印當前文件名           try {               string s = urldecoder.decode(surl, "gbk");               system.out.println("start>> " + s.substring(s.lastindexof("/") + 1));               s = null;           } catch (exception e) {               system.out.println("start>> " + surl);           }                      //創建"寫"線程           for(int i = 0; i < download_threads; i++)               new writer(this, url, buf, i+1);           frame_bytes = file_length = httpreader.getcontentlength();           if(file_length == 0) {               header.strlasterr = "連接url出錯,重試 " + httpreader.max_retry + " 次后放棄。";               throw new exception("retry " + httpreader.max_retry);           }           writer_count = download_threads;                      //緩沖           try_cache();                      //跳過id3 v2           mp3tag mp3tag = new mp3tag();           int v2_size = mp3tag.checkid3v2(buf,0);           if (v2_size > 0) {               frame_bytes -= v2_size;               //seek(v2_size):               fill_bytes -= v2_size;               file_pointer = v2_size;               read_pos = v2_size;               read_pos &= buf_length_mask;               int units = v2_size >> unit_length_bits;               for(int i = 0; i < units; i++) {                   buf_bytes[i] = 0;                   this.notifywriter();               }               buf_bytes[units] -= v2_size;               this.notifywriter();           }           mp3tag = null;       }              private void try_cache() throws interruptedexception {           int cache_size = buf_length;           if(cache_size > (int)file_length - alloc_pos)               cache_size = (int)file_length - alloc_pos;           cache_size -= unit_length;                      //等待填寫當前正在讀的那"一塊"緩沖區           /**//*if(fill_bytes >= cache_size && writer_count > 0) {              synchronized (buf) {                  buf.wait();              }              return;          }*/                     //等待填滿緩沖區           while (fill_bytes < cache_size) {               if (writer_count == 0 || isalive == false)                   return;               if(buf_length > (int)file_length - alloc_pos)                   cache_size = (int)file_length - alloc_pos - unit_length;               system.out.printf("/r[緩沖%1$6.2f%%] ",(float)fill_bytes / cache_size * 100);               synchronized (buf) {                   buf.wait();               }           }           system.out.printf("/r");       }              private int try_reading(int i, int len) throws exception {           int n = (i == unit_count - 1) ? 0 : (i + 1);           int r = (buf_bytes[i] == 0) ? 0 : (buf_bytes[i] + buf_bytes[n]);           while (r < len) {               if (writer_count == 0 || isalive == false)                   return r;               try_cache();               r = (buf_bytes[i] == 0) ? 0 : (buf_bytes[i] + buf_bytes[n]);           }                      return len;       }              /**//*       * 各個"寫"線程互斥等待空閑塊       */      public synchronized boolean trywriting(writer w) throws interruptedexception {           await_count++;           while (buf_bytes[buf_index] != 0 && isalive) {               this.wait();           }                      //下載速度足夠就結束一個"寫"線程           if(writer_count > 1 && w.await_count >= await_count &&                   w.await_count >= writer_count)               return false;                      if(alloc_pos >= file_length)               return false;           w.await_count = await_count;           await_count--;           w.start_pos = alloc_pos;           w.index = buf_index;           alloc_pos += unit_length;           buf_index = (buf_index == unit_count - 1) ? 0 : buf_index + 1;           return isalive;       }              public void updatebuffer(int i, int len) {           synchronized (buf) {               buf_bytes[i] = len;               fill_bytes += len;               buf.notify();           }       }              public void updatewritercount() {           synchronized (buf) {               writer_count--;               buf.notify();           }       }              public synchronized void notifywriter() {           this.notifyall();       }              public void terminatewriters() {           synchronized (buf) {               if (isalive) {                   isalive = false;                   header.strlasterr = "讀取文件超時。重試 " + httpreader.max_retry                           + " 次后放棄,請您稍后再試。";               }               buf.notify();           }                      notifywriter();            }              public int read() throws exception {           int iret = -1;           int i = read_pos >> unit_length_bits;           // 1."等待"有1字節可讀           while (buf_bytes[i] < 1) {               try_cache();               if (writer_count == 0)                   return -1;           }           if(isalive == false)               return -1;             // 2.讀取           iret = buf[read_pos] & 0xff;           fill_bytes--;           file_pointer++;           read_pos++;           read_pos &= buf_length_mask;           if (--buf_bytes[i] == 0)               notifywriter();     // 3.通知             return iret;       }              public int read(byte b[]) throws exception {           return read(b, 0, b.length);       }         public int read(byte[] b, int off, int len) throws exception {           if(len > unit_length)               len = unit_length;           int i = read_pos >> unit_length_bits;                      // 1."等待"有足夠內容可讀           if(try_reading(i, len) < len || isalive == false)               return -1;             // 2.讀取           int tail_len = buf_length - read_pos; // write_pos != buf_length           if (tail_len < len) {               system.arraycopy(buf, read_pos, b, off, tail_len);               system.arraycopy(buf, 0, b, off + tail_len, len - tail_len);           } else              system.arraycopy(buf, read_pos, b, off, len);             fill_bytes -= len;           file_pointer += len;           read_pos += len;           read_pos &= buf_length_mask;           buf_bytes[i] -= len;           if (buf_bytes[i] < 0) {               int ni = read_pos >> unit_length_bits;               buf_bytes[ni] += buf_bytes[i];               buf_bytes[i] = 0;               notifywriter();           } else if (buf_bytes[i] == 0)               notifywriter();                      return len;       }              /**//*       * 從src_off位置復制,不移動文件"指針"       */      public int dump(int src_off, byte b[], int dst_off, int len) throws exception {           int rpos = read_pos + src_off;           if(try_reading(rpos >> unit_length_bits, len) < len || isalive == false)               return -1;           int tail_len = buf_length - rpos;           if (tail_len < len) {               system.arraycopy(buf, rpos, b, dst_off, tail_len);               system.arraycopy(buf, 0, b, dst_off + tail_len, len - tail_len);           } else              system.arraycopy(buf, rpos, b, dst_off, len);           // 不發信號             return len;       }              public long length() {           return file_length;       }              public long getfilepointer() {           return file_pointer;       }         public void close() {           //       }              //       public void seek(long pos) throws exception {           //       }          }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲一区二区在线免费 | 免费一区区三区四区 | 午夜神马电影网 | 欧美精品久久久久久久久久 | 国产精品久久久久久久久久久久午夜 | 欧美精品一区二区久久 | 久久久久久久久久久久免费 | 在线 日本 制服 中文 欧美 | 中文字幕在线永久视频 | 亚洲成人播放 | 史上最强炼体老祖动漫在线观看 | 小视频在线看 | 成年免费大片黄在线观看岛国 | 久久亚洲精品视频 | 91成人免费版 | 精品亚洲国产视频 | 久久精品久 | 欧美视频网 | 久久免费视频精品 | 国产精品99精品 | 羞羞视频入口 | 免费一级在线观看 | 欧美一级视频网站 | 91精品视频免费 | 午夜精品一区二区三区免费 | 国产色91| 91九色视频观看 | 看免费毛片 | 免费永久在线观看黄网 | 欧美日韩亚洲不卡 | 在线高清中文字幕 | 羞羞的视频在线 | 日日鲁夜夜视频热线播放 | 一级免费 | 久久久久久久久久久久久久国产 | 日本a∨精品中文字幕在线 被啪羞羞视频在线观看 | 欧美亚州 | 看一级大毛片 | 国产精品久久久久久久久久久天堂 | 无遮挡一级毛片视频 | 免费毛片在线视频 |