Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8259493: [test] Use HexFormat instead of adhoc hex utilities in network code and locale SoftKeys #2009

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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);
}

}