oft.sendStream(new ByteArrayInputStream(dataToSend), "hello.txt", dataToSend.length, "A greeting"); int duration = 0; while (!oft.isDone()) { switch (oft.getStatus()) { case error: throw new Exception("FileTransfer error: " + oft.getError()); default: LOGGER.info("FileTransfer status: " + oft.getStatus() + ". Progress: " + oft.getProgress()); break;
/** * Creates an OutgoingFileTransfer to send a file to another user. * * @param userID * The fully qualified jabber ID (i.e. full JID) with resource of the user to * send the file to. * @return The send file object on which the negotiated transfer can be run. * @exception IllegalArgumentException if userID is null or not a full JID */ public OutgoingFileTransfer createOutgoingFileTransfer(EntityFullJid userID) { // We need to create outgoing file transfers with a full JID since this method will later // use XEP-0095 to negotiate the stream. This is done with IQ stanzas that need to be addressed to a full JID // in order to reach an client entity. if (userID == null) { throw new IllegalArgumentException("userID was null"); } return new OutgoingFileTransfer(connection().getUser(), userID, FileTransferNegotiator.getNextStreamID(), fileTransferNegotiator); }
throw new IllegalArgumentException("Callback progress cannot be null."); checkTransferThread(); if (isDone() || outputStream != null) { throw new IllegalStateException( "The negotiation process has already" + " been attempted for this file transfer"); setFileInfo(fileName, fileSize); this.callback = progress; transferThread = new Thread(new Runnable() {
private void handleXMPPException(XMPPErrorException e) { StanzaError error = e.getStanzaError(); if (error != null) { switch (error.getCondition()) { case forbidden: setStatus(Status.refused); return; case bad_request: setStatus(Status.error); setError(Error.not_acceptable); break; default: setStatus(FileTransfer.Status.error); } } setException(e); }
if (isDone() || outputStream != null) { throw new IllegalStateException( "The negotiation process has already" setFileInfo(fileName, fileSize); this.outputStream = negotiateStream(fileName, fileSize, description); } catch (XMPPErrorException e) { handleXMPPException(e); throw e;
@Override public void run() { // Create packet filter. try { outputStream = negotiateStream(fileName, fileSize, description); } catch (XMPPErrorException e) { handleXMPPException(e); return; } catch (Exception e) { setException(e); } if (outputStream == null) { return; } if (!updateStatus(Status.negotiated, Status.in_progress)) { return; } try { writeToStream(in, outputStream); } catch (IOException e) { setStatus(FileTransfer.Status.error); setException(e); } finally { CloseableUtil.maybeClose(in, LOGGER); CloseableUtil.maybeClose(outputStream, LOGGER); } updateStatus(Status.in_progress, FileTransfer.Status.complete); }
FileTransferManager manager = new FileTransferManager(connection); OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("usre2@myHost/Smack"); File file = new File(filenameWithPath); try { transfer.sendFile(file, "test_file"); } catch (XMPPException e) { e.printStackTrace(); } while(!transfer.isDone()) { if(transfer.getStatus().equals(Status.error)) { System.out.println("ERROR!!! " + transfer.getError()); } else if (transfer.getStatus().equals(Status.cancelled) || transfer.getStatus().equals(Status.refused)) { System.out.println("Cancelled!!! " + transfer.getError()); } try { Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } } if(transfer.getStatus().equals(Status.refused) || transfer.getStatus().equals(Status.error) || transfer.getStatus().equals(Status.cancelled)){ System.out.println("refused cancelled error " + transfer.getError()); } else { System.out.println("Success"); }
private OutputStream negotiateStream(String fileName, long fileSize, String description) throws XMPPException { // Negotiate the file transfer profile if (!updateStatus(Status.initial, Status.negotiating_transfer)) { throw new XMPPException("Illegal state change"); } StreamNegotiator streamNegotiator = negotiator.negotiateOutgoingTransfer( getPeer(), streamID, fileName, fileSize, description, RESPONSE_TIMEOUT); if (streamNegotiator == null) { setStatus(Status.error); setError(Error.no_response); return null; } // Negotiate the stream if (!updateStatus(Status.negotiating_transfer, Status.negotiating_stream)) { throw new XMPPException("Illegal state change"); } outputStream = streamNegotiator.createOutgoingStream(streamID, initiator, getPeer()); if (!updateStatus(Status.negotiating_stream, Status.negotiated)) { throw new XMPPException("Illegal state change"); } return outputStream; }
checkTransferThread(); setFileInfo(fileName, fileSize); transferThread = new Thread(new Runnable() { @Override
if (!getStatus().equals(Status.NEGOTIATED)) { return; setStatus(Status.IN_PROGRESS); writeToStream(inputStream, outputStream); } catch (FileNotFoundException e) { setStatus(FileTransfer.Status.ERROR); setError(Error.BAD_FILE); setException(e); } catch (XMPPException e) { setStatus(FileTransfer.Status.ERROR); setException(e); } finally { try { if (getStatus().equals(Status.IN_PROGRESS)) { setStatus(FileTransfer.Status.COMPLETE);
Log.d("file sending",file.getAbsolutePath()+" "+file.getName()); configureProviderManager(connection); transfer.sendFile(file, "test_file"); } catch (XMPPException e) { e.printStackTrace(); while(!transfer.isDone()) { Log.d("status", transfer.getStatus().toString()); Log.d("percent", new Long(transfer.getBytesSent()).toString()); if (transfer.getStatus() == Status.error) { Log.e("percent", "Error " + new Long(transfer.getBytesSent()).toString() + " " + transfer.getError() + " " + transfer.getException()); transfer.cancel(); if(transfer.getStatus().equals(Status.refused)) System.out.println("refused " + transfer.getError()); else if( transfer.getStatus().equals(Status.error)) System.out.println(" error " + transfer.getError()); else if(transfer.getStatus().equals(Status.cancelled)) System.out.println(" cancelled " + transfer.getError()); else System.out.println("Success");
if (isDone() || outputStream != null) { throw new IllegalStateException( "The negotation process has already" this.outputStream = negotiateStream(fileName, fileSize, description); } catch (XMPPException e) { handleXMPPException(e); throw e;
/** * Returns the output stream connected to the peer to transfer the file. It * is only available after it has been successfully negotiated by the * {@link StreamNegotiator}. * * @return Returns the output stream connected to the peer to transfer the * file. */ protected OutputStream getOutputStream() { if (getStatus().equals(FileTransfer.Status.negotiated)) { return outputStream; } else { return null; } }
private OutputStream negotiateStream(String fileName, long fileSize, String description) throws XMPPException { // Negotiate the file transfer profile setStatus(Status.NEGOTIATING_TRANSFER); StreamNegotiator streamNegotiator = negotiator.negotiateOutgoingTransfer( getPeer(), streamID, fileName, fileSize, description, RESPONSE_TIMEOUT); if (streamNegotiator == null) { setStatus(Status.ERROR); setError(Error.NO_RESPONSE); return null; } if (!getStatus().equals(Status.NEGOTIATING_TRANSFER)) { return null; } // Negotiate the stream setStatus(Status.NEGOTIATING_STREAM); outputStream = streamNegotiator.createOutgoingStream(streamID, initiator, getPeer()); if (!getStatus().equals(Status.NEGOTIATING_STREAM)) { return null; } setStatus(Status.NEGOTIATED); return outputStream; }
final long fileSize, final String description, NegotiationProgress progress) { checkTransferThread(); if (isDone() || outputStream != null) { throw new IllegalStateException( "The negotation process has already"
private void checkTransferThread() { if ((transferThread != null && transferThread.isAlive()) || isDone()) { throw new IllegalStateException( "File transfer in progress or has already completed."); } }
private void updateBar(final OutgoingFileTransfer transfer, String nickname, String kBperSecond) { FileTransfer.Status status = transfer.getStatus(); if (status == Status.negotiating_stream) { titleLabel.setText(Res.getString("message.negotiation.file.transfer", nickname)); if (transfer.getException() != null) { Log.error("Error occured during file transfer.", transfer.getException()); long p = (transfer.getBytesSent() * 100 / transfer.getFileSize() ); progressBar.setValue(Math.round(p)); } ); String bytesSent = format.format(transfer.getBytesSent()); String est = TransferUtils.calculateEstimate(transfer.getBytesSent(), transfer.getFileSize(), _starttime, System.currentTimeMillis());
private OutputStream negotiateStream(String fileName, long fileSize, String description) throws SmackException, XMPPException, InterruptedException { // Negotiate the file transfer profile if (!updateStatus(Status.initial, Status.negotiating_transfer)) { throw new IllegalStateChangeException(); } StreamNegotiator streamNegotiator = negotiator.negotiateOutgoingTransfer( getPeer(), streamID, fileName, fileSize, description, RESPONSE_TIMEOUT); // Negotiate the stream if (!updateStatus(Status.negotiating_transfer, Status.negotiating_stream)) { throw new IllegalStateChangeException(); } outputStream = streamNegotiator.createOutgoingStream(streamID, initiator, getPeer()); if (!updateStatus(Status.negotiating_stream, Status.negotiated)) { throw new IllegalStateChangeException(); } return outputStream; }
oft.sendStream(new ByteArrayInputStream(dataToSend), "hello.txt", dataToSend.length, "A greeting"); outerloop: while (!oft.isDone()) { switch (oft.getStatus()) { case error: System.out.println("Filetransfer error: " + oft.getError()); break outerloop; default: System.out.println("Filetransfer status: " + oft.getStatus() + ". Progress: " + oft.getProgress()); break;