diff options
author | Daniel Gultsch <daniel.gultsch@rwth-aachen.de> | 2014-04-13 11:32:45 +0200 |
---|---|---|
committer | Daniel Gultsch <daniel.gultsch@rwth-aachen.de> | 2014-04-13 11:32:45 +0200 |
commit | 27d5966ac356fd10365d848332b127b0b8f417da (patch) | |
tree | eb98cb6d7fdcc79b5e405acadcef9d8bbcc1e2c8 /src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java | |
parent | 7b554f20013441e7c2a4f803870edfc9ca5d2cca (diff) |
more efficant way of calculating the sha1 sum. closing connections
Diffstat (limited to 'src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java')
-rw-r--r-- | src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java | 107 |
1 files changed, 69 insertions, 38 deletions
diff --git a/src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java b/src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java index c1219ff7..6a3a3648 100644 --- a/src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java +++ b/src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java @@ -1,6 +1,5 @@ package eu.siacs.conversations.xmpp.jingle; -import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; @@ -17,7 +16,7 @@ import eu.siacs.conversations.utils.CryptoHelper; import android.util.Log; public class SocksConnection { - + private JingleConnection jingleConnection; private Socket socket; private String host; @@ -26,8 +25,9 @@ public class SocksConnection { private boolean isProxy = false; private String destination; private OutputStream outputStream; - - public SocksConnection(JingleConnection jingleConnection, String host, String jid, int port, String type) { + + public SocksConnection(JingleConnection jingleConnection, String host, + String jid, int port, String type) { this.jingleConnection = jingleConnection; this.host = host; this.jid = jid; @@ -40,30 +40,33 @@ public class SocksConnection { destBuilder.append(jingleConnection.getInitiator()); destBuilder.append(jingleConnection.getResponder()); mDigest.reset(); - this.destination = CryptoHelper.bytesToHex(mDigest.digest(destBuilder.toString().getBytes())); - Log.d("xmppService","host="+host+", port="+port+", destination: "+destination); + this.destination = CryptoHelper.bytesToHex(mDigest + .digest(destBuilder.toString().getBytes())); + Log.d("xmppService", "host=" + host + ", port=" + port + + ", destination: " + destination); } catch (NoSuchAlgorithmException e) { - + } } - + public boolean connect() { try { this.socket = new Socket(this.host, this.port); InputStream is = socket.getInputStream(); this.outputStream = socket.getOutputStream(); - byte[] login = {0x05, 0x01, 0x00}; - byte[] expectedReply = {0x05,0x00}; + byte[] login = { 0x05, 0x01, 0x00 }; + byte[] expectedReply = { 0x05, 0x00 }; byte[] reply = new byte[2]; this.outputStream.write(login); is.read(reply); if (Arrays.equals(reply, expectedReply)) { - String connect = ""+'\u0005'+'\u0001'+'\u0000'+'\u0003'+'\u0028'+this.destination+'\u0000'+'\u0000'; + String connect = "" + '\u0005' + '\u0001' + '\u0000' + '\u0003' + + '\u0028' + this.destination + '\u0000' + '\u0000'; this.outputStream.write(connect.getBytes()); byte[] result = new byte[2]; is.read(result); int status = result[0]; - return (status==0); + return (status == 0); } else { socket.close(); return false; @@ -75,39 +78,67 @@ public class SocksConnection { } } - public void send(File file) { - FileInputStream fileInputStream = null; - try { - fileInputStream = new FileInputStream(file); - int count; - byte[] buffer = new byte[8192]; - while ((count = fileInputStream.read(buffer)) > 0) { - this.outputStream.write(buffer, 0, count); - } - - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } finally { - try { - if (fileInputStream!=null) { - fileInputStream.close(); + public void send(final JingleFile file, final OnFileTransmitted callback) { + new Thread(new Runnable() { + + @Override + public void run() { + FileInputStream fileInputStream = null; + try { + MessageDigest digest = MessageDigest.getInstance("SHA-1"); + digest.reset(); + fileInputStream = new FileInputStream(file); + int count; + byte[] buffer = new byte[8192]; + while ((count = fileInputStream.read(buffer)) > 0) { + outputStream.write(buffer, 0, count); + digest.update(buffer, 0, count); + } + outputStream.flush(); + file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest())); + if (callback!=null) { + callback.onFileTransmitted(file); + } + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + try { + if (fileInputStream != null) { + fileInputStream.close(); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } - } + }).start(); + } - + public boolean isProxy() { return this.isProxy; } - + public String getJid() { return this.jid; } + + public void disconnect() { + if (this.socket!=null) { + try { + this.socket.close(); + Log.d("xmppService","cloesd socket with "+this.host); + } catch (IOException e) { + Log.d("xmppService","error closing socket with "+this.host); + } + } + } } |