Smack/smack-experimental/src/main/java/org/jivesoftware/smackx/jingle_filetransfer/ReceiveTask.java

97 lines
3.4 KiB
Java
Raw Normal View History

2017-06-21 16:16:52 +02:00
/**
*
* Copyright 2017 Paul Schaub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2017-06-21 16:16:27 +02:00
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;
2017-07-19 15:17:12 +02:00
import org.jivesoftware.smackx.jingle3.element.JingleReasonElement;
2017-06-21 16:16:27 +02:00
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransfer;
import org.jivesoftware.smackx.jingle_filetransfer.element.JingleFileTransferChild;
/**
2017-06-21 17:34:34 +02:00
* Thread for receiving data.
2017-06-21 16:16:27 +02:00
*/
2017-06-23 23:41:40 +02:00
public class ReceiveTask implements Runnable {
private static final Logger LOGGER = Logger.getLogger(ReceiveTask.class.getName());
2017-06-21 16:16:27 +02:00
private final BytestreamSession byteStream;
2017-06-21 16:16:27 +02:00
private final JingleFileTransfer fileTransfer;
private final File target;
private final JingleFileTransferSession session;
2017-06-21 16:16:27 +02:00
public ReceiveTask(JingleFileTransferSession session, BytestreamSession byteStream, JingleFileTransfer fileTransfer, File target) {
this.byteStream = byteStream;
2017-06-21 16:16:27 +02:00
this.fileTransfer = fileTransfer;
this.target = target;
this.session = session;
2017-06-21 16:16:27 +02:00
}
@Override
public void run() {
JingleFileTransferChild transfer = (JingleFileTransferChild) fileTransfer.getJingleContentDescriptionChildren().get(0);
FileOutputStream outputStream = null;
InputStream inputStream;
try {
outputStream = new FileOutputStream(target);
inputStream = byteStream.getInputStream();
2017-06-21 16:16:27 +02:00
byte[] filebuf = new byte[transfer.getSize()];
int read = 0;
2017-07-03 10:13:00 +02:00
byte[] bufbuf = new byte[4096];
LOGGER.log(Level.INFO, "Begin receiving bytes.");
2017-06-21 16:16:27 +02:00
while (read < filebuf.length) {
int r = inputStream.read(bufbuf);
if (r >= 0) {
System.arraycopy(bufbuf, 0, filebuf, read, r);
read += r;
2017-07-03 10:13:00 +02:00
LOGGER.log(Level.INFO, "Read " + r + " (" + read + " of " + filebuf.length + ") bytes.");
2017-06-21 16:16:27 +02:00
} else {
2017-06-21 17:34:34 +02:00
break;
2017-06-21 16:16:27 +02:00
}
}
outputStream.write(filebuf);
2017-06-29 21:53:57 +02:00
LOGGER.log(Level.INFO, "File successfully received.");
2017-06-21 16:16:27 +02:00
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Error while receiving data: ", e);
} finally {
try {
byteStream.close();
2017-06-21 16:16:27 +02:00
} 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);
}
}
2017-07-19 15:17:12 +02:00
session.notifyEndedListeners(JingleReasonElement.Reason.success);
2017-06-21 16:16:27 +02:00
}
}
}