void runInput(InputStream in, AbstractDownload d) throws IOException {
Logger.getLogger(Downloader.class.getName()).entering(Downloader.class.getName(), "runInput");
if (d.getStatus() == DownloadStatus.DOWNLOADING) {
int read;
StopWatch bufferWatch = new StopWatch();
ByteArrayList chunk = new ByteArrayList();
byte inputBuffer[] = new byte[128];
byte outputBuffer[] = new byte[128];
int multiplier = 1;
int growCount = 0;
int shrinkCount = 0;
int changeOn = 5;
int accelerateOn = 10;
bufferWatch.start();
read = in.read(inputBuffer);
d.initDownloadTime();
while (read != -1 && d.getStatus() == DownloadStatus.DOWNLOADING) {
assert (multiplier > 0);
for (int i = 0; i < multiplier && read != -1 && d.getStatus() == DownloadStatus.DOWNLOADING; i++) {
chunk.addElements(chunk.size(), inputBuffer, 0, read);
read = in.read(inputBuffer);
}
d.updateDownloadTime();
if (chunk.size() > outputBuffer.length) {
outputBuffer = new byte[chunk.size()];
}
processor.doChunck(chunk.size(), chunk.toByteArray(outputBuffer));
d.setDownloaded(d.getDownloaded() + chunk.size());
chunk.clear();
bufferWatch.add();
//System.out.print(bufferWatch);
long maxTime = getdSettings().getBufferTime() * 1000000;
if (bufferWatch.getTime() < maxTime) {
growCount++;
shrinkCount = 0;
if (growCount >= accelerateOn) {
multiplier += growCount;
} else if (growCount >= changeOn) {
multiplier++;
}
//System.out.println(" grow " + multiplier);
} else if (multiplier > 1) {
shrinkCount++;
growCount = 0;
if (shrinkCount >= accelerateOn && multiplier - shrinkCount > 1) {
multiplier -= shrinkCount;
} else if (shrinkCount >= changeOn) {
multiplier--;
}
//System.out.println(" shrink " + multiplier);
}
bufferWatch.restart();
}
}
Logger.getLogger(Downloader.class.getName()).exiting(Downloader.class.getName(), "runInput");
}