安卓ParcelFileDescriptor" createpipe"方法64KB错误 [英] Android: ParcelFileDescriptor "createpipe" method 64KB error

查看:352
本文介绍了安卓ParcelFileDescriptor" createpipe"方法64KB错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ContentProvider类的应用程序。
在中openFile方法,我需要能够脱code文件&安培;返回作为数据流。
所以我决定使用内置的管道。

I have an app that uses the ContentProvider class. In the openFile method, I need to be able to decode a file & return as a stream of data. So I decided to use the built in pipe.

问题是如果我使用createPipe方法,我可以只写64KB到它。
之后,我无法将数据写入到管道。
另外请注意,我不能读,直到数据是完全去codeD&放大器;写入到管

The problem is If I use the createPipe method, I am able to write only 64KB into it. After that I am unable to write data into the Pipe. Also note that I cannot read until the data is fully decoded & written into the pipe.

package com.aujas.html.viewer.content;
public class LocalFileContentProvider extends ContentProvider {

private static final String URI_PREFIX = "content://com.aujas.html.viewer.localfile.dec/";
public static File file;
public String filename;
public ParcelFileDescriptor[] parcels;

public static String constructUri(String url) {
    String editString = url.replaceAll("%20", " ");
    int n = editString.length();
    String uri = editString.substring(5, n - 1);
    Log.d("URI", uri);
    return URI_PREFIX + uri + "\"";
}

public ParcelFileDescriptor openFile(Uri uri, String mode) {

    Log.d("OPEN", uri.getPath());
    return parcels[0];

}

@Override
public boolean onCreate() {
    return true;
}

@Override
public int delete(Uri uri, String s, String[] as) {
    throw new UnsupportedOperationException(
            "Not supported by this provider");
}

@Override
public String getType(Uri uri) {
    throw new UnsupportedOperationException(
            "Not supported by this provider");
}

@Override
public Uri insert(Uri uri, ContentValues contentvalues) {
    throw new UnsupportedOperationException(
            "Not supported by this provider");
}

@Override
public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
    throw new UnsupportedOperationException(
            "Not supported by this provider");
}

@Override
public int update(Uri uri, ContentValues contentvalues, String s,
        String[] as) {
    throw new UnsupportedOperationException(
            "Not supported by this provider");
}

class DecryptAsync extends AsyncTask<String, Integer, String> {

    @Override
    protected String doInBackground(String... paramArrayOfParams) {
        // TODO Auto-generated method stub
        try {
            file = new File(paramArrayOfParams[0]);
            Log.d("DecrypOpened", file.toString());
            parcels = ParcelFileDescriptor.createPipe();
            Log.d("filebeindec", LocalFileContentProvider.file.toString());
            FileInputStream fis = new FileInputStream(LocalFileContentProvider.file);

            android.os.ParcelFileDescriptor.AutoCloseOutputStream out = new android.os.ParcelFileDescriptor.AutoCloseOutputStream(parcels[1]);
            Cipher ecipher;
            Cipher dcipher;
            SecretKey key;
            String input = "768f8a949de079da";
            byte[] encoded = new BigInteger(input, 16).toByteArray();
            key = new SecretKeySpec(encoded, "DES");
            byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C,
                    0x07, 0x72, 0x6F, 0x5A };
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
            ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
            byte[] buf = new byte[1024];
            InputStream in = new CipherInputStream(fis, dcipher);
            int numRead = 0;
            int n = 1;
            while ((numRead = in.read(buf)) >= 0) {
                n++;
                out.write(buf, 0, numRead);
                Log.d("Error", "SD");
                if (n == 64) {
                    out.flush();
                    out.flush();
                    n = 0;
                }
            }

            Log.d("Decypt Done", out.toString());
        } catch (Exception e) {
            Log.d("AsyncError", e.toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }

}

}

当我执行此我可以只写64KB。由于我使用每次写入1KB,我得到64日志后没有任何反应。
是否有对这些管道的大小限制?有没有解决办法?
感谢和放大器;问候
rtindru

When I execute this I get to write 64KB only. Since I use 1KB per write, I get 64 Logs after which nothing happens. Is there a size limit on these pipes? Is there a workaround? Thanks & Regards rtindru

推荐答案

Linux内核(至少是烤成的Andr​​oid版本)有一个管64K缓冲区限制。

Linux kernel (at least the version which is baked into Android) has 64k buffer limit for a pipe.

所以,你可以不写整个文件(大于64K进去)。你所要做的就是

So, you can't write the whole file (bigger than 64k into it). What you have to do is

a)创建管

二)创建作家线程,这将写入该管道。它会阻止并等待,直到读线程将读取管道的内容。

b) Create writer thread, which will write to this pipe. It will block and wait until reader thread will read the content from pipe.

C)启动此线程

D)返回读卡器文件描述符到客户端。

d) Return reader file descriptor to the client.

那么,会发生什么事,无论作者与读者会写和读simulteneously。作家将阻止时,它会在缓冲区填满和读写器会阻止,当它清空缓存。

So, what will happen, both writer and reader will write and read simulteneously. Writer will block when it will fill in the buffer and reader will block, when it emptied the buffer.

这篇关于安卓ParcelFileDescriptor&QUOT; createpipe&QUOT;方法64KB错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆