远程控制顺畅无阻碍-java来实现

2016-02-20 01:00 32 1 收藏

想要天天向上,就要懂得享受学习。图老师为大家推荐远程控制顺畅无阻碍-java来实现,精彩的内容需要你们用心的阅读。还在等什么快点来看看吧!

【 tulaoshi.com - Web开发 】

 

我平时比较喜欢从网上听歌,有些链接下载速度太慢了。如果用HttpURLConnection类的方法打开连接,然后用InputStream类获得输入流,再用BufferedInputStream构造出带缓冲区的输入流,如果网速太慢的话,无论缓冲区设置多大,听起来都是断断续续的,达不到真正缓冲的目的。于是尝试编写代码实现用缓冲方式读取远程文件,以下贴出的代码是我写的MP3解码器的一部分。我是不怎么赞同使用多线程下载的,加之有的链接下载速度本身就比较快,所以在下载速度足够的情况下,就让下载线程退出,直到只剩下一个下载线程。当然,多线程中令人头痛的死锁问题、HttpURLConnection的超时阻塞问题都会使代码看起来异常复杂。

 

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

简要介绍一下实现多线程环形缓冲的方法。将缓冲区buf[]分为16块,每块32K,下载线程负责向缓冲区写数据,每次写一块;读线程(BuffRandAcceURL类)每次读小于32K的任意字节。同步描述:写/写互斥等待空闲块;写/写并发填写buf[];读/写并发使用buf[]。

 

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

经过我很长一段时间使用,我认为比较满意地实现了我的目标,同其它MP3播放器对比,我的这种方法能够比较流畅、稳定地下载并播放。我把实现多线程下载缓冲的方法写出来,不足之处恳请批评指正。

 

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

一、HttpReader类功能:HTTP协议从指定URL读取数据

 

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

/** *//*** 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;   } }

 

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

二、IWriterCallBack接口功能:实现读/写通信。

 

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

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();   }

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

来源:https://www.tulaoshi.com/n/20160220/1633019.html

延伸阅读
标签: 电脑入门
    为系统添加远程桌面 默认状态下,Windows 2000及其之前的系统并没有安装远程桌面,要想在这些系统中使用远程桌面,需要自己手工添加。 在Windows XP系统安装光盘的“SUPPORTTOOLS”目录中,可找到一个名为“Msrdpcli.exe”的程序,它实际上就是远程桌面连接登录器。将此程序复制到没有远程桌面的系统中并运行后,即可自...
iphone远程控制电脑教程 想用你的IPHONE就能远程查看并控制PC吗?呵呵!教程如下: 1.下载TightVNC文件;远程桌面软件(TightVNC)2.5.1绿色汉化版 2.安装完后屏幕右下角多了一个V的图标,鼠标移动至V的上面不要按左右键,即可显示PC的IP地址并用笔记录一下; 3.双击屏幕右下角的V,为了安全起见,请更改远程控制密码,在P...
摘要 通过本章的学习,读者应该能够使用if-then-else语句、while语句、for语句、switch语句控制Java程序的流程,能够在最合适使用的时候,使用它们。并且知道它们与其它程序设计语言的流程控制之间的不同。 -------------------------------------------------------------------------------- 本章目标:...
iPhone如何远程控制电脑   电脑上设置: 首先开机按del,进入bios找到wake on lan或者wake on pci打开它,并且关闭主板上的节能选项。(电脑必须连网线才能唤醒,路由器不能关)。接下来开机右键我的电脑-属性-远程,打开远程控制选项。. 接下来设置网卡wake on lan选项,进本地连接-属性-配置网卡。 开启wake ...
标签: 电脑入门
第一步 架设服务器 其实Windows xp 装好之后,就可以了,当然如果你的XP是不用密码自动登录的,那就不行,没有密码是不让你进行远程桌面连接的,不然还不任何人都可以操作你的电脑,那岂不是完蛋。所以我们要先加密码,详细步骤如下: 点开始------设置---控制面板(繁体版叫控制台)点他。出现以下画面: 接着点-- 用户帐户 --出现如下...

经验教程

332

收藏

39
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部