wangzhibo
2026-08-16 ae6f40460dcd56af6c5f60ba52c883854c3bac55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package nodomain.freeyourgadget.gadgetbridge.test;
 
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
 
import androidx.annotation.NonNull;
 
class HexToBinaryInputStream extends FilterInputStream {
    HexToBinaryInputStream(InputStream in) {
        super(in);
    }
 
    @Override
    public int read() throws IOException {
        int value;
        StringBuilder buffer = new StringBuilder(4);
 
        loop:
        while (true) {
            value = super.read();
            switch (value) {
                case -1:
                case ' ':
                case '\r':
                case '\n':
                    break loop;
                default:
                    buffer.append((char) value);
            }
        }
        if (buffer.length() > 0) {
            return Integer.decode(buffer.toString());
        }
        return -1;
    }
 
    @Override
    public int read(@NonNull byte[] b, int off, int len) throws IOException {
        if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        }
        for (int i = 0; i < len; i++) {
            int value = read();
            if (value == -1) {
                return i;
            }
            b[off + i] = (byte) value;
        }
        return len;
    }
}