Java: Speed Limit on InputStream/OutputStream

Si necesitas reducir la velocidad de un InputStream/OutputStream de un modo sencillo, esta es la solución!

public class ThrottleStream {
	protected final int speedLimitBytesSecond;
	protected int currentTransferredBytes = 0;
	protected long lastTimeStampSeconds = 0;

	protected ThrottleStream(final int maxSpeedInKilobytesPerSecond) {
		this.speedLimitBytesSecond = maxSpeedInKilobytesPerSecond * 1024;
	}

	protected void addAndWait(final int transferred) throws IOException {
		long now = System.currentTimeMillis() / 1000;
		if (now != lastTimeStampSeconds) {
			currentTransferredBytes = 0;
			lastTimeStampSeconds = now;
		}
		currentTransferredBytes += transferred;
		if (currentTransferredBytes >= speedLimitBytesSecond) {
			try {
				while (true) {
					Thread.sleep(10);
					now = System.currentTimeMillis() / 1000;
					if (now != lastTimeStampSeconds) {
						currentTransferredBytes = 0;
						lastTimeStampSeconds = now;
						break;
					}
				}
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
				throw new IOException(e);
			}
		}
	}

	/**
	 * Speed Limit de un InputStream
	 */
	public static class ThrottleInputStream extends FilterInputStream {
		final ThrottleStream limit;

		public ThrottleInputStream(final InputStream in, 
				final int maxSpeedInKilobytesPerSecond) {
			super(in);
			limit = new ThrottleStream(maxSpeedInKilobytesPerSecond);
		}

		@Override
		public int read() throws IOException {
			final int read = super.read();
			limit.addAndWait(read);
			return read;
		}

		@Override
		public int read(final byte[] b) throws IOException {
			final int read = super.read(b);
			limit.addAndWait(read);
			return read;
		}

		@Override
		public int read(final byte[] b, final int off, final int len) 
				throws IOException {
			final int read = super.read(b, off, len);
			limit.addAndWait(read);
			return read;
		}
	}

	/**
	 * Speed Limit de un OutputStream
	 */
	public static class ThrottleOutputStream extends FilterOutputStream {
		final ThrottleStream limit;

		public ThrottleOutputStream(final OutputStream out, 
				final int maxSpeedInKilobytesPerSecond) {
			super(out);
			limit = new ThrottleStream(maxSpeedInKilobytesPerSecond);
		}

		@Override
		public void write(final int b) throws IOException {
			super.write(b);
			limit.addAndWait(1);
		}

		@Override
		public void write(final byte[] b) throws IOException {
			super.write(b);
			limit.addAndWait(b.length);
		}

		@Override
		public void write(final byte[] b, final int off, final int len) 
				throws IOException {
			super.write(b, off, len);
			limit.addAndWait(len);
		}
	}
}

Source code: ThrottleStream.java

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: