2017-06-21 14:11:42 +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.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.jingle_filetransfer;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
|
2017-07-10 12:46:51 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleReason;
|
2017-06-21 14:11:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by vanitas on 21.06.17.
|
|
|
|
*/
|
2017-06-23 23:41:40 +02:00
|
|
|
public class SendTask implements Runnable {
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(SendTask.class.getName());
|
2017-06-21 14:11:42 +02:00
|
|
|
|
2017-07-10 12:46:51 +02:00
|
|
|
private final BytestreamSession byteStream;
|
|
|
|
private final JingleFileTransferSession session;
|
2017-06-21 14:11:42 +02:00
|
|
|
private final File source;
|
|
|
|
|
2017-07-10 12:46:51 +02:00
|
|
|
public SendTask(JingleFileTransferSession session, BytestreamSession byteStream, File source) {
|
|
|
|
this.byteStream = byteStream;
|
2017-06-21 14:11:42 +02:00
|
|
|
this.source = source;
|
2017-07-10 12:46:51 +02:00
|
|
|
this.session = session;
|
2017-06-21 14:11:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2017-06-28 14:48:39 +02:00
|
|
|
InputStream inputStream = null;
|
|
|
|
OutputStream outputStream = null;
|
2017-06-21 14:11:42 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
inputStream = new FileInputStream(source);
|
2017-07-10 12:46:51 +02:00
|
|
|
outputStream = byteStream.getOutputStream();
|
2017-06-21 14:11:42 +02:00
|
|
|
|
|
|
|
byte[] filebuf = new byte[(int) source.length()];
|
|
|
|
int r = inputStream.read(filebuf);
|
|
|
|
|
|
|
|
if (r < 0) {
|
|
|
|
throw new IOException("Read returned -1");
|
|
|
|
}
|
|
|
|
|
2017-06-28 14:48:39 +02:00
|
|
|
LOGGER.log(Level.INFO, "WRITE");
|
2017-06-21 14:11:42 +02:00
|
|
|
outputStream.write(filebuf);
|
2017-07-03 10:13:00 +02:00
|
|
|
outputStream.flush();
|
2017-06-28 14:48:39 +02:00
|
|
|
LOGGER.log(Level.INFO, "WRITING FINISHED");
|
2017-06-21 14:11:42 +02:00
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
LOGGER.log(Level.SEVERE, "Could not send file: " + e, e);
|
|
|
|
}
|
2017-06-28 14:48:39 +02:00
|
|
|
|
2017-06-21 14:11:42 +02:00
|
|
|
finally {
|
|
|
|
try {
|
2017-06-28 14:48:39 +02:00
|
|
|
if (inputStream != null) {
|
|
|
|
inputStream.close();
|
|
|
|
LOGGER.log(Level.INFO, "InputStream closed.");
|
|
|
|
}
|
2017-06-21 14:11:42 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.log(Level.SEVERE, "Could not close session.", e);
|
|
|
|
}
|
2017-07-10 12:46:51 +02:00
|
|
|
|
|
|
|
session.notifyEndedListeners(JingleReason.Reason.success);
|
2017-06-21 14:11:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|