MemoryFile 使用demo

 //A进程写数据

public static ParcelFileDescriptor writeNodeInfo(String name, AccessibilityNodeInfoTree rootNode, int[] outSize) {
        try {
            //from AccessibilityNodeInfoTree to json
            JSONObject root = new JSONObject();
            changeToJson(rootNode, root);
            //format data
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(root.toString());
            byte[] bytes = byteArrayOutputStream.toByteArray();
            byteArrayOutputStream.close();
            objectOutputStream.close();
            outSize[0] = bytes.length;
            Log.e(TAG, "writeNodeInfo, size=" + outSize[0] + ", data:" + root.toString());
            // write data
            MemoryFile memoryFile = new MemoryFile(name, bytes.length);
            memoryFile.getOutputStream().write(bytes);
            Method method = MemoryFile.class.getDeclaredMethod("getFileDescriptor");
            FileDescriptor des = (FileDescriptor) method.invoke(memoryFile);
            ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(des);
            return pfd;
        } catch (Exception e) {
            Log.e(TAG, "writeNodeInfo fail: " + e);
        }
        return null;
}

//B进程读数据

public static JSONObject readNodeInfo(ParcelFileDescriptor pfd, int size) {
        try {
            //read data
            if (pfd == null) return null;
            byte[] data = new byte[size];
            FileDescriptor descriptor = pfd.getFileDescriptor();
            FileInputStream fileInputStream = new FileInputStream(descriptor);
            fileInputStream.read(data);
            //format data
            final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
            final ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            final Object object = objectInputStream.readObject();
            final JSONObject rootNode = new JSONObject((String)object);
            byteArrayInputStream.close();
            objectInputStream.close();
            Log.e(TAG, "readNodeInfo, size=" + size + ", data:" + rootNode.toString());
            return rootNode;
        } catch (Exception e) {
            Log.e(TAG, "readNodeInfo fail: " + e);
        }
        return null;
}

A进程通过MemoryFile将数据(必须是Serializable类型数据,Parcelable不能序列化到本地)写入共享内存,然后创建对应的FileDescriptor,再将FileDescriptor转成可序列划的ParcelFileDescriptor,将此描述符通过aidl传递给B进程。B进程拿到ParcelFileDescriptor,再获取FileDescriptor,读取数据。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HuggingFace是一个开源的自然语言处理(NLP)模型库,提供了各种预训练的AI模型。您可以通过访问HuggingFace的官方网站或GitHub页面来使用它们。引用 使用HuggingFace的Demo非常简单。您可以通过以下步骤来使用HuggingFace的Demo: 1. 首先,您需要打开HuggingFace的官方网站或GitHub页面,找到您感兴趣的Demo模型。 2. 在Demo页面,您可以看到模型的描述和用法说明。这些说明将帮助您理解如何使用Demo模型。 3. 接下来,您可以使用提供的示例代码或命令行指令来运行Demo。这些示例代码或指令通常会在文档中提供。 4. 根据需要,您可以在Demo中输入相应的文本或参数。这些输入将作为模型的输入,模型会根据这些输入生成相应的输出。 举个例子,如果您想使用HuggingFace的ChatGPT模型进行对话,您可以通过编写代码或使用命令行指令来启动Demo。然后,您可以输入要与模型对话的文本,并查看模型生成的响应。引用 另外,HuggingFace还提供了其他类型的Demo。例如,它使用了名为「runwayml/stable-diffusion-1-5」的模型来生成跳舞的猫的图片。您可以根据Demo文档中提供的说明和示例代码来使用这个功能。引用 总结起来,要使用HuggingFace的Demo,您需要找到相应的模型,并按照Demo文档中的说明进行操作。您可以通过提供输入来与模型交互,并查看生成的输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值