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

109 lines
3.2 KiB
Java
Raw Normal View History

2017-07-27 17:35:16 +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-08-14 13:58:48 +02:00
package org.jivesoftware.smackx.jingle_filetransfer.component;
2017-07-26 16:17:33 +02:00
import java.io.File;
import java.io.FileInputStream;
2017-08-16 18:20:56 +02:00
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
2017-07-26 16:17:33 +02:00
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
2017-07-28 14:02:32 +02:00
import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionInfoElement;
import org.jivesoftware.smackx.jingle.element.JingleElement;
2017-08-14 22:15:23 +02:00
import org.jivesoftware.smackx.jingle_filetransfer.controller.OutgoingFileOfferController;
2017-07-26 16:17:33 +02:00
/**
* Created by vanitas on 26.07.17.
*/
2017-08-16 19:58:43 +02:00
public class JingleOutgoingFileOffer extends AbstractJingleFileOffer implements OutgoingFileOfferController {
private static final Logger LOGGER = Logger.getLogger(JingleOutgoingFileOffer.class.getName());
2017-07-26 16:17:33 +02:00
2017-08-16 18:20:56 +02:00
private final InputStream source;
public JingleOutgoingFileOffer(File file) throws FileNotFoundException {
2017-08-16 19:58:43 +02:00
super(new JingleFileTransferFile.LocalFile(file));
this.source = new FileInputStream(file);
2017-08-15 17:43:06 +02:00
}
2017-08-16 19:58:43 +02:00
public JingleOutgoingFileOffer(JingleFileTransferFile.StreamFile streamFile, InputStream inputStream) {
super(streamFile);
2017-08-16 18:20:56 +02:00
this.source = inputStream;
2017-07-26 16:17:33 +02:00
}
2017-07-28 14:02:32 +02:00
@Override
public JingleElement handleDescriptionInfo(JingleContentDescriptionInfoElement info) {
return null;
}
2017-07-26 16:17:33 +02:00
@Override
2017-07-30 15:40:04 +02:00
public void onBytestreamReady(BytestreamSession bytestreamSession) {
2017-08-16 18:20:56 +02:00
if (source == null) {
throw new IllegalStateException("Source InputStream is null!");
}
OutputStream outputStream = null;
2017-07-27 23:31:04 +02:00
try {
outputStream = bytestreamSession.getOutputStream();
2017-08-16 18:20:56 +02:00
byte[] buf = new byte[8192];
2017-08-16 18:20:56 +02:00
while (true) {
int r = source.read(buf);
if (r < 0) {
break;
}
outputStream.write(buf, 0, r);
}
outputStream.flush();
2017-07-31 14:21:49 +02:00
outputStream.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Exception while sending file: " + e, e);
} finally {
2017-08-16 19:58:43 +02:00
try {
source.close();
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close FileInputStream: " + e, e);
}
}
notifyProgressListenersFinished();
2017-07-26 16:17:33 +02:00
}
2017-07-27 16:43:09 +02:00
@Override
public boolean isOffer() {
return true;
}
@Override
public boolean isRequest() {
return false;
}
2017-08-13 14:38:53 +02:00
@Override
2017-08-13 19:44:02 +02:00
public JingleFileTransferFile.LocalFile getFile() {
return (JingleFileTransferFile.LocalFile) file;
2017-08-13 14:38:53 +02:00
}
2017-07-26 16:17:33 +02:00
}