为什么我的InputStream持有旧值? [英] Why does my InputStream hold old values?

查看:208
本文介绍了为什么我的InputStream持有旧值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送一个IR信号,这是通过BT模块到的InputStream Android系统通过红外接收器应用程序。我每次preSS的发射器的按钮,我送100 字节然后我希望进入的InputStream (有没有一种方法来处理异常时,有没有100个字节,因为数据包被损坏了?)。

I am sending an IR signal to the IR receiver which is passed through the BT module to the InputStream of the Android application. Every time I press a button on the emitter, I send 100 bytes which then I expect to get into the InputStream (is there a way to handle an exception when there is no 100 bytes because the packet got corrupted?).

这是我的code阅读的InputStream 并把值到字节[]缓冲

This is my code for reading InputStream and putting values into the byte[] buffer:

 public int read(final InputStream input, final byte[] buffer) throws IOException {
        int remaining = buffer.length;
        try {
            Thread.sleep(100); // Not sure if this helps anything, just a desperate move
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while (remaining > 0) {
            final int location = buffer.length - remaining;
            final int count = input.read(buffer, location, remaining);
            if (count == -1) { // EOF
                break;
            }
            remaining -= count;
        }
        return buffer.length - remaining;
    }

它工作正常的几次,但在某些时候,一般经过4试试,我越来越提到的那些没有字节的第一个字节。好像包已经在某些时候被破坏,只有99字节发送和随后的下一个字节的发送,导致把一个字节为previous缓冲区,并失去了第一个字节。

It works fine for couple of times, but at some point, usually after 4th try, I am getting those mentioned bytes without the first byte. It seems like package has been corrupted at some point and only 99 bytes were sent and then subsequently the next sending of the bytes, result in putting one byte to the previous buffer and losing the first byte.

然而有趣的是,该分组具有总是100字节,并且它们不于有虚设(零值)。

However interesting is, that packets have always 100 bytes, and they are not dummy (zero values) in there.

这些值是加速度计坐标(其实并不重要)。

Those values are accelerometer coordinates (doesn't really matter).

例如正确的值:

0 = 1
1 = 35
2 = 0
3 = -27
4 = 19
5 = -4
6 = 64
7 = 10
8 = -7
9 = 66
10 = 10
11 = 0
12 = 66
13 = 4
14 = -1
15 = 64
16 = 8
17 = -1
18 = 67
19 = 7
20 = -3
21 = 66
22 = 6
23 = -1
24 = 65
25 = 7
26 = -2
27 = 66
28 = 7
29 = -3
30 = 65
31 = 6
32 = -3
33 = 67
34 = 6
35 = -2
36 = 66
37 = 7
38 = -2
39 = 66
40 = 4
41 = -3
42 = 66
43 = 6
44 = -3
45 = 66
46 = 6
47 = -3
48 = 66
49 = 5
50 = -3
51 = 66
52 = 6
53 = -2
54 = 65
55 = 5
56 = -3
57 = 65
58 = 6
59 = -3
60 = 66
61 = 6
62 = -3
63 = 66
64 = 6
65 = -3
66 = 66
67 = 6
68 = -2
69 = 66
70 = 5
71 = -3
72 = 66
73 = 5
74 = -3
75 = 66
76 = 5
77 = -2
78 = 66
79 = 5
80 = -3
81 = 66
82 = 5
83 = -3
84 = 66
85 = 6
86 = -2
87 = 66
88 = 5
89 = -2
90 = 65
91 = 5
92 = -2
93 = 65
94 = 5
95 = -2
96 = 66
97 = 4
98 = -2
99 = 66

实施例不正确的值(它也可能发生的是第一字节丢失,但是在这种情况下,存在在开始多一个字节):

Example incorrect values (it can also happen that first byte is missing, however in this case there is one more byte at the beginning):

0 = 66
1 = 1
2 = 35
3 = 3
4 = 10
5 = -14
6 = -17
7 = 81
8 = 15
9 = -5
10 = 64
11 = 14
12 = -4
13 = 68
14 = 7
15 = -5
16 = 69
17 = 9
18 = -5
19 = 78
20 = 15
21 = -3
22 = 77
23 = 16
24 = -2
25 = 72
26 = 20
27 = 3
28 = 64
29 = 20
30 = 8
31 = 55
32 = 22
33 = 16
34 = 50
35 = 22
36 = 19
37 = 52
38 = 11
39 = 13
40 = 50
41 = 6
42 = 14
43 = 50
44 = 9
45 = 13
46 = 54
47 = 12
48 = 9
49 = 63
50 = 16
51 = 12
52 = 67
53 = 6
54 = 0
55 = 74
56 = 8
57 = -4
58 = 75
59 = 13
60 = -2
61 = 68
62 = 17
63 = -5
64 = 79
65 = 8
66 = -8
67 = 62
68 = 15
69 = -8
70 = 65
71 = 13
72 = -7
73 = 67
74 = 8
75 = -6
76 = 66
77 = 9
78 = -3
79 = 67
80 = 8
81 = -4
82 = 66
83 = 7
84 = -4
85 = 65
86 = 8
87 = -4
88 = 67
89 = 7
90 = -4
91 = 66
92 = 7
93 = -3
94 = 67
95 = 6
96 = -4
97 = 66
98 = 6
99 = -3

不知道如何解决呢?请注意,我不小于3秒发送的字节2包的两倍,如果找到一个解决方案帮助。

Any idea how to solve it? Note, I don't send 2 packages of bytes twice in less than 3 seconds, if that helps in finding a solution.

推荐答案

的InputStream 是一个非常低级别的API,你几乎再也不想直接使用它。在这种情况下,这是最适合你的目的是包装 DataInputStream以,其中有一个<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html#readFully(byte[],%20int,%20int)\"相对=nofollow> 的readFully ,这似乎是你想要的方式;的code是简单得多和更短的

InputStream is a very low level API, you almost never want to use it directly. In this case, the wrapper that is most suited to your purposes is DataInputStream, which has a readFully method that seems to be what you want; the code is much simpler and shorter.

public void read(final InputStream input, final byte[] buffer) throws IOException, EOFException {
  DataInputStream dataStream = new DataInputStream(input);
  dataStream.readFully(buffer, 0, buffer.length);
}

如果这不起作用,那么我怀疑问题是一些来自IR本身的数据流。

If this doesn't work, then I suspect the problem is something with the data stream from the IR itself.

如果你需要从流超时(在一个损坏的数据包的情况下)读取,那么不建议这种方法,因为它会阻止,因为你已经注意到了。在这种情况下,我建议你阅读了这个问题的信息:是否有可能从一个超时的InputStream读

If you need to read from the stream with a timeout (in the case of a corrupted packet), then this method is not advised, because it will block, as you've already noticed. In such a case, I recommend reading the information in this question: Is it possible to read from a InputStream with a timeout?

这篇关于为什么我的InputStream持有旧值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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