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

ehrmann / vcdiff-java / 26

18 Apr 2026 05:11PM UTC coverage: 83.839% (+0.2%) from 83.679%
26

push

circleci

ehrmann
Fix Coveralls integration

663 of 814 branches covered (81.45%)

1603 of 1912 relevant lines covered (83.84%)

0.84 hits per line

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

96.05
/core/src/main/java/com/davidehrmann/vcdiff/util/VarInt.java
1
package com.davidehrmann.vcdiff.util;
2

3
import org.slf4j.Logger;
4
import org.slf4j.LoggerFactory;
5

6
import java.io.IOException;
7
import java.io.OutputStream;
8
import java.nio.ByteBuffer;
9
import java.util.Objects;
10

11
public final class VarInt {
12

13
    private static final Logger LOGGER = LoggerFactory.getLogger(VarInt.class);
1✔
14

15
    private VarInt() { }
16

17
    public static int getInt(ByteBuffer buffer) throws VarIntParseException, VarIntEndOfBufferException {
18
        final int startPosition = buffer.position();
1✔
19
        int result = 0;
1✔
20

21
        while (true) {
22
            if (!buffer.hasRemaining()) {
1✔
23
                throw new VarIntEndOfBufferException();
1✔
24
            }
25
            if (buffer.position() - startPosition >= 5) {
1✔
26
                throw new VarIntParseException("Data too long for a 32-bit int");
1✔
27
            }
28

29
            byte b = buffer.get();
1✔
30
            result += b & 0x7F;
1✔
31

32
            if ((b & 0x80) == 0) {
1✔
33
                return result;
1✔
34
            }
35
            if (result > (Integer.MAX_VALUE >> 7)) {
1✔
36
                // Shifting result by 7 bits would produce a number too large
37
                // to be stored in a non-negative int (an overflow)
38
                throw new VarIntParseException("Value too large to fit in an int");
1✔
39
            }
40

41
            result <<= 7;
1✔
42
        }
1✔
43
    }
44

45
    public static long getLong(ByteBuffer buffer) throws VarIntParseException, VarIntEndOfBufferException {
46
        final int startPosition = buffer.position();
1✔
47
        long result = 0;
1✔
48

49
        while (true) {
50
            if (!buffer.hasRemaining()) {
1✔
51
                throw new VarIntEndOfBufferException();
1✔
52
            }
53
            if (buffer.position() - startPosition >= 10) {
1!
54
                throw new VarIntParseException("Data too long for a 64-bit int");
×
55
            }
56

57
            byte b = buffer.get();
1✔
58
            result += b & 0x7F;
1✔
59

60
            if ((b & 0x80) == 0) {
1✔
61
                return result;
1✔
62
            }
63
            if (result > (Long.MAX_VALUE >> 7)) {
1✔
64
                // Shifting result by 7 bits would produce a number too large
65
                // to be stored in a non-negative int (an overflow)
66
                throw new VarIntParseException("Value too large to fit in an int");
1✔
67
            }
68

69
            result <<= 7;
1✔
70
        }
1✔
71
    }
72

73
    public static void putInt(ByteBuffer dest, int val) {
74
        if (val < 0) {
1✔
75
            throw new IllegalArgumentException(String.format("Value (%d) was negative", val));
1✔
76
        }
77

78
        for (int shift = 28; shift >= 0; shift -= 7) {
1✔
79
            int v2 = val >> shift;
1✔
80
            if (v2 != 0 || shift == 0) {
1✔
81
                byte b = (byte)((v2 & 0x7f) | (shift == 0 ? 0 : 0x80));
1✔
82
                dest.put(b);
1✔
83
            }
84
        }
85
    }
1✔
86

87
    public static void writeInt(OutputStream out, int val) throws IOException {
88
        if (val < 0) {
1!
89
            throw new IllegalArgumentException(String.format("Value (%d) was negative", val));
×
90
        }
91

92
        for (int shift = 28; shift >= 0; shift -= 7) {
1✔
93
            int v2 = val >> shift;
1✔
94
            if (v2 != 0 || shift == 0) {
1✔
95
                byte b = (byte)((v2 & 0x7f) | (shift == 0 ? 0 : 0x80));
1✔
96
                out.write(b);
1✔
97
            }
98
        }
99
    }
1✔
100

101
    public static void putLong(ByteBuffer dest, long val) {
102
        if (val < 0) {
1✔
103
            throw new IllegalArgumentException(String.format("Value (%d) was negative", val));
1✔
104
        }
105

106
        for (int shift = 63; shift >= 0; shift -= 7) {
1✔
107
            long v2 = val >> shift;
1✔
108
            if (v2 != 0 || shift == 0) {
1✔
109
                byte b = (byte)((v2 & 0x7f) | (shift == 0 ? 0 : 0x80));
1✔
110
                dest.put(b);
1✔
111
            }
112
        }
113
    }
1✔
114

115
    public static void writeLong(OutputStream out, long val) throws IOException {
116
        if (val < 0) {
1!
117
            throw new IllegalArgumentException(String.format("Value (%d) was negative", val));
×
118
        }
119

120
        for (int shift = 63; shift >= 0; shift -= 7) {
1✔
121
            long v2 = val >> shift;
1✔
122
            if (v2 != 0 || shift == 0) {
1!
123
                byte b = (byte)((v2 & 0x7f) | (shift == 0 ? 0 : 0x80));
1✔
124
                out.write(b);
1✔
125
            }
126
        }
127
    }
1✔
128

129
    public static int calculateIntLength(int val) {
130
        int size = 0;
1✔
131

132
        for (int shift = 28; shift >= 0; shift -= 7) {
1✔
133
            int v2 = val >> shift;
1✔
134
            if (v2 != 0 || shift == 0) {
1✔
135
                size++;
1✔
136
            }
137
        }
138

139
        return size;
1✔
140
    }
141

142
    public static int calculateLongLength(long val) {
143
        int size = 0;
1✔
144

145
        for (int shift = 63; shift >= 0; shift -= 7) {
1✔
146
            long v2 = val >> shift;
1✔
147
            if (v2 != 0 || shift == 0) {
1!
148
                size++;
1✔
149
            }
150
        }
151

152
        return size;
1✔
153
    }
154

155
    public static class VarIntParseException extends Exception {
156
        private static final long serialVersionUID = 2648357489942607161L;
157

158
        protected VarIntParseException(String message) {
159
            super(Objects.requireNonNull(message));
1✔
160
        }
1✔
161
    }
162

163
    public static class VarIntEndOfBufferException extends Exception {
164
        private static final long serialVersionUID = -2989212562402509511L;
165
        protected VarIntEndOfBufferException() { }
1✔
166
    }
167
}
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