package bandwidth.network;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import bandwidth.AbstractBandwidthController;
import bandwidth.UploadBandwidthController;
import main.settings.Settings;
public class ControlledOutputStream extends FilterOutputStream
{
private static UploadBandwidthController controller;
public static void initialize()
{
controller = new UploadBandwidthController(Settings.connection.bandwidth.upload);
}
public static AbstractBandwidthController getController()
{
return controller;
}
public ControlledOutputStream(OutputStream out)
{
super(out);
}
public void write(int b) throws IOException
{
controller.produce();
out.write(b);
}
public void write(byte b[]) throws IOException
{
write(b, 0, b.length);
}
public void write(byte source[], int offset, int length) throws IOException
{
int actuallyWritten = 0;
while (actuallyWritten < length)
{
int produced = controller.produce(length - actuallyWritten);
out.write(source, offset + actuallyWritten, produced);
actuallyWritten += produced;
}
}
}