forked from KNEMC/KLALB
KLALB UDP TEST
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
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{
|
||||
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();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// TODO 自动生成的 catch 块
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
/*if(fos!=null)
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}*/
|
||||
try {
|
||||
if(in!=null)
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
// TODO 自动生成的 catch 块
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
if(in!=null)
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
// TODO 自动生成的 catch 块
|
||||
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 void runAtNewThread(String name) {
|
||||
ThreadTool.makeVThreadIfSupport(name, this).start();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user