Skip to content

Commit 6162fa2

Browse files
committed
switch to long for parsing of memory sizes (#175)
1 parent d6548a0 commit 6162fa2

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

src/main/java/com/tagtraum/perf/gcviewer/imp/DataReaderIBM_J9_R28.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,14 @@ else if (xmlEvent.isEndElement()) {
207207
}
208208

209209
private void setTotalAndPreUsed(GCEvent event, StartElement startEl) {
210-
long total = NumberParser.parseInt(getAttributeValue(startEl, "total"));
210+
long total = NumberParser.parseLong(getAttributeValue(startEl, "total"));
211211
event.setTotal(toKiloBytes(total));
212-
event.setPreUsed(toKiloBytes(total - NumberParser.parseInt(getAttributeValue(startEl, "free"))));
212+
event.setPreUsed(toKiloBytes(total - NumberParser.parseLong(getAttributeValue(startEl, "free"))));
213213
}
214214

215215
private void setPostUsed(GCEvent event, StartElement startEl) {
216-
long total = NumberParser.parseInt(getAttributeValue(startEl, "total"));
217-
event.setPostUsed(toKiloBytes(total - NumberParser.parseInt(getAttributeValue(startEl, "free"))));
216+
long total = NumberParser.parseLong(getAttributeValue(startEl, "total"));
217+
event.setPostUsed(toKiloBytes(total - NumberParser.parseLong(getAttributeValue(startEl, "free"))));
218218
}
219219

220220
private String getAttributeValue(StartElement event, String name) {

src/main/java/com/tagtraum/perf/gcviewer/util/NumberParser.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,16 @@ public static long parseLong(String s, int offset, int length) throws NumberForm
124124
if (length > 0) {
125125
if (s.charAt(offset) == '-') {
126126
// shortcut for ints
127-
if (length <= MAX_NEGATIVE_INTEGER_CHARS) return parseInt(s, offset, length);
127+
// => shortcut doesn't work!! (e.g. -6442450944 is too big for an int, but has not too many characters)
128+
// if (length <= MAX_NEGATIVE_INTEGER_CHARS) return parseInt(s, offset, length);
128129
if (length > MAX_NEGATIVE_LONG_CHARS) throw new NumberFormatException(s);
129130
negative = true;
130131
limit = Long.MIN_VALUE;
131132
i++;
132133
} else {
133134
// shortcut for ints
134-
if (length <= MAX_POSITIVE_INTEGER_CHARS) return parseInt(s, offset, length);
135+
// => shortcut doesn't work!! (e.g. 6442450944 is too big for an int, but has not too many characters)
136+
//if (length <= MAX_POSITIVE_INTEGER_CHARS) return parseInt(s, offset, length);
135137
if (length > MAX_POSITIVE_LONG_CHARS) throw new NumberFormatException(s);
136138
limit = -Long.MAX_VALUE;
137139
}
@@ -172,14 +174,16 @@ public static long parseLong(char[] cb, int offset, int length) throws NumberFor
172174
if (length > 0) {
173175
if (cb[offset] == '-') {
174176
// shortcut for ints
175-
if (length <= MAX_NEGATIVE_INTEGER_CHARS) return parseInt(cb, offset, length);
177+
// => shortcut doesn't work!! (e.g. -6442450944 is too big for an int, but has not too many characters)
178+
//if (length <= MAX_NEGATIVE_INTEGER_CHARS) return parseInt(cb, offset, length);
176179
if (length > MAX_NEGATIVE_LONG_CHARS) throw new NumberFormatException(new String(cb, offset, length));
177180
negative = true;
178181
limit = Long.MIN_VALUE;
179182
i++;
180183
} else {
181184
// shortcut for ints
182-
if (length <= MAX_POSITIVE_INTEGER_CHARS) return parseInt(cb, offset, length);
185+
// => shortcut doesn't work!! (e.g. 6442450944 is too big for an int, but has not too many characters)
186+
//if (length <= MAX_POSITIVE_INTEGER_CHARS) return parseInt(cb, offset, length);
183187
if (length > MAX_POSITIVE_LONG_CHARS) throw new NumberFormatException(new String(cb, offset, length));
184188
limit = -Long.MAX_VALUE;
185189
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.tagtraum.perf.gcviewer.util;
2+
3+
import org.hamcrest.Matchers;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
/**
8+
* Test methods of {@link NumberParser}.
9+
*/
10+
public class TestNumberParser {
11+
12+
@Test
13+
public void testParseIntString() throws Exception {
14+
int result = NumberParser.parseInt("1442450944");
15+
Assert.assertThat("string int", result, Matchers.is(1442450944));
16+
}
17+
18+
@Test
19+
public void testParseIntStringNegative() throws Exception {
20+
int result = NumberParser.parseInt("-1442450944");
21+
Assert.assertThat("string negative int", result, Matchers.is(-1442450944));
22+
}
23+
24+
@Test
25+
public void testParseIntChar() throws Exception {
26+
int result = NumberParser.parseInt(new char[] {'1', '4', '4', '2', '4', '5', '0', '9', '4', '4'}, 0, 10);
27+
Assert.assertThat("char int", result, Matchers.is(1442450944));
28+
}
29+
30+
@Test
31+
public void testParseIntCharNegative() throws Exception {
32+
int result = NumberParser.parseInt(new char[] {'-', '1', '4', '4', '2', '4', '5', '0', '9', '4', '4'}, 0, 11);
33+
Assert.assertThat("char negative int", result, Matchers.is(-1442450944));
34+
}
35+
36+
@Test(expected = NumberFormatException.class)
37+
public void testParseIntTooLarge() throws Exception {
38+
NumberParser.parseInt("6442450944");
39+
}
40+
41+
@Test
42+
public void testParseLongString() throws Exception {
43+
long result = NumberParser.parseLong("6442450944");
44+
Assert.assertThat("string long", result, Matchers.is(6442450944L));
45+
}
46+
47+
@Test
48+
public void testParseLongStringNegative() throws Exception {
49+
long result = NumberParser.parseLong("-6442450944");
50+
Assert.assertThat("string negative long", result, Matchers.is(-6442450944L));
51+
52+
}
53+
54+
@Test
55+
public void testParseLongChar() throws Exception {
56+
long result = NumberParser.parseLong(new char[] {'6', '4', '4', '2', '4', '5', '0', '9', '4', '4'}, 0, 10);
57+
Assert.assertThat("char long", result, Matchers.is(6442450944L));
58+
}
59+
60+
@Test
61+
public void testParseLongCharNegative() throws Exception {
62+
long result = NumberParser.parseLong(new char[] {'-', '6', '4', '4', '2', '4', '5', '0', '9', '4', '4'}, 0, 11);
63+
Assert.assertThat("char negative long", result, Matchers.is(-6442450944L));
64+
}
65+
66+
}

0 commit comments

Comments
 (0)