• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

link-intersystems / lis-commons / #256

04 Nov 2023 12:59PM UTC coverage: 89.671% (-0.1%) from 89.804%
#256

push

renelink
Added wake on lan and mac address handling.

102 of 126 new or added lines in 5 files covered. (80.95%)

7492 of 8355 relevant lines covered (89.67%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.04
/lis-commons-net/src/main/java/com/link_intersystems/net/MACFormat.java
1
package com.link_intersystems.net;
2

3
import java.nio.ByteBuffer;
4
import java.text.FieldPosition;
5
import java.text.Format;
6
import java.text.ParseException;
7
import java.text.ParsePosition;
8

9
public class MACFormat extends Format {
1✔
10
    public static final char IEEE_802_OCTET_DELIM = '-';
11

12
    private char octetDelim = IEEE_802_OCTET_DELIM;
1✔
13

14
    public void setOctetDelim(char octetDelim) {
15
        this.octetDelim = octetDelim;
1✔
16
    }
1✔
17

18
    @Override
19
    public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
20
        if (!(obj instanceof MAC)) {
1✔
21
            throw new IllegalArgumentException("obj is not a " + MAC.class.getName());
1✔
22
        }
23

24
        MAC mac = (MAC) obj;
1✔
25
        byte[] byteArray = mac.toByteArray();
1✔
26

27
        for (int i = 0; i < byteArray.length; i++) {
1✔
28
            pos.setBeginIndex(i);
1✔
29
            pos.setEndIndex(i + 3);
1✔
30
            byte octet = byteArray[i];
1✔
31
            String formattedOctet = String.format("%02X", octet);
1✔
32
            toAppendTo.append(formattedOctet);
1✔
33
            if (i + 1 < byteArray.length) {
1✔
34
                toAppendTo.append(octetDelim);
1✔
35
            }
36
        }
37

38
        return toAppendTo;
1✔
39
    }
40

41
    public MAC parse(String source) throws ParseException {
42
        ParsePosition pos = new ParsePosition(0);
1✔
43
        MAC result = parseObject(source, pos);
1✔
44
        if (pos.getIndex() == 0) {
1✔
45
            StringBuilder msg = new StringBuilder();
1✔
46
            msg.append("Unable to parse MAC with octet delimiter ");
1✔
47
            msg.append("[");
1✔
48
            msg.append(octetDelim);
1✔
49
            msg.append("]");
1✔
50
            msg.append(" : \"");
1✔
51
            msg.append(source);
1✔
52
            msg.append("\" ");
1✔
53
            msg.append("error at index ");
1✔
54
            msg.append(pos.getErrorIndex());
1✔
55
            msg.append(" [");
1✔
56
            msg.append(source.charAt(pos.getErrorIndex()));
1✔
57
            msg.append("]");
1✔
58

59
            throw new ParseException(msg.toString(), pos.getErrorIndex());
1✔
60
        }
61
        return result;
1✔
62
    }
63

64
    @Override
65
    public MAC parseObject(String source, ParsePosition pos) {
66
        ByteBuffer macBuff = ByteBuffer.allocate(6);
1✔
67

68
        int startPos = pos.getIndex();
1✔
69

70
        for (int i = pos.getIndex(); i < startPos + 17; i += 3) {
1✔
71
            String octet = source.substring(i, i + 2);
1✔
72
            try {
73
                int parsedOctet = Integer.parseInt(octet, 16);
1✔
74
                macBuff.put((byte) parsedOctet);
1✔
75
            } catch (NumberFormatException e) {
1✔
76
                pos.setErrorIndex(i);
1✔
77
                return null;
1✔
78
            }
1✔
79

80
            if (i + 2 < startPos + 17 && source.charAt(i + 2) != octetDelim) {
1✔
81
                pos.setErrorIndex(i + 2);
1✔
82
                return null;
1✔
83
            }
84

85

86
        }
87
        pos.setIndex(startPos + 17);
1✔
88

89
        return new MAC(macBuff.array());
1✔
90
    }
91

92

93
    @Override
94
    public MAC parseObject(String source) throws ParseException {
NEW
95
        return (MAC) super.parseObject(source);
×
96
    }
97
}
98

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc