mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-23 12:32:06 +01:00
Properly allow sending streams
This commit is contained in:
parent
eb413f30b1
commit
7e7be0f47b
3 changed files with 27 additions and 52 deletions
|
@ -17,9 +17,8 @@
|
|||
package org.jivesoftware.smackx.jingle_filetransfer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
@ -31,7 +30,6 @@ import org.jivesoftware.smack.Manager;
|
|||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.jingle.JingleDescriptionManager;
|
||||
import org.jivesoftware.smackx.jingle.JingleManager;
|
||||
|
@ -97,13 +95,13 @@ public final class JingleFileTransferManager extends Manager implements JingleDe
|
|||
|
||||
public OutgoingFileOfferController sendFile(File file, FullJid to)
|
||||
throws SmackException.NotConnectedException, InterruptedException, XMPPException.XMPPErrorException,
|
||||
SmackException.NoResponseException, SmackException.FeatureNotSupportedException {
|
||||
SmackException.NoResponseException, SmackException.FeatureNotSupportedException, FileNotFoundException {
|
||||
return sendFile(file, null, to);
|
||||
}
|
||||
|
||||
public OutgoingFileOfferController sendFile(File file, String alternativeFilename, FullJid to)
|
||||
throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException,
|
||||
SmackException.NoResponseException, SmackException.FeatureNotSupportedException {
|
||||
SmackException.NoResponseException, SmackException.FeatureNotSupportedException, FileNotFoundException {
|
||||
if (file == null || !file.exists()) {
|
||||
throw new IllegalArgumentException("File MUST NOT be null and MUST exist.");
|
||||
}
|
||||
|
@ -142,40 +140,7 @@ public final class JingleFileTransferManager extends Manager implements JingleDe
|
|||
JingleContent content = new JingleContent(JingleContentElement.Creator.initiator, JingleContentElement.Senders.initiator);
|
||||
session.addContent(content);
|
||||
|
||||
JingleOutgoingFileOffer outgoingFileOffer = new JingleOutgoingFileOffer(file) {
|
||||
|
||||
@Override
|
||||
public void onBytestreamReady(BytestreamSession bytestreamSession) {
|
||||
OutputStream outputStream;
|
||||
try {
|
||||
outputStream = bytestreamSession.getOutputStream();
|
||||
|
||||
byte[] buf = new byte[4096];
|
||||
while (true) {
|
||||
int r = stream.read(buf);
|
||||
if (r < 0) {
|
||||
break;
|
||||
}
|
||||
outputStream.write(buf, 0, r);
|
||||
}
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Exception while sending file: " + e, e);
|
||||
} finally {
|
||||
if (stream != null) {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Could not close FileInputStream: " + e, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
notifyProgressListenersFinished();
|
||||
}
|
||||
};
|
||||
JingleOutgoingFileOffer outgoingFileOffer = new JingleOutgoingFileOffer(file, stream);
|
||||
|
||||
content.setDescription(outgoingFileOffer);
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ package org.jivesoftware.smackx.jingle_filetransfer.component;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
@ -35,12 +36,15 @@ import org.jivesoftware.smackx.jingle_filetransfer.controller.OutgoingFileOfferC
|
|||
public class JingleOutgoingFileOffer extends AbstractJingleFileOffer<JingleFileTransferFile.LocalFile> implements OutgoingFileOfferController {
|
||||
private static final Logger LOGGER = Logger.getLogger(JingleOutgoingFileOffer.class.getName());
|
||||
|
||||
public JingleOutgoingFileOffer(File file) {
|
||||
this(new JingleFileTransferFile.LocalFile(file));
|
||||
private final InputStream source;
|
||||
|
||||
public JingleOutgoingFileOffer(File file) throws FileNotFoundException {
|
||||
this(new JingleFileTransferFile.LocalFile(file), new FileInputStream(file));
|
||||
}
|
||||
|
||||
public JingleOutgoingFileOffer(JingleFileTransferFile.LocalFile localFile) {
|
||||
public JingleOutgoingFileOffer(JingleFileTransferFile.LocalFile localFile, InputStream inputStream) {
|
||||
super(localFile);
|
||||
this.source = inputStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -50,28 +54,34 @@ public class JingleOutgoingFileOffer extends AbstractJingleFileOffer<JingleFileT
|
|||
|
||||
@Override
|
||||
public void onBytestreamReady(BytestreamSession bytestreamSession) {
|
||||
File mFile = ((JingleFileTransferFile.LocalFile) file).getFile();
|
||||
if (source == null) {
|
||||
throw new IllegalStateException("Source InputStream is null!");
|
||||
}
|
||||
|
||||
OutputStream outputStream = null;
|
||||
InputStream inputStream = null;
|
||||
|
||||
try {
|
||||
outputStream = bytestreamSession.getOutputStream();
|
||||
inputStream = new FileInputStream(mFile);
|
||||
|
||||
byte[] fileBuf = new byte[(int) mFile.length()];
|
||||
byte[] buf = new byte[8192];
|
||||
|
||||
inputStream.read(fileBuf);
|
||||
while (true) {
|
||||
int r = source.read(buf);
|
||||
if (r < 0) {
|
||||
break;
|
||||
}
|
||||
outputStream.write(buf, 0, r);
|
||||
}
|
||||
|
||||
outputStream.write(fileBuf);
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Exception while sending file: " + e, e);
|
||||
} finally {
|
||||
if (inputStream != null) {
|
||||
if (source != null) {
|
||||
try {
|
||||
inputStream.close();
|
||||
source.close();
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Could not close FileInputStream: " + e, e);
|
||||
}
|
||||
|
|
|
@ -51,10 +51,10 @@ public class JingleContent implements JingleTransportCallback, JingleSecurityCal
|
|||
|
||||
private static final Logger LOGGER = Logger.getLogger(JingleContent.class.getName());
|
||||
|
||||
private JingleSession parent;
|
||||
private JingleContentElement.Creator creator;
|
||||
private final JingleContentElement.Creator creator;
|
||||
private final String name;
|
||||
private final String disposition;
|
||||
private JingleSession parent;
|
||||
private JingleContentElement.Senders senders;
|
||||
private JingleDescription<?> description;
|
||||
private JingleTransport<?> transport;
|
||||
|
|
Loading…
Reference in a new issue