94 lines
1.8 KiB
Java
94 lines
1.8 KiB
Java
package org.kne.cloud.network;
|
|
|
|
import java.io.*;
|
|
import java.util.Objects;
|
|
import java.util.UUID;
|
|
|
|
import org.kne.io.Task;
|
|
|
|
public class StreamBridge extends Task{
|
|
private static final boolean debug =false;
|
|
|
|
protected InputStream in;
|
|
protected OutputStream out;
|
|
protected int blocksize=65535;
|
|
protected long delay=0;
|
|
public StreamBridge(InputStream in, OutputStream out) {
|
|
super();
|
|
Objects.requireNonNull(in);
|
|
Objects.requireNonNull(out);
|
|
this.in = in;
|
|
this.out = out;
|
|
}
|
|
|
|
@Override
|
|
public void runTask() {
|
|
//FileOutputStream fos=null;
|
|
try {
|
|
//fos=new FileOutputStream(UUID.randomUUID()+".txt");
|
|
int v=-1;
|
|
byte[]b=new byte[blocksize];
|
|
while ((v=in.read(b))!=-1) {
|
|
// fos.write(b, 0, v);
|
|
out.write(b,0,v);
|
|
out.flush();
|
|
if(delay>0) {
|
|
try {
|
|
Thread.sleep(delay);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}else {
|
|
Thread.yield();
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
if(debug)
|
|
e.printStackTrace();
|
|
}finally {
|
|
/*if(fos!=null)
|
|
try {
|
|
fos.close();
|
|
} catch (IOException e1) {
|
|
e1.printStackTrace();
|
|
}*/
|
|
try {
|
|
if(in!=null)
|
|
in.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
try {
|
|
if(in!=null)
|
|
out.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
public int getBlocksize() {
|
|
return blocksize;
|
|
}
|
|
|
|
public void setBlocksize(int blocksize) {
|
|
this.blocksize = blocksize;
|
|
}
|
|
public long getDelay() {
|
|
return delay;
|
|
}
|
|
|
|
public void setDelay(long delay) {
|
|
this.delay = delay;
|
|
}
|
|
|
|
public void runAtNewThread() {
|
|
runAtNewThread("StreamBridge thread");
|
|
}
|
|
public Thread runAtNewThread(String name) {
|
|
Thread t=ThreadTool.makeVThreadIfSupport(name, this);
|
|
t.start();
|
|
return t;
|
|
}
|
|
|
|
}
|