mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-24 04:52:05 +01:00
Receive in thread
This commit is contained in:
parent
6a4695b6c6
commit
4c7feeb11b
2 changed files with 76 additions and 44 deletions
|
@ -17,9 +17,6 @@
|
||||||
package org.jivesoftware.smackx.jingle_filetransfer;
|
package org.jivesoftware.smackx.jingle_filetransfer;
|
||||||
|
|
||||||
import java.io.File;
|
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.Level;
|
||||||
import java.util.logging.Logger;
|
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.transports.JingleTransportInitiationCallback;
|
||||||
import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingFileOfferCallback;
|
import org.jivesoftware.smackx.jingle_filetransfer.callback.IncomingFileOfferCallback;
|
||||||
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer;
|
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer;
|
||||||
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild;
|
|
||||||
|
|
||||||
import org.jxmpp.jid.FullJid;
|
import org.jxmpp.jid.FullJid;
|
||||||
|
|
||||||
|
@ -46,6 +42,7 @@ import org.jxmpp.jid.FullJid;
|
||||||
public class IncomingJingleFileOffer extends JingleFileTransferSession implements IncomingFileOfferCallback {
|
public class IncomingJingleFileOffer extends JingleFileTransferSession implements IncomingFileOfferCallback {
|
||||||
private static final Logger LOGGER = Logger.getLogger(IncomingJingleFileOffer.class.getName());
|
private static final Logger LOGGER = Logger.getLogger(IncomingJingleFileOffer.class.getName());
|
||||||
private Jingle pendingSessionInitiate = null;
|
private Jingle pendingSessionInitiate = null;
|
||||||
|
private ReceivingThread receivingThread;
|
||||||
|
|
||||||
public enum State {
|
public enum State {
|
||||||
fresh,
|
fresh,
|
||||||
|
@ -140,46 +137,8 @@ public class IncomingJingleFileOffer extends JingleFileTransferSession implement
|
||||||
transportManager.initiateIncomingSession(getInitiator(), transport, new JingleTransportInitiationCallback() {
|
transportManager.initiateIncomingSession(getInitiator(), transport, new JingleTransportInitiationCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onSessionInitiated(BytestreamSession bytestreamSession) {
|
public void onSessionInitiated(BytestreamSession bytestreamSession) {
|
||||||
JingleFileTransferChild fileTransfer = (JingleFileTransferChild) file.getJingleContentDescriptionChildren().get(0);
|
receivingThread = new ReceivingThread(bytestreamSession, file, target);
|
||||||
FileOutputStream outputStream = null;
|
receivingThread.run();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue