Skip to content

Mabin-J/StreamValve

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

StreamValve

Speed Limiter of InputStream/OutputStream


Example

package info.mabin.java.io.example;

import info.mabin.java.io.RandomByteInputStream;
import info.mabin.java.io.ValvedInputStream;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;

public class ValvedInputStreamExample {
    private static final int DATA_SIZE = 10 * 1024 * 1024;
    private static final int SPEED = 1024 * 1024;

    public static void main(String... args) throws IOException {
        try(
                RandomByteInputStream randomByteInputStream
                        = new RandomByteInputStream(DATA_SIZE, true, true);

                ValvedInputStream valvedInputStream
                        = new ValvedInputStream(randomByteInputStream, SPEED);

                ByteArrayOutputStream byteArrayOutputStream
                        = new ByteArrayOutputStream()
        ){

            long tmpTimestamp = new Date().getTime();

            byte[] buffer = new byte[4096];
            int len;
            int passedBytesAmount = 0;
            while((len = valvedInputStream.read(buffer)) != -1){
                byteArrayOutputStream.write(buffer, 0, len);
                passedBytesAmount += len;

                long currentTimestamp = new Date().getTime();
                long passedTime = currentTimestamp - tmpTimestamp;

                if(passedTime > 1000){
                    System.out.println(String.format(
                            "Current Speed: %,12.2f Bytes per Second",
                            passedBytesAmount / (double) passedTime * 1000
                    ));
                    tmpTimestamp = currentTimestamp;
                    passedBytesAmount = 0;
                }
            }
        }
    }
}

About

Speed Limiter of InputStream/OutputStream

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages