package net.bakingpowder.jspl.tcprelay.buffering;

import java.io.IOException;
import java.io.InputStream;

public class SrcRead implements Runnable {

    InputStream in;

    BoundedQueue bq;

    Buf buf;

    SrcRead(InputStream in, BoundedQueue bq) {
        this.in = in;
        this.bq = bq;
        
        Thread th = new Thread(this);
        th.start();
    }

    private boolean stopper = false;
    public synchronized void stopd(){
        stopper = true;
    }
    
    
    public void run() {


        while (!stopper) {
            Buf buf = new Buf(Input2Output.BUFFER_SIZE);

            try {
                buf.length = in.read(buf.value, 0, Input2Output.BUFFER_SIZE);

                if (buf.length == -1) {                    
                    LP1:while(true){if (bq.enqueue(buf)) {break LP1;}}
                    break;
                }
                if (buf.length != 0){
                    LP2:while(true){if (bq.enqueue(buf)) {break LP2;}}
                }


                //ここにThread.yield()とかThread.sleepとかを入れる
                
                
                
            } catch (IOException e) {
                break;
            }

        }

    }
}