Search in sources :

Example 1 with FileInputStream

use of info.guardianproject.iocipher.FileInputStream in project Zom-Android by zom.

the class OtrDataHandler method onIncomingRequest.

public synchronized void onIncomingRequest(Address requestThem, Address requestUs, byte[] value) {
    // Log.e( TAG, "onIncomingRequest:" + requestThem);
    SessionInputBuffer inBuf = new MemorySessionInputBuffer(value);
    HttpRequestParser parser = new HttpRequestParser(inBuf, lineParser, requestFactory, params);
    HttpRequest req;
    try {
        req = (HttpRequest) parser.parse();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (HttpException e) {
        e.printStackTrace();
        return;
    }
    String requestMethod = req.getRequestLine().getMethod();
    String uid = req.getFirstHeader("Request-Id").getValue();
    String url = req.getRequestLine().getUri();
    if (requestMethod.equals("OFFER")) {
        debug("incoming OFFER " + url);
        for (Header header : req.getAllHeaders()) {
            debug("incoming header: " + header.getName() + "=" + header.getValue());
        }
        if (!url.startsWith(URI_PREFIX_OTR_IN_BAND)) {
            debug("Unknown url scheme " + url);
            sendResponse(requestUs, requestThem, 400, "Unknown scheme", uid, EMPTY_BODY);
            return;
        }
        if (!req.containsHeader("File-Length")) {
            sendResponse(requestUs, requestThem, 400, "File-Length must be supplied", uid, EMPTY_BODY);
            return;
        }
        int length = Integer.parseInt(req.getFirstHeader("File-Length").getValue());
        if (!req.containsHeader("File-Hash-SHA1")) {
            sendResponse(requestUs, requestThem, 400, "File-Hash-SHA1 must be supplied", uid, EMPTY_BODY);
            return;
        }
        sendResponse(requestUs, requestThem, 200, "OK", uid, EMPTY_BODY);
        String sum = req.getFirstHeader("File-Hash-SHA1").getValue();
        String type = null;
        if (req.containsHeader("Mime-Type")) {
            type = req.getFirstHeader("Mime-Type").getValue();
        }
        debug("Incoming sha1sum " + sum);
        Transfer transfer;
        try {
            transfer = new VfsTransfer(url, type, length, requestUs, requestThem, sum);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        transferCache.put(url, transfer);
        // Handle offer
        // TODO ask user to confirm we want this
        boolean accept = false;
        if (mDataListener != null) {
            try {
                mDataListener.onTransferRequested(url, requestThem.getAddress(), requestUs.getAddress(), transfer.url);
            // callback is now async, via "acceptTransfer" method
            // if (accept)
            // transfer.perform();
            } catch (RemoteException e) {
                LogCleaner.error(ImApp.LOG_TAG, "error approving OTRDATA transfer request", e);
            }
        }
    } else if (requestMethod.equals("GET") && url.startsWith(URI_PREFIX_OTR_IN_BAND)) {
        debug("incoming GET " + url);
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        int reqEnd;
        try {
            Offer offer = offerCache.get(url);
            if (offer == null) {
                sendResponse(requestUs, requestThem, 400, "No such offer made", uid, EMPTY_BODY);
                return;
            }
            // in case we don't see a response to underlying request, but peer still proceeds
            offer.seen();
            if (!req.containsHeader("Range")) {
                sendResponse(requestUs, requestThem, 400, "Range must start with bytes=", uid, EMPTY_BODY);
                return;
            }
            String rangeHeader = req.getFirstHeader("Range").getValue();
            String[] spec = rangeHeader.split("=");
            if (spec.length != 2 || !spec[0].equals("bytes")) {
                sendResponse(requestUs, requestThem, 400, "Range must start with bytes=", uid, EMPTY_BODY);
                return;
            }
            String[] startEnd = spec[1].split("-");
            if (startEnd.length != 2) {
                sendResponse(requestUs, requestThem, 400, "Range must be START-END", uid, EMPTY_BODY);
                return;
            }
            int start = Integer.parseInt(startEnd[0]);
            int end = Integer.parseInt(startEnd[1]);
            if (end - start + 1 > MAX_CHUNK_LENGTH) {
                sendResponse(requestUs, requestThem, 400, "Range must be at most " + MAX_CHUNK_LENGTH, uid, EMPTY_BODY);
                return;
            }
            File fileGet = new File(offer.getUri());
            long fileLength = -1;
            if (fileGet.exists()) {
                fileLength = fileGet.length();
                FileInputStream is = new FileInputStream(fileGet);
                readIntoByteBuffer(byteBuffer, is, start, end);
                is.close();
            } else {
                java.io.File fileGetExtern = new java.io.File(offer.getUri());
                if (fileGetExtern.exists()) {
                    fileLength = fileGetExtern.length();
                    java.io.FileInputStream is = new java.io.FileInputStream(fileGetExtern);
                    readIntoByteBuffer(byteBuffer, is, start, end);
                    is.close();
                }
            }
            if (mDataListener != null && fileLength != -1) {
                float percent = ((float) end) / ((float) fileLength);
                mDataListener.onTransferProgress(true, offer.getId(), requestThem.getAddress(), offer.getUri(), percent);
                String mimeType = null;
                if (req.getFirstHeader("Mime-Type") != null)
                    mimeType = req.getFirstHeader("Mime-Type").getValue();
                mDataListener.onTransferComplete(true, offer.getId(), requestThem.getAddress(), offer.getUri(), mimeType, offer.getUri());
            }
        } catch (UnsupportedEncodingException e) {
            // throw new RuntimeException(e);
            sendResponse(requestUs, requestThem, 400, "Unsupported encoding", uid, EMPTY_BODY);
            return;
        } catch (IOException e) {
            // throw new RuntimeException(e);
            sendResponse(requestUs, requestThem, 400, "IOException", uid, EMPTY_BODY);
            return;
        } catch (NumberFormatException e) {
            sendResponse(requestUs, requestThem, 400, "Range is not numeric", uid, EMPTY_BODY);
            return;
        } catch (Exception e) {
            sendResponse(requestUs, requestThem, 500, "Unknown error", uid, EMPTY_BODY);
            return;
        }
        byte[] body = byteBuffer.toByteArray();
        // debug("Sent sha1 is " + sha1sum(body));
        sendResponse(requestUs, requestThem, 200, "OK", uid, body);
    } else {
        debug("Unknown method / url " + requestMethod + " " + url);
        sendResponse(requestUs, requestThem, 400, "OK", uid, EMPTY_BODY);
    }
}
Also used : HttpException(cz.msebera.android.httpclient.HttpException) HttpRequest(cz.msebera.android.httpclient.HttpRequest) BasicHttpRequest(cz.msebera.android.httpclient.message.BasicHttpRequest) AbstractSessionInputBuffer(cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer) SessionInputBuffer(cz.msebera.android.httpclient.io.SessionInputBuffer) HttpRequestParser(cz.msebera.android.httpclient.impl.io.HttpRequestParser) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(info.guardianproject.iocipher.FileInputStream) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RemoteException(android.os.RemoteException) HttpException(cz.msebera.android.httpclient.HttpException) IOException(java.io.IOException) MethodNotSupportedException(cz.msebera.android.httpclient.MethodNotSupportedException) Header(cz.msebera.android.httpclient.Header) RemoteException(android.os.RemoteException) RandomAccessFile(info.guardianproject.iocipher.RandomAccessFile) File(info.guardianproject.iocipher.File)

Example 2 with FileInputStream

use of info.guardianproject.iocipher.FileInputStream in project Zom-Android by zom.

the class SecureMediaStore method getThumbnailVfs.

public static Bitmap getThumbnailVfs(Uri uri, int thumbnailSize) {
    if (!VirtualFileSystem.get().isMounted())
        return null;
    File image = new File(uri.getPath());
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inInputShareable = true;
    options.inPurgeable = true;
    try {
        FileInputStream fis = new FileInputStream(new File(image.getPath()));
        BitmapFactory.decodeStream(fis, null, options);
    } catch (Exception e) {
        LogCleaner.warn(ImApp.LOG_TAG, "unable to read vfs thumbnail" + e.toString());
        return null;
    }
    if ((options.outWidth == -1) || (options.outHeight == -1))
        return null;
    int originalSize = (options.outHeight > options.outWidth) ? options.outHeight : options.outWidth;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / thumbnailSize;
    try {
        FileInputStream fis = new FileInputStream(new File(image.getPath()));
        Bitmap scaledBitmap = BitmapFactory.decodeStream(fis, null, opts);
        return scaledBitmap;
    } catch (FileNotFoundException e) {
        LogCleaner.warn(ImApp.LOG_TAG, "can't find IOcipher file: " + image.getPath());
        return null;
    } catch (OutOfMemoryError oe) {
        LogCleaner.error(ImApp.LOG_TAG, "out of memory loading thumbnail: " + image.getPath(), oe);
        return null;
    }
}
Also used : Bitmap(android.graphics.Bitmap) FileNotFoundException(java.io.FileNotFoundException) BitmapFactory(android.graphics.BitmapFactory) File(info.guardianproject.iocipher.File) FileInputStream(info.guardianproject.iocipher.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with FileInputStream

use of info.guardianproject.iocipher.FileInputStream in project Zom-Android by zom.

the class SecureMediaStore method copyToExternal.

public static void copyToExternal(String sourcePath, java.io.File targetPath) throws IOException {
    // copy
    FileInputStream fis = new FileInputStream(new File(sourcePath));
    java.io.FileOutputStream fos = new java.io.FileOutputStream(targetPath, false);
    IOUtils.copyLarge(fis, fos);
    fos.close();
    fis.close();
}
Also used : FileOutputStream(info.guardianproject.iocipher.FileOutputStream) File(info.guardianproject.iocipher.File) FileInputStream(info.guardianproject.iocipher.FileInputStream)

Example 4 with FileInputStream

use of info.guardianproject.iocipher.FileInputStream in project Zom-Android by zom.

the class HttpMediaStreamer method create.

private Uri create(final File fileStream, final String mimeType) throws IOException {
    try {
        if (serverSocket != null)
            serverSocket.close();
    } catch (Exception e) {
    }
    // use random free port
    serverSocket = new ServerSocket(0);
    new Thread() {

        public void run() {
            try {
                while (true) {
                    Socket socket = serverSocket.accept();
                    byte[] b = new byte[8192];
                    int len;
                    InputStream is = socket.getInputStream();
                    StringBuilder isb = new StringBuilder();
                    len = is.read(b);
                    isb.append(new String(b));
                    // Log.i(TAG, "request: " + isb.toString());
                    StringBuilder sb = new StringBuilder();
                    sb.append("HTTP/1.1 200\r\n");
                    sb.append("Content-Type: " + mimeType + "\r\n");
                    sb.append("Content-Length: " + fileStream.length() + "\r\n\r\n");
                    Log.d(TAG, "sharing content of length: " + fileStream.length());
                    BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
                    bos.write(sb.toString().getBytes());
                    FileInputStream fis = new FileInputStream(fileStream);
                    int idx = 0;
                    while ((len = fis.read(b)) != -1) {
                        bos.write(b, 0, len);
                        idx += len;
                        Log.d(TAG, "sharing via stream: " + idx);
                    }
                    fis.close();
                    bos.flush();
                    bos.close();
                    socket.close();
                }
            } catch (IOException e) {
                Log.d(TAG, "web share error", e);
            }
        }
    }.start();
    Uri uri = Uri.parse("http://localhost:" + serverSocket.getLocalPort() + fileStream.getAbsolutePath());
    return uri;
}
Also used : FileInputStream(info.guardianproject.iocipher.FileInputStream) InputStream(java.io.InputStream) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) Uri(android.net.Uri) IOException(java.io.IOException) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) FileInputStream(info.guardianproject.iocipher.FileInputStream)

Example 5 with FileInputStream

use of info.guardianproject.iocipher.FileInputStream in project Zom-Android by zom.

the class SecureCameraActivity method getThumbnail.

public Bitmap getThumbnail(ContentResolver cr, String filename) throws IOException {
    File file = new File(filename);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inInputShareable = true;
    options.inPurgeable = true;
    FileInputStream fis = new FileInputStream(file);
    BitmapFactory.decodeStream(fis, null, options);
    fis.close();
    if ((options.outWidth == -1) || (options.outHeight == -1))
        throw new IOException("Bad image " + file);
    int originalSize = (options.outHeight > options.outWidth) ? options.outHeight : options.outWidth;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = originalSize / THUMBNAIL_SIZE;
    fis = new FileInputStream(file);
    Bitmap scaledBitmap = BitmapFactory.decodeStream(fis, null, opts);
    return scaledBitmap;
}
Also used : Bitmap(android.graphics.Bitmap) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException) File(info.guardianproject.iocipher.File) FileInputStream(info.guardianproject.iocipher.FileInputStream)

Aggregations

FileInputStream (info.guardianproject.iocipher.FileInputStream)10 IOException (java.io.IOException)9 File (info.guardianproject.iocipher.File)5 DataInputStream (java.io.DataInputStream)4 Bitmap (android.graphics.Bitmap)2 BitmapFactory (android.graphics.BitmapFactory)2 HttpRequest (cz.msebera.android.httpclient.HttpRequest)2 BasicHttpRequest (cz.msebera.android.httpclient.message.BasicHttpRequest)2 RandomAccessFile (info.guardianproject.iocipher.RandomAccessFile)2 FileNotFoundException (java.io.FileNotFoundException)2 Uri (android.net.Uri)1 RemoteException (android.os.RemoteException)1 Header (cz.msebera.android.httpclient.Header)1 HttpException (cz.msebera.android.httpclient.HttpException)1 MethodNotSupportedException (cz.msebera.android.httpclient.MethodNotSupportedException)1 AbstractSessionInputBuffer (cz.msebera.android.httpclient.impl.io.AbstractSessionInputBuffer)1 HttpRequestParser (cz.msebera.android.httpclient.impl.io.HttpRequestParser)1 SessionInputBuffer (cz.msebera.android.httpclient.io.SessionInputBuffer)1 FileOutputStream (info.guardianproject.iocipher.FileOutputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1