public void performOptimizedWrite(byte[] handle, int blocksize,
			int outstandingRequests, java.io.InputStream in, int buffersize,
			FileTransferProgress progress, long position)
			throws SftpStatusException, SshException,
			TransferCancelledException {

		try {
			if (blocksize < 4096) {
				throw new SshException("Block size cannot be less than 4096",
						SshException.BAD_API_USAGE);
			}

			if (position < 0)
				throw new SshException(
						"Position value must be greater than zero!",
						SshException.BAD_API_USAGE);

			if (position > 0) {
				if (progress != null)
					progress.progressed(position);
			}

			if (buffersize <= 0) {
				buffersize = blocksize;
			}

			byte[] buf = new byte[blocksize];

			long transfered = position;
			int buffered = 0;

			Vector<UnsignedInteger32> requests = new Vector<UnsignedInteger32>();

			in = new java.io.BufferedInputStream(in, buffersize);

			UnsignedInteger32 requestId;

			while (true) {

				buffered = in.read(buf);
				if (buffered == -1)
					break;

				requests.addElement(postWriteRequest(handle, transfered, buf,
						0, buffered));

				transfered += buffered;

				if (progress != null) {

					if (progress.isCancelled())
						throw new TransferCancelledException();

					progress.progressed(transfered);
				}

				if (requests.size() > outstandingRequests) {
					requestId = (UnsignedInteger32) requests.elementAt(0);
					requests.removeElementAt(0);
					getOKRequestStatus(requestId);
				}

			}

			for (Enumeration<UnsignedInteger32> e = requests.elements(); e
					.hasMoreElements();) {
				getOKRequestStatus(e.nextElement());
			}

			requests.removeAllElements();
		} catch (SshIOException ex) {
			throw ex.getRealException();
		} catch (EOFException ex) {
			// The channel has reached EOF before the transfer could complete so
			// make sure the channel is closed and throw a status exception
			try {
				close();
			} catch (SshIOException ex1) {
				throw ex1.getRealException();
			} catch (IOException ex1) {
				throw new SshException(ex1.getMessage(),
						SshException.CHANNEL_FAILURE);
			}

			throw new SftpStatusException(
					SftpStatusException.SSH_FX_CONNECTION_LOST,
					"The SFTP channel terminated unexpectedly");
		} catch (IOException ex) {
			throw new SshException(ex);
		} catch (OutOfMemoryError ex) {
			throw new SshException(
					"Resource Shortage: try reducing the local file buffer size",
					SshException.BAD_API_USAGE);
		}

	}
public void performOptimizedWrite ( byte [ ] handle , int blocksize , int outstandingRequests , java . io . InputStream in , int buffersize , FileTransferProgress progress , long position ) throws SftpStatusException , SshException , TransferCancelledException { try { if ( blocksize < 4096 ) { throw new SshException ( "Block size cannot be less than 4096" , SshException . BAD_API_USAGE ) ; } if ( position < 0 ) throw new SshException ( "Position value must be greater than zero!" , SshException . BAD_API_USAGE ) ; if ( position > 0 ) { if ( progress != null ) progress . progressed ( position ) ; } if ( buffersize <= 0 ) { buffersize = blocksize ; } byte [ ] buf = new byte [ blocksize ] ; long transfered = position ; int buffered = 0 ; Vector < UnsignedInteger32 > requests = new Vector < UnsignedInteger32 > ( ) ; in = new java . io . BufferedInputStream ( in , buffersize ) ; UnsignedInteger32 requestId ; while ( true ) { buffered = in . read ( buf ) ; if ( buffered == - 1 ) break ; requests . addElement ( postWriteRequest ( handle , transfered , buf , 0 , buffered ) ) ; transfered += buffered ; if ( progress != null ) { if ( progress . isCancelled ( ) ) throw new TransferCancelledException ( ) ; progress . progressed ( transfered ) ; } if ( requests . size ( ) > outstandingRequests ) { requestId = ( UnsignedInteger32 ) requests . elementAt ( 0 ) ; requests . removeElementAt ( 0 ) ; getOKRequestStatus ( requestId ) ; } } for ( Enumeration < UnsignedInteger32 > e = requests . elements ( ) ; e . hasMoreElements ( ) ; ) { getOKRequestStatus ( e . nextElement ( ) ) ; } requests . removeAllElements ( ) ; } catch ( SshIOException ex ) { throw ex . getRealException ( ) ; } catch ( EOFException ex ) { try { close ( ) ; } catch ( SshIOException ex1 ) { throw ex1 . getRealException ( ) ; } catch ( IOException ex1 ) { throw new SshException ( ex1 . getMessage ( ) , SshException . CHANNEL_FAILURE ) ; } throw new SftpStatusException ( SftpStatusException . SSH_FX_CONNECTION_LOST , "The SFTP channel terminated unexpectedly" ) ; } catch ( IOException ex ) { throw new SshException ( ex ) ; } catch ( OutOfMemoryError ex ) { throw new SshException ( "Resource Shortage: try reducing the local file buffer size" , SshException . BAD_API_USAGE ) ; } }
Performs an optimized write of a file through asynchronous messaging and
through buffering the local file into memory.

@param handle
the open file handle to write to
@param blocksize
the block size to send data, should be between 4096 and 65535
@param outstandingRequests
the maximum number of requests that can be outstanding at any
one time
@param in
the InputStream to read from
@param buffersize
the size of the temporary buffer to read from the InputStream.
Data is buffered into a temporary buffer so that the number of
local filesystem reads is reducted to a minimum. This
increases performance and so the buffer size should be as high
as possible. The default operation, if buffersize <= 0 is to
allocate a buffer the same size as the blocksize, meaning no
buffer optimization is performed.
@param progress
provides progress information, may be null.
@param position
the position in the file to start writing to.
@throws SshException