diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpContentResponse.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpContentResponse.java index c3b50fdf7e1..fe54f85f077 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpContentResponse.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpContentResponse.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.client; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.util.List; @@ -92,13 +93,20 @@ public class HttpContentResponse implements ContentResponse public String getContentAsString() { String encoding = this.encoding; - try + if (encoding == null) { - return new String(getContent(), encoding == null ? "UTF-8" : encoding); + return new String(getContent(), StandardCharsets.UTF_8); } - catch (UnsupportedEncodingException e) + else { - throw new UnsupportedCharsetException(encoding); + try + { + return new String(getContent(), encoding); + } + catch (UnsupportedEncodingException e) + { + throw new UnsupportedCharsetException(encoding); + } } } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/util/BasicAuthentication.java b/jetty-client/src/main/java/org/eclipse/jetty/client/util/BasicAuthentication.java index 7bf9fc29abd..d50d6d9c4e6 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/util/BasicAuthentication.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/util/BasicAuthentication.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.client.util; import java.net.URI; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.Authentication; @@ -73,8 +74,7 @@ public class BasicAuthentication implements Authentication @Override public Result authenticate(Request request, ContentResponse response, HeaderInfo headerInfo, Attributes context) { - String encoding = StringUtil.__ISO_8859_1; - String value = "Basic " + B64Code.encode(user + ":" + password, encoding); + String value = "Basic " + B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1); return new BasicResult(headerInfo.getHeader(), uri, value); } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/util/BufferingResponseListener.java b/jetty-client/src/main/java/org/eclipse/jetty/client/util/BufferingResponseListener.java index c0ded4b616d..0b7202fe9c8 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/util/BufferingResponseListener.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/util/BufferingResponseListener.java @@ -20,6 +20,8 @@ package org.eclipse.jetty.client.util; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.util.Locale; @@ -131,7 +133,7 @@ public abstract class BufferingResponseListener extends Listener.Adapter { String encoding = this.encoding; if (encoding == null) - encoding = "UTF-8"; + return getContentAsString(StandardCharsets.UTF_8); return getContentAsString(encoding); } @@ -151,4 +153,14 @@ public abstract class BufferingResponseListener extends Listener.Adapter throw new UnsupportedCharsetException(encoding); } } + + /** + * @param encoding the encoding of the content bytes + * @return the content as a string, with the specified encoding + * @see #getContentAsString() + */ + public String getContentAsString(Charset encoding) + { + return new String(getContent(), encoding); + } } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/util/DigestAuthentication.java b/jetty-client/src/main/java/org/eclipse/jetty/client/util/DigestAuthentication.java index eaa9a6f3178..0b4cb564111 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/util/DigestAuthentication.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/util/DigestAuthentication.java @@ -218,14 +218,13 @@ public class DigestAuthentication implements Authentication if (digester == null) return; - Charset charset = StandardCharsets.ISO_8859_1; String A1 = user + ":" + realm + ":" + password; - String hashA1 = toHexString(digester.digest(A1.getBytes(charset))); + String hashA1 = toHexString(digester.digest(A1.getBytes(StandardCharsets.ISO_8859_1))); String A2 = request.getMethod() + ":" + request.getURI(); if ("auth-int".equals(qop)) A2 += ":" + toHexString(digester.digest(content)); - String hashA2 = toHexString(digester.digest(A2.getBytes(charset))); + String hashA2 = toHexString(digester.digest(A2.getBytes(StandardCharsets.ISO_8859_1))); String nonceCount; String clientNonce; @@ -242,7 +241,7 @@ public class DigestAuthentication implements Authentication clientNonce = null; A3 = hashA1 + ":" + nonce + ":" + hashA2; } - String hashA3 = toHexString(digester.digest(A3.getBytes(charset))); + String hashA3 = toHexString(digester.digest(A3.getBytes(StandardCharsets.ISO_8859_1))); StringBuilder value = new StringBuilder("Digest"); value.append(" username=\"").append(user).append("\""); diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/util/StringContentProvider.java b/jetty-client/src/main/java/org/eclipse/jetty/client/util/StringContentProvider.java index 95036b77c24..49fa4421726 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/util/StringContentProvider.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/util/StringContentProvider.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.client.util; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.client.api.ContentProvider; @@ -32,7 +33,7 @@ public class StringContentProvider extends BytesContentProvider { public StringContentProvider(String content) { - this(content, "UTF-8"); + this(content, StandardCharsets.UTF_8); } public StringContentProvider(String content, String encoding) diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/GZIPContentDecoderTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/GZIPContentDecoderTest.java index 436d68455cb..8c0a44ddbf9 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/GZIPContentDecoderTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/GZIPContentDecoderTest.java @@ -26,6 +26,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; @@ -59,7 +60,7 @@ public class GZIPContentDecoderTest data += data; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); @@ -68,7 +69,7 @@ public class GZIPContentDecoderTest int read; while ((read = input.read()) >= 0) baos.write(read); - assertEquals(data, new String(baos.toByteArray(), "UTF-8")); + assertEquals(data, new String(baos.toByteArray(), StandardCharsets.UTF_8)); } @Test @@ -91,13 +92,13 @@ public class GZIPContentDecoderTest ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); GZIPContentDecoder decoder = new GZIPContentDecoder(); ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes)); - assertEquals(data, Charset.forName("UTF-8").decode(decoded).toString()); + assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString()); } @Test @@ -107,7 +108,7 @@ public class GZIPContentDecoderTest ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); @@ -121,7 +122,7 @@ public class GZIPContentDecoderTest ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1)); assertEquals(0, decoded.capacity()); decoded = decoder.decode(ByteBuffer.wrap(bytes2)); - assertEquals(data, Charset.forName("UTF-8").decode(decoded).toString()); + assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString()); } @Test @@ -131,7 +132,7 @@ public class GZIPContentDecoderTest ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); @@ -143,7 +144,7 @@ public class GZIPContentDecoderTest GZIPContentDecoder decoder = new GZIPContentDecoder(); ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1)); - assertEquals(data, Charset.forName("UTF-8").decode(decoded).toString()); + assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString()); assertFalse(decoder.isFinished()); decoded = decoder.decode(ByteBuffer.wrap(bytes2)); assertEquals(0, decoded.remaining()); @@ -157,7 +158,7 @@ public class GZIPContentDecoderTest ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); @@ -171,7 +172,7 @@ public class GZIPContentDecoderTest ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1)); assertEquals(0, decoded.capacity()); decoded = decoder.decode(ByteBuffer.wrap(bytes2)); - assertEquals(data, Charset.forName("UTF-8").decode(decoded).toString()); + assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString()); } @Test @@ -180,14 +181,14 @@ public class GZIPContentDecoderTest String data1 = "0"; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data1.getBytes("UTF-8")); + output.write(data1.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes1 = baos.toByteArray(); String data2 = "1"; baos = new ByteArrayOutputStream(); output = new GZIPOutputStream(baos); - output.write(data2.getBytes("UTF-8")); + output.write(data2.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes2 = baos.toByteArray(); @@ -198,11 +199,11 @@ public class GZIPContentDecoderTest GZIPContentDecoder decoder = new GZIPContentDecoder(); ByteBuffer buffer = ByteBuffer.wrap(bytes); ByteBuffer decoded = decoder.decode(buffer); - assertEquals(data1, Charset.forName("UTF-8").decode(decoded).toString()); + assertEquals(data1, StandardCharsets.UTF_8.decode(decoded).toString()); assertTrue(decoder.isFinished()); assertTrue(buffer.hasRemaining()); decoded = decoder.decode(buffer); - assertEquals(data2, Charset.forName("UTF-8").decode(decoded).toString()); + assertEquals(data2, StandardCharsets.UTF_8.decode(decoded).toString()); assertTrue(decoder.isFinished()); assertFalse(buffer.hasRemaining()); } @@ -215,7 +216,7 @@ public class GZIPContentDecoderTest data += data; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); @@ -225,7 +226,7 @@ public class GZIPContentDecoderTest while (buffer.hasRemaining()) { ByteBuffer decoded = decoder.decode(buffer); - result += Charset.forName("UTF-8").decode(decoded).toString(); + result += StandardCharsets.UTF_8.decode(decoded).toString(); } assertEquals(data, result); } @@ -238,7 +239,7 @@ public class GZIPContentDecoderTest data += data; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes = baos.toByteArray(); @@ -249,7 +250,7 @@ public class GZIPContentDecoderTest { ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(new byte[]{buffer.get()})); if (decoded.hasRemaining()) - result += Charset.forName("UTF-8").decode(decoded).toString(); + result += StandardCharsets.UTF_8.decode(decoded).toString(); } assertEquals(data, result); assertTrue(decoder.isFinished()); @@ -263,12 +264,12 @@ public class GZIPContentDecoderTest data1 += data1; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream output = new GZIPOutputStream(baos); - output.write(data1.getBytes("UTF-8")); + output.write(data1.getBytes(StandardCharsets.UTF_8)); output.close(); byte[] bytes1 = baos.toByteArray(); String data2 = "HELLO"; - byte[] bytes2 = data2.getBytes("UTF-8"); + byte[] bytes2 = data2.getBytes(StandardCharsets.UTF_8); byte[] bytes = new byte[bytes1.length + bytes2.length]; System.arraycopy(bytes1, 0, bytes, 0, bytes1.length); @@ -281,12 +282,12 @@ public class GZIPContentDecoderTest { ByteBuffer decoded = decoder.decode(buffer); if (decoded.hasRemaining()) - result += Charset.forName("UTF-8").decode(decoded).toString(); + result += StandardCharsets.UTF_8.decode(decoded).toString(); if (decoder.isFinished()) break; } assertEquals(data1, result); assertTrue(buffer.hasRemaining()); - assertEquals(data2, Charset.forName("UTF-8").decode(buffer).toString()); + assertEquals(data2, StandardCharsets.UTF_8.decode(buffer).toString()); } } diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientContinueTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientContinueTest.java index 7acbb3bcb6e..bb2818b0822 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientContinueTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientContinueTest.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.client; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.Iterator; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -60,13 +61,13 @@ public class HttpClientContinueTest extends AbstractHttpClientServerTest @Test public void test_Expect100Continue_WithOneContent_Respond100Continue() throws Exception { - test_Expect100Continue_Respond100Continue("data1".getBytes("UTF-8")); + test_Expect100Continue_Respond100Continue("data1".getBytes(StandardCharsets.UTF_8)); } @Test public void test_Expect100Continue_WithMultipleContents_Respond100Continue() throws Exception { - test_Expect100Continue_Respond100Continue("data1".getBytes("UTF-8"), "data2".getBytes("UTF-8"), "data3".getBytes("UTF-8")); + test_Expect100Continue_Respond100Continue("data1".getBytes(StandardCharsets.UTF_8), "data2".getBytes(StandardCharsets.UTF_8), "data3".getBytes(StandardCharsets.UTF_8)); } private void test_Expect100Continue_Respond100Continue(byte[]... contents) throws Exception diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientProxyTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientProxyTest.java index 78d97840724..4cf7f661318 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientProxyTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientProxyTest.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.client; import java.io.IOException; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import javax.servlet.ServletException; @@ -82,7 +83,7 @@ public class HttpClientProxyTest extends AbstractHttpClientServerTest { final String user = "foo"; final String password = "bar"; - final String credentials = B64Code.encode(user + ":" + password, "ISO-8859-1"); + final String credentials = B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1); final String serverHost = "server"; final String realm = "test_realm"; final int status = HttpStatus.NO_CONTENT_204; diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java index ef6240a206c..e317b5263a2 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java @@ -25,6 +25,7 @@ import java.net.URI; import java.net.URLEncoder; import java.nio.ByteBuffer; import java.nio.channels.UnresolvedAddressException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -174,10 +175,10 @@ public class HttpClientTest extends AbstractHttpClientServerTest response.setCharacterEncoding("UTF-8"); ServletOutputStream output = response.getOutputStream(); String paramValue1 = request.getParameter(paramName1); - output.write(paramValue1.getBytes("UTF-8")); + output.write(paramValue1.getBytes(StandardCharsets.UTF_8)); String paramValue2 = request.getParameter(paramName2); Assert.assertEquals("", paramValue2); - output.write("empty".getBytes("UTF-8")); + output.write("empty".getBytes(StandardCharsets.UTF_8)); baseRequest.setHandled(true); } }); @@ -189,7 +190,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - String content = new String(response.getContent(), "UTF-8"); + String content = new String(response.getContent(), StandardCharsets.UTF_8); Assert.assertEquals(value1 + "empty", content); } @@ -207,9 +208,9 @@ public class HttpClientTest extends AbstractHttpClientServerTest ServletOutputStream output = response.getOutputStream(); String[] paramValues1 = request.getParameterValues(paramName1); for (String paramValue : paramValues1) - output.write(paramValue.getBytes("UTF-8")); + output.write(paramValue.getBytes(StandardCharsets.UTF_8)); String paramValue2 = request.getParameter(paramName2); - output.write(paramValue2.getBytes("UTF-8")); + output.write(paramValue2.getBytes(StandardCharsets.UTF_8)); baseRequest.setHandled(true); } }); @@ -225,7 +226,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - String content = new String(response.getContent(), "UTF-8"); + String content = new String(response.getContent(), StandardCharsets.UTF_8); Assert.assertEquals(value11 + value12 + value2, content); } @@ -257,7 +258,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - Assert.assertEquals(paramValue, new String(response.getContent(), "UTF-8")); + Assert.assertEquals(paramValue, new String(response.getContent(), StandardCharsets.UTF_8)); } @Test @@ -289,7 +290,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest Assert.assertNotNull(response); Assert.assertEquals(200, response.getStatus()); - Assert.assertEquals(paramValue, new String(response.getContent(), "UTF-8")); + Assert.assertEquals(paramValue, new String(response.getContent(), StandardCharsets.UTF_8)); } @Test diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpReceiverTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpReceiverTest.java index d0cd047285e..492b1a196fc 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpReceiverTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpReceiverTest.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.client; +import java.nio.charset.StandardCharsets; public class HttpReceiverTest { // @Rule @@ -102,7 +103,7 @@ public class HttpReceiverTest // Assert.assertNotNull(headers); // Assert.assertEquals(1, headers.size()); // Assert.assertEquals(String.valueOf(content.length()), headers.get(HttpHeader.CONTENT_LENGTH)); -// String received = listener.getContentAsString("UTF-8"); +// String received = listener.getContentAsString(StandardCharsets.UTF_8); // Assert.assertEquals(content, received); // } // diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpSenderTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpSenderTest.java index 08de6e45033..4b181134738 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/HttpSenderTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/HttpSenderTest.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.client; +import java.nio.charset.StandardCharsets; public class HttpSenderTest { // @Rule @@ -172,7 +173,7 @@ public class HttpSenderTest // HttpConnection connection = new HttpConnection(client, endPoint, destination); // Request request = client.newRequest(URI.create("http://localhost/")); // String content = "abcdef"; -// request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content.getBytes("UTF-8")))); +// request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)))); // final CountDownLatch headersLatch = new CountDownLatch(1); // final CountDownLatch successLatch = new CountDownLatch(1); // request.listener(new Request.Listener.Adapter() @@ -207,7 +208,7 @@ public class HttpSenderTest // Request request = client.newRequest(URI.create("http://localhost/")); // String content1 = "0123456789"; // String content2 = "abcdef"; -// request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes("UTF-8")), ByteBuffer.wrap(content2.getBytes("UTF-8")))); +// request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap(content2.getBytes(StandardCharsets.UTF_8)))); // final CountDownLatch headersLatch = new CountDownLatch(1); // final CountDownLatch successLatch = new CountDownLatch(1); // request.listener(new Request.Listener.Adapter() @@ -242,7 +243,7 @@ public class HttpSenderTest // Request request = client.newRequest(URI.create("http://localhost/")); // String content1 = "0123456789"; // String content2 = "ABCDEF"; -// request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes("UTF-8")), ByteBuffer.wrap(content2.getBytes("UTF-8"))) +// request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap(content2.getBytes(StandardCharsets.UTF_8))) // { // @Override // public long getLength() diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/api/Usage.java b/jetty-client/src/test/java/org/eclipse/jetty/client/api/Usage.java index 397d5bb9305..eddfcb3d672 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/api/Usage.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/api/Usage.java @@ -24,6 +24,7 @@ import java.io.OutputStream; import java.net.HttpCookie; import java.net.URI; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.nio.file.Paths; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -269,7 +270,7 @@ public class Usage HttpClient client = new HttpClient(); client.start(); - InputStream input = new ByteArrayInputStream("content".getBytes("UTF-8")); + InputStream input = new ByteArrayInputStream("content".getBytes(StandardCharsets.UTF_8)); ContentResponse response = client.newRequest("localhost", 8080) // Provide the content as InputStream diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesClientTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesClientTest.java index ad75b485338..b2b860dafbe 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesClientTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesClientTest.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.SocketTimeoutException; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; @@ -151,7 +152,7 @@ public class SslBytesClientTest extends SslBytesTest SimpleProxy.AutomaticFlow automaticProxyFlow = proxy.startAutomaticFlow(); // Read request - BufferedReader reader = new BufferedReader(new InputStreamReader(server.getInputStream(), "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(server.getInputStream(), StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertTrue(line.startsWith("GET")); while (line.length() > 0) @@ -161,7 +162,7 @@ public class SslBytesClientTest extends SslBytesTest OutputStream output = server.getOutputStream(); output.write(("HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); output.flush(); Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS)); @@ -197,7 +198,7 @@ public class SslBytesClientTest extends SslBytesTest // Read request InputStream serverInput = server.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput, "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput, StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertTrue(line.startsWith("GET")); while (line.length() > 0) @@ -206,16 +207,16 @@ public class SslBytesClientTest extends SslBytesTest OutputStream serverOutput = server.getOutputStream(); byte[] data1 = new byte[1024]; Arrays.fill(data1, (byte)'X'); - String content1 = new String(data1, "UTF-8"); + String content1 = new String(data1, StandardCharsets.UTF_8); byte[] data2 = new byte[1024]; Arrays.fill(data2, (byte)'Y'); - final String content2 = new String(data2, "UTF-8"); + final String content2 = new String(data2, StandardCharsets.UTF_8); // Write first part of the response serverOutput.write(("HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: " + (content1.length() + content2.length()) + "\r\n" + "\r\n" + - content1).getBytes("UTF-8")); + content1).getBytes(StandardCharsets.UTF_8)); serverOutput.flush(); Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS)); @@ -319,7 +320,7 @@ public class SslBytesClientTest extends SslBytesTest // Read request InputStream serverInput = server.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput, "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput, StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertTrue(line.startsWith("GET")); while (line.length() > 0) @@ -328,16 +329,16 @@ public class SslBytesClientTest extends SslBytesTest OutputStream serverOutput = server.getOutputStream(); byte[] data1 = new byte[1024]; Arrays.fill(data1, (byte)'X'); - String content1 = new String(data1, "UTF-8"); + String content1 = new String(data1, StandardCharsets.UTF_8); byte[] data2 = new byte[1024]; Arrays.fill(data2, (byte)'Y'); - final String content2 = new String(data2, "UTF-8"); + final String content2 = new String(data2, StandardCharsets.UTF_8); // Write first part of the response serverOutput.write(("HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: " + (content1.length() + content2.length()) + "\r\n" + "\r\n" + - content1).getBytes("UTF-8")); + content1).getBytes(StandardCharsets.UTF_8)); serverOutput.flush(); Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS)); diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesServerTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesServerTest.java index 31f34914904..65b7f298c79 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesServerTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/ssl/SslBytesServerTest.java @@ -31,6 +31,7 @@ import java.net.SocketTimeoutException; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.SocketChannel; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; @@ -586,7 +587,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -628,7 +629,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -644,7 +645,7 @@ public class SslBytesServerTest extends SslBytesTest Assert.assertEquals(TLSRecord.Type.APPLICATION, record.getType()); proxy.flushToClient(record); - BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertTrue(line.startsWith("HTTP/1.1 200 ")); @@ -721,7 +722,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -738,7 +739,7 @@ public class SslBytesServerTest extends SslBytesTest Assert.assertEquals(TLSRecord.Type.APPLICATION, record.getType()); proxy.flushToClient(record); - BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertTrue(line.startsWith("HTTP/1.1 200 ")); @@ -801,7 +802,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -876,7 +877,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -946,7 +947,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -1005,7 +1006,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -1055,7 +1056,7 @@ public class SslBytesServerTest extends SslBytesTest byte[] data = new byte[128 * 1024]; Arrays.fill(data, (byte)'X'); - final String content = new String(data, "UTF-8"); + final String content = new String(data, StandardCharsets.UTF_8); Future request = threadPool.submit(new Callable() { @Override @@ -1067,7 +1068,7 @@ public class SslBytesServerTest extends SslBytesTest "Host: localhost\r\n" + "Content-Length: " + content.length() + "\r\n" + "\r\n" + - content).getBytes("UTF-8")); + content).getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -1113,7 +1114,7 @@ public class SslBytesServerTest extends SslBytesTest byte[] data = new byte[128 * 1024]; Arrays.fill(data, (byte)'X'); - final String content = new String(data, "UTF-8"); + final String content = new String(data, StandardCharsets.UTF_8); Future request = threadPool.submit(new Callable() { @Override @@ -1125,7 +1126,7 @@ public class SslBytesServerTest extends SslBytesTest "Host: localhost\r\n" + "Content-Length: " + content.length() + "\r\n" + "\r\n" + - content).getBytes("UTF-8")); + content).getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -1187,7 +1188,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -1266,7 +1267,7 @@ public class SslBytesServerTest extends SslBytesTest "Content-Type: text/plain\r\n" + "Content-Length: " + content.length() + "\r\n" + "\r\n" + - content).getBytes("UTF-8")); + content).getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -1287,7 +1288,7 @@ public class SslBytesServerTest extends SslBytesTest Assert.assertEquals(TLSRecord.Type.APPLICATION, record.getType()); proxy.flushToClient(record); - BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertTrue(line.startsWith("HTTP/1.1 200 ")); @@ -1318,7 +1319,7 @@ public class SslBytesServerTest extends SslBytesTest // Use a content that is larger than the TLS record which is 2^14 (around 16k) byte[] data = new byte[128 * 1024]; Arrays.fill(data, (byte)'X'); - final String content = new String(data, "UTF-8"); + final String content = new String(data, StandardCharsets.UTF_8); Future request = threadPool.submit(new Callable() { @@ -1332,7 +1333,7 @@ public class SslBytesServerTest extends SslBytesTest "Content-Type: text/plain\r\n" + "Content-Length: " + content.length() + "\r\n" + "\r\n" + - content).getBytes("UTF-8")); + content).getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -1364,7 +1365,7 @@ public class SslBytesServerTest extends SslBytesTest Assert.assertEquals(TLSRecord.Type.APPLICATION, record.getType()); proxy.flushToClient(record); - BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertTrue(line.startsWith("HTTP/1.1 200 ")); @@ -1399,10 +1400,10 @@ public class SslBytesServerTest extends SslBytesTest byte[] data1 = new byte[1024]; Arrays.fill(data1, (byte)'X'); - String content1 = new String(data1, "UTF-8"); + String content1 = new String(data1, StandardCharsets.UTF_8); byte[] data2 = new byte[1024]; Arrays.fill(data2, (byte)'Y'); - final String content2 = new String(data2, "UTF-8"); + final String content2 = new String(data2, StandardCharsets.UTF_8); // Write only part of the body automaticProxyFlow = proxy.startAutomaticFlow(); @@ -1412,7 +1413,7 @@ public class SslBytesServerTest extends SslBytesTest "Content-Type: text/plain\r\n" + "Content-Length: " + (content1.length() + content2.length()) + "\r\n" + "\r\n" + - content1).getBytes("UTF-8")); + content1).getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS)); @@ -1446,7 +1447,7 @@ public class SslBytesServerTest extends SslBytesTest @Override public Object call() throws Exception { - clientOutput.write(content2.getBytes("UTF-8")); + clientOutput.write(content2.getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -1490,10 +1491,10 @@ public class SslBytesServerTest extends SslBytesTest // Use a content that is larger than the TLS record which is 2^14 (around 16k) byte[] data1 = new byte[80 * 1024]; Arrays.fill(data1, (byte)'X'); - String content1 = new String(data1, "UTF-8"); + String content1 = new String(data1, StandardCharsets.UTF_8); byte[] data2 = new byte[48 * 1024]; Arrays.fill(data2, (byte)'Y'); - final String content2 = new String(data2, "UTF-8"); + final String content2 = new String(data2, StandardCharsets.UTF_8); // Write only part of the body automaticProxyFlow = proxy.startAutomaticFlow(); @@ -1503,7 +1504,7 @@ public class SslBytesServerTest extends SslBytesTest "Content-Type: text/plain\r\n" + "Content-Length: " + (content1.length() + content2.length()) + "\r\n" + "\r\n" + - content1).getBytes("UTF-8")); + content1).getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS)); @@ -1568,7 +1569,7 @@ public class SslBytesServerTest extends SslBytesTest @Override public Object call() throws Exception { - clientOutput.write(content2.getBytes("UTF-8")); + clientOutput.write(content2.getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -1590,7 +1591,7 @@ public class SslBytesServerTest extends SslBytesTest Assert.assertEquals(TLSRecord.Type.APPLICATION, record.getType()); proxy.flushToClient(record); - BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertTrue(line.startsWith("HTTP/1.1 200 ")); @@ -1624,10 +1625,10 @@ public class SslBytesServerTest extends SslBytesTest // Use a content that is larger than the TLS record which is 2^14 (around 16k) byte[] data1 = new byte[80 * 1024]; Arrays.fill(data1, (byte)'X'); - String content1 = new String(data1, "UTF-8"); + String content1 = new String(data1, StandardCharsets.UTF_8); byte[] data2 = new byte[48 * 1024]; Arrays.fill(data2, (byte)'Y'); - final String content2 = new String(data2, "UTF-8"); + final String content2 = new String(data2, StandardCharsets.UTF_8); // Write only part of the body automaticProxyFlow = proxy.startAutomaticFlow(); @@ -1637,7 +1638,7 @@ public class SslBytesServerTest extends SslBytesTest "Content-Type: text/plain\r\n" + "Content-Length: " + (content1.length() + content2.length()) + "\r\n" + "\r\n" + - content1).getBytes("UTF-8")); + content1).getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS)); @@ -1720,7 +1721,7 @@ public class SslBytesServerTest extends SslBytesTest @Override public Object call() throws Exception { - clientOutput.write(content2.getBytes("UTF-8")); + clientOutput.write(content2.getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); return null; } @@ -1752,7 +1753,7 @@ public class SslBytesServerTest extends SslBytesTest Assert.assertEquals(TLSRecord.Type.APPLICATION, record.getType()); proxy.flushToClient(record); - BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertTrue(line.startsWith("HTTP/1.1 200 ")); @@ -1783,7 +1784,7 @@ public class SslBytesServerTest extends SslBytesTest byte[] data = new byte[3 * 1024]; Arrays.fill(data, (byte)'Y'); - String content = new String(data, "UTF-8"); + String content = new String(data, StandardCharsets.UTF_8); automaticProxyFlow = proxy.startAutomaticFlow(); clientOutput.write(("" + "POST / HTTP/1.1\r\n" + @@ -1792,10 +1793,10 @@ public class SslBytesServerTest extends SslBytesTest "Content-Length: " + content.length() + "\r\n" + "Connection: close\r\n" + "\r\n" + - content).getBytes("UTF-8")); + content).getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); - BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream(), StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertNotNull(line); Assert.assertTrue(line.startsWith("HTTP/1.1 200 ")); @@ -1841,7 +1842,7 @@ public class SslBytesServerTest extends SslBytesTest }); // Instead of passing the Client Hello, we simulate plain text was passed in - proxy.flushToServer(0, "GET / HTTP/1.1\r\n".getBytes("UTF-8")); + proxy.flushToServer(0, "GET / HTTP/1.1\r\n".getBytes(StandardCharsets.UTF_8)); // We expect that the server closes the connection immediately TLSRecord record = proxy.readFromServer(); @@ -1875,7 +1876,7 @@ public class SslBytesServerTest extends SslBytesTest clientOutput.write(("" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); clientOutput.flush(); latch.countDown(); } diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java index 1394091f00b..f898f64d307 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpField.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.http; +import java.nio.charset.StandardCharsets; /* ------------------------------------------------------------ */ /** A HTTP Field diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java index 9e73ee5d69a..7fbfe0fc2d7 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.http; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.http.HttpTokens.EndOfContent; import org.eclipse.jetty.util.ArrayTernaryTrie; @@ -1025,12 +1026,12 @@ public class HttpParser // Have to get the fields exactly from the buffer to match case String fn=field.getName(); String fv=field.getValue(); - n=BufferUtil.toString(buffer,buffer.position()-1,fn.length(),StringUtil.__US_ASCII_CHARSET); + n=BufferUtil.toString(buffer,buffer.position()-1,fn.length(),StandardCharsets.US_ASCII); if (fv==null) v=null; else { - v=BufferUtil.toString(buffer,buffer.position()+fn.length()+1,fv.length(),StringUtil.__ISO_8859_1_CHARSET); + v=BufferUtil.toString(buffer,buffer.position()+fn.length()+1,fv.length(),StandardCharsets.ISO_8859_1); field=new HttpField(field.getHeader(),n,v); } } diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpTester.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpTester.java index 0170d3e2049..ee4801f1f55 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpTester.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpTester.java @@ -22,6 +22,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.http.HttpGenerator.RequestInfo; import org.eclipse.jetty.http.HttpGenerator.ResponseInfo; @@ -346,7 +347,7 @@ public class HttpTester String content_type=get(HttpHeader.CONTENT_TYPE); String encoding=MimeTypes.getCharsetFromContentType(content_type); - Charset charset=encoding==null?StringUtil.__UTF8_CHARSET:Charset.forName(encoding); + Charset charset=encoding==null?StandardCharsets.UTF_8:Charset.forName(encoding); return new String(bytes,charset); } diff --git a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java index dcd00cf2174..47101268f3f 100644 --- a/jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java +++ b/jetty-http/src/main/java/org/eclipse/jetty/http/HttpURI.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.http; import java.io.UnsupportedEncodingException; import java.net.URI; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.util.MultiMap; import org.eclipse.jetty.util.StringUtil; @@ -100,15 +101,7 @@ public class HttpURI public HttpURI(String raw) { _rawString=raw; - byte[] b; - try - { - b = raw.getBytes(StringUtil.__UTF8); - } - catch (UnsupportedEncodingException e) - { - throw new RuntimeException(e.getMessage()); - } + byte[] b = raw.getBytes(StandardCharsets.UTF_8); parse(b,0,b.length); _charset = URIUtil.__CHARSET; } @@ -617,8 +610,13 @@ public class HttpURI return StringUtil.toUTF8String(_raw, _path, _param-_path); return utf8b.toString(); } - + public String getDecodedPath(String encoding) + { + return getDecodedPath(Charset.forName(encoding)); + } + + public String getDecodedPath(Charset encoding) { if (_path==_param) return null; @@ -678,9 +676,9 @@ public class HttpURI if (bytes==null) - return StringUtil.toString(_raw,_path,_param-_path,encoding); + return new String(_raw,_path,_param-_path,encoding); - return StringUtil.toString(bytes,0,n,encoding); + return new String(bytes,0,n,encoding); } public String getPathAndParam() @@ -734,10 +732,10 @@ public class HttpURI { if (_query==_fragment) return; - if (_charset==StringUtil.__UTF8_CHARSET) + if (_charset.equals(StandardCharsets.UTF_8)) UrlEncoded.decodeUtf8To(_raw,_query+1,_fragment-_query-1,parameters); else - UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,_charset.toString()),parameters,_charset.toString(),-1); + UrlEncoded.decodeTo(new String(_raw,_query+1,_fragment-_query-1,_charset),parameters,_charset,-1); } public void decodeQueryTo(MultiMap parameters, String encoding) throws UnsupportedEncodingException @@ -751,6 +749,17 @@ public class HttpURI UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,encoding),parameters,encoding,-1); } + public void decodeQueryTo(MultiMap parameters, Charset encoding) throws UnsupportedEncodingException + { + if (_query==_fragment) + return; + + if (encoding==null || StandardCharsets.UTF_8.equals(encoding)) + UrlEncoded.decodeUtf8To(_raw,_query+1,_fragment-_query-1,parameters); + else + UrlEncoded.decodeTo(new String(_raw,_query+1,_fragment-_query-1,encoding),parameters,encoding,-1); + } + public void clear() { _scheme=_authority=_host=_port=_path=_param=_query=_fragment=_end=0; diff --git a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java index d3d77b3e42b..5c15c985293 100644 --- a/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java +++ b/jetty-http/src/test/java/org/eclipse/jetty/http/HttpParserTest.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -112,7 +113,7 @@ public class HttpParserTest @Test public void testLineParse3() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer("POST /fo\u0690 HTTP/1.0\015\012" + "\015\012",StringUtil.__UTF8_CHARSET); + ByteBuffer buffer= BufferUtil.toBuffer("POST /fo\u0690 HTTP/1.0\015\012" + "\015\012",StandardCharsets.UTF_8); HttpParser.RequestHandler handler = new Handler(); HttpParser parser= new HttpParser(handler); @@ -126,7 +127,7 @@ public class HttpParserTest @Test public void testLineParse4() throws Exception { - ByteBuffer buffer= BufferUtil.toBuffer("POST /foo?param=\u0690 HTTP/1.0\015\012" + "\015\012",StringUtil.__UTF8_CHARSET); + ByteBuffer buffer= BufferUtil.toBuffer("POST /foo?param=\u0690 HTTP/1.0\015\012" + "\015\012",StandardCharsets.UTF_8); HttpParser.RequestHandler handler = new Handler(); HttpParser parser= new HttpParser(handler); @@ -346,10 +347,10 @@ public class HttpParserTest ByteBuffer buffer=BufferUtil.allocate(4096); BufferUtil.flipToFill(buffer); BufferUtil.put(BufferUtil.toBuffer("GET "),buffer); - buffer.put("/foo/\u0690/".getBytes(StringUtil.__UTF8_CHARSET)); + buffer.put("/foo/\u0690/".getBytes(StandardCharsets.UTF_8)); BufferUtil.put(BufferUtil.toBuffer(" HTTP/1.0\r\n"),buffer); BufferUtil.put(BufferUtil.toBuffer("Header1: "),buffer); - buffer.put("\u00e6 \u00e6".getBytes(StringUtil.__ISO_8859_1_CHARSET)); + buffer.put("\u00e6 \u00e6".getBytes(StandardCharsets.ISO_8859_1)); BufferUtil.put(BufferUtil.toBuffer(" \r\n\r\n"),buffer); BufferUtil.flipToFlush(buffer,0); @@ -1348,7 +1349,7 @@ public class HttpParserTest { if (_content==null) _content=""; - String c = BufferUtil.toString(ref,StringUtil.__UTF8_CHARSET); + String c = BufferUtil.toString(ref,StandardCharsets.UTF_8); //System.err.println("content '"+c+"'"); _content= _content + c; ref.position(ref.limit()); diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteArrayEndPoint.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteArrayEndPoint.java index 156cd15ffb9..460b298a85b 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ByteArrayEndPoint.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ByteArrayEndPoint.java @@ -23,6 +23,7 @@ import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.StringUtil; @@ -153,7 +154,7 @@ public class ByteArrayEndPoint extends AbstractEndPoint /* ------------------------------------------------------------ */ public void setInput(String s) { - setInput(BufferUtil.toBuffer(s,StringUtil.__UTF8_CHARSET)); + setInput(BufferUtil.toBuffer(s,StandardCharsets.UTF_8)); } /* ------------------------------------------------------------ */ @@ -177,7 +178,7 @@ public class ByteArrayEndPoint extends AbstractEndPoint */ public String getOutputString() { - return getOutputString(StringUtil.__UTF8_CHARSET); + return getOutputString(StandardCharsets.UTF_8); } /* ------------------------------------------------------------ */ @@ -207,7 +208,7 @@ public class ByteArrayEndPoint extends AbstractEndPoint */ public String takeOutputString() { - return takeOutputString(StringUtil.__UTF8_CHARSET); + return takeOutputString(StandardCharsets.UTF_8); } /* ------------------------------------------------------------ */ diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/WriterOutputStream.java b/jetty-io/src/main/java/org/eclipse/jetty/io/WriterOutputStream.java index d94c270013d..6ea17ffbd5f 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/WriterOutputStream.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/WriterOutputStream.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.io; import java.io.IOException; import java.io.OutputStream; import java.io.Writer; +import java.nio.charset.Charset; /* ------------------------------------------------------------ */ @@ -33,14 +34,14 @@ import java.io.Writer; public class WriterOutputStream extends OutputStream { protected final Writer _writer; - protected final String _encoding; + protected final Charset _encoding; private final byte[] _buf=new byte[1]; /* ------------------------------------------------------------ */ public WriterOutputStream(Writer writer, String encoding) { _writer=writer; - _encoding=encoding; + _encoding=encoding==null?null:Charset.forName(encoding); } /* ------------------------------------------------------------ */ diff --git a/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java b/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java index 18f04041aab..2d77b9321cf 100644 --- a/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java +++ b/jetty-io/src/test/java/org/eclipse/jetty/io/IOTest.java @@ -36,6 +36,7 @@ import java.nio.channels.AsynchronousServerSocketChannel; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; +import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -444,7 +445,7 @@ public class IOTest ByteBuffer read = ByteBuffer.allocate(1024); Future reading = server.read(read); - byte[] data = "Testing 1 2 3".getBytes("UTF-8"); + byte[] data = "Testing 1 2 3".getBytes(StandardCharsets.UTF_8); ByteBuffer write = BufferUtil.toBuffer(data); Future writing = client.write(write); diff --git a/jetty-io/src/test/java/org/eclipse/jetty/io/SelectChannelEndPointSslTest.java b/jetty-io/src/test/java/org/eclipse/jetty/io/SelectChannelEndPointSslTest.java index 7bd05967742..c796e862c80 100644 --- a/jetty-io/src/test/java/org/eclipse/jetty/io/SelectChannelEndPointSslTest.java +++ b/jetty-io/src/test/java/org/eclipse/jetty/io/SelectChannelEndPointSslTest.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.net.Socket; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; +import java.nio.charset.StandardCharsets; import javax.net.ssl.SSLEngine; import javax.net.ssl.SSLEngineResult; import javax.net.ssl.SSLEngineResult.HandshakeStatus; @@ -170,7 +171,7 @@ public class SelectChannelEndPointSslTest extends SelectChannelEndPointTest if (debug) System.err.println("\nSay Hello"); // write a message - appOut.put("HelloWorld".getBytes("UTF-8")); + appOut.put("HelloWorld".getBytes(StandardCharsets.UTF_8)); appOut.flip(); SSLEngineResult result =engine.wrap(appOut,sslOut); if (debug) System.err.println(result); diff --git a/jetty-io/src/test/java/org/eclipse/jetty/io/SelectChannelEndPointTest.java b/jetty-io/src/test/java/org/eclipse/jetty/io/SelectChannelEndPointTest.java index 1849dbfc19d..205070636fe 100644 --- a/jetty-io/src/test/java/org/eclipse/jetty/io/SelectChannelEndPointTest.java +++ b/jetty-io/src/test/java/org/eclipse/jetty/io/SelectChannelEndPointTest.java @@ -29,6 +29,7 @@ import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; +import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -228,7 +229,7 @@ public class SelectChannelEndPointTest _manager.accept(server); // Write client to server - client.getOutputStream().write("HelloWorld".getBytes("UTF-8")); + client.getOutputStream().write("HelloWorld".getBytes(StandardCharsets.UTF_8)); // Verify echo server to client for (char c : "HelloWorld".toCharArray()) @@ -253,7 +254,7 @@ public class SelectChannelEndPointTest } // write then shutdown - client.getOutputStream().write("Goodbye Cruel TLS".getBytes("UTF-8")); + client.getOutputStream().write("Goodbye Cruel TLS".getBytes(StandardCharsets.UTF_8)); // Verify echo server to client for (char c : "Goodbye Cruel TLS".toCharArray()) @@ -287,7 +288,7 @@ public class SelectChannelEndPointTest _manager.accept(server); // Write client to server - client.getOutputStream().write("HelloWorld".getBytes("UTF-8")); + client.getOutputStream().write("HelloWorld".getBytes(StandardCharsets.UTF_8)); // Verify echo server to client for (char c : "HelloWorld".toCharArray()) @@ -310,7 +311,7 @@ public class SelectChannelEndPointTest } // write then shutdown - client.getOutputStream().write("Goodbye Cruel TLS".getBytes("UTF-8")); + client.getOutputStream().write("Goodbye Cruel TLS".getBytes(StandardCharsets.UTF_8)); client.shutdownOutput(); // Verify echo server to client @@ -343,7 +344,7 @@ public class SelectChannelEndPointTest // Write 8 and cause block waiting for 10 _blockAt = 10; - clientOutputStream.write("12345678".getBytes("UTF-8")); + clientOutputStream.write("12345678".getBytes(StandardCharsets.UTF_8)); clientOutputStream.flush(); Assert.assertTrue(_lastEndPointLatch.await(1, TimeUnit.SECONDS)); @@ -363,7 +364,7 @@ public class SelectChannelEndPointTest } // write remaining characters - clientOutputStream.write("90ABCDEF".getBytes("UTF-8")); + clientOutputStream.write("90ABCDEF".getBytes(StandardCharsets.UTF_8)); clientOutputStream.flush(); // Verify echo server to client @@ -388,7 +389,7 @@ public class SelectChannelEndPointTest _manager.accept(server); // Write client to server - client.getOutputStream().write("HelloWorld".getBytes("UTF-8")); + client.getOutputStream().write("HelloWorld".getBytes(StandardCharsets.UTF_8)); // Verify echo server to client for (char c : "HelloWorld".toCharArray()) @@ -436,7 +437,7 @@ public class SelectChannelEndPointTest _manager.accept(server); // Write client to server - clientOutputStream.write("HelloWorld".getBytes("UTF-8")); + clientOutputStream.write("HelloWorld".getBytes(StandardCharsets.UTF_8)); // Verify echo server to client for (char c : "HelloWorld".toCharArray()) @@ -452,7 +453,7 @@ public class SelectChannelEndPointTest // Write 8 and cause block waiting for 10 _blockAt = 10; - clientOutputStream.write("12345678".getBytes("UTF-8")); + clientOutputStream.write("12345678".getBytes(StandardCharsets.UTF_8)); clientOutputStream.flush(); // read until idle shutdown received @@ -493,8 +494,8 @@ public class SelectChannelEndPointTest _manager.accept(server); final int writes = 200000; - final byte[] bytes = "HelloWorld-".getBytes(StringUtil.__UTF8_CHARSET); - byte[] count = "0\n".getBytes(StringUtil.__UTF8_CHARSET); + final byte[] bytes = "HelloWorld-".getBytes(StandardCharsets.UTF_8); + byte[] count = "0\n".getBytes(StandardCharsets.UTF_8); BufferedOutputStream out = new BufferedOutputStream(client.getOutputStream()); final CountDownLatch latch = new CountDownLatch(writes); final InputStream in = new BufferedInputStream(client.getInputStream()); @@ -561,7 +562,7 @@ public class SelectChannelEndPointTest for (int i = 1; i < writes; i++) { out.write(bytes); - out.write(Integer.toString(i).getBytes(StringUtil.__ISO_8859_1_CHARSET)); + out.write(Integer.toString(i).getBytes(StandardCharsets.ISO_8859_1)); out.write('\n'); if (i % 1000 == 0) { @@ -599,7 +600,7 @@ public class SelectChannelEndPointTest // Write client to server _writeCount = 10000; String data = "Now is the time for all good men to come to the aid of the party"; - client.getOutputStream().write(data.getBytes("UTF-8")); + client.getOutputStream().write(data.getBytes(StandardCharsets.UTF_8)); BufferedInputStream in = new BufferedInputStream(client.getInputStream()); int byteNum = 0; diff --git a/jetty-io/src/test/java/org/eclipse/jetty/io/SslConnectionTest.java b/jetty-io/src/test/java/org/eclipse/jetty/io/SslConnectionTest.java index c02bd000cae..83a2c40f509 100644 --- a/jetty-io/src/test/java/org/eclipse/jetty/io/SslConnectionTest.java +++ b/jetty-io/src/test/java/org/eclipse/jetty/io/SslConnectionTest.java @@ -27,6 +27,7 @@ import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; +import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -234,14 +235,14 @@ public class SslConnectionTest server.configureBlocking(false); _manager.accept(server); - client.getOutputStream().write("Hello".getBytes("UTF-8")); + client.getOutputStream().write("Hello".getBytes(StandardCharsets.UTF_8)); byte[] buffer = new byte[1024]; int len=client.getInputStream().read(buffer); Assert.assertEquals(5, len); - Assert.assertEquals("Hello",new String(buffer,0,len,StringUtil.__UTF8_CHARSET)); + Assert.assertEquals("Hello",new String(buffer,0,len,StandardCharsets.UTF_8)); _dispatches.set(0); - client.getOutputStream().write("World".getBytes("UTF-8")); + client.getOutputStream().write("World".getBytes(StandardCharsets.UTF_8)); len=5; while(len>0) len-=client.getInputStream().read(buffer); @@ -266,7 +267,7 @@ public class SslConnectionTest byte[] buffer = new byte[1024]; int len=client.getInputStream().read(buffer); - Assert.assertEquals("Hello Client",new String(buffer,0,len,StringUtil.__UTF8_CHARSET)); + Assert.assertEquals("Hello Client",new String(buffer,0,len,StandardCharsets.UTF_8)); Assert.assertEquals(null,_writeCallback.get(100,TimeUnit.MILLISECONDS)); client.close(); } @@ -292,7 +293,7 @@ public class SslConnectionTest { try { - BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream(),StringUtil.__UTF8_CHARSET)); + BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream(),StandardCharsets.UTF_8)); while(count.getCount()>0) { String line=in.readLine(); @@ -311,7 +312,7 @@ public class SslConnectionTest for (int i=0;i < KD ( H(A1), unq(nonce-value) ":" H(A2) // ) > <"> - md.update(TypeUtil.toString(ha1, 16).getBytes(StringUtil.__ISO_8859_1)); + md.update(TypeUtil.toString(ha1, 16).getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(nonce.getBytes(StringUtil.__ISO_8859_1)); + md.update(nonce.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(nc.getBytes(StringUtil.__ISO_8859_1)); + md.update(nc.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(cnonce.getBytes(StringUtil.__ISO_8859_1)); + md.update(cnonce.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(qop.getBytes(StringUtil.__ISO_8859_1)); + md.update(qop.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(TypeUtil.toString(ha2, 16).getBytes(StringUtil.__ISO_8859_1)); + md.update(TypeUtil.toString(ha2, 16).getBytes(StandardCharsets.ISO_8859_1)); byte[] digest = md.digest(); // check digest diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerSSLTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerSSLTest.java index 656f4e84359..e7b18c878e4 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerSSLTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerSSLTest.java @@ -25,6 +25,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; +import java.nio.charset.StandardCharsets; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; @@ -78,7 +79,7 @@ public class ConnectHandlerSSLTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -98,7 +99,7 @@ public class ConnectHandlerSSLTest extends AbstractConnectHandlerTest "GET /echo HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); @@ -121,7 +122,7 @@ public class ConnectHandlerSSLTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -145,7 +146,7 @@ public class ConnectHandlerSSLTest extends AbstractConnectHandlerTest "Content-Length: 5\r\n" + "\r\n" + "HELLO"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java index ed929efee55..e24e57e5e83 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ConnectHandlerTest.java @@ -27,6 +27,7 @@ import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; +import java.nio.charset.StandardCharsets; import java.util.Locale; import java.util.concurrent.ConcurrentMap; @@ -71,7 +72,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -93,7 +94,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -104,7 +105,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); @@ -130,7 +131,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 403 from the CONNECT request @@ -151,7 +152,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 from the CONNECT request @@ -162,7 +163,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); @@ -188,7 +189,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 403 from the CONNECT request @@ -209,7 +210,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 from the CONNECT request @@ -220,7 +221,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "GET /echo" + " HTTP/1.1\r\n" + "Host: 127.0.0.1:" + port + "\r\n" + "\r\n"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); @@ -245,7 +246,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest return false; } String b64 = proxyAuthorization.substring("Basic ".length()); - String credentials = B64Code.decode(b64, "UTF-8"); + String credentials = B64Code.decode(b64, StandardCharsets.UTF_8); return "test:test".equals(credentials); } }; @@ -265,7 +266,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 407 from the CONNECT request @@ -289,7 +290,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 from the CONNECT request @@ -300,7 +301,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); @@ -342,7 +343,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 500 OK from the CONNECT request @@ -368,7 +369,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -379,7 +380,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); @@ -404,7 +405,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -431,7 +432,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -444,7 +445,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); @@ -467,7 +468,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -478,7 +479,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "GET /echo HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); @@ -506,7 +507,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -517,7 +518,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "GET /close HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); int read = input.read(); @@ -538,7 +539,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -551,7 +552,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "Content-Length: 5\r\n" + "\r\n" + "HELLO"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); @@ -562,7 +563,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); @@ -585,7 +586,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -603,7 +604,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "Content-Length: " + body.length() + "\r\n" + "\r\n" + body; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); @@ -649,7 +650,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -663,7 +664,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "Content-Length: " + body.length() + "\r\n" + "\r\n" + body; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); response = readResponse(input); @@ -688,7 +689,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); socket.shutdownOutput(); @@ -716,7 +717,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest OutputStream output = socket.getOutputStream(); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); // Expect 200 OK from the CONNECT request @@ -727,7 +728,7 @@ public class ConnectHandlerTest extends AbstractConnectHandlerTest "GET /echo" + " HTTP/1.1\r\n" + "Host: " + hostPort + "\r\n" + "\r\n"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); socket.shutdownOutput(); diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRuleTest.java index e3dfe5c629b..58357fda78e 100644 --- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRuleTest.java +++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RewriteRegexRuleTest.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.rewrite.handler; import static org.junit.Assert.assertEquals; import java.io.IOException; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.http.HttpURI; import org.eclipse.jetty.util.MultiMap; @@ -87,7 +88,7 @@ public class RewriteRegexRuleTest extends AbstractRuleTestCase if (test[5]!=null) { MultiMap params=new MultiMap(); - UrlEncoded.decodeTo(test[5],params,StringUtil.__UTF8_CHARSET,-1); + UrlEncoded.decodeTo(test[5],params, StandardCharsets.UTF_8,-1); for (String n:params.keySet()) assertEquals(params.getString(n),_request.getParameter(n)); diff --git a/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/BasicAuthenticator.java b/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/BasicAuthenticator.java index 56d3fe41add..4eeeec8b119 100644 --- a/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/BasicAuthenticator.java +++ b/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/BasicAuthenticator.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.security.authentication; import java.io.IOException; +import java.nio.charset.StandardCharsets; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; @@ -82,7 +83,7 @@ public class BasicAuthenticator extends LoginAuthenticator if ("basic".equalsIgnoreCase(method)) { credentials = credentials.substring(space+1); - credentials = B64Code.decode(credentials,StringUtil.__ISO_8859_1); + credentials = B64Code.decode(credentials, StandardCharsets.ISO_8859_1); int i = credentials.indexOf(':'); if (i>0) { diff --git a/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/DigestAuthenticator.java b/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/DigestAuthenticator.java index 0fe374774ba..49f924a53bb 100644 --- a/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/DigestAuthenticator.java +++ b/jetty-security/src/main/java/org/eclipse/jetty/security/authentication/DigestAuthenticator.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.security.authentication; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.SecureRandom; import java.util.BitSet; @@ -367,18 +368,18 @@ public class DigestAuthenticator extends LoginAuthenticator else { // calc A1 digest - md.update(username.getBytes(StringUtil.__ISO_8859_1)); + md.update(username.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(realm.getBytes(StringUtil.__ISO_8859_1)); + md.update(realm.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(password.getBytes(StringUtil.__ISO_8859_1)); + md.update(password.getBytes(StandardCharsets.ISO_8859_1)); ha1 = md.digest(); } // calc A2 digest md.reset(); - md.update(method.getBytes(StringUtil.__ISO_8859_1)); + md.update(method.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(uri.getBytes(StringUtil.__ISO_8859_1)); + md.update(uri.getBytes(StandardCharsets.ISO_8859_1)); byte[] ha2 = md.digest(); // calc digest @@ -388,17 +389,17 @@ public class DigestAuthenticator extends LoginAuthenticator // request-digest = <"> < KD ( H(A1), unq(nonce-value) ":" H(A2) // ) > <"> - md.update(TypeUtil.toString(ha1, 16).getBytes(StringUtil.__ISO_8859_1)); + md.update(TypeUtil.toString(ha1, 16).getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(nonce.getBytes(StringUtil.__ISO_8859_1)); + md.update(nonce.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(nc.getBytes(StringUtil.__ISO_8859_1)); + md.update(nc.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(cnonce.getBytes(StringUtil.__ISO_8859_1)); + md.update(cnonce.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(qop.getBytes(StringUtil.__ISO_8859_1)); + md.update(qop.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(TypeUtil.toString(ha2, 16).getBytes(StringUtil.__ISO_8859_1)); + md.update(TypeUtil.toString(ha2, 16).getBytes(StandardCharsets.ISO_8859_1)); byte[] digest = md.digest(); // check digest diff --git a/jetty-security/src/test/java/org/eclipse/jetty/security/ConstraintTest.java b/jetty-security/src/test/java/org/eclipse/jetty/security/ConstraintTest.java index 42ddacfe62f..b33e801a36c 100644 --- a/jetty-security/src/test/java/org/eclipse/jetty/security/ConstraintTest.java +++ b/jetty-security/src/test/java/org/eclipse/jetty/security/ConstraintTest.java @@ -27,6 +27,7 @@ import static org.junit.Assert.assertTrue; import static org.junit.matchers.JUnitMatchers.containsString; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.util.ArrayList; import java.util.Arrays; @@ -569,17 +570,17 @@ public class ConstraintTest MessageDigest md = MessageDigest.getInstance("MD5"); byte[] ha1; // calc A1 digest - md.update(username.getBytes(StringUtil.__ISO_8859_1)); + md.update(username.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update("TestRealm".getBytes(StringUtil.__ISO_8859_1)); + md.update("TestRealm".getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(password.getBytes(StringUtil.__ISO_8859_1)); + md.update(password.getBytes(StandardCharsets.ISO_8859_1)); ha1 = md.digest(); // calc A2 digest md.reset(); - md.update("GET".getBytes(StringUtil.__ISO_8859_1)); + md.update("GET".getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(uri.getBytes(StringUtil.__ISO_8859_1)); + md.update(uri.getBytes(StandardCharsets.ISO_8859_1)); byte[] ha2 = md.digest(); // calc digest @@ -589,17 +590,17 @@ public class ConstraintTest // request-digest = <"> < KD ( H(A1), unq(nonce-value) ":" H(A2) // ) > <"> - md.update(TypeUtil.toString(ha1, 16).getBytes(StringUtil.__ISO_8859_1)); + md.update(TypeUtil.toString(ha1, 16).getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(nonce.getBytes(StringUtil.__ISO_8859_1)); + md.update(nonce.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(nc.getBytes(StringUtil.__ISO_8859_1)); + md.update(nc.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(CNONCE.getBytes(StringUtil.__ISO_8859_1)); + md.update(CNONCE.getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update("auth".getBytes(StringUtil.__ISO_8859_1)); + md.update("auth".getBytes(StandardCharsets.ISO_8859_1)); md.update((byte) ':'); - md.update(TypeUtil.toString(ha2, 16).getBytes(StringUtil.__ISO_8859_1)); + md.update(TypeUtil.toString(ha2, 16).getBytes(StandardCharsets.ISO_8859_1)); byte[] digest = md.digest(); // check digest diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java index 43e3a0fb814..78cad7c89d5 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/HttpChannel.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; +import java.nio.charset.StandardCharsets; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -491,7 +492,7 @@ public class HttpChannel implements HttpParser.RequestHandler, Runnable { LOG.warn("Failed UTF-8 decode for request path, trying ISO-8859-1"); LOG.ignore(e); - path = _uri.getDecodedPath(StringUtil.__ISO_8859_1); + path = _uri.getDecodedPath(StandardCharsets.ISO_8859_1); } String info = URIUtil.canonicalPath(path); diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/LocalConnector.java b/jetty-server/src/main/java/org/eclipse/jetty/server/LocalConnector.java index acf1cf073fc..bee177ad579 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/LocalConnector.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/LocalConnector.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.server; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; @@ -100,8 +101,8 @@ public class LocalConnector extends AbstractConnector */ public String getResponses(String requests,long idleFor,TimeUnit units) throws Exception { - ByteBuffer result = getResponses(BufferUtil.toBuffer(requests,StringUtil.__UTF8_CHARSET),idleFor,units); - return result==null?null:BufferUtil.toString(result,StringUtil.__UTF8_CHARSET); + ByteBuffer result = getResponses(BufferUtil.toBuffer(requests,StandardCharsets.UTF_8),idleFor,units); + return result==null?null:BufferUtil.toString(result,StandardCharsets.UTF_8); } /** Sends requests and get's responses based on thread activity. @@ -150,7 +151,7 @@ public class LocalConnector extends AbstractConnector */ public LocalEndPoint executeRequest(String rawRequest) { - return executeRequest(BufferUtil.toBuffer(rawRequest,StringUtil.__UTF8_CHARSET)); + return executeRequest(BufferUtil.toBuffer(rawRequest, StandardCharsets.UTF_8)); } private LocalEndPoint executeRequest(ByteBuffer rawRequest) @@ -190,7 +191,7 @@ public class LocalConnector extends AbstractConnector // TODO this is a busy wait while(getIn()==null || BufferUtil.hasContent(getIn())) Thread.yield(); - setInput(BufferUtil.toBuffer(s, StringUtil.__UTF8_CHARSET)); + setInput(BufferUtil.toBuffer(s, StandardCharsets.UTF_8)); } @Override diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java b/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java index caa320cfda0..49feda6b1e9 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/Request.java @@ -28,6 +28,7 @@ import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; import java.security.Principal; import java.util.ArrayList; @@ -1873,7 +1874,7 @@ public class Request implements HttpServletRequest /* ------------------------------------------------------------ */ /** * Set the character encoding used for the query string. This call will effect the return of getQueryString and getParamaters. It must be called before any - * geParameter methods. + * getParameter methods. * * The request attribute "org.eclipse.jetty.server.server.Request.queryEncoding" may be set as an alternate method of calling setQueryEncoding. * @@ -2100,6 +2101,7 @@ public class Request implements HttpServletRequest setAttribute(__MULTIPART_INPUT_STREAM, _multiPartInputStream); setAttribute(__MULTIPART_CONTEXT, _context); Collection parts = _multiPartInputStream.getParts(); //causes parsing + ByteArrayOutputStream os = null; for (Part p:parts) { MultiPartInputStreamParser.MultiPart mp = (MultiPartInputStreamParser.MultiPart)p; @@ -2110,21 +2112,17 @@ public class Request implements HttpServletRequest if (mp.getContentType() != null) charset = MimeTypes.getCharsetFromContentType(mp.getContentType()); - ByteArrayOutputStream os = null; - InputStream is = mp.getInputStream(); //get the bytes regardless of being in memory or in temp file - try + //get the bytes regardless of being in memory or in temp file + try (InputStream is = mp.getInputStream()) { - os = new ByteArrayOutputStream(); + if (os == null) + os = new ByteArrayOutputStream(); IO.copy(is, os); - String content=new String(os.toByteArray(),charset==null?StringUtil.__UTF8:charset); + String content=new String(os.toByteArray(),charset==null?StandardCharsets.UTF_8:Charset.forName(charset)); getParameter(""); //cause params to be evaluated getParameters().add(mp.getName(), content); } - finally - { - IO.close(os); - IO.close(is); - } + os.reset(); } } } @@ -2169,7 +2167,7 @@ public class Request implements HttpServletRequest { // extract parameters from dispatch query MultiMap parameters = new MultiMap<>(); - UrlEncoded.decodeTo(query,parameters, StringUtil.__UTF8_CHARSET,-1); //have to assume UTF-8 because we can't know otherwise + UrlEncoded.decodeTo(query,parameters, StandardCharsets.UTF_8,-1); //have to assume UTF-8 because we can't know otherwise boolean merge_old_query = false; @@ -2194,7 +2192,7 @@ public class Request implements HttpServletRequest MultiMap overridden_new_query = new MultiMap<>(); - UrlEncoded.decodeTo(query,overridden_new_query,StringUtil.__UTF8_CHARSET,-1); //have to assume utf8 as we cannot know otherwise + UrlEncoded.decodeTo(query,overridden_new_query,StandardCharsets.UTF_8,-1); //have to assume utf8 as we cannot know otherwise for(String name: overridden_old_query.keySet()) { diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ShutdownMonitor.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ShutdownMonitor.java index ded5bdc4feb..5e590ddbb4c 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ShutdownMonitor.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ShutdownMonitor.java @@ -25,6 +25,7 @@ import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.Properties; import org.eclipse.jetty.util.StringUtil; @@ -112,7 +113,7 @@ public class ShutdownMonitor // Reply to client debug("Informing client that we are stopped."); - out.write("Stopped\r\n".getBytes(StringUtil.__UTF8)); + out.write("Stopped\r\n".getBytes(StandardCharsets.UTF_8)); out.flush(); // Shutdown Monitor @@ -131,7 +132,7 @@ public class ShutdownMonitor else if ("status".equals(cmd)) { // Reply to client - out.write("OK\r\n".getBytes(StringUtil.__UTF8)); + out.write("OK\r\n".getBytes(StandardCharsets.UTF_8)); out.flush(); } } diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java index 7ad0282e12a..fe84adc956b 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/AbstractHttpTest.java @@ -25,6 +25,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -76,7 +77,7 @@ public abstract class AbstractHttpTest { Socket socket = new Socket("localhost", connector.getLocalPort()); socket.setSoTimeout((int)connector.getIdleTimeout()); - BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); String request = "GET / " + httpVersion + "\r\n"; diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncRequestReadTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncRequestReadTest.java index a1b673d18f3..f0ca3ac90be 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncRequestReadTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncRequestReadTest.java @@ -30,6 +30,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Exchanger; @@ -96,7 +97,7 @@ public class AsyncRequestReadTest "Content-Length: "+content.length+"\r\n"+ "Content-Type: bytes\r\n"+ "\r\n"; - byte[] h=header.getBytes(StringUtil.__ISO_8859_1); + byte[] h=header.getBytes(StandardCharsets.ISO_8859_1); out.write(h); out.write(content); @@ -108,7 +109,7 @@ public class AsyncRequestReadTest "Content-Type: bytes\r\n"+ "Connection: close\r\n"+ "\r\n"; - h=header.getBytes(StringUtil.__ISO_8859_1); + h=header.getBytes(StandardCharsets.ISO_8859_1); out.write(h); out.write(content); out.flush(); @@ -244,7 +245,7 @@ public class AsyncRequestReadTest "Content-Length: "+content.length+"\r\n"+ "Content-Type: bytes\r\n"+ "\r\n"; - byte[] h=header.getBytes(StringUtil.__ISO_8859_1); + byte[] h=header.getBytes(StandardCharsets.ISO_8859_1); out.write(h); out.write(content); @@ -254,7 +255,7 @@ public class AsyncRequestReadTest "Content-Type: bytes\r\n"+ "Connection: close\r\n"+ "\r\n"; - h=header.getBytes(StringUtil.__ISO_8859_1); + h=header.getBytes(StandardCharsets.ISO_8859_1); out.write(h); out.write(content); out.flush(); @@ -294,7 +295,7 @@ public class AsyncRequestReadTest "Content-Length: "+content.length+"\r\n"+ "Content-Type: bytes\r\n"+ "\r\n"; - byte[] h=header.getBytes(StringUtil.__ISO_8859_1); + byte[] h=header.getBytes(StandardCharsets.ISO_8859_1); out.write(h); out.write(content,0,4096); out.flush(); @@ -329,7 +330,7 @@ public class AsyncRequestReadTest "Content-Length: "+content.length+"\r\n"+ "Content-Type: bytes\r\n"+ "\r\n"; - byte[] h=header.getBytes(StringUtil.__ISO_8859_1); + byte[] h=header.getBytes(StandardCharsets.ISO_8859_1); out.write(h); out.write(content,0,4096); out.flush(); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncStressTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncStressTest.java index 00358ca8755..ec929a31b03 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncStressTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/AsyncStressTest.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.Random; import java.util.Timer; import java.util.TimerTask; @@ -140,7 +141,7 @@ public class AsyncStressTest "result: "+__paths[p][1]+"\r\n"+ ((l+1

"+label+"

"); writer.write("
\npathInfo="+request.getPathInfo()+"\n
\n"); writer.write("
\ncontentType="+request.getContentType()+"\n
\n"); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/GracefulStopTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/GracefulStopTest.java index 0c11502ff68..37021b5fc2a 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/GracefulStopTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/GracefulStopTest.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.server; import java.io.IOException; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; import javax.servlet.ServletException; @@ -76,7 +77,7 @@ public class GracefulStopTest try(Socket socket = new Socket("localhost",server.getBean(NetworkConnector.class).getLocalPort());) { - socket.getOutputStream().write("GET / HTTP/1.0\r\n\r\n".getBytes(StringUtil.__ISO_8859_1_CHARSET)); + socket.getOutputStream().write("GET / HTTP/1.0\r\n\r\n".getBytes(StandardCharsets.ISO_8859_1)); String out = IO.toString(socket.getInputStream()); Assert.assertThat(out,Matchers.containsString("200 OK")); } diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/HostHeaderCustomizerTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/HostHeaderCustomizerTest.java index 666ebfbcdbf..56bd076cf59 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/HostHeaderCustomizerTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/HostHeaderCustomizerTest.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; +import java.nio.charset.StandardCharsets; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -71,10 +72,10 @@ public class HostHeaderCustomizerTest String request = "" + "GET / HTTP/1.0\r\n" + "\r\n"; - output.write(request.getBytes("UTF-8")); + output.write(request.getBytes(StandardCharsets.UTF_8)); output.flush(); - BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); + BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); SimpleHttpParser parser = new SimpleHttpParser(); SimpleHttpResponse response = parser.readResponse(input); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java index a34ff5752a3..0b2a5de3d18 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpServerTestBase.java @@ -38,6 +38,7 @@ import java.io.OutputStream; import java.net.Socket; import java.net.URI; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Exchanger; @@ -969,12 +970,12 @@ public abstract class HttpServerTestBase extends HttpServerTestFixture } } - String in = new String(b, 0, i, "utf-8"); + String in = new String(b, 0, i, StandardCharsets.UTF_8); assertTrue(in.contains("123456789")); assertTrue(in.contains("abcdefghZ")); assertFalse(in.contains("Wibble")); - in = new String(b, i, b.length - i, "utf-16"); + in = new String(b, i, b.length - i, StandardCharsets.UTF_16); assertEquals("Wibble\n", in); } } @@ -1414,11 +1415,11 @@ public abstract class HttpServerTestBase extends HttpServerTestFixture { for (int i = 0; i < REQS; i++) { - out.write("GET / HTTP/1.1\r\nHost: localhost\r\n".getBytes(StringUtil.__ISO_8859_1)); - out.write(("Content-Length: " + bytes.length + "\r\n" + "\r\n").getBytes(StringUtil.__ISO_8859_1)); + out.write("GET / HTTP/1.1\r\nHost: localhost\r\n".getBytes(StandardCharsets.ISO_8859_1)); + out.write(("Content-Length: " + bytes.length + "\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1)); out.write(bytes, 0, bytes.length); } - out.write("GET / HTTP/1.1\r\nHost: last\r\nConnection: close\r\n\r\n".getBytes(StringUtil.__ISO_8859_1)); + out.write("GET / HTTP/1.1\r\nHost: last\r\nConnection: close\r\n\r\n".getBytes(StandardCharsets.ISO_8859_1)); out.flush(); } catch (Exception e) diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpURITest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpURITest.java index 05958ccd3c6..bcb92ca70f5 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpURITest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpURITest.java @@ -28,6 +28,7 @@ import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.http.HttpURI; import org.eclipse.jetty.util.MultiMap; @@ -330,7 +331,7 @@ public class HttpURITest huri=new HttpURI(uri); params = new MultiMap<>(); - huri.decodeQueryTo(params,"UTF-8"); + huri.decodeQueryTo(params,StandardCharsets.UTF_8); assertEquals("data"+Utf8Appendable.REPLACEMENT+"here"+Utf8Appendable.REPLACEMENT,params.getValue("invalid",0)); } @@ -343,7 +344,7 @@ public class HttpURITest HttpURI uri = new HttpURI("/path?value="+URLEncoder.encode(value,"UTF-8")); MultiMap parameters = new MultiMap<>(); - uri.decodeQueryTo(parameters,"UTF-8"); + uri.decodeQueryTo(parameters,StandardCharsets.UTF_8); assertEquals(value,parameters.getString("value")); } } @@ -366,7 +367,7 @@ public class HttpURITest { try { - byte[] buf = connect_tests[i][0].getBytes(StringUtil.__UTF8); + byte[] buf = connect_tests[i][0].getBytes(StandardCharsets.UTF_8); uri.parseConnect(buf,2,buf.length-4); assertEquals("path"+i,connect_tests[i][0].trim(),uri.getPath()); @@ -384,13 +385,13 @@ public class HttpURITest public void testNonURIAscii() throws Exception { String url = "http://www.foo.com/ma\u00F1ana"; - byte[] asISO = url.getBytes("ISO-8859-1"); - new String(asISO, "ISO-8859-1"); + byte[] asISO = url.getBytes(StandardCharsets.ISO_8859_1); + new String(asISO, StandardCharsets.ISO_8859_1); //use a non UTF-8 charset as the encoding and url-escape as per //http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars String s = URLEncoder.encode(url, "ISO-8859-1"); - HttpURI uri = new HttpURI(Charset.forName("ISO-8859-1")); + HttpURI uri = new HttpURI(StandardCharsets.ISO_8859_1); //parse it, using the same encoding uri.parse(s); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java index 71aa3cf6939..d24d0a0c83a 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/HttpWriterTest.java @@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.MappedByteBufferPool; @@ -67,7 +68,7 @@ public class HttpWriterTest { HttpWriter _writer = new Utf8HttpWriter(_httpOut); _writer.write("Now is the time"); - assertArrayEquals("Now is the time".getBytes(StringUtil.__UTF8),BufferUtil.toArray(_bytes)); + assertArrayEquals("Now is the time".getBytes(StandardCharsets.UTF_8),BufferUtil.toArray(_bytes)); } @Test @@ -75,7 +76,7 @@ public class HttpWriterTest { HttpWriter _writer = new Utf8HttpWriter(_httpOut); _writer.write("How now \uFF22rown cow"); - assertArrayEquals("How now \uFF22rown cow".getBytes(StringUtil.__UTF8),BufferUtil.toArray(_bytes)); + assertArrayEquals("How now \uFF22rown cow".getBytes(StandardCharsets.UTF_8),BufferUtil.toArray(_bytes)); } @Test @@ -83,7 +84,7 @@ public class HttpWriterTest { HttpWriter _writer = new EncodingHttpWriter(_httpOut,StringUtil.__UTF16); _writer.write("How now \uFF22rown cow"); - assertArrayEquals("How now \uFF22rown cow".getBytes(StringUtil.__UTF16),BufferUtil.toArray(_bytes)); + assertArrayEquals("How now \uFF22rown cow".getBytes(StandardCharsets.UTF_16),BufferUtil.toArray(_bytes)); } @Test @@ -93,7 +94,7 @@ public class HttpWriterTest String data="xxx\uD801\uDC00xxx"; _writer.write(data); assertEquals("787878F0909080787878",TypeUtil.toHexString(BufferUtil.toArray(_bytes))); - assertArrayEquals(data.getBytes(StringUtil.__UTF8),BufferUtil.toArray(_bytes)); + assertArrayEquals(data.getBytes(StandardCharsets.UTF_8),BufferUtil.toArray(_bytes)); assertEquals(3+4+3,_bytes.remaining()); Utf8StringBuilder buf = new Utf8StringBuilder(); @@ -109,7 +110,7 @@ public class HttpWriterTest final String multiByteDuplicateStr = "\uFF22"; int remainSize = 1; - int multiByteStrByteLength = multiByteDuplicateStr.getBytes("UTF-8").length; + int multiByteStrByteLength = multiByteDuplicateStr.getBytes(StandardCharsets.UTF_8).length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < HttpWriter.MAX_OUTPUT_CHARS - multiByteStrByteLength; i++) { sb.append(singleByteStr); @@ -125,7 +126,7 @@ public class HttpWriterTest _writer.write(buf, 0, length); - assertEquals(sb.toString(),new String(BufferUtil.toArray(_bytes),StringUtil.__UTF8)); + assertEquals(sb.toString(),new String(BufferUtil.toArray(_bytes),StandardCharsets.UTF_8)); } @Test @@ -133,7 +134,7 @@ public class HttpWriterTest { HttpWriter _writer = new Iso88591HttpWriter(_httpOut); _writer.write("How now \uFF22rown cow"); - assertEquals("How now ?rown cow",new String(BufferUtil.toArray(_bytes),StringUtil.__ISO_8859_1)); + assertEquals("How now ?rown cow",new String(BufferUtil.toArray(_bytes),StandardCharsets.ISO_8859_1)); } @Test @@ -143,11 +144,11 @@ public class HttpWriterTest String source = "\uD842\uDF9F"; - byte[] bytes = source.getBytes(StringUtil.__UTF8); + byte[] bytes = source.getBytes(StandardCharsets.UTF_8); _writer.write(source.toCharArray(),0,source.toCharArray().length); java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); - java.io.OutputStreamWriter osw = new java.io.OutputStreamWriter(baos ,StringUtil.__UTF8 ); + java.io.OutputStreamWriter osw = new java.io.OutputStreamWriter(baos, StandardCharsets.UTF_8); osw.write(source.toCharArray(),0,source.toCharArray().length); osw.flush(); @@ -181,11 +182,11 @@ public class HttpWriterTest } String source = sb.toString(); - byte[] bytes = source.getBytes(StringUtil.__UTF8); + byte[] bytes = source.getBytes(StandardCharsets.UTF_8); _writer.write(source.toCharArray(),0,source.toCharArray().length); java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); - java.io.OutputStreamWriter osw = new java.io.OutputStreamWriter(baos ,StringUtil.__UTF8); + java.io.OutputStreamWriter osw = new java.io.OutputStreamWriter(baos, StandardCharsets.UTF_8); osw.write(source.toCharArray(),0,source.toCharArray().length); osw.flush(); @@ -219,11 +220,11 @@ public class HttpWriterTest } String source = sb.toString(); - byte[] bytes = source.getBytes(StringUtil.__UTF8); + byte[] bytes = source.getBytes(StandardCharsets.UTF_8); _writer.write(source.toCharArray(),0,source.toCharArray().length); java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); - java.io.OutputStreamWriter osw = new java.io.OutputStreamWriter(baos,StringUtil.__UTF8); + java.io.OutputStreamWriter osw = new java.io.OutputStreamWriter(baos,StandardCharsets.UTF_8); osw.write(source.toCharArray(),0,source.toCharArray().length); osw.flush(); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/LowResourcesMonitorTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/LowResourcesMonitorTest.java index b4f38a10f2c..3d2c1813c0a 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/LowResourcesMonitorTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/LowResourcesMonitorTest.java @@ -24,6 +24,7 @@ import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertThat; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.concurrent.CountDownLatch; @@ -161,7 +162,7 @@ public class LowResourcesMonitorTest for (int i=0;i _params; /* ------------------------------------------------------------------------------- */ @@ -339,7 +342,14 @@ public class MultiPartFilter implements Filter public void setCharacterEncoding(String enc) throws UnsupportedEncodingException { - _encoding=enc; + try + { + _encoding=Charset.forName(enc); + } + catch (UnsupportedCharsetException e) + { + throw new UnsupportedEncodingException(e.getMessage()); + } } @@ -350,11 +360,18 @@ public class MultiPartFilter implements Filter //check if there is a specific encoding for the parameter Object ct = _params.getValue(name+CONTENT_TYPE_SUFFIX,0); //use default if not - String contentType = _encoding; + Charset contentType = _encoding; if (ct != null) { String tmp = MimeTypes.getCharsetFromContentType((String)ct); - contentType = (tmp == null?_encoding:tmp); + try + { + contentType = (tmp == null?_encoding:Charset.forName(tmp)); + } + catch (UnsupportedCharsetException e) + { + throw new UnsupportedEncodingException(e.getMessage()); + } } return new String(bytes,contentType); diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java index 63c6711f9b0..516c690d5bc 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/AbstractDoSFilterTest.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.EnumSet; import javax.servlet.DispatcherType; @@ -122,7 +123,7 @@ public abstract class AbstractDoSFilterTest for (int i = loops; i-- > 0;) { - out.write(loopRequests.getBytes("UTF-8")); + out.write(loopRequests.getBytes(StandardCharsets.UTF_8)); out.flush(); if (i > 0 && pauseBetweenLoops > 0) { @@ -133,7 +134,7 @@ public abstract class AbstractDoSFilterTest { Thread.sleep(pauseBeforeLast); } - out.write(lastRequest.getBytes("UTF-8")); + out.write(lastRequest.getBytes(StandardCharsets.UTF_8)); out.flush(); InputStream in = socket.getInputStream(); @@ -142,7 +143,7 @@ public abstract class AbstractDoSFilterTest // don't read in anything, forcing the request to time out Thread.sleep(_requestMaxTime * 2); } - String response = IO.toString(in,"UTF-8"); + String response = IO.toString(in,StandardCharsets.UTF_8); return response; } } diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/EventSourceServletTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/EventSourceServletTest.java index 6f2d6ab7153..75fc4b7191c 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/EventSourceServletTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/EventSourceServletTest.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; @@ -326,7 +327,7 @@ public class EventSourceServletTest handshake += "Host: localhost:" + serverPort + "\r\n"; handshake += "Accept: text/event-stream\r\n"; handshake += "\r\n"; - output.write(handshake.getBytes("UTF-8")); + output.write(handshake.getBytes(StandardCharsets.UTF_8)); output.flush(); } @@ -334,7 +335,7 @@ public class EventSourceServletTest { // Read and discard the HTTP response InputStream input = socket.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); String line = reader.readLine(); while (line != null) { diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java index 9098ea3bcf6..ba434298e07 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/MultipartFilterTest.java @@ -31,6 +31,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; import java.util.EnumSet; import java.util.Map; @@ -867,7 +868,7 @@ public class MultipartFilterTest baos.write(("--" + boundary + "\r\n"+ "Content-Disposition: form-data; name=\"ttt\"\r\n"+ "Content-Type: application/octet-stream; charset=UTF-8\r\n\r\n").getBytes()); - baos.write("ttt\u01FCzzz".getBytes(StringUtil.__UTF8)); + baos.write("ttt\u01FCzzz".getBytes(StandardCharsets.UTF_8)); baos.write(("\r\n--" + boundary + "--\r\n\r\n").getBytes()); diff --git a/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/api/DataInfo.java b/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/api/DataInfo.java index 4949b985088..d0d66b4657e 100644 --- a/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/api/DataInfo.java +++ b/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/api/DataInfo.java @@ -208,9 +208,20 @@ public abstract class DataInfo extends Info * @return a String with the content of this {@link DataInfo} */ public String asString(String charset, boolean consume) + { + return asString(Charset.forName(charset), consume); + } + + /** + * + * @param charset the charset used to convert the bytes + * @param consume whether to consume the content + * @return a String with the content of this {@link DataInfo} + */ + public String asString(Charset charset, boolean consume) { ByteBuffer buffer = asByteBuffer(consume); - return Charset.forName(charset).decode(buffer).toString(); + return charset.decode(buffer).toString(); } /** diff --git a/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/generator/HeadersBlockGenerator.java b/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/generator/HeadersBlockGenerator.java index d67e8ec2574..9387a8f0b9a 100644 --- a/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/generator/HeadersBlockGenerator.java +++ b/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/generator/HeadersBlockGenerator.java @@ -20,7 +20,7 @@ package org.eclipse.jetty.spdy.generator; import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Locale; @@ -43,7 +43,7 @@ public class HeadersBlockGenerator public ByteBuffer generate(short version, Fields headers) { // TODO: ByteArrayOutputStream is quite inefficient, but grows on demand; optimize using ByteBuffer ? - Charset iso1 = StandardCharsets.ISO_8859_1; + final Charset iso1 = StandardCharsets.ISO_8859_1; ByteArrayOutputStream buffer = new ByteArrayOutputStream(headers.getSize() * 64); writeCount(version, buffer, headers.getSize()); for (Fields.Field header : headers) diff --git a/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/parser/HeadersBlockParser.java b/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/parser/HeadersBlockParser.java index f461b46cf93..3e950d75703 100644 --- a/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/parser/HeadersBlockParser.java +++ b/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/parser/HeadersBlockParser.java @@ -19,7 +19,7 @@ package org.eclipse.jetty.spdy.parser; import java.nio.ByteBuffer; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets; import java.util.zip.ZipException; @@ -59,7 +59,6 @@ public abstract class HeadersBlockParser ByteBuffer decompressedHeaders = decompress(version, compressedHeaders); Charset iso1 = StandardCharsets.ISO_8859_1; - // We know the decoded bytes contain the full headers, // so optimize instead of looping byte by byte int count = readCount(version, decompressedHeaders); diff --git a/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/api/ClientUsageTest.java b/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/api/ClientUsageTest.java index 81a7698ef3a..12b5f421630 100644 --- a/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/api/ClientUsageTest.java +++ b/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/api/ClientUsageTest.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.spdy.api; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -242,7 +243,7 @@ public class ClientUsageTest public void onData(Stream stream, DataInfo dataInfo) { StringBuilder builder = (StringBuilder)stream.getAttribute("builder"); - builder.append(dataInfo.asString("UTF-8", true)); + builder.append(dataInfo.asString(StandardCharsets.UTF_8, true)); } }, new Promise.Adapter() @@ -250,9 +251,9 @@ public class ClientUsageTest @Override public void succeeded(Stream stream) { - stream.data(new BytesDataInfo("wee".getBytes(Charset.forName("UTF-8")), false), new Callback.Adapter()); + stream.data(new BytesDataInfo("wee".getBytes(StandardCharsets.UTF_8), false), new Callback.Adapter()); stream.data(new StringDataInfo("foo", false), new Callback.Adapter()); - stream.data(new ByteBufferDataInfo(Charset.forName("UTF-8").encode("bar"), true), new Callback.Adapter()); + stream.data(new ByteBufferDataInfo(StandardCharsets.UTF_8.encode("bar"), true), new Callback.Adapter()); } } ); diff --git a/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/parser/ParseVersusCacheBenchmarkTest.java b/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/parser/ParseVersusCacheBenchmarkTest.java index 7787b2e5125..143a510c367 100644 --- a/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/parser/ParseVersusCacheBenchmarkTest.java +++ b/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/parser/ParseVersusCacheBenchmarkTest.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.spdy.parser; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -43,8 +44,7 @@ public class ParseVersusCacheBenchmarkTest String name = "Content-Type"; String value = "application/octect-stream"; - Charset charset = Charset.forName("ISO-8859-1"); - ByteBuffer buffer = ByteBuffer.wrap((name + value).getBytes(charset)); + ByteBuffer buffer = ByteBuffer.wrap((name + value).getBytes(StandardCharsets.ISO_8859_1)); int iterations = 100_000_000; long begin = System.nanoTime(); @@ -52,12 +52,12 @@ public class ParseVersusCacheBenchmarkTest { byte[] nameBytes = new byte[name.length()]; buffer.get(nameBytes); - String name2 = new String(nameBytes, charset); + String name2 = new String(nameBytes, StandardCharsets.ISO_8859_1); Assert.assertEquals(name2, name); byte[] valueBytes = new byte[value.length()]; buffer.get(valueBytes); - String value2 = new String(valueBytes, charset); + String value2 = new String(valueBytes, StandardCharsets.ISO_8859_1); Assert.assertEquals(value2, value); buffer.flip(); @@ -66,8 +66,8 @@ public class ParseVersusCacheBenchmarkTest System.err.printf("parse time: %d%n", TimeUnit.NANOSECONDS.toMillis(end - begin)); Map map = new HashMap<>(); - map.put(ByteBuffer.wrap(name.getBytes(charset)), name); - map.put(ByteBuffer.wrap(value.getBytes(charset)), value); + map.put(ByteBuffer.wrap(name.getBytes(StandardCharsets.ISO_8859_1)), name); + map.put(ByteBuffer.wrap(value.getBytes(StandardCharsets.ISO_8859_1)), value); final Map cache = Collections.unmodifiableMap(map); begin = System.nanoTime(); diff --git a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/ProtocolNegotiationTest.java b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/ProtocolNegotiationTest.java index 7903b419b73..fdd4076e7b3 100644 --- a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/ProtocolNegotiationTest.java +++ b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/ProtocolNegotiationTest.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; import java.util.List; import javax.net.ssl.SSLContext; @@ -129,11 +130,11 @@ public class ProtocolNegotiationTest "GET / HTTP/1.1\r\n" + "Host: localhost:" + address.getPort() + "\r\n" + "\r\n" + - "").getBytes("UTF-8")); + "").getBytes(StandardCharsets.UTF_8)); output.flush(); InputStream input = client.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertTrue(line.contains(" 404 ")); @@ -188,11 +189,11 @@ public class ProtocolNegotiationTest "GET / HTTP/1.1\r\n" + "Host: localhost:" + address.getPort() + "\r\n" + "\r\n" + - "").getBytes("UTF-8")); + "").getBytes(StandardCharsets.UTF_8)); output.flush(); InputStream input = client.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertTrue(line.contains(" 404 ")); @@ -241,11 +242,11 @@ public class ProtocolNegotiationTest "GET / HTTP/1.1\r\n" + "Host: localhost:" + address.getPort() + "\r\n" + "\r\n" + - "").getBytes("UTF-8")); + "").getBytes(StandardCharsets.UTF_8)); output.flush(); InputStream input = client.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8")); + BufferedReader reader = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)); String line = reader.readLine(); Assert.assertTrue(line.contains(" 404 ")); diff --git a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/ServerHTTPSPDYTest.java b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/ServerHTTPSPDYTest.java index e016047d9f7..682db46b72e 100644 --- a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/ServerHTTPSPDYTest.java +++ b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/ServerHTTPSPDYTest.java @@ -24,6 +24,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; @@ -438,7 +439,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest request.setHandled(true); httpResponse.setStatus(HttpServletResponse.SC_OK); ServletOutputStream output = httpResponse.getOutputStream(); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); handlerLatch.countDown(); } }, 30000), null); @@ -461,7 +462,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest public void onData(Stream stream, DataInfo dataInfo) { assertTrue(dataInfo.isClose()); - assertEquals(data, dataInfo.asString("UTF-8", true)); + assertEquals(data, dataInfo.asString(StandardCharsets.UTF_8, true)); dataLatch.countDown(); } }); @@ -533,9 +534,9 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest request.setHandled(true); httpResponse.setStatus(HttpServletResponse.SC_OK); ServletOutputStream output = httpResponse.getOutputStream(); - output.write(data1.getBytes("UTF-8")); + output.write(data1.getBytes(StandardCharsets.UTF_8)); output.flush(); - output.write(data2.getBytes("UTF-8")); + output.write(data2.getBytes(StandardCharsets.UTF_8)); handlerLatch.countDown(); } }, 30000), null); @@ -564,9 +565,9 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest int data = dataFrames.incrementAndGet(); assertTrue(data >= 1 && data <= 2); if (data == 1) - assertEquals(data1, dataInfo.asString("UTF8", true)); + assertEquals(data1, dataInfo.asString(StandardCharsets.UTF_8, true)); else - assertEquals(data2, dataInfo.asString("UTF8", true)); + assertEquals(data2, dataInfo.asString(StandardCharsets.UTF_8, true)); dataLatch.countDown(); } }); @@ -750,7 +751,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest request.setHandled(true); httpResponse.setStatus(HttpServletResponse.SC_OK); ServletOutputStream output = httpResponse.getOutputStream(); - output.write(data.getBytes("UTF-8")); + output.write(data.getBytes(StandardCharsets.UTF_8)); output.flush(); output.close(); handlerLatch.countDown(); @@ -781,7 +782,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest buffer.write(byteBuffer.get()); if (dataInfo.isClose()) { - assertEquals(data, new String(buffer.toByteArray(), Charset.forName("UTF-8"))); + assertEquals(data, new String(buffer.toByteArray(), StandardCharsets.UTF_8)); dataLatch.countDown(); } } @@ -807,10 +808,10 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest httpResponse.setStatus(HttpServletResponse.SC_OK); ServletOutputStream output = httpResponse.getOutputStream(); // Write some - output.write(data1.getBytes("UTF-8")); + output.write(data1.getBytes(StandardCharsets.UTF_8)); // But then change your mind and reset the buffer httpResponse.resetBuffer(); - output.write(data2.getBytes("UTF-8")); + output.write(data2.getBytes(StandardCharsets.UTF_8)); handlerLatch.countDown(); } }, 30000), null); @@ -839,7 +840,7 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest buffer.write(byteBuffer.get()); if (dataInfo.isClose()) { - assertEquals(data2, new String(buffer.toByteArray(), Charset.forName("UTF-8"))); + assertEquals(data2, new String(buffer.toByteArray(), StandardCharsets.UTF_8)); dataLatch.countDown(); } } @@ -996,10 +997,10 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest request.setHandled(true); httpResponse.setHeader("Transfer-Encoding", "chunked"); ServletOutputStream output = httpResponse.getOutputStream(); - output.write(pangram1.getBytes("UTF-8")); + output.write(pangram1.getBytes(StandardCharsets.UTF_8)); httpResponse.setHeader("EXTRA", "X"); output.flush(); - output.write(pangram2.getBytes("UTF-8")); + output.write(pangram2.getBytes(StandardCharsets.UTF_8)); handlerLatch.countDown(); } }, 30000), null); @@ -1030,12 +1031,12 @@ public class ServerHTTPSPDYTest extends AbstractHTTPSPDYTest if (count == 1) { Assert.assertFalse(dataInfo.isClose()); - assertEquals(pangram1, dataInfo.asString("UTF-8", true)); + assertEquals(pangram1, dataInfo.asString(StandardCharsets.UTF_8, true)); } else if (count == 2) { assertTrue(dataInfo.isClose()); - assertEquals(pangram2, dataInfo.asString("UTF-8", true)); + assertEquals(pangram2, dataInfo.asString(StandardCharsets.UTF_8, true)); } dataLatch.countDown(); } diff --git a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/proxy/ProxyHTTPToSPDYTest.java b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/proxy/ProxyHTTPToSPDYTest.java index fcbd90e73bb..2ea975482e1 100644 --- a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/proxy/ProxyHTTPToSPDYTest.java +++ b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/proxy/ProxyHTTPToSPDYTest.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.spdy.server.proxy; import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; import java.util.concurrent.CountDownLatch; @@ -251,7 +252,7 @@ public class ProxyHTTPToSPDYTest @Test public void testGETThenSmallResponseContent() throws Exception { - final byte[] data = "0123456789ABCDEF".getBytes("UTF-8"); + final byte[] data = "0123456789ABCDEF".getBytes(StandardCharsets.UTF_8); InetSocketAddress proxyAddress = startProxy(startServer(new ServerSessionFrameListener.Adapter() { @Override @@ -328,7 +329,7 @@ public class ProxyHTTPToSPDYTest public void testPOSTWithSmallRequestContentThenSmallResponseContent() throws Exception { String dataString = "0123456789ABCDEF"; - final byte[] data = dataString.getBytes("UTF-8"); + final byte[] data = dataString.getBytes(StandardCharsets.UTF_8); InetSocketAddress proxyAddress = startProxy(startServer(new ServerSessionFrameListener.Adapter() { @Override @@ -370,7 +371,7 @@ public class ProxyHTTPToSPDYTest @Test public void testGETThenSPDYPushIsIgnored() throws Exception { - final byte[] data = "0123456789ABCDEF".getBytes("UTF-8"); + final byte[] data = "0123456789ABCDEF".getBytes(StandardCharsets.UTF_8); InetSocketAddress proxyAddress = startProxy(startServer(new ServerSessionFrameListener.Adapter() { @Override diff --git a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/proxy/ProxySPDYToHTTPTest.java b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/proxy/ProxySPDYToHTTPTest.java index 67a793dd9e5..e5fcc6c65ab 100644 --- a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/proxy/ProxySPDYToHTTPTest.java +++ b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/proxy/ProxySPDYToHTTPTest.java @@ -22,6 +22,7 @@ import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; import java.util.concurrent.CountDownLatch; @@ -197,7 +198,7 @@ public class ProxySPDYToHTTPTest @Test public void testSYNThenREPLYAndDATA() throws Exception { - final byte[] data = "0123456789ABCDEF".getBytes("UTF-8"); + final byte[] data = "0123456789ABCDEF".getBytes(StandardCharsets.UTF_8); final String header = "foo"; InetSocketAddress proxyAddress = startProxy(startServer(new TestServerHandler(header, data)), 30000, 30000); diff --git a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/proxy/ProxySPDYToSPDYTest.java b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/proxy/ProxySPDYToSPDYTest.java index 0786e01bf11..afc748e4f5e 100644 --- a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/proxy/ProxySPDYToSPDYTest.java +++ b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/proxy/ProxySPDYToSPDYTest.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.spdy.server.proxy; import java.io.ByteArrayOutputStream; import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; import java.util.concurrent.CountDownLatch; @@ -227,7 +228,7 @@ public class ProxySPDYToSPDYTest @Test public void testSYNThenREPLYAndDATA() throws Exception { - final byte[] data = "0123456789ABCDEF".getBytes("UTF-8"); + final byte[] data = "0123456789ABCDEF".getBytes(StandardCharsets.UTF_8); final String header = "foo"; InetSocketAddress proxyAddress = startProxy(startServer(new ServerSessionFrameListener.Adapter() { @@ -287,7 +288,7 @@ public class ProxySPDYToSPDYTest @Test public void testSYNThenSPDYPushIsReceived() throws Exception { - final byte[] data = "0123456789ABCDEF".getBytes("UTF-8"); + final byte[] data = "0123456789ABCDEF".getBytes(StandardCharsets.UTF_8); InetSocketAddress proxyAddress = startProxy(startServer(new ServerSessionFrameListener.Adapter() { @Override @@ -368,7 +369,7 @@ public class ProxySPDYToSPDYTest @Test public void testSYNThenSPDYNestedPushIsReceived() throws Exception { - final byte[] data = "0123456789ABCDEF".getBytes("UTF-8"); + final byte[] data = "0123456789ABCDEF".getBytes(StandardCharsets.UTF_8); InetSocketAddress proxyAddress = startProxy(startServer(new ServerSessionFrameListener.Adapter() { @Override diff --git a/jetty-spdy/spdy-server/src/test/java/org/eclipse/jetty/spdy/server/SynReplyTest.java b/jetty-spdy/spdy-server/src/test/java/org/eclipse/jetty/spdy/server/SynReplyTest.java index 4a76d1bb250..efe68b44425 100644 --- a/jetty-spdy/spdy-server/src/test/java/org/eclipse/jetty/spdy/server/SynReplyTest.java +++ b/jetty-spdy/spdy-server/src/test/java/org/eclipse/jetty/spdy/server/SynReplyTest.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.spdy.server; import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -118,7 +119,7 @@ public class SynReplyTest extends AbstractTest @Test public void testSynDataReply() throws Exception { - final byte[] dataBytes = "foo".getBytes(Charset.forName("UTF-8")); + final byte[] dataBytes = "foo".getBytes(StandardCharsets.UTF_8); final CountDownLatch synLatch = new CountDownLatch(1); final CountDownLatch dataLatch = new CountDownLatch(1); @@ -235,13 +236,13 @@ public class SynReplyTest extends AbstractTest int dataCount = this.dataCount.incrementAndGet(); if (dataCount == 1) { - String chunk1 = dataInfo.asString("UTF-8", true); + String chunk1 = dataInfo.asString(StandardCharsets.UTF_8, true); Assert.assertEquals(data1, chunk1); dataLatch1.countDown(); } else if (dataCount == 2) { - String chunk2 = dataInfo.asString("UTF-8", true); + String chunk2 = dataInfo.asString(StandardCharsets.UTF_8, true); Assert.assertEquals(data2, chunk2); dataLatch2.countDown(); } @@ -277,7 +278,7 @@ public class SynReplyTest extends AbstractTest @Override public void onData(Stream stream, DataInfo dataInfo) { - String data = dataInfo.asString("UTF-8", true); + String data = dataInfo.asString(StandardCharsets.UTF_8, true); Assert.assertEquals(clientData, data); clientDataLatch.countDown(); } @@ -311,7 +312,7 @@ public class SynReplyTest extends AbstractTest public void onData(Stream stream, DataInfo dataInfo) { ByteBuffer buffer = dataInfo.asByteBuffer(false); - String data = Charset.forName("UTF-8").decode(buffer).toString(); + String data = StandardCharsets.UTF_8.decode(buffer).toString(); Assert.assertEquals(serverData, data); serverDataLatch.countDown(); } @@ -361,7 +362,7 @@ public class SynReplyTest extends AbstractTest @Override public void onData(Stream stream, DataInfo dataInfo) { - String chunk = dataInfo.asString("UTF-8", true); + String chunk = dataInfo.asString(StandardCharsets.UTF_8, true); Assert.assertEquals(data, chunk); dataLatch.countDown(); } diff --git a/jetty-spring/src/main/java/org/eclipse/jetty/spring/SpringConfigurationProcessor.java b/jetty-spring/src/main/java/org/eclipse/jetty/spring/SpringConfigurationProcessor.java index f226e6725e6..26f2b8d3aac 100644 --- a/jetty-spring/src/main/java/org/eclipse/jetty/spring/SpringConfigurationProcessor.java +++ b/jetty-spring/src/main/java/org/eclipse/jetty/spring/SpringConfigurationProcessor.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.spring; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Map; import java.util.ServiceLoader; @@ -80,7 +81,7 @@ public class SpringConfigurationProcessor implements ConfigurationProcessor : new ByteArrayResource(("" + "" + "" + - config).getBytes("UTF-8")); + config).getBytes(StandardCharsets.UTF_8)); _beanFactory = new DefaultListableBeanFactory() { diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/AbstractTrie.java b/jetty-util/src/main/java/org/eclipse/jetty/util/AbstractTrie.java index a9cb82791c1..2d152b2026c 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/AbstractTrie.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/AbstractTrie.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.util; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; /* ------------------------------------------------------------ */ @@ -72,7 +73,7 @@ public abstract class AbstractTrie implements Trie @Override public V getBest(byte[] b, int offset, int len) { - return getBest(new String(b,offset,len,StringUtil.__ISO_8859_1_CHARSET)); + return getBest(new String(b,offset,len,StandardCharsets.ISO_8859_1)); } @Override diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/B64Code.java b/jetty-util/src/main/java/org/eclipse/jetty/util/B64Code.java index 2cb33a41b37..3868f0987e1 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/B64Code.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/B64Code.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.util; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.charset.UnsupportedCharsetException; @@ -65,7 +66,7 @@ public class B64Code */ public static String encode(String s) { - return encode(s,null); + return encode(s, (Charset)null); } /** @@ -80,12 +81,25 @@ public class B64Code { byte[] bytes; if (charEncoding==null) - bytes=s.getBytes(Charset.forName(StringUtil.__ISO_8859_1)); + bytes=s.getBytes(StandardCharsets.ISO_8859_1); else bytes=s.getBytes(Charset.forName(charEncoding)); return new String(encode(bytes)); } + /** + * Base 64 encode as described in RFC 1421. + *

Does not insert whitespace as described in RFC 1521. + * @param s String to encode. + * @param charEncoding The character encoding of the provided input String. + * @return String containing the encoded form of the input. + */ + public static String encode(String s, Charset charEncoding) + { + byte[] bytes=s.getBytes(charEncoding==null ? StandardCharsets.ISO_8859_1 : charEncoding); + return new String(encode(bytes)); + } + /** * Fast Base 64 encode as described in RFC 1421. *

Does not insert whitespace as described in RFC 1521. @@ -236,6 +250,24 @@ public class B64Code return new String(decoded,Charset.forName(charEncoding)); } + /** + * Base 64 decode as described in RFC 2045. + *

Unlike {@link #decode(char[])}, extra whitespace is ignored. + * @param encoded String to decode. + * @param charEncoding Character encoding + * used to map the decoded bytes into a String. + * @return String decoded byte array. + * @throws IllegalArgumentException if the input is not a valid + * B64 encoding. + */ + public static String decode(String encoded, Charset charEncoding) + { + byte[] decoded=decode(encoded); + if (charEncoding==null) + return new String(decoded); + return new String(decoded, charEncoding); + } + /** * Fast Base 64 decode as described in RFC 1421. * diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java index 3c779495cf4..a947c360a3b 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/BufferUtil.java @@ -29,6 +29,7 @@ import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.util.resource.Resource; @@ -456,7 +457,7 @@ public class BufferUtil */ public static String toString(ByteBuffer buffer) { - return toString(buffer, StringUtil.__ISO_8859_1_CHARSET); + return toString(buffer, StandardCharsets.ISO_8859_1); } /* ------------------------------------------------------------ */ @@ -466,7 +467,7 @@ public class BufferUtil */ public static String toUTF8String(ByteBuffer buffer) { - return toString(buffer, StringUtil.__UTF8_CHARSET); + return toString(buffer, StandardCharsets.UTF_8); } /* ------------------------------------------------------------ */ @@ -735,12 +736,12 @@ public class BufferUtil public static ByteBuffer toBuffer(String s) { - return ByteBuffer.wrap(s.getBytes(StringUtil.__ISO_8859_1_CHARSET)); + return ByteBuffer.wrap(s.getBytes(StandardCharsets.ISO_8859_1)); } public static ByteBuffer toDirectBuffer(String s) { - byte[] bytes = s.getBytes(StringUtil.__ISO_8859_1_CHARSET); + byte[] bytes = s.getBytes(StandardCharsets.ISO_8859_1); ByteBuffer buf = ByteBuffer.allocateDirect(bytes.length); buf.put(bytes); buf.flip(); diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/ByteArrayISO8859Writer.java b/jetty-util/src/main/java/org/eclipse/jetty/util/ByteArrayISO8859Writer.java index 3787a801c79..c4ed7f4725e 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/ByteArrayISO8859Writer.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/ByteArrayISO8859Writer.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; +import java.nio.charset.StandardCharsets; /* ------------------------------------------------------------ */ @@ -211,7 +212,7 @@ public class ByteArrayISO8859Writer extends Writer if (_bout==null) { _bout = new ByteArrayOutputStream2(2*length); - _writer = new OutputStreamWriter(_bout,StringUtil.__ISO_8859_1); + _writer = new OutputStreamWriter(_bout,StandardCharsets.ISO_8859_1); } else _bout.reset(); diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java b/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java index 0194edd8499..c1e501be330 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java @@ -29,6 +29,7 @@ import java.io.PrintWriter; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; +import java.nio.charset.Charset; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; @@ -311,7 +312,7 @@ public class IO public static String toString(InputStream in) throws IOException { - return toString(in,null); + return toString(in,(Charset)null); } /* ------------------------------------------------------------ */ @@ -319,14 +320,22 @@ public class IO */ public static String toString(InputStream in,String encoding) throws IOException + { + return toString(in, encoding==null?null:Charset.forName(encoding)); + } + + /** Read input stream to string. + */ + public static String toString(InputStream in, Charset encoding) + throws IOException { StringWriter writer=new StringWriter(); InputStreamReader reader = encoding==null?new InputStreamReader(in):new InputStreamReader(in,encoding); - + copy(reader,writer); return writer.toString(); } - + /* ------------------------------------------------------------ */ /** Read input stream to string. */ diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartInputStreamParser.java b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartInputStreamParser.java index 228ebee3a66..c5ec8177d97 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartInputStreamParser.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartInputStreamParser.java @@ -29,6 +29,7 @@ import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -480,7 +481,7 @@ public class MultiPartInputStreamParser } String boundary="--"+contentTypeBoundary; - byte[] byteBoundary=(boundary+"--").getBytes(StringUtil.__ISO_8859_1); + byte[] byteBoundary=(boundary+"--").getBytes(StandardCharsets.ISO_8859_1); // Get first boundary String line = null; diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartOutputStream.java b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartOutputStream.java index 85cc700254b..047a1c012ec 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartOutputStream.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartOutputStream.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.util; import java.io.FilterOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; /* ================================================================ */ @@ -53,7 +54,7 @@ public class MultiPartOutputStream extends FilterOutputStream boundary = "jetty"+System.identityHashCode(this)+ Long.toString(System.currentTimeMillis(),36); - boundaryBytes=boundary.getBytes(StringUtil.__ISO_8859_1); + boundaryBytes=boundary.getBytes(StandardCharsets.ISO_8859_1); } public MultiPartOutputStream(OutputStream out, String boundary) @@ -62,7 +63,7 @@ public class MultiPartOutputStream extends FilterOutputStream super(out); this.boundary = boundary; - boundaryBytes=boundary.getBytes(StringUtil.__ISO_8859_1); + boundaryBytes=boundary.getBytes(StandardCharsets.ISO_8859_1); } /* ------------------------------------------------------------ */ @@ -110,7 +111,7 @@ public class MultiPartOutputStream extends FilterOutputStream out.write(boundaryBytes); out.write(__CRLF); if (contentType != null) - out.write(("Content-Type: "+contentType).getBytes(StringUtil.__ISO_8859_1)); + out.write(("Content-Type: "+contentType).getBytes(StandardCharsets.ISO_8859_1)); out.write(__CRLF); out.write(__CRLF); } @@ -128,11 +129,11 @@ public class MultiPartOutputStream extends FilterOutputStream out.write(boundaryBytes); out.write(__CRLF); if (contentType != null) - out.write(("Content-Type: "+contentType).getBytes(StringUtil.__ISO_8859_1)); + out.write(("Content-Type: "+contentType).getBytes(StandardCharsets.ISO_8859_1)); out.write(__CRLF); for (int i=0;headers!=null && im) - return new String(buf,m,pos-m,StringUtil.__UTF8_CHARSET); + return new String(buf,m,pos-m, StandardCharsets.UTF_8); return null; } @@ -77,7 +78,7 @@ public class ReadLineInputStream extends BufferedInputStream _skipLF=true; int m=markpos; markpos=-1; - return new String(buf,m,p-m-1,StringUtil.__UTF8_CHARSET); + return new String(buf,m,p-m-1,StandardCharsets.UTF_8); } if (b=='\n') @@ -91,7 +92,7 @@ public class ReadLineInputStream extends BufferedInputStream } int m=markpos; markpos=-1; - return new String(buf,m,pos-m-1,StringUtil.__UTF8_CHARSET); + return new String(buf,m,pos-m-1,StandardCharsets.UTF_8); } } } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/StringUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/StringUtil.java index 03e8b2c480f..988948191b2 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/StringUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/StringUtil.java @@ -21,13 +21,14 @@ package org.eclipse.jetty.util; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Logger; /** Fast String Utilities. * - * These string utilities provide both conveniance methods and + * These string utilities provide both convenience methods and * performance improvements over most standard library versions. The * main aim of the optimizations is to avoid object creation unless * absolutely required. @@ -49,19 +50,30 @@ public class StringUtil public static final String __ISO_8859_1="ISO-8859-1"; public final static String __UTF8="UTF-8"; public final static String __UTF16="UTF-16"; - - public final static Charset __UTF8_CHARSET; - public final static Charset __ISO_8859_1_CHARSET; - public final static Charset __UTF16_CHARSET; - public final static Charset __US_ASCII_CHARSET; + + /** + * @deprecated Use {@link StandardCharsets#UTF_8} + */ + @Deprecated + public final static Charset __UTF8_CHARSET=StandardCharsets.UTF_8; + /** + * @deprecated Use {@link StandardCharsets#ISO_8859_1} + */ + @Deprecated + public final static Charset __ISO_8859_1_CHARSET=StandardCharsets.ISO_8859_1; + /** + * @deprecated Use {@link StandardCharsets#UTF_16} + */ + @Deprecated + public final static Charset __UTF16_CHARSET=StandardCharsets.UTF_16; + /** + * @deprecated Use {@link StandardCharsets#US_ASCII} + */ + @Deprecated + public final static Charset __US_ASCII_CHARSET=StandardCharsets.US_ASCII; static { - __UTF8_CHARSET=Charset.forName(__UTF8); - __ISO_8859_1_CHARSET=Charset.forName(__ISO_8859_1); - __UTF16_CHARSET=Charset.forName(__UTF16); - __US_ASCII_CHARSET=Charset.forName("US-ASCII"); - CHARSETS.put("UTF-8",__UTF8); CHARSETS.put("UTF8",__UTF8); CHARSETS.put("UTF-16",__UTF16); @@ -343,7 +355,7 @@ public class StringUtil /* ------------------------------------------------------------ */ public static String toUTF8String(byte[] b,int offset,int length) { - return new String(b,offset,length,__UTF8_CHARSET); + return new String(b,offset,length,StandardCharsets.UTF_8); } /* ------------------------------------------------------------ */ @@ -481,12 +493,12 @@ public class StringUtil public static byte[] getBytes(String s) { - return s.getBytes(__ISO_8859_1_CHARSET); + return s.getBytes(StandardCharsets.ISO_8859_1); } public static byte[] getUtf8Bytes(String s) { - return s.getBytes(__UTF8_CHARSET); + return s.getBytes(StandardCharsets.UTF_8); } public static byte[] getBytes(String s,String charset) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/TreeTrie.java b/jetty-util/src/main/java/org/eclipse/jetty/util/TreeTrie.java index b82b6f4bd83..7f074551607 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/TreeTrie.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/TreeTrie.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.util; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -215,7 +216,7 @@ public class TreeTrie extends AbstractTrie public V getBest(String s, int offset, int len) { // TODO inefficient - byte[] b=s.substring(offset,offset+len).getBytes(StringUtil.__ISO_8859_1_CHARSET); + byte[] b=s.substring(offset,offset+len).getBytes(StandardCharsets.ISO_8859_1); return getBest(b,0,b.length); } diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java index ddae8d052ee..6bf81d9e3ed 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/URIUtil.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.util; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; @@ -43,8 +44,14 @@ public class URIUtil public static final String HTTPS_COLON="https:"; // Use UTF-8 as per http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars - public static final Charset __CHARSET=Charset.forName(System.getProperty("org.eclipse.jetty.util.URI.charset",StringUtil.__UTF8)); - + public static final Charset __CHARSET; + + static + { + String charset = System.getProperty("org.eclipse.jetty.util.URI.charset"); + __CHARSET = charset == null ? StandardCharsets.UTF_8 : Charset.forName(charset); + } + private URIUtil() {} diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/UrlEncoded.java b/jetty-util/src/main/java/org/eclipse/jetty/util/UrlEncoded.java index 6a6c3e06997..2c90292eb70 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/UrlEncoded.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/UrlEncoded.java @@ -25,6 +25,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringWriter; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; @@ -59,15 +60,16 @@ public class UrlEncoded extends MultiMap implements Cloneable public static final Charset ENCODING; static { - Charset encoding=null; + Charset encoding; try { - encoding=Charset.forName(System.getProperty("org.eclipse.jetty.util.UrlEncoding.charset",StringUtil.__UTF8)); + String charset = System.getProperty("org.eclipse.jetty.util.UrlEncoding.charset"); + encoding = charset == null ? StandardCharsets.UTF_8 : Charset.forName(charset); } catch(Exception e) { LOG.warn(e); - encoding=StringUtil.__UTF8_CHARSET; + encoding=StandardCharsets.UTF_8; } ENCODING=encoding; } @@ -598,11 +600,11 @@ public class UrlEncoded extends MultiMap implements Cloneable /* -------------------------------------------------------------- */ public static void decodeUtf16To(InputStream in, MultiMap map, int maxLength, int maxKeys) throws IOException { - InputStreamReader input = new InputStreamReader(in,StringUtil.__UTF16); + InputStreamReader input = new InputStreamReader(in,StandardCharsets.UTF_16); StringWriter buf = new StringWriter(8192); IO.copy(input,buf,maxLength); - decodeTo(buf.getBuffer().toString(),map,StringUtil.__UTF16,maxKeys); + decodeTo(buf.getBuffer().toString(),map,StandardCharsets.UTF_16,maxKeys); } /* -------------------------------------------------------------- */ @@ -614,7 +616,7 @@ public class UrlEncoded extends MultiMap implements Cloneable { if (charset==null) { - if (ENCODING==StringUtil.__UTF8_CHARSET) + if (ENCODING.equals(StandardCharsets.UTF_8)) decodeUtf8To(in,map,maxLength,maxKeys); else decodeTo(in,map,ENCODING,maxLength,maxKeys); @@ -640,19 +642,19 @@ public class UrlEncoded extends MultiMap implements Cloneable if (charset==null) charset=ENCODING; - if (StringUtil.__UTF8_CHARSET.equals(charset)) + if (StandardCharsets.UTF_8.equals(charset)) { decodeUtf8To(in,map,maxLength,maxKeys); return; } - if (StringUtil.__ISO_8859_1_CHARSET.equals(charset)) + if (StandardCharsets.ISO_8859_1.equals(charset)) { decode88591To(in,map,maxLength,maxKeys); return; } - if (StringUtil.__UTF16_CHARSET.equals(charset)) // Should be all 2 byte encodings + if (StandardCharsets.UTF_16.equals(charset)) // Should be all 2 byte encodings { decodeUtf16To(in,map,maxLength,maxKeys); return; @@ -757,7 +759,7 @@ public class UrlEncoded extends MultiMap implements Cloneable */ public static String decodeString(String encoded,int offset,int length,Charset charset) { - if (charset==null || StringUtil.__UTF8_CHARSET.equals(charset)) + if (charset==null || StandardCharsets.UTF_8.equals(charset)) { Utf8StringBuffer buffer=null; diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/security/Credential.java b/jetty-util/src/main/java/org/eclipse/jetty/util/security/Credential.java index a6a135dabcf..8ce30d77302 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/security/Credential.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/security/Credential.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.util.security; import java.io.Serializable; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import org.eclipse.jetty.util.StringUtil; @@ -156,7 +157,7 @@ public abstract class Credential implements Serializable { if (__md == null) __md = MessageDigest.getInstance("MD5"); __md.reset(); - __md.update(credentials.toString().getBytes(StringUtil.__ISO_8859_1)); + __md.update(credentials.toString().getBytes(StandardCharsets.ISO_8859_1)); digest = __md.digest(); } if (digest == null || digest.length != _digest.length) return false; @@ -213,7 +214,7 @@ public abstract class Credential implements Serializable } __md.reset(); - __md.update(password.getBytes(StringUtil.__ISO_8859_1)); + __md.update(password.getBytes(StandardCharsets.ISO_8859_1)); digest = __md.digest(); } diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/B64CodeTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/B64CodeTest.java index 5d0ecbff865..05033fad05e 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/B64CodeTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/B64CodeTest.java @@ -23,6 +23,8 @@ import junit.framework.Assert; import org.junit.Test; +import java.nio.charset.StandardCharsets; + public class B64CodeTest { String text = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."; @@ -30,14 +32,14 @@ public class B64CodeTest @Test public void testRFC1421() throws Exception { - String b64 = B64Code.encode(text,StringUtil.__ISO_8859_1); + String b64 = B64Code.encode(text, StandardCharsets.ISO_8859_1); Assert.assertEquals("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz"+ "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg"+ "dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu"+ "dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo"+ "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=",b64); - char[] chars = B64Code.encode(text.getBytes(StringUtil.__ISO_8859_1),false); + char[] chars = B64Code.encode(text.getBytes(StandardCharsets.ISO_8859_1),false); b64 = new String(chars,0,chars.length); Assert.assertEquals("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz"+ "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg"+ @@ -50,7 +52,7 @@ public class B64CodeTest @Test public void testRFC2045() throws Exception { - char[] chars = B64Code.encode(text.getBytes(StringUtil.__ISO_8859_1),true); + char[] chars = B64Code.encode(text.getBytes(StandardCharsets.ISO_8859_1),true); String b64 = new String(chars,0,chars.length); Assert.assertEquals("TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\r\n"+ "IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\r\n"+ @@ -64,7 +66,7 @@ public class B64CodeTest @Test public void testInteger() throws Exception { - byte[] bytes = text.getBytes(StringUtil.__ISO_8859_1); + byte[] bytes = text.getBytes(StandardCharsets.ISO_8859_1); int value=(bytes[0]<<24)+(bytes[1]<<16)+(bytes[2]<<8)+(bytes[3]); StringBuilder b = new StringBuilder(); @@ -74,7 +76,7 @@ public class B64CodeTest @Test public void testLong() throws Exception { - byte[] bytes = text.getBytes(StringUtil.__ISO_8859_1); + byte[] bytes = text.getBytes(StandardCharsets.ISO_8859_1); long value=((0xffL&bytes[0])<<56)+((0xffL&bytes[1])<<48)+((0xffL&bytes[2])<<40)+((0xffL&bytes[3])<<32)+ ((0xffL&bytes[4])<<24)+((0xffL&bytes[5])<<16)+((0xffL&bytes[6])<<8)+(0xffL&bytes[7]); diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/ReadLineInputStreamTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/ReadLineInputStreamTest.java index 943ff5fff41..6260edf6d54 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/ReadLineInputStreamTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/ReadLineInputStreamTest.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.util; import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; +import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; import org.junit.After; @@ -60,7 +61,7 @@ public class ReadLineInputStreamTest _pout.close(); else { - _pout.write(s.getBytes(StringUtil.__UTF8_CHARSET)); + _pout.write(s.getBytes(StandardCharsets.UTF_8)); Thread.sleep(50); } } @@ -196,7 +197,7 @@ public class ReadLineInputStreamTest byte[] body = new byte[6]; _in.read(body); - Assert.assertEquals("\nBody\n",new String(body,0,6,StringUtil.__UTF8)); + Assert.assertEquals("\nBody\n",new String(body,0,6,StandardCharsets.UTF_8)); Assert.assertEquals("",_in.readLine()); Assert.assertEquals(null,_in.readLine()); @@ -216,7 +217,7 @@ public class ReadLineInputStreamTest byte[] body = new byte[6]; _in.read(body); - Assert.assertEquals("\nBody\n",new String(body,0,6,StringUtil.__UTF8)); + Assert.assertEquals("\nBody\n",new String(body,0,6,StandardCharsets.UTF_8)); Assert.assertEquals("",_in.readLine()); Assert.assertEquals(null,_in.readLine()); @@ -236,7 +237,7 @@ public class ReadLineInputStreamTest byte[] body = new byte[6]; _in.read(body); - Assert.assertEquals("\nBody\n",new String(body,0,6,StringUtil.__UTF8)); + Assert.assertEquals("\nBody\n",new String(body,0,6,StandardCharsets.UTF_8)); Assert.assertEquals("",_in.readLine()); Assert.assertEquals(null,_in.readLine()); diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/StringUtilTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/StringUtilTest.java index 4a668f4ea6e..6503ee3673f 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/StringUtilTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/StringUtilTest.java @@ -25,6 +25,8 @@ import static org.junit.Assert.assertTrue; import org.junit.Assert; import org.junit.Test; +import java.nio.charset.StandardCharsets; + public class StringUtilTest { @Test @@ -160,7 +162,7 @@ public class StringUtilTest { String string = "Now \u0690xxxxxxxx"; System.err.println(string); - byte[] bytes=string.getBytes("UTF-8"); + byte[] bytes=string.getBytes(StandardCharsets.UTF_8); System.err.println(new String(bytes)); System.err.println(bytes.length); long calc=0; @@ -170,7 +172,7 @@ public class StringUtilTest long s1=System.currentTimeMillis(); for (int j=1000000; j-->0;) { - calc+=new String(bytes,0,bytes.length,"UTF-8").hashCode(); + calc+=new String(bytes,0,bytes.length,StandardCharsets.UTF_8).hashCode(); } long s2=System.currentTimeMillis(); for (int j=1000000; j-->0;) diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/URITest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/URITest.java index 691b8e12c7f..42b6b61ee9b 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/URITest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/URITest.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.util; import static org.junit.Assert.assertEquals; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.junit.Test; @@ -65,7 +66,7 @@ public class URITest // Test for null character (real world ugly test case) byte oddBytes[] = { '/', 0x00, '/' }; - String odd = new String(oddBytes, Charset.forName("ISO-8859-1")); + String odd = new String(oddBytes, StandardCharsets.ISO_8859_1); assertEquals(odd,URIUtil.decodePath("/%00/")); } diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/URLEncodedTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/URLEncodedTest.java index ea8de423e75..f6980f37333 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/URLEncodedTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/URLEncodedTest.java @@ -23,6 +23,7 @@ import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.junit.Assert; import org.junit.Test; @@ -121,22 +122,22 @@ public class URLEncodedTest assertEquals("encoded get", url_encoded.getString("Name8"),"xx, yy ,zz"); url_encoded.clear(); - url_encoded.decode("Name11=%u30EDxxVerdi+%C6+og+2zz", StringUtil.__ISO_8859_1_CHARSET); + url_encoded.decode("Name11=%u30EDxxVerdi+%C6+og+2zz", StandardCharsets.ISO_8859_1); assertEquals("encoded param size",1, url_encoded.size()); assertEquals("encoded get", "?xxVerdi \u00c6 og 2zz",url_encoded.getString("Name11")); url_encoded.clear(); - url_encoded.decode("Name12=%u30EDxxVerdi+%2F+og+2zz", StringUtil.__UTF8_CHARSET); + url_encoded.decode("Name12=%u30EDxxVerdi+%2F+og+2zz", StandardCharsets.UTF_8); assertEquals("encoded param size",1, url_encoded.size()); assertEquals("encoded get", url_encoded.getString("Name12"),"\u30edxxVerdi / og 2zz"); url_encoded.clear(); - url_encoded.decode("Name14=%uXXXXa%GGb%+%c%+%d", StringUtil.__ISO_8859_1_CHARSET); + url_encoded.decode("Name14=%uXXXXa%GGb%+%c%+%d", StandardCharsets.ISO_8859_1); assertEquals("encoded param size",1, url_encoded.size()); assertEquals("encoded get","?a?b?c?d", url_encoded.getString("Name14")); url_encoded.clear(); - url_encoded.decode("Name14=%uXXXX%GG%+%%+%", StringUtil.__UTF8_CHARSET); + url_encoded.decode("Name14=%uXXXX%GG%+%%+%", StandardCharsets.UTF_8); assertEquals("encoded param size",1, url_encoded.size()); assertEquals("encoded get", "\ufffd\ufffd\ufffd\ufffd",url_encoded.getString("Name14")); @@ -160,38 +161,38 @@ public class URLEncodedTest public void testBadEncoding() { UrlEncoded url_encoded = new UrlEncoded(); - url_encoded.decode("Name15=xx%zzyy", StringUtil.__UTF8_CHARSET); + url_encoded.decode("Name15=xx%zzyy", StandardCharsets.UTF_8); assertEquals("encoded param size",1, url_encoded.size()); assertEquals("encoded get", "xx\ufffdyy", url_encoded.getString("Name15")); - byte[] bad="Name=%FF%FF%FF".getBytes(StringUtil.__UTF8_CHARSET); + byte[] bad="Name=%FF%FF%FF".getBytes(StandardCharsets.UTF_8); MultiMap map = new MultiMap(); UrlEncoded.decodeUtf8To(bad,0,bad.length,map); assertEquals("encoded param size",1, map.size()); assertEquals("encoded get", "\ufffd\ufffd\ufffd", map.getString("Name")); url_encoded.clear(); - url_encoded.decode("Name=%FF%FF%FF", StringUtil.__UTF8_CHARSET); + url_encoded.decode("Name=%FF%FF%FF", StandardCharsets.UTF_8); assertEquals("encoded param size",1, url_encoded.size()); assertEquals("encoded get", "\ufffd\ufffd\ufffd", url_encoded.getString("Name")); url_encoded.clear(); - url_encoded.decode("Name=%EF%EF%EF", StringUtil.__UTF8_CHARSET); + url_encoded.decode("Name=%EF%EF%EF", StandardCharsets.UTF_8); assertEquals("encoded param size",1, url_encoded.size()); assertEquals("encoded get", "\ufffd\ufffd", url_encoded.getString("Name")); - assertEquals("x",UrlEncoded.decodeString("x",0,1,StringUtil.__UTF8_CHARSET)); - assertEquals("x\ufffd",UrlEncoded.decodeString("x%",0,2,StringUtil.__UTF8_CHARSET)); - assertEquals("x\ufffd",UrlEncoded.decodeString("x%2",0,3,StringUtil.__UTF8_CHARSET)); - assertEquals("x ",UrlEncoded.decodeString("x%20",0,4,StringUtil.__UTF8_CHARSET)); + assertEquals("x",UrlEncoded.decodeString("x",0,1,StandardCharsets.UTF_8)); + assertEquals("x\ufffd",UrlEncoded.decodeString("x%",0,2,StandardCharsets.UTF_8)); + assertEquals("x\ufffd",UrlEncoded.decodeString("x%2",0,3,StandardCharsets.UTF_8)); + assertEquals("x ",UrlEncoded.decodeString("x%20",0,4,StandardCharsets.UTF_8)); - assertEquals("xxx",UrlEncoded.decodeString("xxx",0,3,StringUtil.__UTF8_CHARSET)); - assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%",0,4,StringUtil.__UTF8_CHARSET)); - assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%u",0,5,StringUtil.__UTF8_CHARSET)); - assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%u1",0,6,StringUtil.__UTF8_CHARSET)); - assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%u12",0,7,StringUtil.__UTF8_CHARSET)); - assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%u123",0,8,StringUtil.__UTF8_CHARSET)); - assertEquals("xxx\u1234",UrlEncoded.decodeString("xxx%u1234",0,9,StringUtil.__UTF8_CHARSET)); + assertEquals("xxx",UrlEncoded.decodeString("xxx",0,3,StandardCharsets.UTF_8)); + assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%",0,4,StandardCharsets.UTF_8)); + assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%u",0,5,StandardCharsets.UTF_8)); + assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%u1",0,6,StandardCharsets.UTF_8)); + assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%u12",0,7,StandardCharsets.UTF_8)); + assertEquals("xxx\ufffd",UrlEncoded.decodeString("xxx%u123",0,8,StandardCharsets.UTF_8)); + assertEquals("xxx\u1234",UrlEncoded.decodeString("xxx%u1234",0,9,StandardCharsets.UTF_8)); } @@ -224,7 +225,7 @@ public class URLEncodedTest if (java.nio.charset.Charset.isSupported("Shift_JIS")) { - ByteArrayInputStream in2 = new ByteArrayInputStream ("name=%83e%83X%83g".getBytes(StringUtil.__ISO_8859_1)); + ByteArrayInputStream in2 = new ByteArrayInputStream("name=%83e%83X%83g".getBytes(StandardCharsets.ISO_8859_1)); MultiMap m2 = new MultiMap<>(); UrlEncoded.decodeTo(in2, m2, Charset.forName("Shift_JIS"), -1,-1); assertEquals("stream length",1,m2.size()); @@ -278,12 +279,12 @@ public class URLEncodedTest MultiMap map = new MultiMap<>(); UrlEncoded.LOG.info("EXPECT 4 Not Valid UTF8 warnings..."); - UrlEncoded.decodeUtf8To(query.getBytes(StringUtil.__ISO_8859_1),0,query.length(),map); + UrlEncoded.decodeUtf8To(query.getBytes(StandardCharsets.ISO_8859_1),0,query.length(),map); assertEquals("X"+Utf8Appendable.REPLACEMENT+Utf8Appendable.REPLACEMENT+"Z",map.getValue("name",0)); map.clear(); - UrlEncoded.decodeUtf8To(new ByteArrayInputStream(query.getBytes(StringUtil.__ISO_8859_1)),map,100,2); + UrlEncoded.decodeUtf8To(new ByteArrayInputStream(query.getBytes(StandardCharsets.ISO_8859_1)),map,100,2); assertEquals("X"+Utf8Appendable.REPLACEMENT+Utf8Appendable.REPLACEMENT+"Z",map.getValue("name",0)); } } diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/Utf8LineParserTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/Utf8LineParserTest.java index eec5684af4e..8a99eb5b22e 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/Utf8LineParserTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/Utf8LineParserTest.java @@ -32,7 +32,7 @@ public class Utf8LineParserTest { private void appendUtf8(ByteBuffer buf, String line) { - buf.put(ByteBuffer.wrap(StringUtil.getBytes(line,StringUtil.__UTF8))); + buf.put(ByteBuffer.wrap(StringUtil.getUtf8Bytes(line))); } private void assertEquals(List expected, List actual) diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/Utf8StringBufferTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/Utf8StringBufferTest.java index ab9371f7094..a622e01e750 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/Utf8StringBufferTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/Utf8StringBufferTest.java @@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import org.junit.Test; @@ -31,7 +32,7 @@ public class Utf8StringBufferTest public void testUtfStringBuffer() throws Exception { String source = "abcd012345\n\r\u0000\u00a4\u10fb\ufffdjetty"; - byte[] bytes = source.getBytes(StringUtil.__UTF8); + byte[] bytes = source.getBytes(StandardCharsets.UTF_8); Utf8StringBuffer buffer = new Utf8StringBuffer(); for (byte aByte : bytes) buffer.append(aByte); @@ -43,7 +44,7 @@ public class Utf8StringBufferTest public void testUtf8WithMissingByte() throws Exception { String source = "abc\u10fb"; - byte[] bytes = source.getBytes(StringUtil.__UTF8); + byte[] bytes = source.getBytes(StandardCharsets.UTF_8); Utf8StringBuffer buffer = new Utf8StringBuffer(); for (int i = 0; i < bytes.length - 1; i++) buffer.append(bytes[i]); @@ -54,7 +55,7 @@ public class Utf8StringBufferTest public void testUtf8WithAdditionalByte() throws Exception { String source = "abcXX"; - byte[] bytes = source.getBytes(StringUtil.__UTF8); + byte[] bytes = source.getBytes(StandardCharsets.UTF_8); bytes[3] = (byte)0xc0; bytes[4] = (byte)0x00; @@ -68,9 +69,9 @@ public class Utf8StringBufferTest public void testUTF32codes() throws Exception { String source = "\uD842\uDF9F"; - byte[] bytes = source.getBytes("UTF-8"); + byte[] bytes = source.getBytes(StandardCharsets.UTF_8); - String jvmcheck = new String(bytes,0,bytes.length,"UTF-8"); + String jvmcheck = new String(bytes,0,bytes.length,StandardCharsets.UTF_8); assertEquals(source,jvmcheck); Utf8StringBuffer buffer = new Utf8StringBuffer(); diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/Utf8StringBuilderTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/Utf8StringBuilderTest.java index 2af6be077d9..9967074e385 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/Utf8StringBuilderTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/Utf8StringBuilderTest.java @@ -24,6 +24,8 @@ import static org.junit.Assert.assertTrue; import org.junit.Assert; import org.junit.Test; +import java.nio.charset.StandardCharsets; + public class Utf8StringBuilderTest { @Test @@ -76,7 +78,7 @@ public class Utf8StringBuilderTest public void testUtfStringBuilder() throws Exception { String source = "abcd012345\n\r\u0000\u00a4\u10fb\ufffdjetty"; - byte[] bytes = source.getBytes(StringUtil.__UTF8); + byte[] bytes = source.getBytes(StandardCharsets.UTF_8); Utf8StringBuilder buffer = new Utf8StringBuilder(); for (byte aByte : bytes) buffer.append(aByte); @@ -88,7 +90,7 @@ public class Utf8StringBuilderTest public void testShort() throws Exception { String source = "abc\u10fb"; - byte[] bytes = source.getBytes(StringUtil.__UTF8); + byte[] bytes = source.getBytes(StandardCharsets.UTF_8); Utf8StringBuilder buffer = new Utf8StringBuilder(); for (int i = 0; i < bytes.length - 1; i++) buffer.append(bytes[i]); @@ -99,7 +101,7 @@ public class Utf8StringBuilderTest public void testLong() throws Exception { String source = "abcXX"; - byte[] bytes = source.getBytes(StringUtil.__UTF8); + byte[] bytes = source.getBytes(StandardCharsets.UTF_8); bytes[3] = (byte)0xc0; bytes[4] = (byte)0x00; @@ -122,9 +124,9 @@ public class Utf8StringBuilderTest public void testUTF32codes() throws Exception { String source = "\uD842\uDF9F"; - byte[] bytes = source.getBytes("UTF-8"); + byte[] bytes = source.getBytes(StandardCharsets.UTF_8); - String jvmcheck = new String(bytes,0,bytes.length,"UTF-8"); + String jvmcheck = new String(bytes,0,bytes.length,StandardCharsets.UTF_8); assertEquals(source,jvmcheck); Utf8StringBuilder buffer = new Utf8StringBuilder(); diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java index 5386e09eecc..dfd0317051b 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/log/StdErrLogTest.java @@ -26,6 +26,7 @@ import java.io.PrintStream; import java.io.PrintWriter; import java.io.StringWriter; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.Properties; import org.junit.Assert; @@ -647,7 +648,7 @@ public class StdErrLogTest log.debug("Show me the source!"); - String output = new String(test.toByteArray(),"UTF-8"); + String output = new String(test.toByteArray(), StandardCharsets.UTF_8); // System.err.print(output); Assert.assertThat(output, containsString(".StdErrLogTest#testPrintSource(StdErrLogTest.java:")); diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/resource/FileResourceTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/resource/FileResourceTest.java index 26674934485..5c754c50d5c 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/resource/FileResourceTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/resource/FileResourceTest.java @@ -26,6 +26,7 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.toolchain.test.OS; import org.eclipse.jetty.toolchain.test.TestingDir; @@ -52,7 +53,7 @@ public class FileResourceTest private URL decode(URL url) throws MalformedURLException { String raw = url.toExternalForm(); - String decoded = UrlEncoded.decodeString(raw,0,raw.length(),StringUtil.__UTF8_CHARSET); + String decoded = UrlEncoded.decodeString(raw,0,raw.length(), StandardCharsets.UTF_8); return new URL(decoded); } diff --git a/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketWriter.java b/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketWriter.java index abd194d24f8..43db5aaee6d 100644 --- a/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketWriter.java +++ b/jetty-websocket/websocket-api/src/main/java/org/eclipse/jetty/websocket/api/io/WebSocketWriter.java @@ -20,13 +20,11 @@ package org.eclipse.jetty.websocket.api.io; import java.io.IOException; import java.io.Writer; -import java.nio.charset.Charset; import org.eclipse.jetty.websocket.api.Session; public class WebSocketWriter extends Writer { - private final Charset charset = Charset.forName("UTF-8"); private final Session session; public WebSocketWriter(Session session) diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/ClientUpgradeRequest.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/ClientUpgradeRequest.java index 6e5096cfb9e..5885586966f 100644 --- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/ClientUpgradeRequest.java +++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/ClientUpgradeRequest.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.client; import java.net.CookieStore; import java.net.HttpCookie; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -223,7 +224,7 @@ public class ClientUpgradeRequest extends UpgradeRequest if (StringUtil.isNotBlank(query)) { MultiMap params = new MultiMap(); - UrlEncoded.decodeTo(uri.getQuery(),params,"UTF-8",MAX_KEYS); + UrlEncoded.decodeTo(uri.getQuery(),params,StandardCharsets.UTF_8,MAX_KEYS); for (String key : params.keySet()) { diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/UpgradeConnection.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/UpgradeConnection.java index eeea82a4f8a..488e58f2c8c 100644 --- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/UpgradeConnection.java +++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/io/UpgradeConnection.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.client.io; import java.io.IOException; import java.net.URI; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; @@ -70,7 +71,7 @@ public class UpgradeConnection extends AbstractConnection String rawRequest = request.generate(); - ByteBuffer buf = BufferUtil.toBuffer(rawRequest,StringUtil.__UTF8_CHARSET); + ByteBuffer buf = BufferUtil.toBuffer(rawRequest, StandardCharsets.UTF_8); getEndPoint().write(this,buf); } diff --git a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/TestClient.java b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/TestClient.java index f7974210412..2ab48983db3 100644 --- a/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/TestClient.java +++ b/jetty-websocket/websocket-client/src/test/java/org/eclipse/jetty/websocket/client/examples/TestClient.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.websocket.client.examples; import java.net.InetSocketAddress; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Random; import java.util.concurrent.BlockingQueue; @@ -208,7 +209,7 @@ public class TestClient { b.append('A' + __random.nextInt(26)); } - data = b.toString().getBytes(StringUtil.__UTF8_CHARSET); + data = b.toString().getBytes(StandardCharsets.UTF_8); break; } case OpCode.BINARY: diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/AcceptHash.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/AcceptHash.java index c4d5ba285d0..83027c0660e 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/AcceptHash.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/AcceptHash.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.websocket.common; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import org.eclipse.jetty.util.B64Code; @@ -36,19 +37,7 @@ public class AcceptHash *

* See Opening Handshake (Section 1.3) */ - private final static byte[] MAGIC; - - static - { - try - { - MAGIC = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11".getBytes(StringUtil.__ISO_8859_1); - } - catch (UnsupportedEncodingException e) - { - throw new RuntimeException(e); - } - } + private final static byte[] MAGIC = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11".getBytes(StandardCharsets.ISO_8859_1); /** * Concatenate the provided key with the Magic GUID and return the Base64 encoded form. @@ -62,7 +51,7 @@ public class AcceptHash try { MessageDigest md = MessageDigest.getInstance("SHA1"); - md.update(key.getBytes("UTF-8")); + md.update(key.getBytes(StandardCharsets.UTF_8)); md.update(MAGIC); return new String(B64Code.encode(md.digest())); } diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketFrame.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketFrame.java index ddbe0b0e8df..6bc30efae57 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketFrame.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketFrame.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.websocket.common; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import org.eclipse.jetty.util.BufferUtil; diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java index 6b9c4de0dea..47c91eb8524 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common; import java.io.IOException; import java.net.InetSocketAddress; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Map; diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ClosePayloadParserTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ClosePayloadParserTest.java index 4fe7cf28fe1..defae5b633b 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ClosePayloadParserTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ClosePayloadParserTest.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common; import static org.hamcrest.Matchers.*; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.websocket.api.StatusCode; @@ -36,7 +37,7 @@ public class ClosePayloadParserTest { String expectedReason = "Game Over"; - byte utf[] = expectedReason.getBytes(StringUtil.__UTF8_CHARSET); + byte utf[] = expectedReason.getBytes(StandardCharsets.UTF_8); ByteBuffer payload = ByteBuffer.allocate(utf.length + 2); payload.putChar((char)StatusCode.NORMAL); payload.put(utf,0,utf.length); diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/TextPayloadParserTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/TextPayloadParserTest.java index 274f3450289..2222b7e4d50 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/TextPayloadParserTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/TextPayloadParserTest.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common; import static org.hamcrest.Matchers.*; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import org.eclipse.jetty.util.StringUtil; @@ -77,7 +78,7 @@ public class TextPayloadParserTest sb.append(". The end."); String expectedText = sb.toString(); - byte utf[] = expectedText.getBytes(StringUtil.__UTF8); + byte utf[] = expectedText.getBytes(StandardCharsets.UTF_8); Assert.assertThat("Must be a long length payload",utf.length,greaterThan(0xFFFF)); @@ -113,7 +114,7 @@ public class TextPayloadParserTest sb.append(". The end."); String expectedText = sb.toString(); - byte utf[] = expectedText.getBytes(StringUtil.__UTF8); + byte utf[] = expectedText.getBytes(StandardCharsets.UTF_8); Assert.assertThat("Must be a medium length payload",utf.length,allOf(greaterThan(0x7E),lessThan(0xFFFF))); @@ -143,8 +144,8 @@ public class TextPayloadParserTest String part1 = "Hello "; String part2 = "World"; - byte b1[] = part1.getBytes(StringUtil.__UTF8_CHARSET); - byte b2[] = part2.getBytes(StringUtil.__UTF8_CHARSET); + byte b1[] = part1.getBytes(StandardCharsets.UTF_8); + byte b2[] = part2.getBytes(StandardCharsets.UTF_8); ByteBuffer buf = ByteBuffer.allocate(32); @@ -181,7 +182,7 @@ public class TextPayloadParserTest public void testShortMaskedText() throws Exception { String expectedText = "Hello World"; - byte utf[] = expectedText.getBytes(StringUtil.__UTF8_CHARSET); + byte utf[] = expectedText.getBytes(StandardCharsets.UTF_8); ByteBuffer buf = ByteBuffer.allocate(24); buf.put((byte)0x81); @@ -207,7 +208,7 @@ public class TextPayloadParserTest { String expectedText = "Hell\uFF4f W\uFF4Frld"; - byte utf[] = expectedText.getBytes(StringUtil.__UTF8); + byte utf[] = expectedText.getBytes(StandardCharsets.UTF_8); ByteBuffer buf = ByteBuffer.allocate(24); buf.put((byte)0x81); diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ab/TestABCase7_3.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ab/TestABCase7_3.java index 2f833eeb9a0..b0639072427 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ab/TestABCase7_3.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/ab/TestABCase7_3.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common.ab; import static org.hamcrest.Matchers.*; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import org.eclipse.jetty.util.BufferUtil; @@ -224,7 +225,7 @@ public class TestABCase7_3 ByteBuffer actual = UnitGenerator.generate(close.asFrame()); ByteBuffer expected = ByteBuffer.allocate(132); - byte messageBytes[] = message.toString().getBytes(StringUtil.__UTF8_CHARSET); + byte messageBytes[] = message.toString().getBytes(StandardCharsets.UTF_8); expected.put(new byte[] { (byte)0x88 }); @@ -250,7 +251,7 @@ public class TestABCase7_3 message.append("*"); } - byte[] messageBytes = message.toString().getBytes(StringUtil.__UTF8_CHARSET); + byte[] messageBytes = message.toString().getBytes(StandardCharsets.UTF_8); ByteBuffer expected = ByteBuffer.allocate(132); diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/FragmentExtensionTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/FragmentExtensionTest.java index 6e356457035..0984e71e99c 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/FragmentExtensionTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/FragmentExtensionTest.java @@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.*; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -92,7 +93,7 @@ public class FragmentExtensionTest Assert.assertThat(prefix + ".rsv2",actual.isRsv2(),is(false)); Assert.assertThat(prefix + ".rsv3",actual.isRsv3(),is(false)); - ByteBuffer expected = BufferUtil.toBuffer(quote.get(i),StringUtil.__UTF8_CHARSET); + ByteBuffer expected = BufferUtil.toBuffer(quote.get(i),StandardCharsets.UTF_8); Assert.assertThat(prefix + ".payloadLength",actual.getPayloadLength(),is(expected.remaining())); ByteBufferAssert.assertEquals(prefix + ".payload",expected,actual.getPayload().slice()); } @@ -128,7 +129,7 @@ public class FragmentExtensionTest Assert.assertThat("Frame.rsv2",actual.isRsv2(),is(false)); Assert.assertThat("Frame.rsv3",actual.isRsv3(),is(false)); - ByteBuffer expected = BufferUtil.toBuffer(payload,StringUtil.__UTF8_CHARSET); + ByteBuffer expected = BufferUtil.toBuffer(payload,StandardCharsets.UTF_8); Assert.assertThat("Frame.payloadLength",actual.getPayloadLength(),is(expected.remaining())); ByteBufferAssert.assertEquals("Frame.payload",expected,actual.getPayload().slice()); } @@ -301,7 +302,7 @@ public class FragmentExtensionTest Assert.assertThat("Frame.rsv2",actual.isRsv2(),is(false)); Assert.assertThat("Frame.rsv3",actual.isRsv3(),is(false)); - ByteBuffer expected = BufferUtil.toBuffer(payload,StringUtil.__UTF8_CHARSET); + ByteBuffer expected = BufferUtil.toBuffer(payload,StandardCharsets.UTF_8); Assert.assertThat("Frame.payloadLength",actual.getPayloadLength(),is(expected.remaining())); ByteBufferAssert.assertEquals("Frame.payload",expected,actual.getPayload().slice()); } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/IdentityExtensionTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/IdentityExtensionTest.java index f6569f2140b..ac347620240 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/IdentityExtensionTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/IdentityExtensionTest.java @@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.*; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.StringUtil; @@ -63,7 +64,7 @@ public class IdentityExtensionTest Assert.assertThat("Frame.rsv2",actual.isRsv2(),is(false)); Assert.assertThat("Frame.rsv3",actual.isRsv3(),is(false)); - ByteBuffer expected = BufferUtil.toBuffer("hello",StringUtil.__UTF8_CHARSET); + ByteBuffer expected = BufferUtil.toBuffer("hello",StandardCharsets.UTF_8); Assert.assertThat("Frame.payloadLength",actual.getPayloadLength(),is(expected.remaining())); ByteBufferAssert.assertEquals("Frame.payload",expected,actual.getPayload().slice()); } @@ -93,7 +94,7 @@ public class IdentityExtensionTest Assert.assertThat("Frame.rsv2",actual.isRsv2(),is(false)); Assert.assertThat("Frame.rsv3",actual.isRsv3(),is(false)); - ByteBuffer expected = BufferUtil.toBuffer("hello",StringUtil.__UTF8_CHARSET); + ByteBuffer expected = BufferUtil.toBuffer("hello",StandardCharsets.UTF_8); Assert.assertThat("Frame.payloadLength",actual.getPayloadLength(),is(expected.remaining())); ByteBufferAssert.assertEquals("Frame.payload",expected,actual.getPayload().slice()); } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/DeflateFrameExtensionTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/DeflateFrameExtensionTest.java index 9d8a2935823..d7e30c95bba 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/DeflateFrameExtensionTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/DeflateFrameExtensionTest.java @@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.*; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; import java.util.zip.Deflater; @@ -92,7 +93,7 @@ public class DeflateFrameExtensionTest Assert.assertThat(prefix + ".rsv2",actual.isRsv2(),is(false)); Assert.assertThat(prefix + ".rsv3",actual.isRsv3(),is(false)); - ByteBuffer expected = BufferUtil.toBuffer(expectedTextDatas[i],StringUtil.__UTF8_CHARSET); + ByteBuffer expected = BufferUtil.toBuffer(expectedTextDatas[i],StandardCharsets.UTF_8); Assert.assertThat(prefix + ".payloadLength",actual.getPayloadLength(),is(expected.remaining())); ByteBufferAssert.assertEquals(prefix + ".payload",expected,actual.getPayload().slice()); } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/PerMessageDeflateExtensionTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/PerMessageDeflateExtensionTest.java index 1e0b33cf147..7944a7fdf55 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/PerMessageDeflateExtensionTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/extensions/compress/PerMessageDeflateExtensionTest.java @@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.*; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -136,7 +137,7 @@ public class PerMessageDeflateExtensionTest Assert.assertThat(prefix + ".rsv2",actual.isRsv2(),is(false)); Assert.assertThat(prefix + ".rsv3",actual.isRsv3(),is(false)); - ByteBuffer expected = BufferUtil.toBuffer(expectedStr,StringUtil.__UTF8_CHARSET); + ByteBuffer expected = BufferUtil.toBuffer(expectedStr, StandardCharsets.UTF_8); Assert.assertThat(prefix + ".payloadLength",actual.getPayloadLength(),is(expected.remaining())); ByteBufferAssert.assertEquals(prefix + ".payload",expected,actual.getPayload().slice()); } @@ -382,7 +383,7 @@ public class PerMessageDeflateExtensionTest Assert.assertThat("Frame.rsv2",actual.isRsv2(),is(false)); Assert.assertThat("Frame.rsv3",actual.isRsv3(),is(false)); - ByteBuffer expected = BufferUtil.toBuffer(payload,StringUtil.__UTF8_CHARSET); + ByteBuffer expected = BufferUtil.toBuffer(payload,StandardCharsets.UTF_8); Assert.assertThat("Frame.payloadLength",actual.getPayloadLength(),is(expected.remaining())); ByteBufferAssert.assertEquals("Frame.payload",expected,actual.getPayload().slice()); } @@ -436,7 +437,7 @@ public class PerMessageDeflateExtensionTest Assert.assertThat(prefix + ".rsv2",actual.isRsv2(),is(false)); Assert.assertThat(prefix + ".rsv3",actual.isRsv3(),is(false)); - ByteBuffer expected = BufferUtil.toBuffer(quote.get(i),StringUtil.__UTF8_CHARSET); + ByteBuffer expected = BufferUtil.toBuffer(quote.get(i),StandardCharsets.UTF_8); Assert.assertThat(prefix + ".payloadLength",actual.getPayloadLength(),is(expected.remaining())); ByteBufferAssert.assertEquals(prefix + ".payload",expected,actual.getPayload().slice()); } @@ -476,7 +477,7 @@ public class PerMessageDeflateExtensionTest Assert.assertThat("Frame.rsv2",actual.isRsv2(),is(false)); Assert.assertThat("Frame.rsv3",actual.isRsv3(),is(false)); - ByteBuffer expected = BufferUtil.toBuffer(payload,StringUtil.__UTF8_CHARSET); + ByteBuffer expected = BufferUtil.toBuffer(payload,StandardCharsets.UTF_8); Assert.assertThat("Frame.payloadLength",actual.getPayloadLength(),is(expected.remaining())); ByteBufferAssert.assertEquals("Frame.payload",expected,actual.getPayload().slice()); } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/http/HttpResponseHeaderParserTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/http/HttpResponseHeaderParserTest.java index 837cf10bcbe..7532b0da410 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/http/HttpResponseHeaderParserTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/io/http/HttpResponseHeaderParserTest.java @@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -39,7 +40,7 @@ public class HttpResponseHeaderParserTest private void appendUtf8(ByteBuffer buf, String line) { - buf.put(ByteBuffer.wrap(StringUtil.getBytes(line,StringUtil.__UTF8))); + buf.put(ByteBuffer.wrap(StringUtil.getUtf8Bytes(line))); } @Test @@ -56,7 +57,7 @@ public class HttpResponseHeaderParserTest // and some body content resp.append("What you are looking for is not here\r\n"); - ByteBuffer buf = BufferUtil.toBuffer(resp.toString(),StringUtil.__UTF8_CHARSET); + ByteBuffer buf = BufferUtil.toBuffer(resp.toString(),StandardCharsets.UTF_8); HttpResponseParseCapture capture = new HttpResponseParseCapture(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(capture); @@ -178,7 +179,7 @@ public class HttpResponseHeaderParserTest resp.append("Sec-WebSocket-Protocol: chat\r\n"); resp.append("\r\n"); - ByteBuffer buf = BufferUtil.toBuffer(resp.toString(),StringUtil.__UTF8_CHARSET); + ByteBuffer buf = BufferUtil.toBuffer(resp.toString(),StandardCharsets.UTF_8); HttpResponseParseCapture capture = new HttpResponseParseCapture(); HttpResponseHeaderParser parser = new HttpResponseHeaderParser(capture); diff --git a/jetty-websocket/websocket-mux-extension/src/main/java/org/eclipse/jetty/websocket/mux/op/MuxAddChannelRequest.java b/jetty-websocket/websocket-mux-extension/src/main/java/org/eclipse/jetty/websocket/mux/op/MuxAddChannelRequest.java index 4647c5b967a..05065c6b780 100644 --- a/jetty-websocket/websocket-mux-extension/src/main/java/org/eclipse/jetty/websocket/mux/op/MuxAddChannelRequest.java +++ b/jetty-websocket/websocket-mux-extension/src/main/java/org/eclipse/jetty/websocket/mux/op/MuxAddChannelRequest.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.websocket.mux.op; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.StringUtil; @@ -104,7 +105,7 @@ public class MuxAddChannelRequest implements MuxControlBlock public void setHandshake(String rawstring) { - setHandshake(BufferUtil.toBuffer(rawstring,StringUtil.__UTF8_CHARSET)); + setHandshake(BufferUtil.toBuffer(rawstring, StandardCharsets.UTF_8)); } public void setRsv(byte rsv) diff --git a/jetty-websocket/websocket-mux-extension/src/main/java/org/eclipse/jetty/websocket/mux/op/MuxAddChannelResponse.java b/jetty-websocket/websocket-mux-extension/src/main/java/org/eclipse/jetty/websocket/mux/op/MuxAddChannelResponse.java index b2a2dd64f76..13c6493b41c 100644 --- a/jetty-websocket/websocket-mux-extension/src/main/java/org/eclipse/jetty/websocket/mux/op/MuxAddChannelResponse.java +++ b/jetty-websocket/websocket-mux-extension/src/main/java/org/eclipse/jetty/websocket/mux/op/MuxAddChannelResponse.java @@ -19,6 +19,7 @@ package org.eclipse.jetty.websocket.mux.op; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.StringUtil; @@ -115,7 +116,7 @@ public class MuxAddChannelResponse implements MuxControlBlock public void setHandshake(String responseHandshake) { - setHandshake(BufferUtil.toBuffer(responseHandshake,StringUtil.__UTF8_CHARSET)); + setHandshake(BufferUtil.toBuffer(responseHandshake, StandardCharsets.UTF_8)); } public void setRsv(byte rsv) diff --git a/jetty-websocket/websocket-mux-extension/src/test/java/org/eclipse/jetty/websocket/mux/MuxParserRFCTest.java b/jetty-websocket/websocket-mux-extension/src/test/java/org/eclipse/jetty/websocket/mux/MuxParserRFCTest.java index e32a457c5df..40b1583240a 100644 --- a/jetty-websocket/websocket-mux-extension/src/test/java/org/eclipse/jetty/websocket/mux/MuxParserRFCTest.java +++ b/jetty-websocket/websocket-mux-extension/src/test/java/org/eclipse/jetty/websocket/mux/MuxParserRFCTest.java @@ -23,6 +23,7 @@ import static org.hamcrest.Matchers.*; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -236,7 +237,7 @@ public class MuxParserRFCTest } else { - out.write(part.getBytes(StringUtil.__UTF8_CHARSET)); + out.write(part.getBytes(StandardCharsets.UTF_8)); } } return out.toByteArray(); diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java index 098e16c5de5..4a8308a1dd5 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/blockhead/BlockheadClient.java @@ -33,6 +33,7 @@ import java.net.SocketTimeoutException; import java.net.URI; import java.net.URISyntaxException; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; @@ -728,7 +729,7 @@ public class BlockheadClient implements IncomingFrames, OutgoingFrames, Connecti public void writeRaw(String str) throws IOException { LOG.debug("write((String)[{}]){}{})",str.length(),'\n',str); - out.write(StringUtil.getBytes(str,StringUtil.__ISO_8859_1)); + out.write(str.getBytes(StandardCharsets.ISO_8859_1)); } public void writeRawSlowly(ByteBuffer buf, int segmentSize) throws IOException diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/helper/SafariD00.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/helper/SafariD00.java index 3ad6594ec3b..ca1071fddf3 100644 --- a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/helper/SafariD00.java +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/helper/SafariD00.java @@ -30,6 +30,7 @@ import java.net.Socket; import java.net.SocketAddress; import java.net.URI; import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.TypeUtil; @@ -90,7 +91,7 @@ public class SafariD00 // System.out.printf("--- Request ---%n%s",req); - byte reqBytes[] = req.toString().getBytes("UTF-8"); + byte reqBytes[] = req.toString().getBytes(StandardCharsets.UTF_8); byte hixieBytes[] = TypeUtil.fromHexString("e739617916c9daf3"); byte buf[] = new byte[reqBytes.length + hixieBytes.length]; System.arraycopy(reqBytes,0,buf,0,reqBytes.length); @@ -140,7 +141,7 @@ public class SafariD00 for (String msg : msgs) { buf.put((byte)0x00); - buf.put(msg.getBytes("UTF-8")); + buf.put(msg.getBytes(StandardCharsets.UTF_8)); buf.put((byte)0xFF); } diff --git a/tests/test-continuation/src/test/java/org/eclipse/jetty/continuation/ContinuationBase.java b/tests/test-continuation/src/test/java/org/eclipse/jetty/continuation/ContinuationBase.java index 9a97fef7477..13586d088a6 100644 --- a/tests/test-continuation/src/test/java/org/eclipse/jetty/continuation/ContinuationBase.java +++ b/tests/test-continuation/src/test/java/org/eclipse/jetty/continuation/ContinuationBase.java @@ -27,6 +27,7 @@ import static org.junit.Assert.assertThat; import java.io.IOException; import java.io.InputStream; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.Timer; import java.util.TimerTask; @@ -242,7 +243,7 @@ public abstract class ContinuationBase try (Socket socket = new Socket("localhost",port);) { socket.setSoTimeout(10000); - socket.getOutputStream().write(request.getBytes("UTF-8")); + socket.getOutputStream().write(request.getBytes(StandardCharsets.UTF_8)); socket.getOutputStream().flush(); response = toString(socket.getInputStream()); diff --git a/tests/test-integration/src/test/java/org/eclipse/jetty/test/DigestPostTest.java b/tests/test-integration/src/test/java/org/eclipse/jetty/test/DigestPostTest.java index 4911cd246c9..d9928b50836 100644 --- a/tests/test-integration/src/test/java/org/eclipse/jetty/test/DigestPostTest.java +++ b/tests/test-integration/src/test/java/org/eclipse/jetty/test/DigestPostTest.java @@ -22,6 +22,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.net.Socket; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.util.Collections; import java.util.concurrent.TimeUnit; @@ -129,14 +130,14 @@ public class DigestPostTest public void testServerDirectlyHTTP10() throws Exception { Socket socket = new Socket("127.0.0.1",((NetworkConnector)_server.getConnectors()[0]).getLocalPort()); - byte[] bytes = __message.getBytes("UTF-8"); + byte[] bytes = __message.getBytes(StandardCharsets.UTF_8); _received=null; socket.getOutputStream().write( ("POST /test/ HTTP/1.0\r\n"+ "Host: 127.0.0.1:"+((NetworkConnector)_server.getConnectors()[0]).getLocalPort()+"\r\n"+ "Content-Length: "+bytes.length+"\r\n"+ - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); socket.getOutputStream().write(bytes); socket.getOutputStream().flush(); @@ -163,7 +164,7 @@ public class DigestPostTest "Host: 127.0.0.1:"+((NetworkConnector)_server.getConnectors()[0]).getLocalPort()+"\r\n"+ "Content-Length: "+bytes.length+"\r\n"+ "Authorization: "+digest+"\r\n"+ - "\r\n").getBytes("UTF-8")); + "\r\n").getBytes(StandardCharsets.UTF_8)); socket.getOutputStream().write(bytes); socket.getOutputStream().flush(); @@ -177,7 +178,7 @@ public class DigestPostTest public void testServerDirectlyHTTP11() throws Exception { Socket socket = new Socket("127.0.0.1",((NetworkConnector)_server.getConnectors()[0]).getLocalPort()); - byte[] bytes = __message.getBytes("UTF-8"); + byte[] bytes = __message.getBytes(StandardCharsets.UTF_8); _received=null; socket.getOutputStream().write( @@ -192,7 +193,7 @@ public class DigestPostTest byte[] buf=new byte[4096]; int len=socket.getInputStream().read(buf); - String result=new String(buf,0,len,"UTF-8"); + String result=new String(buf,0,len,StandardCharsets.UTF_8); Assert.assertTrue(result.startsWith("HTTP/1.1 401 Unauthorized")); Assert.assertEquals(null,_received);