Skip to content

Commit 4affc7b

Browse files
author
Gary Keim
authored
Merge pull request #4 from cljohnso/TAB-9567
TAB-9567 Remove unsynchronized DecimalFormat
2 parents b97d184 + eb43fcc commit 4affc7b

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

src/main/java/com/tc/util/Conversion.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
import com.tc.exception.TCRuntimeException;
1414

1515
import java.io.UnsupportedEncodingException;
16-
import java.text.DecimalFormat;
17-
import java.text.DecimalFormatSymbols;
1816
import java.util.regex.Matcher;
1917
import java.util.regex.Pattern;
2018

2119
/**
2220
* Data conversion algorithms and whatnot can be found in java.io.DataInput and java.io.DataOutput. Contains methods for
2321
* converting from one kind of thing to another.
24-
*
22+
*
2523
* @author orion
2624
*/
2725
public class Conversion {
@@ -31,11 +29,7 @@ public class Conversion {
3129

3230
private static final Pattern MEMORY_SIZE_PATTERN = Pattern.compile("[0-9]*([.][0-9]+)? *([bkmg])?");
3331
private static final Pattern SIZE_MODIFIER_PATTERN = Pattern.compile("[bkmg]");
34-
private final static DecimalFormat twoDForm = new DecimalFormat();
3532
private static final byte[] EMPTY_BYTE_ARRAY = new byte[] {};
36-
static {
37-
twoDForm.applyLocalizedPattern("#" + new DecimalFormatSymbols().getDecimalSeparator() + "##");
38-
}
3933

4034
private static int makeInt(byte b3, byte b2, byte b1, byte b0) {
4135
return ((((b3 & 0xff) << 24) | ((b2 & 0xff) << 16) | ((b1 & 0xff) << 8) | ((b0 & 0xff) << 0)));
@@ -110,7 +104,7 @@ public static long bytes2uint(byte b[], int offset, int length) {
110104

111105
/**
112106
* Helper method to write a 4 byte unsigned integer value into a given byte array at a given offset
113-
*
107+
*
114108
* @param l the unsigned int value to write
115109
* @param dest the byte array to write the uint into
116110
* @param index starting offset into the destination byte array
@@ -128,7 +122,7 @@ public static void writeUint(long l, byte[] dest, int index) {
128122

129123
/**
130124
* Helper method to write a 4 byte java (signed) integer value into a given byte array at a given offset
131-
*
125+
*
132126
* @param i the signed int value to write
133127
* @param dest the byte array to write the uint into
134128
* @param index starting offset into the destination byte array
@@ -314,7 +308,7 @@ public static String bytesToHex(byte[] b) {
314308

315309
/**
316310
* Converts a single byte to a hex string representation, can be decoded with Byte.parseByte().
317-
*
311+
*
318312
* @param b the byte to encode
319313
* @return a
320314
*/
@@ -421,13 +415,13 @@ public static String memoryBytesAsSize(final long bytes) throws NumberFormatExce
421415
return bytes + "b";
422416
} else if (bytes < MEGA.asBytes()) {
423417
double rv = (bytes / (KILO.asBytes() * 1.0));
424-
return twoDForm.format(rv) + 'k';
418+
return String.format("%.2fk", rv);
425419
} else if (bytes < GIGA.asBytes()) {
426420
double rv = (bytes / (MEGA.asBytes() * 1.0));
427-
return twoDForm.format(rv) + 'm';
421+
return String.format("%.2fm", rv);
428422
} else {
429423
double rv = (bytes / (GIGA.asBytes() * 1.0));
430-
return twoDForm.format(rv) + 'g';
424+
return String.format("%.2fg", rv);
431425
}
432426
}
433427

src/test/java/com/tc/util/ConversionTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* JUnit TestCase to exercise the Conversion class.
2424
* <p>
2525
* TODO: change tests to use min and max values where appropriate.
26-
*
26+
*
2727
* @see Conversion
2828
* @author orion
2929
*/
@@ -294,16 +294,16 @@ public void testMemoryBytesAsSize() {
294294

295295
char dfs = new DecimalFormatSymbols().getDecimalSeparator();
296296
try {
297-
Assert.assertEquals("1k", Conversion.memoryBytesAsSize(MemorySizeUnits.KILO.asBytes()));
298-
Assert.assertEquals("1m", Conversion.memoryBytesAsSize(MemorySizeUnits.MEGA.asBytes()));
299-
Assert.assertEquals("1g", Conversion.memoryBytesAsSize(MemorySizeUnits.GIGA.asBytes()));
297+
Assert.assertEquals("1" + dfs + "00k", Conversion.memoryBytesAsSize(MemorySizeUnits.KILO.asBytes()));
298+
Assert.assertEquals("1" + dfs + "00m", Conversion.memoryBytesAsSize(MemorySizeUnits.MEGA.asBytes()));
299+
Assert.assertEquals("1" + dfs + "00g", Conversion.memoryBytesAsSize(MemorySizeUnits.GIGA.asBytes()));
300300

301-
Assert.assertEquals("4k", Conversion.memoryBytesAsSize(MemorySizeUnits.KILO.asBytes() * 4));
302-
Assert.assertEquals("8m", Conversion.memoryBytesAsSize(MemorySizeUnits.MEGA.asBytes() * 8));
303-
Assert.assertEquals("10g", Conversion.memoryBytesAsSize(MemorySizeUnits.GIGA.asBytes() * 10));
301+
Assert.assertEquals("4" + dfs + "00k", Conversion.memoryBytesAsSize(MemorySizeUnits.KILO.asBytes() * 4));
302+
Assert.assertEquals("8" + dfs + "00m", Conversion.memoryBytesAsSize(MemorySizeUnits.MEGA.asBytes() * 8));
303+
Assert.assertEquals("10" + dfs + "00g", Conversion.memoryBytesAsSize(MemorySizeUnits.GIGA.asBytes() * 10));
304304

305305
Assert.assertEquals("924b", Conversion.memoryBytesAsSize(MemorySizeUnits.KILO.asBytes() - 100));
306-
Assert.assertEquals("1024m", Conversion.memoryBytesAsSize(MemorySizeUnits.GIGA.asBytes() - 100));
306+
Assert.assertEquals("1024" + dfs + "00m", Conversion.memoryBytesAsSize(MemorySizeUnits.GIGA.asBytes() - 100));
307307
Assert.assertEquals("901b", Conversion.memoryBytesAsSize(MemorySizeUnits.KILO.asBytes() - 123));
308308
Assert.assertEquals("1021" + dfs + "71k", Conversion.memoryBytesAsSize(MemorySizeUnits.MEGA.asBytes() - 2344));
309309
Assert.assertEquals("933" + dfs + "84m",

0 commit comments

Comments
 (0)