1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-26 04:28:00 +01:00

Receive in thread

This commit is contained in:
vanitasvitae 2017-06-21 16:16:27 +02:00
parent 6a4695b6c6
commit 4c7feeb11b
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 76 additions and 44 deletions

View file

@ -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

View file

@ -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);
}
}
}
}
}