限制上传和下载

不多说了,自己看吧
public class FlowControlInputStream extends FilterInputStream{    
private long timestamp;
private int maxbps;
private int currentbps;
private int bytesread;

//----------------------------------------------------------
//constructor
public FlowControlInputStream(InputStream in, int maxbps){
super(in);
this.maxbps = maxbps;
this.currentbps = 0;
this.bytesread = 0;
this.timestamp = System.currentTimeMillis();
}

//----------------------------------------------------------
//decorated methods

public int read() throws IOException{
synchronized(in){
int avaliable = check();
if(avaliable == 0){
waitForAvailable();
avaliable = check();
}
int value = in.read();
update(1);
return value;
}
}

public int read(byte[] b) throws IOException{
return read(b, 0, b.length);

}

public int read(byte[] b, int off, int len) throws IOException{
synchronized(in){
int avaliable = check();
if(avaliable == 0){
waitForAvailable();
avaliable = check();
}
int n = in.read(b, off, Math.min(len, avaliable));
update(n);
return n;
}
}

private int check(){
long now = System.currentTimeMillis();
if(now - timestamp >= 1000){
timestamp = now;
currentbps = bytesread;
bytesread = 0;
return maxbps;
}else{
return maxbps - bytesread;
}
}

private void waitForAvailable(){
long time = System.currentTimeMillis() - timestamp;
boolean isInterrupted = false;
while(time < 1000){
try{
Thread.sleep(1000 - time);
}catch(InterruptedException e){
isInterrupted = true;
}
time = System.currentTimeMillis() - timestamp;
}
if(isInterrupted)
Thread.currentThread().interrupt();
return;

}

private void update(int n){
bytesread += n;
}

public int getCurrentbps(){
return currentbps;
}
}

public class FlowControlOutputStream extends FilterOutputStream {
private long timestamp;
private int maxbps;
private int currentbps;
private int byteswrite;

public FlowControlOutputStream(OutputStream out, int maxbps) {
super(out);
this.maxbps = maxbps;
this.currentbps = 0;
this.byteswrite = 0;
this.timestamp = System.currentTimeMillis();
}

public void setMaxbps(int maxbps){
this.maxbps = maxbps;
}

@Override
public void write(int oneByte) throws IOException {
synchronized(out){
int avaliable = check();
if(avaliable <= 0){
waitForAvailable();
}
out.write(oneByte);
update(1);
}
}

@Override
public void write(byte[] buffer) throws IOException {
write(buffer, 0, buffer.length);
}

@Override
public void write(byte[] buffer, int offset, int length) throws IOException {
synchronized(out){
int avaliable = check();
if(avaliable <= 0){
waitForAvailable();
}
out.write(buffer, offset, length);
update(length);
}
}

private int check(){
long now = System.currentTimeMillis();
if(now - timestamp >= 1000){
timestamp = now;
currentbps = byteswrite;
byteswrite = 0;
return maxbps;
}else{
return maxbps - byteswrite;
}
}

private void waitForAvailable(){
long time = System.currentTimeMillis() - timestamp;
boolean isInterrupted = false;
while(time < 1000){
try{
Thread.sleep(1000 - time);
}catch(InterruptedException e){
isInterrupted = true;
}
time = System.currentTimeMillis() - timestamp;
}
if(isInterrupted)
Thread.currentThread().interrupt();
return;

}

private void update(int n){
byteswrite += n;
}

public int getCurrentbps(){
return currentbps;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值