Use stock JRE Charset instead of magic string
Clean up exceptions on non-public methods
diff --git a/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/AbstractAsyncXMLReaderTestCase.java b/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/AbstractAsyncXMLReaderTestCase.java
index e1dda4d..a77e257 100644
--- a/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/AbstractAsyncXMLReaderTestCase.java
+++ b/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/AbstractAsyncXMLReaderTestCase.java
@@ -20,6 +20,7 @@
*/
package org.apache.vysper.xml.sax.impl;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -145,7 +146,7 @@
reader.setContentHandler(handler);
reader.setErrorHandler(handler);
- reader.parse(IoBuffer.wrap(xml.getBytes("UTF-8")), CharsetUtil.getDecoder());
+ reader.parse(IoBuffer.wrap(xml.getBytes(StandardCharsets.UTF_8)), CharsetUtil.getDecoder());
return handler.getEvents();
}
diff --git a/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/DefaultAsyncXMLReaderTestCase.java b/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/DefaultAsyncXMLReaderTestCase.java
index e411708..274bb41 100644
--- a/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/DefaultAsyncXMLReaderTestCase.java
+++ b/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/DefaultAsyncXMLReaderTestCase.java
@@ -19,6 +19,8 @@
*/
package org.apache.vysper.xml.sax.impl;
+import java.nio.charset.StandardCharsets;
+
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.vysper.charset.CharsetUtil;
import org.apache.vysper.xml.sax.NonBlockingXMLReader;
@@ -39,11 +41,11 @@
reader.setErrorHandler(handler);
// causes a fatal error
- reader.parse(IoBuffer.wrap("<root></error>".getBytes("UTF-8")), CharsetUtil.getDecoder());
+ reader.parse(IoBuffer.wrap("<root></error>".getBytes(StandardCharsets.UTF_8)), CharsetUtil.getDecoder());
try {
// not allowed to parse after an error
- reader.parse(IoBuffer.wrap("<root>".getBytes("UTF-8")), CharsetUtil.getDecoder());
+ reader.parse(IoBuffer.wrap("<root>".getBytes(StandardCharsets.UTF_8)), CharsetUtil.getDecoder());
fail("Must throw SAXException");
} catch (SAXException e) {
// OK
@@ -57,11 +59,11 @@
reader.setErrorHandler(handler);
// causes a fatal error
- reader.parse(IoBuffer.wrap("<root></root>".getBytes("UTF-8")), CharsetUtil.getDecoder());
+ reader.parse(IoBuffer.wrap("<root></root>".getBytes(StandardCharsets.UTF_8)), CharsetUtil.getDecoder());
try {
// not allowed to parse after end of document
- reader.parse(IoBuffer.wrap("<root>".getBytes("UTF-8")), CharsetUtil.getDecoder());
+ reader.parse(IoBuffer.wrap("<root>".getBytes(StandardCharsets.UTF_8)), CharsetUtil.getDecoder());
fail("Must throw SAXException");
} catch (SAXException e) {
// OK
@@ -135,7 +137,7 @@
public void testSetFeatureDuringParse() throws Exception {
DefaultNonBlockingXMLReader reader = new DefaultNonBlockingXMLReader();
- reader.parse(IoBuffer.wrap("<foo />".getBytes("UTF-8")), CharsetUtil.getDecoder());
+ reader.parse(IoBuffer.wrap("<foo />".getBytes(StandardCharsets.UTF_8)), CharsetUtil.getDecoder());
try {
reader.setFeature("http://xml.org/sax/features/namespaces", true);
fail("Must throw SAXNotSupportedException");
diff --git a/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/ParseElementsTestCase.java b/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/ParseElementsTestCase.java
index 6d28128..329ed09 100644
--- a/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/ParseElementsTestCase.java
+++ b/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/ParseElementsTestCase.java
@@ -19,12 +19,12 @@
*/
package org.apache.vysper.xml.sax.impl;
+import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.vysper.charset.CharsetUtil;
import org.apache.vysper.xml.sax.NonBlockingXMLReader;
-import org.apache.vysper.xml.sax.impl.TestHandler.CharacterEvent;
import org.apache.vysper.xml.sax.impl.TestHandler.TestEvent;
/**
@@ -181,7 +181,7 @@
String s = "<root>\u1251</root>";
// split in the middle of the Unicode char
- byte[] xml = s.getBytes("UTF-8");
+ byte[] xml = s.getBytes(StandardCharsets.UTF_8);
byte[] xml1 = new byte[8];
byte[] xml2 = new byte[8];
diff --git a/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/XMPPContentHandlerTestCase.java b/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/XMPPContentHandlerTestCase.java
index 6a5f453..18c7d53 100644
--- a/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/XMPPContentHandlerTestCase.java
+++ b/nbxml/src/test/java/org/apache/vysper/xml/sax/impl/XMPPContentHandlerTestCase.java
@@ -19,6 +19,7 @@
*/
package org.apache.vysper.xml.sax.impl;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -76,7 +77,7 @@
}
private void parse(NonBlockingXMLReader reader, String xml) throws Exception {
- reader.parse(IoBuffer.wrap(xml.getBytes("UTF-8")), CharsetUtil.getDecoder());
+ reader.parse(IoBuffer.wrap(xml.getBytes(StandardCharsets.UTF_8)), CharsetUtil.getDecoder());
}
}
\ No newline at end of file
diff --git a/nbxml/src/test/java/org/apache/vysper/xml/sax/perf/PerfRunner.java b/nbxml/src/test/java/org/apache/vysper/xml/sax/perf/PerfRunner.java
index 5371f3f..77c5709 100644
--- a/nbxml/src/test/java/org/apache/vysper/xml/sax/perf/PerfRunner.java
+++ b/nbxml/src/test/java/org/apache/vysper/xml/sax/perf/PerfRunner.java
@@ -19,6 +19,8 @@
*/
package org.apache.vysper.xml.sax.perf;
+import java.nio.charset.StandardCharsets;
+
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.vysper.charset.CharsetUtil;
import org.apache.vysper.xml.decoder.XMLElementListener;
@@ -49,8 +51,8 @@
public static void main(String[] args) throws Exception {
- IoBuffer opening = IoBuffer.wrap("<p:root xmlns:p='http://example.com'>".getBytes("UTF-8"));
- IoBuffer buffer = IoBuffer.wrap(SINGLE_LEVEL_XML.getBytes("UTF-8"));
+ IoBuffer opening = IoBuffer.wrap("<p:root xmlns:p='http://example.com'>".getBytes(StandardCharsets.UTF_8));
+ IoBuffer buffer = IoBuffer.wrap(SINGLE_LEVEL_XML.getBytes(StandardCharsets.UTF_8));
DefaultNonBlockingXMLReader reader = new DefaultNonBlockingXMLReader();
CounterStanzaListener listener = new CounterStanzaListener();
diff --git a/server/core/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0220_server_dailback/DialbackIdGenerator.java b/server/core/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0220_server_dailback/DialbackIdGenerator.java
index c4ce95e..a10473d 100644
--- a/server/core/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0220_server_dailback/DialbackIdGenerator.java
+++ b/server/core/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0220_server_dailback/DialbackIdGenerator.java
@@ -18,7 +18,7 @@
*
*/
package org.apache.vysper.xmpp.modules.extension.xep0220_server_dailback;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
@@ -54,19 +54,15 @@
Validate.notNull(receiving, "receiving can not be null");
Validate.notNull(originating, "originating can not be null");
Validate.notNull(streamId, "streamId can not be null");
-
- try {
- mac.update(receiving.getDomain().getBytes("UTF-16"));
- mac.update(" ".getBytes("UTF-16"));
- mac.update(originating.getDomain().getBytes("UTF-16"));
- mac.update(" ".getBytes("UTF-16"));
- mac.update(streamId.getBytes("UTF-16"));
-
- byte[] rawHmac = mac.doFinal();
- return Hex.encodeHexString(rawHmac);
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- }
+
+ mac.update(receiving.getDomain().getBytes(StandardCharsets.UTF_16));
+ mac.update(" ".getBytes(StandardCharsets.UTF_16));
+ mac.update(originating.getDomain().getBytes(StandardCharsets.UTF_16));
+ mac.update(" ".getBytes(StandardCharsets.UTF_16));
+ mac.update(streamId.getBytes(StandardCharsets.UTF_16));
+
+ byte[] rawHmac = mac.doFinal();
+ return Hex.encodeHexString(rawHmac);
}
public boolean verify(String dailbackId, Entity receiving, Entity originating, String streamId) {
diff --git a/server/core/src/main/java/org/apache/vysper/xmpp/parser/XMLParserUtil.java b/server/core/src/main/java/org/apache/vysper/xmpp/parser/XMLParserUtil.java
index cd59af2..408b658 100644
--- a/server/core/src/main/java/org/apache/vysper/xmpp/parser/XMLParserUtil.java
+++ b/server/core/src/main/java/org/apache/vysper/xmpp/parser/XMLParserUtil.java
@@ -21,6 +21,7 @@
package org.apache.vysper.xmpp.parser;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@@ -65,7 +66,7 @@
public void close() {}
});
- IoBuffer buffer = IoBuffer.wrap(xml.getBytes("UTF-8"));
+ IoBuffer buffer = IoBuffer.wrap(xml.getBytes(StandardCharsets.UTF_8));
reader.parse(buffer, CharsetUtil.getDecoder());
if(!documents.isEmpty()) {
diff --git a/server/extensions/websockets/src/main/java/org/apache/vysper/xmpp/extension/websockets/WebSocketBackedSessionContext.java b/server/extensions/websockets/src/main/java/org/apache/vysper/xmpp/extension/websockets/WebSocketBackedSessionContext.java
index c96c446..8e1b703 100644
--- a/server/extensions/websockets/src/main/java/org/apache/vysper/xmpp/extension/websockets/WebSocketBackedSessionContext.java
+++ b/server/extensions/websockets/src/main/java/org/apache/vysper/xmpp/extension/websockets/WebSocketBackedSessionContext.java
@@ -22,6 +22,7 @@
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
+import java.nio.charset.StandardCharsets;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.vysper.mina.codec.StanzaBuilderFactory;
@@ -51,7 +52,7 @@
*/
public class WebSocketBackedSessionContext extends AbstractSessionContext implements XMLElementListener, StanzaWriter {
- private final static Charset CHARSET = Charset.forName("UTF-8");
+ private final static Charset CHARSET = StandardCharsets.UTF_8;
private final static CharsetDecoder CHARSET_DECODER = CHARSET.newDecoder();
diff --git a/server/extensions/xep0065-socks/src/test/java/org/apache/vysper/xmpp/extension/xep0065_socks/Socks5IntegrationTest.java b/server/extensions/xep0065-socks/src/test/java/org/apache/vysper/xmpp/extension/xep0065_socks/Socks5IntegrationTest.java
index 96d4390..bbe5659 100644
--- a/server/extensions/xep0065-socks/src/test/java/org/apache/vysper/xmpp/extension/xep0065_socks/Socks5IntegrationTest.java
+++ b/server/extensions/xep0065-socks/src/test/java/org/apache/vysper/xmpp/extension/xep0065_socks/Socks5IntegrationTest.java
@@ -24,6 +24,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
@@ -80,7 +82,7 @@
private static final String PASSWORD = "password";
- private static final String CHARSET = "ASCII";
+ private static final Charset CHARSET = StandardCharsets.US_ASCII;
private static final String TEST_DATA = "hello world";
diff --git a/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshServlet.java b/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshServlet.java
index 9f71369..87f37a6 100644
--- a/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshServlet.java
+++ b/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshServlet.java
@@ -29,7 +29,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
import java.util.List;
import static org.apache.vysper.xmpp.extension.xep0124.BoshBackedSessionContext.BOSH_REQUEST_ATTRIBUTE;
@@ -101,20 +101,17 @@
*/
protected byte[] createFlashCrossDomainPolicy() {
StringBuffer crossDomain = new StringBuffer();
- crossDomain.append("<?xml version='1.0'?>");
- crossDomain.append("<!DOCTYPE cross-domain-policy SYSTEM 'http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd'>");
+ crossDomain.append("<?xml version='1.0'?>");
+ crossDomain.append(
+ "<!DOCTYPE cross-domain-policy SYSTEM 'http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd'>");
crossDomain.append("<cross-domain-policy>");
- for(String domain : accessControlAllowOrigin) {
- crossDomain.append("<allow-access-from domain='");
- crossDomain.append(domain);
- crossDomain.append("' />");
- }
- crossDomain.append("</cross-domain-policy>");
- try {
- return crossDomain.toString().getBytes("UTF-8");
- } catch (UnsupportedEncodingException shouldNotHappen) {
- throw new RuntimeException(shouldNotHappen);
+ for (String domain : accessControlAllowOrigin) {
+ crossDomain.append("<allow-access-from domain='");
+ crossDomain.append(domain);
+ crossDomain.append("' />");
}
+ crossDomain.append("</cross-domain-policy>");
+ return crossDomain.toString().getBytes(StandardCharsets.UTF_8);
}
protected String createAccessControlAllowOrigin() {
diff --git a/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/HBaseUtils.java b/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/HBaseUtils.java
index a0d8f49..8e6e728 100644
--- a/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/HBaseUtils.java
+++ b/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/HBaseUtils.java
@@ -1,8 +1,8 @@
package org.apache.vysper.storage.hbase;
-import org.apache.vysper.xmpp.addressing.Entity;
+import java.nio.charset.StandardCharsets;
-import java.io.UnsupportedEncodingException;
+import org.apache.vysper.xmpp.addressing.Entity;
/**
*/
@@ -10,12 +10,7 @@
public static byte[] asBytes(String str) {
if (str == null) return null;
- try {
- return str.getBytes("UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace(); // won't happen! UTF-8 is supported
- return null;
- }
+ return str.getBytes(StandardCharsets.UTF_8);
}
public static byte[] entityAsBytes(Entity entity) {
@@ -25,10 +20,6 @@
public static String toStr(byte[] bytes) {
if (bytes == null) return null;
- try {
- return new String(bytes, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- return null; // will not happen for UTF-8
- }
+ return new String(bytes, StandardCharsets.UTF_8);
}
}
diff --git a/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/user/HBaseUserManagement.java b/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/user/HBaseUserManagement.java
index e98c019..0af3b6d 100644
--- a/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/user/HBaseUserManagement.java
+++ b/server/storage/hbase/src/main/java/org/apache/vysper/storage/hbase/user/HBaseUserManagement.java
@@ -33,6 +33,7 @@
import org.slf4j.LoggerFactory;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Arrays;
@@ -89,7 +90,7 @@
MessageDigest digest = MessageDigest.getInstance("SHA-256");
int rounds = Math.max(1, hashingRounds);
- byte[] pwdBytes = passwordCleartext.getBytes("UTF-8");
+ byte[] pwdBytes = passwordCleartext.getBytes(StandardCharsets.UTF_8);
for (int i = 0; i < rounds; i++) {
pwdBytes = digest.digest(pwdBytes);
}