networking
Class FileTransfer

java.lang.Object
  extended by networking.FileTransfer
All Implemented Interfaces:
IFileTransferController, IFileTransferListener

public class FileTransfer
extends java.lang.Object
implements IFileTransferListener, IFileTransferController

FileTransfer - an utility class for transferring files from/to Input/Output streams


Field Summary
private static int ABORTED
          Event constant - transfer aborted
private static int BUF_SIZE
          Pump buffer size
private static boolean DEBUG
          Mode
private  IFileTransferController m_ctrl
          Controller object
private  java.io.File m_file
          File being transferred
private  java.util.List m_listeners
          Listeners list
private  int m_percent
          Current percent
private static int SUCCESS
          Event constant - transfer successful
private static int TRUNCATED
          Event constant - transfer truncated
 
Constructor Summary
FileTransfer(java.io.File file)
          Construct new FileTransfer object
 
Method Summary
 void aborted()
          Transfer aborted event handler
 void addListener(IFileTransferListener listener)
          Add new IFileTransferListener
private  void delay()
          Delay current thread for debugging purposes
private  void fire(int type)
          Fire event
private  void firePercentComplete(int n)
          Fire percentComplete event
private  void firePercentComplete(long upto, long total)
          Fire percentComplete event
 java.io.File getFile()
          Get file
static void main(java.lang.String[] args)
          Debugging aid
 boolean mayContinue()
          Ask controller weither it's possible to continue
 void percentComplete(int n)
          Percent complete event handler
protected  void pump(java.io.InputStream in, java.io.OutputStream out, long total)
          Pump data from an InputStream to an OutputStream and fire percentComplete events
 void recvFile(java.io.InputStream in)
          Receive file from an InputStream.
 void recvFile(java.io.InputStream in, long len)
          Receive file of length len from an InputStream.
 void sendFile(java.io.OutputStream out)
          Send file to an OutputStream
 void setController(IFileTransferController ctrl)
          Set controller object
 void setFile(java.io.File file)
          Set file to transfer
 void successful()
          Transfer successful event handler
 void truncated()
          Transfer truncated event handler
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

private static final boolean DEBUG
Mode

See Also:
Constant Field Values

BUF_SIZE

private static final int BUF_SIZE
Pump buffer size

See Also:
Constant Field Values

SUCCESS

private static final int SUCCESS
Event constant - transfer successful

See Also:
Constant Field Values

ABORTED

private static final int ABORTED
Event constant - transfer aborted

See Also:
Constant Field Values

TRUNCATED

private static final int TRUNCATED
Event constant - transfer truncated

See Also:
Constant Field Values

m_listeners

private java.util.List m_listeners
Listeners list


m_file

private java.io.File m_file
File being transferred


m_ctrl

private IFileTransferController m_ctrl
Controller object


m_percent

private int m_percent
Current percent

Constructor Detail

FileTransfer

public FileTransfer(java.io.File file)
Construct new FileTransfer object

Parameters:
file - file to transfer
Method Detail

getFile

public final java.io.File getFile()
Get file

Returns:
file

setFile

public void setFile(java.io.File file)
Set file to transfer

Parameters:
file - file

setController

public void setController(IFileTransferController ctrl)
Set controller object

Parameters:
controller - object

delay

private final void delay()
Delay current thread for debugging purposes


pump

protected void pump(java.io.InputStream in,
                    java.io.OutputStream out,
                    long total)
             throws java.io.IOException
Pump data from an InputStream to an OutputStream and fire percentComplete events

Parameters:
in - InputStream
out - OutputStream
total - total number of bytes to pump
Throws:
java.io.IOException

recvFile

public void recvFile(java.io.InputStream in,
                     long len)
              throws java.io.IOException
Receive file of length len from an InputStream. File will be read until len bytes is read or the stream is closed

Parameters:
in - InputStream
Throws:
java.io.IOException

recvFile

public void recvFile(java.io.InputStream in)
              throws java.io.IOException
Receive file from an InputStream. File will be read until the stream is closed

Parameters:
in - InputStream
Throws:
java.io.IOException

sendFile

public void sendFile(java.io.OutputStream out)
              throws java.lang.Exception
Send file to an OutputStream

Parameters:
out - OutputStream
Throws:
java.lang.Exception

addListener

public void addListener(IFileTransferListener listener)
Add new IFileTransferListener

Parameters:
listener - new IFileTransferListener

firePercentComplete

private void firePercentComplete(long upto,
                                 long total)
Fire percentComplete event

Parameters:
n - n% complete

firePercentComplete

private void firePercentComplete(int n)
Fire percentComplete event

Parameters:
n - n% complete

fire

private void fire(int type)
Fire event

Parameters:
type - event type

percentComplete

public void percentComplete(int n)
Description copied from interface: IFileTransferListener
Percent complete event handler

Specified by:
percentComplete in interface IFileTransferListener
Parameters:
n - n% complete

aborted

public void aborted()
Description copied from interface: IFileTransferListener
Transfer aborted event handler

Specified by:
aborted in interface IFileTransferListener

successful

public void successful()
Description copied from interface: IFileTransferListener
Transfer successful event handler

Specified by:
successful in interface IFileTransferListener

truncated

public void truncated()
Description copied from interface: IFileTransferListener
Transfer truncated event handler

Specified by:
truncated in interface IFileTransferListener

mayContinue

public boolean mayContinue()
Description copied from interface: IFileTransferController
Ask controller weither it's possible to continue

Specified by:
mayContinue in interface IFileTransferController
Returns:
true if it's possible to continue otherwise false

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Debugging aid

Throws:
java.lang.Exception


Copyright © 2009 Alexey Veremenko. All rights reserved.