确保从InputStream读取正确的数据 [英] Ensuring right data is read from an InputStream

查看:458
本文介绍了确保从InputStream读取正确的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根应用程序,它应该在执行期间的某个时刻捕获屏幕。为了实现这一点,我使用以下代码与Android shell进行交互:

I have a root application that is supposed to capture the screen at some point during the execution. In order to accomplish this, I interact with the Android shell using the following code:

private static Process su = Runtime.getRuntime().exec("su");
private static DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
private static DataInputStream inputStream = new DataInputStream(su.getInputStream());

private void CaptureScreen() {
    outputStream.writeBytes("/system/bin/screencap -p\n");
    outputStream.flush();
    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
    //outputStream.writeBytes("echo test\n");
    //outputStream.flush();
}

它工作正常,即使我多次调用它,但是当下我发出一个伪命令,在CaptureScreen调用之间产生一个shell输出,BitmapFactory.decodeStream失败。考虑到这一点,我有几个问题:

It works fine, even when I call it multiple times, but the moment I issue a dummy command that produces a shell output between CaptureScreen calls, BitmapFactory.decodeStream fails. Considering this, I have a few questions:


  • 我认为这是因为InputStream中的数据不是
    更长的纯粹相关图像数据。由于运行时是单个
    实例(提到这里),我再次假设其他
    进程也可以在
    实时系统的InputStream中引入它们的输出。如何确保只从
    输入流中获取所需的数据?

  • 为什么CaptureScreen多次调用时工作正常?如何
    BitmapFactory.decodeStream如何设法从
    InputStream获取最后一个图像?它成功后会消耗相关数据吗?
    它是否从InputStream中搜索最后一个图像数据?如果是这样,为什么

    InputStream中的图像数据之前存在无关数据时失败?

  • I assume this happens because the data within the InputStream is no longer purely related to image data. Since the runtime is single instance (as referred to here), I again assume that other processes can also introduce their outputs within my InputStream on a live system. How can I make sure to only get the data I need from the InputStream?
  • Why does CaptureScreen work correctly when called multiple times? How does BitmapFactory.decodeStream manage to get the last image from the InputStream? Does it "consume" relevant data when it succeeds? Does it search for the last image data from the InputStream? If so, why does it fail when there is irrelevant data before the image data within the InputStream?

我知道我可以通过将图像写入文件然后从那里读取来解决这个问题,但我希望避免I / O操作以支持性能。

I know I can get around this problem by writing the image to a file then read it from there, but I would like to avoid I/O operations in favor of performance.

推荐答案

经过一段时间的努力,我找到了自己问题的答案:

After toying around with this for a while, I found the answer to my own questions:


  • 虽然运行时是单个实例,但在其上执行代码会自动启动一个新进程。与此过程相关的输入和输出流只能由此过程写入/读取(除非另一个过程专门将其流重定向到此过程,这在我的情况下极不可能)。

  • 目录在读取时确实消耗输入流,因此多次调用该函数完全写入/读取图像数据。使用与图像无关的数据污染流程的输入流会破坏decodeStream的功能。

还要记住su 不是结束的命令。在被调用之前它不会终止。这是我在代码中使用的修订类:

Also keep in mind that "su" isn't a command that ends. It does not terminate until called to do so. Here's the revised class I use in my code:

public class BitmapScreencap {
    public final static BitmapScreencap Get = new BitmapScreencap();
    private BitmapScreencap() { }
    public Bitmap Screen() {
        try {
            Process process = Runtime.getRuntime().exec("su");
            OutputStreamWriter outputStream = new OutputStreamWriter(process.getOutputStream());
            outputStream.write("/system/bin/screencap -p\n");
            outputStream.flush();
            Bitmap screen = BitmapFactory.decodeStream(process.getInputStream());
            outputStream.write("exit\n");
            outputStream.flush();
            outputStream.close();
            return screen;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

可以从项目中的任何位置调用它:

And it can be called from anywhere within your project as:

BitmapScreencap.Get.Screen();

这篇关于确保从InputStream读取正确的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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