Skip to content
Permalink
Browse files
8259493: [test] Use HexFormat instead of adhoc hex utilities in netwo…
…rk code and locale SoftKeys

Reviewed-by: lancea, naoto
  • Loading branch information
Roger Riggs committed Jan 8, 2021
1 parent 090bd3a commit 876c7fb5f7aab9fb13f6581b79ec14b27c3944d7
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -186,11 +186,6 @@ private String computeDigest(
return finalHash;
}

private final static char charArray[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

private String encode(String src, char[] passwd, MessageDigest md) {
md.update(src.getBytes());
if (passwd != null) {
@@ -201,15 +196,7 @@ private String encode(String src, char[] passwd, MessageDigest md) {
Arrays.fill(passwdBytes, (byte)0x00);
}
byte[] digest = md.digest();

StringBuffer res = new StringBuffer(digest.length * 2);
for (int i = 0; i < digest.length; i++) {
int hashchar = ((digest[i] >>> 4) & 0xf);
res.append(charArray[hashchar]);
hashchar = (digest[i] & 0xf);
res.append(charArray[hashchar]);
}
return res.toString();
return HexFormat.of().formatHex(digest);
}
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -52,6 +52,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HexFormat;
import java.util.List;
import java.util.Objects;
import java.util.Random;
@@ -582,11 +583,6 @@ String getQoP(String defval) {

// Code stolen from DigestAuthentication:

private static final char charArray[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

private static String encode(String src, char[] passwd, MessageDigest md) {
try {
md.update(src.getBytes("ISO-8859-1"));
@@ -601,15 +597,7 @@ private static String encode(String src, char[] passwd, MessageDigest md) {
Arrays.fill(passwdBytes, (byte)0x00);
}
byte[] digest = md.digest();

StringBuilder res = new StringBuilder(digest.length * 2);
for (int i = 0; i < digest.length; i++) {
int hashchar = ((digest[i] >>> 4) & 0xf);
res.append(charArray[hashchar]);
hashchar = (digest[i] & 0xf);
res.append(charArray[hashchar]);
}
return res.toString();
return HexFormat.of().formatHex(digest);
}

public static String computeDigest(boolean isRequest,
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -54,6 +54,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HexFormat;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
@@ -821,11 +822,6 @@ String getQoP(String defval) {

// Code stolen from DigestAuthentication:

private static final char charArray[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

private static String encode(String src, char[] passwd, MessageDigest md) {
try {
md.update(src.getBytes("ISO-8859-1"));
@@ -840,15 +836,7 @@ private static String encode(String src, char[] passwd, MessageDigest md) {
Arrays.fill(passwdBytes, (byte)0x00);
}
byte[] digest = md.digest();

StringBuilder res = new StringBuilder(digest.length * 2);
for (int i = 0; i < digest.length; i++) {
int hashchar = ((digest[i] >>> 4) & 0xf);
res.append(charArray[hashchar]);
hashchar = (digest[i] & 0xf);
res.append(charArray[hashchar]);
}
return res.toString();
return HexFormat.of().formatHex(digest);
}

public static String computeDigest(boolean isRequest,
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -33,8 +33,6 @@

public class SoftKeys {

private static final char[] CHARS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

public static void main(String[] args) {
try {
// With 4 characters in "language", we'll fill up a 16M heap quickly,
@@ -44,7 +42,7 @@ public static void main(String[] args) {
// been cleared.
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 512*1024; j++) {
new Locale(langForInt(j), "", "");
new Locale(HexFormat.of().toHexDigits((short)j), "", "");
}
}
} catch (OutOfMemoryError e) {
@@ -57,14 +55,5 @@ public static void main(String[] args) {
System.gc();
}
}

private static String langForInt(int val) {
StringBuilder buf = new StringBuilder(4);
buf.append(CHARS[(val >> 12) & 0xF]);
buf.append(CHARS[(val >> 8) & 0xF]);
buf.append(CHARS[(val >> 4) & 0xF]);
buf.append(CHARS[(val >> 0) & 0xF]);
return buf.toString();
}
}

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -172,11 +172,6 @@ private String computeDigest(
return finalHash;
}

private final static char charArray[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

private String encode(String src, char[] passwd, MessageDigest md) {
md.update(src.getBytes());
if (passwd != null) {
@@ -187,15 +182,7 @@ private String encode(String src, char[] passwd, MessageDigest md) {
Arrays.fill(passwdBytes, (byte)0x00);
}
byte[] digest = md.digest();

StringBuffer res = new StringBuffer(digest.length * 2);
for (int i = 0; i < digest.length; i++) {
int hashchar = ((digest[i] >>> 4) & 0xf);
res.append(charArray[hashchar]);
hashchar = (digest[i] & 0xf);
res.append(charArray[hashchar]);
}
return res.toString();
return HexFormat.of().formatHex(digest);
}

}

1 comment on commit 876c7fb

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 876c7fb Jan 8, 2021

Please sign in to comment.