在输入流中搜索

发布于 2024-11-28 01:03:16 字数 204 浏览 3 评论 0原文

有没有办法在输入流上有效搜索 2 个固定字节?

背景

我必须处理 Android 上的多部分 Http 流量。 (来自 IP 网络摄像头的动态 JPEG)。

我已经在 anddev.org 上找到了一些类来处理它。现在我做了一些性能改进。为了找到 JPEG 的开头,我需要在 InputStream 中找到 JPEG 的幻数 (SOI=FFD8)。

Is there a way to do a efficient search for 2 fix bytes on an InputStream?

Background

I have to deal with Multipart Http traffic on Android. (Motion JPEG from a IP Webcam).

I already found some Classes on anddev.org to deal with it. Now I do some performance improvements. To find the start of a JPEG, I need to find the magic number for JPEGs (SOI=FFD8) in the InputStream.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杯别 2024-12-05 01:03:16

由于您不知道这 2 个字节在流中的位置,因此您必须查看整个输入。这意味着,您的表现至少是线性的。线性查找两个字节很简单:

static long search(InputStream inputStream) throws IOException {
    BufferedInputStream is = new BufferedInputStream(inputStream);
    int previous = is.read(read);
    long pos = 0;
    int current;
    while((current = is.read()) != -1) {
        pos++;
        if(previous == 0xff && current == 0xd8) {
            return pos;
        }
        last = current;
    }
    throw new RuntimeException("There ain't no pic in here.");
}

Since you've no idea where in the stream those 2 bytes are, you'll have to look at the entire input. That means, your performance will be at least linear. Two find two bytes linearly is straightforward:

static long search(InputStream inputStream) throws IOException {
    BufferedInputStream is = new BufferedInputStream(inputStream);
    int previous = is.read(read);
    long pos = 0;
    int current;
    while((current = is.read()) != -1) {
        pos++;
        if(previous == 0xff && current == 0xd8) {
            return pos;
        }
        last = current;
    }
    throw new RuntimeException("There ain't no pic in here.");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文