Android Open Source - android_network_discovery Download File






From Project

Back to project page android_network_discovery.

License

The source code is released under:

GNU General Public License

If you think the Android project android_network_discovery listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2009-2010 Aubort Jean-Baptiste (Rorist)
 * Licensed under GNU's GPL 2, see README
 *///from  w w  w  .  j  a va2s.  co m

// Taken from: http://github.com/ctrlaltdel/TahoeLAFS-android

package info.lamatricexiste.network.Network;

import info.lamatricexiste.network.ActivityMain;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.util.zip.GZIPInputStream;

import javax.net.ssl.SSLException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

public class DownloadFile {

    private static String TAG = "DownloadFile";
    private final String USERAGENT = "Android/" + android.os.Build.VERSION.RELEASE + " ("
            + android.os.Build.MODEL + ") NetworkDiscovery/";
    private HttpClient httpclient;

    public DownloadFile(final Context ctxt, String url, FileOutputStream out) throws IOException, NullPointerException {
        String version = "0.3.x";
        try {
            version = ctxt.getPackageManager().getPackageInfo(ActivityMain.TAG, 0).versionName;
        } catch (NameNotFoundException e) {
        }

        httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter("http.useragent", USERAGENT + version);
        InputStream in = openURL(url);
        if(in == null){
            Log.e(TAG, "Unable to download: " + url);
            return;
        }

        final ReadableByteChannel inputChannel = Channels.newChannel(in);
        final WritableByteChannel outputChannel = Channels.newChannel(out);

        try {
            Log.i(TAG, "Downloading " + url);
            fastChannelCopy(inputChannel, outputChannel);
        } finally {
            try {
                if (inputChannel != null) {
                    inputChannel.close();
                }
                if (outputChannel != null) {
                    outputChannel.close();
                }
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (Exception e) {
                if (e != null && e.getMessage() != null) {
                    Log.e(TAG, e.getMessage());
                } else {
                    Log.e(TAG, "fastChannelCopy() unknown error");
                }
            }
        }
    }

    private InputStream openURL(String url) {
        HttpGet httpget = new HttpGet(url);
        HttpResponse response;
        try {
            try {
                response = httpclient.execute(httpget);
            } catch (SSLException e) {
                Log.i(TAG, "SSL Certificate is not trusted");
                response = httpclient.execute(httpget);
            }
            Log.i(TAG, "Status:[" + response.getStatusLine().toString() + "]");
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                return new GZIPInputStream(entity.getContent());
            }
        } catch (ClientProtocolException e) {
            Log.e(TAG, "There was a protocol based error", e);
        } catch (UnknownHostException e) {
            Log.e(TAG, e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "There was an IO Stream related error", e);
        }

        return null;
    }

    public static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
            throws IOException, NullPointerException {
        if (src != null && dest != null) {
            final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
            while (src.read(buffer) != -1) {
                // prepare the buffer to be drained
                buffer.flip();
                // write to the channel, may block
                dest.write(buffer);
                // If partial transfer, shift remainder down
                // If buffer is empty, same as doing clear()
                buffer.compact();
            }
            // EOF will leave buffer in fill state
            buffer.flip();
            // make sure the buffer is fully drained.
            while (buffer.hasRemaining()) {
                dest.write(buffer);
            }
        }
    }
}




Java Source Code List

info.lamatricexiste.network.AbstractDiscovery.java
info.lamatricexiste.network.ActivityDiscovery.java
info.lamatricexiste.network.ActivityMain.java
info.lamatricexiste.network.ActivityNet.java
info.lamatricexiste.network.ActivityPortscan.java
info.lamatricexiste.network.AsyncPortscan.java
info.lamatricexiste.network.DatabaseHelper.java
info.lamatricexiste.network.DefaultDiscovery.java
info.lamatricexiste.network.DiscoverActivity.java
info.lamatricexiste.network.DnsDiscovery.java
info.lamatricexiste.network.NetworkChange.java
info.lamatricexiste.network.Network.Banner.java
info.lamatricexiste.network.Network.DownloadFile.java
info.lamatricexiste.network.Network.HardwareAddress.java
info.lamatricexiste.network.Network.HostBean.java
info.lamatricexiste.network.Network.NetInfo.java
info.lamatricexiste.network.Network.OsFingerprint.java
info.lamatricexiste.network.Network.Ping.java
info.lamatricexiste.network.Network.RateControl.java
info.lamatricexiste.network.Network.SendSmbNegotiate.java
info.lamatricexiste.network.Utils.DbUpdate.java
info.lamatricexiste.network.Utils.Db.java
info.lamatricexiste.network.Utils.Export.java
info.lamatricexiste.network.Utils.Help.java
info.lamatricexiste.network.Utils.Prefs.java
info.lamatricexiste.network.Utils.Save.java
info.lamatricexiste.network.Utils.UpdateNicDb.java
info.lamatricexiste.network.connectivity.java
info.lamatricexiste.network.portscan.java
info.lamatricexiste.network.wifiinfo.java