From 4c7feeb11bc901dfb55d5c262b3e0938eaac82f3 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Wed, 21 Jun 2017 16:16:27 +0200 Subject: [PATCH] Receive in thread --- .../IncomingJingleFileOffer.java | 47 +----------- .../jingle_filetransfer/ReceivingThread.java | 73 +++++++++++++++++++ 2 files changed, 76 insertions(+), 44 deletions(-) create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/ReceivingThread.java diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/IncomingJingleFileOffer.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/IncomingJingleFileOffer.java index ea0e78ff9..5b37f4da2 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/IncomingJingleFileOffer.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/IncomingJingleFileOffer.java @@ -17,9 +17,6 @@ package org.jivesoftware.smackx.jingle_filetransfer; import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; import java.util.logging.Level; import java.util.logging.Logger; @@ -36,7 +33,6 @@ import org.jivesoftware.smackx.jingle.element.JingleContentTransport; import org.jivesoftware.smackx.jingle.transports.JingleTransportInitiationCallback; import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingFileOfferCallback; import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer; -import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild; import org.jxmpp.jid.FullJid; @@ -46,6 +42,7 @@ import org.jxmpp.jid.FullJid; public class IncomingJingleFileOffer extends JingleFileTransferSession implements IncomingFileOfferCallback { private static final Logger LOGGER = Logger.getLogger(IncomingJingleFileOffer.class.getName()); private Jingle pendingSessionInitiate = null; + private ReceivingThread receivingThread; public enum State { fresh, @@ -140,46 +137,8 @@ public class IncomingJingleFileOffer extends JingleFileTransferSession implement transportManager.initiateIncomingSession(getInitiator(), transport, new JingleTransportInitiationCallback() { @Override public void onSessionInitiated(BytestreamSession bytestreamSession) { - JingleFileTransferChild fileTransfer = (JingleFileTransferChild) file.getJingleContentDescriptionChildren().get(0); - FileOutputStream outputStream = null; - InputStream inputStream; - - try { - outputStream = new FileOutputStream(target); - inputStream = bytestreamSession.getInputStream(); - - byte[] filebuf = new byte[fileTransfer.getSize()]; - int read = 0; - byte[] bufbuf = new byte[2048]; - - while (read < filebuf.length) { - int r = inputStream.read(bufbuf); - if (r >= 0) { - System.arraycopy(bufbuf, 0, filebuf, read, r); - read += r; - } else { - //TODO - } - } - - outputStream.write(filebuf); - - } catch (IOException e) { - LOGGER.log(Level.SEVERE, "Error while receiving data: ", e); - } finally { - try { - bytestreamSession.close(); - } catch (IOException e) { - LOGGER.log(Level.SEVERE, "Could not close InputStream.", e); - } - if (outputStream != null) { - try { - outputStream.close(); - } catch (IOException e) { - LOGGER.log(Level.SEVERE, "Could not close FileOutputStream.", e); - } - } - } + receivingThread = new ReceivingThread(bytestreamSession, file, target); + receivingThread.run(); } @Override diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/ReceivingThread.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/ReceivingThread.java new file mode 100644 index 000000000..42038314f --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/ReceivingThread.java @@ -0,0 +1,73 @@ +package org.jivesoftware.smackx.jingle_filetransfer; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.jivesoftware.smackx.bytestreams.BytestreamSession; +import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer; +import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild; + +/** + * Created by vanitas on 21.06.17. + */ +public class ReceivingThread extends Thread { + private static final Logger LOGGER = Logger.getLogger(ReceivingThread.class.getName()); + + private final BytestreamSession session; + private final JingleFileTransfer fileTransfer; + private final File target; + + public ReceivingThread(BytestreamSession session, JingleFileTransfer fileTransfer, File target) { + this.session = session; + this.fileTransfer = fileTransfer; + this.target = target; + } + + @Override + public void run() { + JingleFileTransferChild transfer = (JingleFileTransferChild) fileTransfer.getJingleContentDescriptionChildren().get(0); + FileOutputStream outputStream = null; + InputStream inputStream; + + try { + outputStream = new FileOutputStream(target); + inputStream = session.getInputStream(); + + byte[] filebuf = new byte[transfer.getSize()]; + int read = 0; + byte[] bufbuf = new byte[2048]; + + while (read < filebuf.length) { + int r = inputStream.read(bufbuf); + if (r >= 0) { + System.arraycopy(bufbuf, 0, filebuf, read, r); + read += r; + } else { + //TODO + } + } + + outputStream.write(filebuf); + + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "Error while receiving data: ", e); + } finally { + try { + session.close(); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "Could not close InputStream.", e); + } + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, "Could not close FileOutputStream.", e); + } + } + } + } +}