Package bandwidth.network

Source Code of bandwidth.network.ControlledOutputStream

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;
    }
  }

}
TOP

Related Classes of bandwidth.network.ControlledOutputStream

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.