首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android: ParcelFileDescriptor "createpipe“方法64KB错误

Android: ParcelFileDescriptor "createpipe“方法64KB错误
EN

Stack Overflow用户
提问于 2012-06-21 20:58:39
回答 2查看 3.4K关注 0票数 1

我有一个使用ContentProvider类的应用程序。在openFile方法中,我需要能够解码文件并将其作为数据流返回。所以我决定使用内置管道。

问题是,如果我使用createPipe方法,我只能向其中写入64KB。在那之后,我就无法将数据写入到管道中。还要注意,在数据完全解码并写入管道之前,我无法读取。

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 2012-08-03 03:12:12

Linux内核(至少是Android中预制的版本)对管道有64k的缓冲限制。

因此,您不能写入整个文件(大于64k )。你要做的是

a)创建管道

b)创建写线程,该线程将写入该管道。它将阻塞并等待,直到读取器线程从管道读取内容。

c)启动该线程

d)向客户端返回阅读器文件描述符。

那么,将会发生什么,作者和读者将同时写作和阅读。写入器将在填充缓冲区时阻塞,而读取器将在清空缓冲区时阻塞。

票数 7
EN

Stack Overflow用户

发布于 2013-02-07 01:38:42

在回答另一个问题时,我使用@commonsware中的代码将@victor-ronin的答案扩展为代码:

Custom ContentProvider - openInputStream(), openOutputStream()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11138696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档