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

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

Introduction

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

Prototype

public void append(int i) 

Source Link

Usage

From source file:com.undatech.opaque.RemoteCanvasActivity.java

/**
 * Launches a remote desktop session using a .vv file.
 * @param i//ww  w .j a v  a  2s.  c  o  m
 * @return the vv file name or NULL if no file was discovered.
 */
private String startSessionFromVvFile(Intent i) {
    final Uri data = i.getData();
    String vvFileName = null;

    android.util.Log.d(TAG, "got intent: " + i.toString());

    if (data != null) {
        android.util.Log.d(TAG, "got data: " + data.toString());

        if (data.toString().startsWith("http")) {
            android.util.Log.d(TAG, "Intent is with http scheme.");
            final String tempVvFile = getFilesDir() + "/tempfile.vv";
            vvFileName = tempVvFile;
            // Spin up a thread to grab the file over the network.
            Thread t = new Thread() {
                @Override
                public void run() {
                    try {
                        URL url = new URL(data.toString());
                        File file = new File(tempVvFile);

                        URLConnection ucon = url.openConnection();
                        InputStream is = ucon.getInputStream();
                        BufferedInputStream bis = new BufferedInputStream(is);

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

                        FileOutputStream fos = new FileOutputStream(file);
                        fos.write(baf.toByteArray());
                        fos.close();

                        synchronized (RemoteCanvasActivity.this) {
                            RemoteCanvasActivity.this.notify();
                        }
                    } catch (Exception e) {
                    }
                }
            };
            t.start();

            synchronized (this) {
                try {
                    this.wait(5000);
                } catch (InterruptedException e) {
                    vvFileName = null;
                    e.printStackTrace();
                }
            }
        } else if (data.toString().startsWith("file")) {
            android.util.Log.d(TAG, "Intent is with file scheme.");
            vvFileName = data.getPath();
        } else if (data.toString().startsWith("content")) {
            android.util.Log.d(TAG, "Intent is with content scheme.");

            String[] projection = { MediaStore.MediaColumns.DATA };
            ContentResolver resolver = getApplicationContext().getContentResolver();
            Cursor cursor = resolver.query(data, projection, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    vvFileName = cursor.getString(0);
                }
                cursor.close();
            }
        }

        File f = new File(vvFileName);
        android.util.Log.d(TAG, "got filename: " + vvFileName);

        if (f.exists()) {
            android.util.Log.d(TAG, "Initializing session from vv file: " + vvFileName);
            connection = new ConnectionSettings("defaultSettings");
            connection.loadFromSharedPreferences(getApplicationContext());
        } else {
            vvFileName = null;
            // Quit with an error if the file does not exist.
            MessageDialogs.displayMessageAndFinish(this, R.string.vv_file_not_found,
                    R.string.error_dialog_title);
        }
    }
    return vvFileName;
}