+ /**
+ * ExecutableRunnable that uploads a file to a given host and port, netcat-style
+ *
+ * @author Marius Gavrilescu <marius@ieval.ro>
+ */
+ private static final class NcfileExecutableRunnable extends ExecutableRunnable {
+ private final Context context;
+ private final Address replyTo;
+ private final String filename;
+ private final String hostname;
+ private final int port;
+
+ /**
+ * Construct a NcfileExecutableRunnable
+ *
+ * @param context Context instance
+ * @param replyTo reply Address
+ * @param filename filename to upload
+ * @param hostname hostname to upload to
+ * @param port port to upload to
+ */
+ NcfileExecutableRunnable(final Context context, final Address replyTo, final String filename, final String hostname, final int port){
+ this.context=context;
+ this.replyTo=replyTo;
+ this.filename=filename;
+ this.hostname=hostname;
+ this.port=port;
+ }
+
+ @Override public void run(){
+ final FileChannel in;
+ final SocketChannel sock;
+ try{
+ in=new FileInputStream(filename).getChannel();
+ } catch (final FileNotFoundException e){
+ Utils.sendMessage(context, replyTo, file_not_found, filename);
+ return;
+ }
+
+ try{
+ sock = SocketChannel.open(new InetSocketAddress(hostname, port));
+ } catch (final IOException e){
+ Utils.sendMessage(context, replyTo, toNonNull(context.getString(cannot_connect_to_host_on_port, hostname, Integer.valueOf(port))));
+ try {
+ in.close();
+ } catch (IOException ex) {
+ //ignored
+ }
+ return;
+ }
+
+ try{
+ in.transferTo(0, in.size(), sock);
+ } catch (final IOException e){
+ Utils.sendMessage(context, replyTo, toNonNull(context.getString(io_error, e.getMessage())));
+ } finally {
+ try{
+ in.close();
+ } catch (IOException e){
+ //ignored
+ }
+ try{
+ sock.close();
+ } catch(IOException e){
+ //ignored
+ }
+ }
+ Utils.sendMessage(context, replyTo, file_sent);
+ }
+ }
+
+ /**
+ * ExecutableRunnable that downloads a file from a given host and port, netcat-style
+ *
+ * @author Marius Gavrilescu <marius@ieval.ro>
+ */
+ private static final class GetfileExecutableRunnable extends ExecutableRunnable {
+ private final Context context;
+ private final Address replyTo;
+ private final String filename;
+ private final String hostname;
+ private final int port;
+
+ /**
+ * Construct a GetfileExecutableRunnable
+ *
+ * @param context Context instance
+ * @param replyTo reply Address
+ * @param filename filename to save to
+ * @param hostname hostname to download from
+ * @param port port to download from
+ */
+ GetfileExecutableRunnable(final Context context, final Address replyTo, final String filename, final String hostname, final int port){
+ this.context=context;
+ this.replyTo=replyTo;
+ this.filename=filename;
+ this.hostname=hostname;
+ this.port=port;
+ }
+
+ @Override public void run(){
+ final InputStream in;
+ final FileOutputStream out;
+ final Socket sock;
+ try{
+ out=new FileOutputStream(filename);
+ } catch (final IOException e){
+ Utils.sendMessage(context, replyTo, error_opening_file, filename, e.getMessage());
+ return;
+ }
+
+ try{
+ sock = new Socket(hostname, port);
+ in = sock.getInputStream();
+ } catch (final IOException e){
+ Utils.sendMessage(context, replyTo, toNonNull(context.getString(cannot_connect_to_host_on_port, hostname, Integer.valueOf(port))));
+ try {
+ out.close();
+ } catch (IOException ex) {
+ //ignored
+ }
+ return;
+ }
+
+ try{
+ byte[] buffer=new byte[1024*1024*2];
+ int nread;
+ while((nread = in.read(buffer)) > 0)
+ out.write(buffer, 0, nread);
+ } catch (final IOException e){
+ Utils.sendMessage(context, replyTo, toNonNull(context.getString(io_error, e.getMessage())));
+ } finally {
+ try{
+ out.close();
+ } catch (IOException e){
+ //ignored
+ }
+ try{
+ sock.close();
+ } catch(IOException e){
+ //ignored
+ }
+ }
+ Utils.sendMessage(context, replyTo, file_received);
+ }
+ }
+