我创建了用rc4,aes和椭圆曲线加密的voip应用程序,但是由于方法flow(),通信有时会阻塞。 谁能向我解释为什么会出现此错误,如何解决? 这是在Voip应用程序中执行RC4算法的代码,其他两类aes和ecc具有几乎相同的代码
class VOIPClientRC4 extends Thread {
public static long tc2;
private String ip,pseudo;
private int port;
private Socket client;
int VOIP_BUFFER = 25000;
/**
* @param ip String
* @param port int
*/
public VOIPClientRC4(String ip, int port) {
this.ip = ip;
this.port = port;
try {
client = new Socket(ip, port);
this.start();
} catch (Exception e) {
}
}
public VOIPClientRC4(Socket s) {
client = s;
this.start();
}
public void run() {
try {
long t = System.currentTimeMillis();
OutputStream o = client.getOutputStream();
DataOutputStream dos = new DataOutputStream(o);
dos.writeLong(t);
InputStream inp = client.getInputStream();
DataInputStream dis = new DataInputStream(inp);
long time = dis.readLong();
dos = null;
dis = null;
class VOIPEcouteur extends Thread {
private SourceDataLine ligneSource = null;
private BufferedInputStream buffis;
private InputStream is;
private byte[] data;
private int count,cE;
private AudioFormat audio;
private AudioInputStream audiois;
public VOIPEcouteur() {
try {
buffis = new BufferedInputStream(client.getInputStream());
//configuration Audio
audio = confAudio();
} catch (IOException ex) {
}
}
public void run() {
data = new byte[VOIP_BUFFER];
try {
while ((count = buffis.read(data, 0, data.length)) !=
-1 && !boolStopTalking) {
is = new ByteArrayInputStream(data);
audiois = new AudioInputStream(is, audio,
data.length / audio.getFrameSize());
DataLine.Info dataLineInfo = new DataLine.Info(
SourceDataLine.class, audio);
ligneSource = (SourceDataLine) AudioSystem.getLine(
dataLineInfo);
ligneSource.open(audio);
ligneSource.start();
while ((count = audiois.read(data, 0, data.length)) !=
-1) {
if (count > 0) {
Encryption encryption = new Encryption();
encryption = new RC4("0123456789azerty".getBytes());
byte[] JJ = encryption.decrypt(data);
ligneSource.write(JJ, 0, count);
}
}
}
ligneSource.drain();
// ligneSource.flush();
// ligneSource.stop();
ligneSource.close();
buffis.close();
buffis = null;
client.close();
} catch (IOException | LineUnavailableException ex) {
}
}
}
class VOIPParleur extends Thread {
private BufferedOutputStream buffos;
private TargetDataLine ligneCible;
private AudioFormat audio;
private byte[] data;
private int count,c1;
public VOIPParleur() {
try {
buffos = new BufferedOutputStream(client.
getOutputStream());
audio = confAudio();
} catch (IOException ex) {
}
}
public void run() {
DataLine.Info dataLineInfo = new DataLine.Info(
TargetDataLine.class, audio);
try {
ligneCible = (TargetDataLine) AudioSystem.getLine(
dataLineInfo);
} catch (LineUnavailableException ex) {
}
try {
ligneCible.open(audio);
} catch (LineUnavailableException ex1) {
}
ligneCible.start();
data = new byte[VOIP_BUFFER];
boolStopTalking = false;
try {
while (!boolStopTalking) {
count = ligneCible.read(data, 0, data.length);
if (count > 0) {
Encryption encryption = new Encryption();
encryption = new RC4("0123456789azerty".getBytes());
long time = System.currentTimeMillis();
byte[] JJ = encryption.encrypt(data);
tc2 = System.currentTimeMillis()-time;
buffos.write( JJ , 0, count);
c1++;
if (c1 == 1){
System.out.println("temp d'execution RC4 : "+tc2);
}
}
}
buffos.close();
ligneCible.stop();
ligneCible.close();
buffos = null;
ligneCible = null;
client.close();
} catch (Exception e) {
}
}
}
new VOIPParleur().start();
new VOIPEcouteur().start();
} catch (IOException ex) {
Logger.getLogger(VOIPClientRC4.class.getName()).log(Level.SEVERE, null, ex);
}
}
private AudioFormat confAudio() {
float sampleRate = 8000.0F;
int sampleSizeInBits = 16;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(sampleRate, sampleSizeInBits,channels, signed, bigEndian);
}
}
,这是错误的:行526对应于ligne ligneSource.drain()
;
线程“ Thread-16”中的异常java.lang.NullPointerException 在chat.VOIPClientRC4 $ 1VOIPEcouteur.run(VOIPClient.java:526)