亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > 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 {           //       }          }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产欧美欧洲在线观看| 国色天香2019中文字幕在线观看| 欧美伊久线香蕉线新在线| 在线日韩第一页| 亚洲人成绝费网站色www| 国产欧美一区二区三区四区| 欧美高清videos高潮hd| 欧美猛交ⅹxxx乱大交视频| 国产一区二区三区高清在线观看| 亚洲激情小视频| 欧美资源在线观看| 亚洲日本中文字幕免费在线不卡| 国产精品久久久久久久久久东京| yellow中文字幕久久| 欧美贵妇videos办公室| 久热精品在线视频| 91青草视频久久| 久久久噜噜噜久久中文字免| 欧美日韩在线观看视频| 中文字幕不卡在线视频极品| 日本久久久久久久久久久| 欧美日韩国产专区| 91伊人影院在线播放| 日韩av在线播放资源| 欧美另类极品videosbestfree| 国产精品高潮视频| 国产精品日韩一区| 91日本视频在线| 久久久电影免费观看完整版| 97在线看福利| 最近中文字幕mv在线一区二区三区四区| 国产视频久久久| 日韩精品在线私人| 亚洲第一黄色网| 中文在线不卡视频| 51色欧美片视频在线观看| 狠狠久久五月精品中文字幕| 久久这里只有精品视频首页| 国产精品欧美在线| 九九热精品视频国产| 日韩中文字幕在线视频| 亚洲区一区二区| 亚洲高清色综合| 欧美日韩精品二区| 久久视频中文字幕| 日日骚av一区| 色婷婷久久一区二区| 国产成人精品免高潮在线观看| 欧美国产日韩视频| 日韩av一区在线| 国产成人福利网站| 久久久久久久久网站| 亚洲欧美日韩国产中文专区| 91tv亚洲精品香蕉国产一区7ujn| 欧美专区在线播放| 91精品国产91| 亚洲理论电影网| 欧美亚洲午夜视频在线观看| 日韩av一卡二卡| 青草青草久热精品视频在线观看| 欧美xxxx做受欧美| 日本a级片电影一区二区| 中文字幕久久久av一区| 亚洲女人天堂成人av在线| 国产成人精品国内自产拍免费看| 久久久久久69| 亚洲精品国产精品乱码不99按摩| 69国产精品成人在线播放| 97国产精品免费视频| 亚洲wwwav| 久久视频在线观看免费| 欧美日韩一二三四五区| 国产日产久久高清欧美一区| 不卡伊人av在线播放| 中文字幕av一区| 1769国内精品视频在线播放| 欧美国产亚洲视频| 91高清视频免费观看| 日韩av不卡电影| 久久国产精品99国产精| 久久久久国产精品免费网站| 国产精品视频公开费视频| 亚洲国产精品免费| 亚洲国产女人aaa毛片在线| 国产在线精品一区免费香蕉| 久久人人爽人人| 在线观看视频亚洲| 黑人巨大精品欧美一区二区免费| 欧洲精品久久久| 日韩最新免费不卡| 51精品国产黑色丝袜高跟鞋| 国产精品ⅴa在线观看h| 久久久91精品国产一区不卡| 久久成人精品一区二区三区| 色一区av在线| 精品国产一区久久久| 国产精品久久久久77777| 国产精品第二页| 国产97人人超碰caoprom| 国产亚洲一级高清| 亚洲一级片在线看| 亚洲欧美日韩精品久久奇米色影视| 欧美激情久久久| 日韩美女在线看| 欧美国产第一页| 欧美日韩成人网| 亚洲国产精品yw在线观看| 亚洲3p在线观看| 亚洲视频专区在线| 91牛牛免费视频| 日韩电影中文字幕av| 亚洲黄色在线看| 黑人巨大精品欧美一区二区一视频| 久久精品亚洲国产| 亚洲国产精品美女| 在线丨暗呦小u女国产精品| 一区二区三区回区在观看免费视频| 色yeye香蕉凹凸一区二区av| 亚洲自拍高清视频网站| 日韩精品极品毛片系列视频| 欧美激情视频网站| 国产日产亚洲精品| 国产精品私拍pans大尺度在线| 91精品国产综合久久久久久蜜臀| 91sa在线看| www日韩欧美| 国产69精品久久久久9| 国产深夜精品福利| 亚洲aa中文字幕| 欧美日韩在线观看视频小说| 国产欧美一区二区三区在线| 久久亚洲影音av资源网| 国产精品欧美日韩| 欧美成人午夜剧场免费观看| 亚洲最大福利视频| 亚洲精品自拍偷拍| 国产日韩精品综合网站| 亚洲人成网站777色婷婷| 精品一区二区三区电影| 亚洲激情在线视频| 欧美日韩精品在线| 色偷偷噜噜噜亚洲男人| 精品久久香蕉国产线看观看gif| 亚洲一区二区久久久| 欧洲永久精品大片ww免费漫画| 中文字幕日韩视频| 久久国产加勒比精品无码| 久久久精品视频在线观看| 91精品久久久久久| 夜夜躁日日躁狠狠久久88av| 黑人巨大精品欧美一区二区三区| 中文字幕亚洲国产| 日韩在线免费视频| 日韩欧美在线视频观看| 欧美激情视频网站| 亚洲毛片在线观看.| 亚洲成成品网站| 成人h视频在线观看播放| 色先锋资源久久综合5566| 国产日韩av在线| 亚洲激情电影中文字幕| 日韩在线视频观看正片免费网站| 北条麻妃一区二区三区中文字幕| 国产欧美一区二区三区视频|