如何实现Java限制的下载速度? [英] How can I implement a download rate limited in Java?

查看:273
本文介绍了如何实现Java限制的下载速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在Java中实现一个(简单的)下载应用程序作为个人练习。它将在不同的线程中运行多个作业,这样我将在执行期间随时同时下载几个文件。



我想能够定义所有下载作业之间共享的下载速率限制,但即使对于单个下载任务也不知道如何执行。我该怎么办呢?我应该尝试实现哪些解决方案?



谢谢。

解决方案

我将从一个管理所有下载的DownloadManager开始。

  interface DownloadManager 
{
public InputStream registerDownload(InputStream stream);
}

所有想要参与管理带宽的代码都将注册它的流下载管理器开始阅读之前。在它的registerDownload()方法中,管理器将给定的输入流包装在一个 ManagedBandwidthStream 中。

  public class ManagedBandwidthStream extends InputStream 
{
private DownloadManagerImpl owner;

public ManagedBandwidthStream(
InputStream original,
DownloadManagerImpl owner

{
super(original);
this.owner = owner;
}

public int read(byte [] b,int offset,int length)
{
owner.read(this,b,offset,length);
}

//由DownloadManager用来实际从流中读取
int actualRead(byte [] b,int offset,int length)
{
super.read(b,offset,length);
}

//还可以覆盖其他read()方法来委托给上面的read()
}

该流确保所有read()的调用都被引导回下载管理器。

 code> class DownloadManagerImpl implements DownloadManager 
{
public InputStream registerDownload(InputStream in)
{
return new ManagedDownloadStream(in);
}

void read(ManagedDownloadStream source,byte [] b,int offset,int len)
{
//所有流都调用此方法。
//您可以决定实际读取的数据量。
int allowed = getAllowedDataRead(source,len);
int read = source.actuallyRead(b,offset,len);
recordBytesRead(read); //更新计数器的字节数
}
}

您的带宽分配策略是关于如何实现getAllowedDataRead()。



一种简单的节流带宽的方法是
保留多少个字节的计数器在给定的时间内读取(例如1秒)。每次调用读取检查计数器并使用它来限制读取的实际字节数。一个计时器用于重置计数器。



实际上,多个流之间的带宽分配可能会变得相当复杂,特别是避免饥饿和促进公平,但这应该给你一个公平的开始。


I'm going to implement a (simple) downloader application in Java as a personal exercise. It is going to run several jobs in different threads, in a way that I will be downloading a few files at the same time at all times during execution.

I want to be able to define a download rate limit that is shared between all the download jobs, but I don't know how to do it even for a single download task. How should I go about doing this? What are the solutions I should try implementing?

Thanks.

解决方案

I'd start with a DownloadManager that manages all downloads.

interface DownloadManager
{
    public InputStream registerDownload(InputStream stream);
}

All code that wants to take part in managed bandwidth will register it's stream with the download manager before it starts reading from it. In it's registerDownload() method, the manager wraps the given input stream in a ManagedBandwidthStream.

   public class ManagedBandwidthStream extends InputStream
   {
      private DownloadManagerImpl owner;

      public ManagedBandwidthStream(
            InputStream original,
            DownloadManagerImpl owner
         )
      {
         super(original);
         this.owner = owner;
      }

      public int read(byte[] b, int offset, int length)
      {
          owner.read(this, b, offset, length);
      }

      // used by DownloadManager to actually read from the stream
      int actuallyRead(byte[] b, int offset, int length)
      {
          super.read(b, offset, length);
      }

      // also override other read() methods to delegate to the read() above
   }

The stream ensures all calls to read() are directed back to the download manager.

class DownloadManagerImpl implements DownloadManager
{
   public InputStream registerDownload(InputStream in)
   {
       return new ManagedDownloadStream(in);
   }

   void read(ManagedDownloadStream source, byte[] b, int offset, int len)
   {
      // all your streams now call this method.
      // You can decide how much data to actually read.
      int allowed = getAllowedDataRead(source, len);
      int read = source.actuallyRead(b, offset, len);
      recordBytesRead(read);  // update counters for number of bytes read
   }
}

Your bandwidth allocation strategy is then about how you implement getAllowedDataRead().

A simple way of throttling the bandwidth is, keep a counter of how many more bytes can be read in a given period (e.g. 1 second). Each call to read examines the counter and uses that to restrict the actual number of bytes read. A timer is used to reset the counter.

In practice, the allocation of bandwith amongst multiple streams can get quite complex, espeically to avoid starvation and promote fairness, but this should give you a fair start.

这篇关于如何实现Java限制的下载速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆