簡體   English   中英

為什么我的InputStream保留舊值?

[英]Why does my InputStream hold old values?

我正在向IR接收器發送IR信號,該信號通過BT模塊傳遞到Android應用程序的InputStream 每次我按下發射器上的一個按鈕時,我都會發送100 bytes ,然后我希望它進入InputStream (是否存在一種方法來處理由於包損壞而沒有100字節的異常?)。

這是我的代碼,用於讀取InputStream並將值放入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個字節,隨后又發送了下一個字節,結果是將一個字節放入上一個緩沖區,並丟失了第一個字節。

但是有趣的是,數據包始終具有100個字節,並且在那里不是虛擬的(零值)。

這些值是加速度計坐標(並不重要)。

正確值示例:

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

錯誤值的示例(丟失第一個字節也可能發生,但是在這種情況下,開頭還有一個字節):

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個字節的數據包。

InputStream是一個非常低級的API,您幾乎永遠不想直接使用它。 在這種情況下,最適合您目的的包裝器是DataInputStream ,它具有一個readFully方法,該方法似乎正是您想要的。 代碼更簡單,更短。

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

如果這不起作用,那么我懷疑問題出在IR本身的數據流中。


如果您需要在超時的情況下從流中讀取數據(在數據包損壞的情況下),則不建議使用此方法,因為它會阻塞,正如您已經注意到的那樣。 在這種情況下,我建議閱讀以下問題中的信息: 是否可以在超時的情況下從InputStream讀取?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM