Example usage for org.apache.http.util ByteArrayBuffer toByteArray

List of usage examples for org.apache.http.util ByteArrayBuffer toByteArray

Introduction

In this page you can find the example usage for org.apache.http.util ByteArrayBuffer toByteArray.

Prototype

public byte[] toByteArray() 

Source Link

Usage

From source file:com.joefernandez.irrduino.android.remote.HttpCommandTask.java

/** The system calls this to perform work in a worker thread and
  * delivers it the parameters given to AsyncTask.execute() */
protected String doInBackground(String... urls) {
    try {/*w w  w. j  av a  2s.co  m*/
        URL commandURL = new URL(urls[0]);
        URLConnection conn = commandURL.openConnection();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(50);

        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }

        /* Convert the Bytes read to a String. */
        return new String(baf.toByteArray());

    } catch (Exception e) {
        Log.d(TAG, "http request exception for: " + urls[0]);
        Log.d(TAG, "    Exception: " + e.getMessage());
    }
    return null;

}

From source file:es.udc.robotcontrol.testapp.comunication.ConectorPlaca.java

public byte[] leer() {
    int BUFFER_LENGHT = 64;
    byte[] salida = new byte[BUFFER_LENGHT];
    /* Read the push button status */
    int result = -1;

    result = connection.bulkTransfer(endpointIN, salida, salida.length, 1000);
    Log.d(Constantes.TAG_CONECTOR, "Resultado de lectura [ " + result + " ]");

    StringBuffer sb = new StringBuffer();
    for (int x = 0; x < BUFFER_LENGHT; x++) {
        sb.append(String.valueOf(salida[x]));
        sb.append(" ");
    }//from  w  w w  .  j a v a  2 s.c  o m
    Log.i(Constantes.TAG_CONECTOR, "Leido en el cable [ " + sb.toString() + " ]");
    if (result > 0) {
        ByteArrayBuffer bas = new ByteArrayBuffer(result);
        bas.append(salida, 0, result);
        bas.setLength(result);
        return bas.toByteArray();
    } else {
        Log.i(Constantes.TAG_CONECTOR, "Resultado de lectura [ " + result + " ]");
        return null;
    }
}

From source file:es.udc.fic.android.robot_control.robot.ConectorPlaca.java

public byte[] leer() {
    int BUFFER_LENGHT = 64;
    byte[] salida = new byte[BUFFER_LENGHT];
    int result = -1;

    result = connection.bulkTransfer(endpointIN, salida, salida.length, 1000);
    Log.d(C.ROBOT_TAG, "Resultado de lectura [ " + result + " ]");

    StringBuffer sb = new StringBuffer();
    for (int x = 0; x < BUFFER_LENGHT; x++) {
        sb.append(String.valueOf(salida[x]));
        sb.append(" ");
    }//from  w ww.j a v  a  2s  . c  o m
    Log.i(C.ROBOT_TAG, "Leido en el cable [ " + sb.toString() + " ]");
    if (result > 0) {
        ByteArrayBuffer bas = new ByteArrayBuffer(result);
        bas.append(salida, 0, result);
        bas.setLength(result);
        return bas.toByteArray();
    } else {
        Log.i(C.ROBOT_TAG, "Resultado de lectura [ " + result + " ]");
        return null;
    }
}

From source file:org.simalliance.openmobileapi.service.security.AccessControlDB.java

public byte[] readAPCertificate() throws AccessControlException, CardException {

    ByteArrayBuffer bytes = new ByteArrayBuffer(1024);
    ByteArrayBuffer buffer = new ByteArrayBuffer(256);
    int offset = 0;
    int length = 0;
    while (!mApplet.readAPCertificate(buffer, offset, length)) {
        if (buffer.length() == 0) {
            break;
        }// w ww . j a  v a  2 s .  co  m
        offset += buffer.length();
        bytes.append(buffer.toByteArray(), 0, buffer.length());
    }
    return bytes.toByteArray();
}

From source file:org.nhnnext.android.androidnaming.ImageDownload.java

public void copy_img(String url, String save_name) {

    File img_cache_path;/*from  w ww .j  a v a  2 s  . c  om*/

    img_cache_path = new File(HomeView.FILES_DIR + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            FileOutputStream fos = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:org.nhnnext.android.day3.Img_Downloader.java

public void copy_img(String url, String save_name) {

    File img_cache_path;/*from  w  w  w .jav a  2s  . c o m*/

    img_cache_path = new File(MainActivity.FILES_DIR + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            FileOutputStream fos = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:org.nhnnext.android.day5_simple.ImageDownloader.java

public void copy_img(String url, String save_name) {

    File img_cache_path;/*from   w  ww. j a v a2  s .  co  m*/

    img_cache_path = new File(NextgramController.FILES_DIR + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            FileOutputStream fos = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:org.nhnnext.android.basic.ImageDownload.java

public void copy_img(String url, String save_name) {

    File img_cache_path;//  w w  w . j  a  va 2 s  .  co m

    img_cache_path = new File(pref.getString(context.getString(R.string.files_directory), "") + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            FileOutputStream fos = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:it.unicaradio.android.gcm.GcmServerRpcCall.java

/**
 * @param conn/*from w  w w. j  ava2  s.  c  om*/
 * @throws IOException
 */
private String getResult(HttpURLConnection conn) throws IOException {
    InputStream is = conn.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);

    // Read bytes to the Buffer until there is nothing more to read(-1).
    ByteArrayBuffer baf = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }

    String result = new String(baf.toByteArray());
    Log.d(TAG, result);

    return result;
}

From source file:eu.liveandgov.ar.utilities.Download_Data.java

/**                      DownAndCopy
 * //from  w w w. j a  v  a2  s  .  c om
 * Download file from URL and Copy to Android file system folder
 * 
 * @param fileUrl
 * @param StringAndroidPath
 */
public static boolean DownAndCopy(String fileUrlSTR, String StringAndroidPath, boolean preservefilename,
        String Caller) {

    if (compareRemoteWithLocal(fileUrlSTR, StringAndroidPath)) {
        //Log.e("fileUrlSTR", "SKIP WITH HASH");
        return true;
    } else {
        Log.e("TRY TO DOWNLOAD BY " + Caller, fileUrlSTR);
    }

    SimpleDateFormat sdf = new SimpleDateFormat("mm");

    // Check if downloaded at least just 2 minutes ago
    for (String[] mem : MemDown)
        if (fileUrlSTR.equals(mem[0])) {
            int diff = Integer.parseInt(sdf.format(new Date())) - Integer.parseInt(mem[1]);
            Log.e("diff", " " + diff);
            if (diff < 2) {
                Log.d("Download_Data", "I am not downloading " + fileUrlSTR + " because it was downloaded "
                        + diff + " minutes ago");
                return true;
            }
        }

    if (!OS_Utils.exists(fileUrlSTR)) {
        Log.e("Download_Data", "URL: " + fileUrlSTR + " called from " + Caller + " not exists to copy it to "
                + StringAndroidPath);
        return false;
    }

    int DEBUG_FLAG = 0;

    HttpURLConnection conn;
    URL fileUrl = null;
    try {
        fileUrl = new URL(fileUrlSTR);
    } catch (MalformedURLException e1) {
        return false;
    }

    try {
        conn = (HttpURLConnection) fileUrl.openConnection();

        DEBUG_FLAG = 1;

        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(100);

        DEBUG_FLAG = 2;

        int current = 0;
        byte[] buffer = new byte[10];
        while ((current = bis.read(buffer)) != -1)
            baf.append(buffer, 0, current);

        DEBUG_FLAG = 3;

        /* Convert the Bytes read to a String. */
        File fileAndroid;

        try {

            if (preservefilename) {
                int iSlash = fileUrlSTR.lastIndexOf("/");
                fileAndroid = new File(StringAndroidPath + "/" + fileUrlSTR.substring(iSlash + 1));
            } else
                fileAndroid = new File(StringAndroidPath);

        } catch (Exception e) {
            Log.e("Download_Data.DownAndCopy", "I can not create " + StringAndroidPath);
            bis.close();
            conn.disconnect();
            return false;
        }

        DEBUG_FLAG = 4;

        FileOutputStream fos = new FileOutputStream(fileAndroid);
        fos.write(baf.toByteArray());

        DEBUG_FLAG = 5;

        bis.close();
        fos.close();
        conn.disconnect();

        MemDown.add(new String[] { fileUrlSTR, sdf.format(new Date()) });

        return true; //returns including the filename
    } catch (IOException e) {
        Log.e("Download_Data", "Download_Data: Error when trying to download:  " + fileUrl.toString() + " to "
                + StringAndroidPath + " DEBUG_FLAG=" + DEBUG_FLAG);
        return false;
    }

}