Skip to content

Commit 93df8cc

Browse files
author
Stephane Landelle
committed
Drop own StandardCharsets now we target JDK7, close AsyncHttpClient#702
1 parent 0f66c2d commit 93df8cc

33 files changed

+115
-119
lines changed

api/src/main/java/org/asynchttpclient/ProxyServer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package org.asynchttpclient;
1818

19-
import org.asynchttpclient.util.StandardCharsets;
19+
import static java.nio.charset.StandardCharsets.*;
2020

2121
import java.nio.charset.Charset;
2222
import java.util.ArrayList;
@@ -54,8 +54,8 @@ public String toString() {
5454
private final String password;
5555
private final int port;
5656
private final String url;
57-
private String encoding = StandardCharsets.UTF_8.name();
58-
private Charset charset = StandardCharsets.UTF_8;
57+
private String encoding = UTF_8.name();
58+
private Charset charset = UTF_8;
5959
private String ntlmDomain = System.getProperty("http.auth.ntlm.domain", "");
6060

6161
public ProxyServer(final Protocol protocol, final String host, final int port, String principal, String password) {

api/src/main/java/org/asynchttpclient/Realm.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
*/
1717
package org.asynchttpclient;
1818

19+
import static java.nio.charset.StandardCharsets.*;
1920
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
2021

2122
import org.asynchttpclient.uri.Uri;
22-
import org.asynchttpclient.util.StandardCharsets;
2323

2424
import java.nio.charset.Charset;
2525
import java.security.MessageDigest;
@@ -279,7 +279,7 @@ public static class RealmBuilder {
279279
private String methodName = "GET";
280280
private boolean usePreemptive;
281281
private String ntlmDomain = System.getProperty("http.auth.ntlm.domain", "");
282-
private String enc = StandardCharsets.UTF_8.name();
282+
private String enc = UTF_8.name();
283283
private String host = "localhost";
284284
private boolean messageType2Received;
285285
private boolean useAbsoluteURI = true;
@@ -510,7 +510,7 @@ public RealmBuilder clone(Realm clone) {
510510
private void newCnonce() {
511511
try {
512512
MessageDigest md = MessageDigest.getInstance("MD5");
513-
byte[] b = md.digest(String.valueOf(System.currentTimeMillis()).getBytes(StandardCharsets.ISO_8859_1));
513+
byte[] b = md.digest(String.valueOf(System.currentTimeMillis()).getBytes(ISO_8859_1));
514514
cnonce = toHexString(b);
515515
} catch (Exception e) {
516516
throw new SecurityException(e);
@@ -556,22 +556,22 @@ private void newResponse() {
556556
md.update(new StringBuilder(principal).append(":")//
557557
.append(realmName).append(":")//
558558
.append(password).toString()//
559-
.getBytes(StandardCharsets.ISO_8859_1));
559+
.getBytes(ISO_8859_1));
560560
byte[] ha1 = md.digest();
561561

562562
md.reset();
563563

564564
// HA2 if qop is auth-int is methodName:url:md5(entityBody)
565565
md.update(new StringBuilder(methodName).append(':')//
566566
.append(uri).toString()//
567-
.getBytes(StandardCharsets.ISO_8859_1));
567+
.getBytes(ISO_8859_1));
568568
byte[] ha2 = md.digest();
569569

570570
if (qop == null || qop.length() == 0) {
571571
md.update(new StringBuilder(toBase16(ha1)).append(':')//
572572
.append(nonce).append(':')//
573573
.append(toBase16(ha2)).toString()//
574-
.getBytes(StandardCharsets.ISO_8859_1));
574+
.getBytes(ISO_8859_1));
575575

576576
} else {
577577
// qop ="auth" or "auth-int"
@@ -581,7 +581,7 @@ private void newResponse() {
581581
.append(cnonce).append(':')//
582582
.append(qop).append(':')//
583583
.append(toBase16(ha2)).toString()//
584-
.getBytes(StandardCharsets.ISO_8859_1));
584+
.getBytes(ISO_8859_1));
585585
}
586586

587587
byte[] digest = md.digest();

api/src/main/java/org/asynchttpclient/consumers/AppendableBodyConsumer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
*/
1313
package org.asynchttpclient.consumers;
1414

15+
import static java.nio.charset.StandardCharsets.*;
16+
1517
import org.asynchttpclient.BodyConsumer;
16-
import org.asynchttpclient.util.StandardCharsets;
1718

1819
import java.io.Closeable;
1920
import java.io.IOException;
@@ -34,7 +35,7 @@ public AppendableBodyConsumer(Appendable appendable, String encoding) {
3435

3536
public AppendableBodyConsumer(Appendable appendable) {
3637
this.appendable = appendable;
37-
this.encoding = StandardCharsets.UTF_8.name();
38+
this.encoding = UTF_8.name();
3839
}
3940

4041
@Override

api/src/main/java/org/asynchttpclient/multipart/AbstractFilePart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
package org.asynchttpclient.multipart;
1414

15-
import static org.asynchttpclient.util.StandardCharsets.US_ASCII;
15+
import static java.nio.charset.StandardCharsets.*;
1616

1717
import java.io.ByteArrayOutputStream;
1818
import java.io.IOException;

api/src/main/java/org/asynchttpclient/multipart/MultipartUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
*/
1616
package org.asynchttpclient.multipart;
1717

18+
import static java.nio.charset.StandardCharsets.*;
1819
import static org.asynchttpclient.multipart.Part.CRLF_BYTES;
1920
import static org.asynchttpclient.multipart.Part.EXTRA_BYTES;
2021
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
2122

2223
import org.asynchttpclient.FluentCaseInsensitiveStringsMap;
23-
import org.asynchttpclient.util.StandardCharsets;
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
2626

@@ -48,7 +48,7 @@ public class MultipartUtils {
4848
* The pool of ASCII chars to be used for generating a multipart boundary.
4949
*/
5050
private static byte[] MULTIPART_CHARS = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
51-
.getBytes(StandardCharsets.US_ASCII);
51+
.getBytes(US_ASCII);
5252

5353
private MultipartUtils() {
5454
}
@@ -74,7 +74,7 @@ public static MultipartBody newMultipartBody(List<Part> parts, FluentCaseInsensi
7474
// boundary defined in existing Content-Type
7575
contentType = contentTypeHeader;
7676
multipartBoundary = (contentTypeHeader.substring(boundaryLocation + "boundary=".length()).trim())
77-
.getBytes(StandardCharsets.US_ASCII);
77+
.getBytes(US_ASCII);
7878
} else {
7979
// generate boundary and append it to existing Content-Type
8080
multipartBoundary = generateMultipartBoundary();
@@ -103,7 +103,7 @@ private static String computeContentType(String base, byte[] multipartBoundary)
103103
StringBuilder buffer = new StringBuilder(base);
104104
if (!base.endsWith(";"))
105105
buffer.append(";");
106-
return buffer.append(" boundary=").append(new String(multipartBoundary, StandardCharsets.US_ASCII)).toString();
106+
return buffer.append(" boundary=").append(new String(multipartBoundary, US_ASCII)).toString();
107107
}
108108

109109
public static long writeBytesToChannel(WritableByteChannel target, byte[] bytes) throws IOException {

api/src/main/java/org/asynchttpclient/multipart/Part.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
package org.asynchttpclient.multipart;
1414

15-
import static org.asynchttpclient.util.StandardCharsets.US_ASCII;
15+
import static java.nio.charset.StandardCharsets.*;
1616

1717
import java.io.IOException;
1818
import java.io.OutputStream;

api/src/main/java/org/asynchttpclient/multipart/PartBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
package org.asynchttpclient.multipart;
1414

15-
import static org.asynchttpclient.util.StandardCharsets.US_ASCII;
15+
import static java.nio.charset.StandardCharsets.*;
1616

1717
import java.io.IOException;
1818
import java.io.OutputStream;

api/src/main/java/org/asynchttpclient/multipart/StringPart.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
*/
1313
package org.asynchttpclient.multipart;
1414

15+
import static java.nio.charset.StandardCharsets.*;
16+
1517
import java.io.ByteArrayOutputStream;
1618
import java.io.IOException;
1719
import java.io.OutputStream;
1820
import java.nio.channels.WritableByteChannel;
1921
import java.nio.charset.Charset;
2022

21-
import org.asynchttpclient.util.StandardCharsets;
22-
2323
public class StringPart extends PartBase {
2424

2525
/**
@@ -30,7 +30,7 @@ public class StringPart extends PartBase {
3030
/**
3131
* Default charset of string parameters
3232
*/
33-
public static final Charset DEFAULT_CHARSET = StandardCharsets.US_ASCII;
33+
public static final Charset DEFAULT_CHARSET = US_ASCII;
3434

3535
/**
3636
* Default transfer encoding of string parameters

api/src/main/java/org/asynchttpclient/ntlm/NTLMEngine.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
package org.asynchttpclient.ntlm;
2828

29+
import static java.nio.charset.StandardCharsets.*;
30+
2931
import org.asynchttpclient.util.Base64;
30-
import org.asynchttpclient.util.StandardCharsets;
3132

3233
import javax.crypto.Cipher;
3334
import javax.crypto.spec.SecretKeySpec;
@@ -81,8 +82,8 @@ public class NTLMEngine {
8182
*/
8283
private String credentialCharset = DEFAULT_CHARSET;
8384

84-
private static final byte[] NTLMSSP_BYTES = "NTLMSSP".getBytes(StandardCharsets.US_ASCII);
85-
private static final byte[] MAGIC_CONSTANT = "KGS!@#$%".getBytes(StandardCharsets.US_ASCII);
85+
private static final byte[] NTLMSSP_BYTES = "NTLMSSP".getBytes(US_ASCII);
86+
private static final byte[] MAGIC_CONSTANT = "KGS!@#$%".getBytes(US_ASCII);
8687

8788
/**
8889
* The signature string as bytes in the default encoding
@@ -99,6 +100,22 @@ public class NTLMEngine {
99100

100101
public static final NTLMEngine INSTANCE = new NTLMEngine();
101102

103+
private static byte[] getUnicodeLittleUnmarkedBytes(String s) throws NTLMEngineException {
104+
try {
105+
return s.getBytes("UnicodeLittleUnmarked");
106+
} catch (java.io.UnsupportedEncodingException e) {
107+
throw new NTLMEngineException("UnicodeLittleUnmarked not supported! " + e.getMessage(), e);
108+
}
109+
}
110+
111+
private static String fromUnicodeLittleUnmarkedBytes(byte[] bytes) throws NTLMEngineException {
112+
try {
113+
return new String(bytes, "UnicodeLittleUnmarked");
114+
} catch (UnsupportedEncodingException e) {
115+
throw new NTLMEngineException(e.getMessage(), e);
116+
}
117+
}
118+
102119
/**
103120
* Returns the response for the given message.
104121
*
@@ -368,7 +385,7 @@ static byte[] getNTLM2SessionResponse(String password, byte[] challenge, byte[]
368385
*/
369386
private static byte[] lmHash(String password) throws NTLMEngineException {
370387
try {
371-
byte[] oemPassword = password.toUpperCase(Locale.ENGLISH).getBytes(StandardCharsets.US_ASCII);
388+
byte[] oemPassword = password.toUpperCase(Locale.ENGLISH).getBytes(US_ASCII);
372389
int length = Math.min(oemPassword.length, 14);
373390
byte[] keyBytes = new byte[14];
374391
System.arraycopy(oemPassword, 0, keyBytes, 0, length);
@@ -396,7 +413,7 @@ private static byte[] lmHash(String password) throws NTLMEngineException {
396413
* the NTLM Response and the NTLMv2 and LMv2 Hashes.
397414
*/
398415
private static byte[] ntlmHash(String password) throws NTLMEngineException {
399-
byte[] unicodePassword = password.getBytes(StandardCharsets.UNICODE_LITTLE_UNMARKED);
416+
byte[] unicodePassword = getUnicodeLittleUnmarkedBytes(password);
400417
MD4 md4 = new MD4();
401418
md4.update(unicodePassword);
402419
return md4.getOutput();
@@ -415,8 +432,8 @@ private static byte[] ntlmv2Hash(String target, String user, String password) th
415432
byte[] ntlmHash = ntlmHash(password);
416433
HMACMD5 hmacMD5 = new HMACMD5(ntlmHash);
417434
// Upper case username, mixed case target!!
418-
hmacMD5.update(user.toUpperCase(Locale.ENGLISH).getBytes(StandardCharsets.UNICODE_LITTLE_UNMARKED));
419-
hmacMD5.update(target.getBytes(StandardCharsets.UNICODE_LITTLE_UNMARKED));
435+
hmacMD5.update(getUnicodeLittleUnmarkedBytes(user.toUpperCase(Locale.ENGLISH)));
436+
hmacMD5.update(getUnicodeLittleUnmarkedBytes(target));
420437
return hmacMD5.getOutput();
421438
}
422439

@@ -743,8 +760,8 @@ static class Type1Message extends NTLMMessage {
743760
// Use only the base domain name!
744761
domain = convertDomain(domain);
745762

746-
hostBytes = host.getBytes(StandardCharsets.UNICODE_LITTLE_UNMARKED);
747-
domainBytes = domain.toUpperCase(Locale.ENGLISH).getBytes(StandardCharsets.UNICODE_LITTLE_UNMARKED);
763+
hostBytes = getUnicodeLittleUnmarkedBytes(host);
764+
domainBytes = getUnicodeLittleUnmarkedBytes(domain.toUpperCase(Locale.ENGLISH));
748765
}
749766

750767
/**
@@ -821,7 +838,7 @@ static class Type2Message extends NTLMMessage {
821838
if (getMessageLength() >= 12 + 8) {
822839
byte[] bytes = readSecurityBuffer(12);
823840
if (bytes.length != 0) {
824-
target = new String(bytes, StandardCharsets.UNICODE_LITTLE_UNMARKED);
841+
target = fromUnicodeLittleUnmarkedBytes(bytes);
825842
}
826843
}
827844

@@ -924,9 +941,9 @@ static class Type3Message extends NTLMMessage {
924941
lmResp = getLMResponse(password, nonce);
925942
}
926943

927-
domainBytes = domain.toUpperCase(Locale.ENGLISH).getBytes(StandardCharsets.UNICODE_LITTLE_UNMARKED);
928-
hostBytes = host.getBytes(StandardCharsets.UNICODE_LITTLE_UNMARKED);
929-
userBytes = user.getBytes(StandardCharsets.UNICODE_LITTLE_UNMARKED);
944+
domainBytes = getUnicodeLittleUnmarkedBytes(domain.toUpperCase(Locale.ENGLISH));
945+
hostBytes = getUnicodeLittleUnmarkedBytes(host);
946+
userBytes = getUnicodeLittleUnmarkedBytes(user);
930947
}
931948

932949
/**

api/src/main/java/org/asynchttpclient/oauth/OAuthSignatureCalculator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.asynchttpclient.oauth;
1818

19+
import static java.nio.charset.StandardCharsets.*;
1920
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
2021

2122
import org.asynchttpclient.Param;
@@ -24,7 +25,6 @@
2425
import org.asynchttpclient.SignatureCalculator;
2526
import org.asynchttpclient.uri.Uri;
2627
import org.asynchttpclient.util.Base64;
27-
import org.asynchttpclient.util.StandardCharsets;
2828
import org.asynchttpclient.util.UTF8UrlEncoder;
2929

3030
import java.util.ArrayList;
@@ -153,7 +153,7 @@ else if (scheme.equals("https"))
153153
signedText.append('&');
154154
UTF8UrlEncoder.appendEncoded(signedText, encodedParams);
155155

156-
byte[] rawBase = signedText.toString().getBytes(StandardCharsets.UTF_8);
156+
byte[] rawBase = signedText.toString().getBytes(UTF_8);
157157
byte[] rawSignature = mac.digest(rawBase);
158158
// and finally, base64 encoded... phew!
159159
return Base64.encode(rawSignature);

api/src/main/java/org/asynchttpclient/oauth/ThreadSafeHMAC.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
*/
1717
package org.asynchttpclient.oauth;
1818

19-
import org.asynchttpclient.util.StandardCharsets;
19+
import static java.nio.charset.StandardCharsets.*;
20+
2021
import org.asynchttpclient.util.UTF8UrlEncoder;
2122

2223
import javax.crypto.Mac;
@@ -38,7 +39,7 @@ public class ThreadSafeHMAC {
3839

3940
public ThreadSafeHMAC(ConsumerKey consumerAuth, RequestToken userAuth) {
4041
byte[] keyBytes = (UTF8UrlEncoder.encode(consumerAuth.getSecret()) + "&" + UTF8UrlEncoder.encode(userAuth.getSecret()))
41-
.getBytes(StandardCharsets.UTF_8);
42+
.getBytes(UTF_8);
4243
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, HMAC_SHA1_ALGORITHM);
4344

4445
// Get an hmac_sha1 instance and initialize with the signing key

api/src/main/java/org/asynchttpclient/resumable/PropertiesBasedResumableProcessor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package org.asynchttpclient.resumable;
1414

15+
import static java.nio.charset.StandardCharsets.*;
1516
import static org.asynchttpclient.util.MiscUtils.closeSilently;
1617

1718
import java.io.File;
@@ -21,7 +22,6 @@
2122
import java.util.Scanner;
2223
import java.util.concurrent.ConcurrentHashMap;
2324

24-
import org.asynchttpclient.util.StandardCharsets;
2525
import org.slf4j.Logger;
2626
import org.slf4j.LoggerFactory;
2727

@@ -76,7 +76,7 @@ public void save(Map<String, Long> map) {
7676
os = new FileOutputStream(f);
7777

7878
for (Map.Entry<String, Long> e : properties.entrySet()) {
79-
os.write(append(e).getBytes(StandardCharsets.UTF_8));
79+
os.write(append(e).getBytes(UTF_8));
8080
}
8181
os.flush();
8282
} catch (Throwable e) {
@@ -98,7 +98,7 @@ private static String append(Map.Entry<String, Long> e) {
9898
public Map<String, Long> load() {
9999
Scanner scan = null;
100100
try {
101-
scan = new Scanner(new File(TMP, storeName), StandardCharsets.UTF_8.name());
101+
scan = new Scanner(new File(TMP, storeName), UTF_8.name());
102102
scan.useDelimiter("[=\n]");
103103

104104
String key;

0 commit comments

Comments
 (0)