import java.io.File;
import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.FileNotFoundException;
+import java.io.InputStream;
import java.io.IOException;
+import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
+import java.net.URL;
+import java.net.URLConnection;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
}
}
+ /**
+ * 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);
+ }
+ }
+
/**
* PictureCallback that sends the picture to a server.
*
* @param port server port
*/
public static void ncfile(final Context context, final Address replyTo, final String filename,final String hostname,final int port){
- new AsyncTask<Void, Void, Void>() {
- @Override
- protected @Nullable Void doInBackground(@Nullable final Void... params) {
- final FileChannel in;
- try{
- in=new FileInputStream(filename).getChannel();
- } catch (final FileNotFoundException e){
- Utils.sendMessage(context, replyTo, file_not_found, filename);
- return null;
- }
- final SocketChannel sock;
- 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 null;
- }
-
- 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
- }
- }
- return null;
- }
-
- @Override
- protected void onPostExecute(@Nullable final Void result) {
- Utils.sendMessage(context, replyTo, file_sent);
- }
- }.execute();
+ new NcfileExecutableRunnable(context, replyTo, filename, hostname, port).execute();
}
/**
context.sendBroadcast(new Intent("net.cactii.flash2.TOGGLE_FLASHLIGHT"));
Utils.sendMessage(context, replyTo, toggling_torch_state);
}
+
+ /**
+ * Download a file from a given URL to a given filename
+ *
+ * @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
+ */
+ public static void getfile(final Context context, final Address replyTo, final String filename, final String hostname, final int port){
+ new GetfileExecutableRunnable(context, replyTo, filename, hostname, port).execute();
+ }
}