Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java (working copy) @@ -21,6 +21,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.StringReader; +import java.nio.charset.StandardCharsets; import java.security.AccessControlContext; import java.security.AccessController; import java.util.ArrayList; @@ -587,7 +588,7 @@ uuidFile.makeParentDirs(); OutputStream out = uuidFile.getOutputStream(); try { - out.write(ROOT_NODE_ID.toString().getBytes("US-ASCII")); + out.write(ROOT_NODE_ID.toString().getBytes(StandardCharsets.US_ASCII)); return ROOT_NODE_ID; } finally { IOUtils.closeQuietly(out); Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/TestContentLoader.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/TestContentLoader.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/TestContentLoader.java (working copy) @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Collection; +import java.nio.charset.StandardCharsets; import javax.jcr.Node; import javax.jcr.PathNotFoundException; @@ -137,7 +138,7 @@ * Creates three nodes under the given node: one of type nt:resource * and the other nodes referencing it. */ - private void addNodeTestData(Node node) throws RepositoryException, IOException { + private void addNodeTestData(Node node) throws RepositoryException { if (node.hasNode("multiReference")) { node.getNode("multiReference").remove(); } @@ -155,7 +156,7 @@ resource.setProperty("jcr:mimeType", "text/plain"); resource.setProperty( "jcr:data", - new ByteArrayInputStream("Hello w\u00F6rld.".getBytes(ENCODING))); + new ByteArrayInputStream("Hello w\u00F6rld.".getBytes(StandardCharsets.UTF_8))); resource.setProperty("jcr:lastModified", Calendar.getInstance()); Node resReference = getOrAddNode(node, "reference"); @@ -173,7 +174,7 @@ // NodeDefTest requires a test node with a mandatory child node JcrUtils.putFile( node, "testFile", "text/plain", - new ByteArrayInputStream("Hello, World!".getBytes("UTF-8"))); + new ByteArrayInputStream("Hello, World!".getBytes(StandardCharsets.UTF_8))); } /** @@ -255,7 +256,7 @@ resource = getOrAddNode(node, "invalidBin"); resource.setProperty("jcr:encoding", ENCODING); resource.setProperty("jcr:mimeType", "text/plain"); - byte[] bytes = "Hello w\u00F6rld.".getBytes(ENCODING); + byte[] bytes = "Hello w\u00F6rld.".getBytes(StandardCharsets.UTF_8); resource.setProperty(name, new ByteArrayInputStream(bytes)); resource.setProperty("jcr:lastModified", Calendar.getInstance()); } Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/nodetype/NodeTypeRegistry.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/nodetype/NodeTypeRegistry.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/nodetype/NodeTypeRegistry.java (working copy) @@ -21,6 +21,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -790,7 +791,7 @@ try { in = getClass().getClassLoader().getResourceAsStream(BUILTIN_NODETYPES_RESOURCE_PATH); if (in != null) { - Reader r = new InputStreamReader(in, "utf-8"); + Reader r = new InputStreamReader(in, StandardCharsets.UTF_8); store.loadCND(r, BUILTIN_NODETYPES_RESOURCE_PATH); } } catch (IOException ioe) { Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/nodetype/virtual/VirtualNodeTypeStateProvider.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/nodetype/virtual/VirtualNodeTypeStateProvider.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/nodetype/virtual/VirtualNodeTypeStateProvider.java (working copy) @@ -16,7 +16,7 @@ */ package org.apache.jackrabbit.core.nodetype.virtual; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Collection; @@ -251,12 +251,10 @@ private static NodeId calculateStableId(String name) throws RepositoryException { try { MessageDigest md = MessageDigest.getInstance("MD5"); - byte[] digest = md.digest(name.getBytes("utf-8")); + byte[] digest = md.digest(name.getBytes(StandardCharsets.UTF_8)); return new NodeId(digest); } catch (NoSuchAlgorithmException e) { throw new RepositoryException(e); - } catch (UnsupportedEncodingException e) { - throw new RepositoryException(e); } } Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleDumper.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleDumper.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleDumper.java (working copy) @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.RandomAccessFile; import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; @@ -368,7 +369,7 @@ int len = in.readInt(); byte[] bytes = new byte[len]; in.readFully(bytes); - buffer.append(" value: string: ").append(new String(bytes, "UTF-8")).append("\n"); + buffer.append(" value: string: ").append(new String(bytes, StandardCharsets.UTF_8)).append("\n"); } } } @@ -463,7 +464,7 @@ namespaces[ns] = uri; } } - String local = new String(readBytes((b & 0x0f) + 1, 0x10), "UTF-8"); + String local = new String(readBytes((b & 0x0f) + 1, 0x10), StandardCharsets.UTF_8); return uri + ":" + local; } } @@ -603,7 +604,7 @@ private String readString() throws IOException { if (version >= VERSION_3) { - return new String(readBytes(0, 0), "UTF-8"); + return new String(readBytes(0, 0), StandardCharsets.UTF_8); } else { return in.readUTF(); } Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleReader.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleReader.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleReader.java (working copy) @@ -30,6 +30,7 @@ import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.Calendar; import java.util.Collections; import java.util.GregorianCalendar; @@ -410,7 +411,7 @@ int len = in.readInt(); byte[] bytes = new byte[len]; in.readFully(bytes); - String stringVal = new String(bytes, "UTF-8"); + String stringVal = new String(bytes, StandardCharsets.UTF_8); // https://issues.apache.org/jira/browse/JCR-3083 if (PropertyType.DATE == entry.getType()) { @@ -518,7 +519,7 @@ } } - String local = new String(readBytes((b & 0x0f) + 1, 0x10), "UTF-8"); + String local = new String(readBytes((b & 0x0f) + 1, 0x10), StandardCharsets.UTF_8); return NameFactoryImpl.getInstance().create(uri, local); } @@ -659,7 +660,7 @@ private String readString() throws IOException { if (version >= BundleBinding.VERSION_3) { - return new String(readBytes(0, 0), "UTF-8"); + return new String(readBytes(0, 0), StandardCharsets.UTF_8); } else { return in.readUTF(); } Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleWriter.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleWriter.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/BundleWriter.java (working copy) @@ -22,6 +22,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; import java.util.Calendar; import java.util.Collection; import java.util.GregorianCalendar; @@ -464,7 +465,7 @@ if (local.length() == 0) { throw new IOException("Attempt to write an empty local name: " + name); } - byte[] bytes = local.getBytes("UTF-8"); + byte[] bytes = local.getBytes(StandardCharsets.UTF_8); int len = Math.min(bytes.length - 1, 0x0f); out.writeByte(0x80 | ns << 4 | len); @@ -707,7 +708,7 @@ * @throws IOException if an I/O error occurs */ private void writeString(String value) throws IOException { - writeBytes(value.getBytes("UTF-8"), 0); + writeBytes(value.getBytes(StandardCharsets.UTF_8), 0); } /** Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/Serializer.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/Serializer.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/util/Serializer.java (working copy) @@ -34,6 +34,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; @@ -53,11 +54,6 @@ }; /** - * encoding used for serializing String values - */ - private static final String ENCODING = "UTF-8"; - - /** * Serializes the specified NodeState object to the given * binary stream. * @@ -224,7 +220,7 @@ * Strings are serialized as */ //out.writeUTF(val.toString()); // value - byte[] bytes = val.toString().getBytes(ENCODING); + byte[] bytes = val.toString().getBytes(StandardCharsets.UTF_8); out.writeInt(bytes.length); // lenght of byte[] out.write(bytes); // byte[] } @@ -298,7 +294,7 @@ int len = in.readInt(); // lenght of byte[] byte[] bytes = new byte[len]; in.readFully(bytes); // byte[] - String s = new String(bytes, ENCODING); + String s = new String(bytes, StandardCharsets.UTF_8); val = InternalValue.valueOf(s, type); } values[i] = val; Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/xml/XMLPersistenceManager.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/xml/XMLPersistenceManager.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/persistence/xml/XMLPersistenceManager.java (working copy) @@ -53,6 +53,7 @@ import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; @@ -544,18 +545,11 @@ OutputStream os = nodeFile.getOutputStream(); Writer writer = null; try { - String encoding = DEFAULT_ENCODING; - try { - writer = new BufferedWriter(new OutputStreamWriter(os, encoding)); - } catch (UnsupportedEncodingException e) { - // should never get here! - OutputStreamWriter osw = new OutputStreamWriter(os); - encoding = osw.getEncoding(); - writer = new BufferedWriter(osw); - } + writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)); String parentId = (state.getParentId() == null) ? "" : state.getParentId().toString(); String encodedNodeType = Text.encodeIllegalXMLCharacters(state.getNodeTypeName().toString()); + String encoding = DEFAULT_ENCODING; writer.write("\n"); writer.write("<" + NODE_ELEMENT + " " + UUID_ATTRIBUTE + "=\"" + id + "\" " @@ -618,15 +612,7 @@ // write property state to xml file Writer writer = null; try { - String encoding = DEFAULT_ENCODING; - try { - writer = new BufferedWriter(new OutputStreamWriter(os, encoding)); - } catch (UnsupportedEncodingException e) { - // should never get here! - OutputStreamWriter osw = new OutputStreamWriter(os); - encoding = osw.getEncoding(); - writer = new BufferedWriter(osw); - } + writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)); String typeName; int type = state.getType(); @@ -637,6 +623,7 @@ throw new ItemStateException("unexpected property-type ordinal: " + type, iae); } + String encoding = DEFAULT_ENCODING; writer.write("\n"); writer.write("<" + PROPERTY_ELEMENT + " " + NAME_ATTRIBUTE + "=\"" + Text.encodeIllegalXMLCharacters(state.getName().toString()) + "\" " @@ -815,15 +802,9 @@ OutputStream os = refsFile.getOutputStream(); BufferedWriter writer = null; try { + writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)); + String encoding = DEFAULT_ENCODING; - try { - writer = new BufferedWriter(new OutputStreamWriter(os, encoding)); - } catch (UnsupportedEncodingException e) { - // should never get here! - OutputStreamWriter osw = new OutputStreamWriter(os); - encoding = osw.getEncoding(); - writer = new BufferedWriter(osw); - } writer.write("\n"); writer.write("<" + NODEREFERENCES_ELEMENT + " " + TARGETID_ATTRIBUTE + "=\"" + refs.getTargetId() + "\">\n"); Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexingQueueStore.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexingQueueStore.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/IndexingQueueStore.java (working copy) @@ -25,6 +25,7 @@ import java.io.InputStream; import java.io.BufferedReader; import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.Set; import java.util.HashSet; @@ -43,11 +44,6 @@ private static final Logger log = LoggerFactory.getLogger(IndexingQueueStore.class); /** - * Encoding of the indexing queue store. - */ - private static final String ENCODING = "UTF-8"; - - /** * Operation identifier for an added node. */ private static final String ADD = "ADD"; @@ -135,7 +131,7 @@ if (dir.fileExists(INDEXING_QUEUE_FILE)) { InputStream in = new IndexInputStream(dir.openInput(INDEXING_QUEUE_FILE)); BufferedReader reader = new BufferedReader( - new InputStreamReader(in, ENCODING)); + new InputStreamReader(in, StandardCharsets.UTF_8)); try { String line; while ((line = reader.readLine()) != null) { Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/PasswordUtility.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/PasswordUtility.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/PasswordUtility.java (working copy) @@ -18,6 +18,7 @@ package org.apache.jackrabbit.core.security.user; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; @@ -127,8 +128,6 @@ } // hashedPassword is plaintext -> return false } catch (NoSuchAlgorithmException e) { log.warn(e.getMessage()); - } catch (UnsupportedEncodingException e) { - log.warn(e.getMessage()); } return false; } @@ -164,7 +163,7 @@ //------------------------------------------------------------< private >--- - private static String generateHash(String pwd, String algorithm, String salt, int iterations) throws NoSuchAlgorithmException, UnsupportedEncodingException { + private static String generateHash(String pwd, String algorithm, String salt, int iterations) throws NoSuchAlgorithmException { StringBuilder passwordHash = new StringBuilder(); passwordHash.append('{').append(algorithm).append('}'); if (salt != null && salt.length() > 0) { @@ -178,7 +177,7 @@ passwordHash.append(generateDigest(data.toString(), algorithm, iterations)); } else { // backwards compatible to jr 2.0: no salt, no iterations - passwordHash.append(Text.digest(algorithm, pwd.getBytes(ENCODING))); + passwordHash.append(Text.digest(algorithm, pwd.getBytes(StandardCharsets.UTF_8))); } return passwordHash.toString(); } @@ -196,8 +195,8 @@ return res.toString(); } - private static String generateDigest(String data, String algorithm, int iterations) throws UnsupportedEncodingException, NoSuchAlgorithmException { - byte[] bytes = data.getBytes(ENCODING); + private static String generateDigest(String data, String algorithm, int iterations) throws NoSuchAlgorithmException { + byte[] bytes = data.getBytes(StandardCharsets.UTF_8); MessageDigest md = MessageDigest.getInstance(algorithm); for (int i = 0; i < iterations; i++) { Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/UserManagerImpl.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/UserManagerImpl.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/user/UserManagerImpl.java (working copy) @@ -52,6 +52,7 @@ import javax.jcr.nodetype.ConstraintViolationException; import javax.jcr.version.VersionException; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.security.NoSuchAlgorithmException; import java.security.Principal; import java.util.HashSet; @@ -1054,13 +1055,9 @@ * @return a new NodeId. * @throws RepositoryException If an error occurs. */ - private NodeId buildNodeId(String id) throws RepositoryException { - try { - UUID uuid = UUID.nameUUIDFromBytes(id.toLowerCase().getBytes("UTF-8")); - return new NodeId(uuid); - } catch (UnsupportedEncodingException e) { - throw new RepositoryException("Unexpected error while build ID hash", e); - } + private NodeId buildNodeId(String id) { + UUID uuid = UUID.nameUUIDFromBytes(id.toLowerCase().getBytes(StandardCharsets.UTF_8)); + return new NodeId(uuid); } /** Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/value/InternalValue.java (working copy) @@ -24,6 +24,7 @@ import java.math.BigDecimal; import java.net.URI; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.util.Calendar; import javax.jcr.Binary; @@ -214,11 +215,7 @@ throws RepositoryException { switch (value.getType()) { case PropertyType.BINARY: - try { - return create(value.getString().getBytes(AbstractQValueFactory.DEFAULT_ENCODING)); - } catch (UnsupportedEncodingException e) { - throw new InternalError(AbstractQValueFactory.DEFAULT_ENCODING + " not supported"); - } + return create(value.getString().getBytes(StandardCharsets.UTF_8)); case PropertyType.BOOLEAN: return new InternalValue(value.getBoolean()); case PropertyType.DATE: @@ -661,12 +658,8 @@ if (type == PropertyType.BINARY) { return ((Binary) val).getStream(); } else { - try { - // convert via string - return new ByteArrayInputStream(getString().getBytes(InternalValueFactory.DEFAULT_ENCODING)); - } catch (UnsupportedEncodingException e) { - throw new RepositoryException(InternalValueFactory.DEFAULT_ENCODING + " is not supported encoding on this platform", e); - } + // convert via string + return new ByteArrayInputStream(getString().getBytes(StandardCharsets.UTF_8)); } } @@ -680,13 +673,9 @@ // affecting this value return ((BLOBFileValue) val).copy(); } else { - try { - // convert via string - byte[] data = getString().getBytes(InternalValueFactory.DEFAULT_ENCODING); - return BLOBInMemory.getInstance(data); - } catch (UnsupportedEncodingException e) { - throw new RepositoryException(InternalValueFactory.DEFAULT_ENCODING + " is not supported encoding on this platform", e); - } + // convert via string + byte[] data = getString().getBytes(StandardCharsets.UTF_8); + return BLOBInMemory.getInstance(data); } } Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/BufferedStringValue.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/BufferedStringValue.java (revision 1800801) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/xml/BufferedStringValue.java (working copy) @@ -45,6 +45,7 @@ import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; +import java.nio.charset.StandardCharsets; /** * BufferedStringValue represents an appendable @@ -122,7 +123,7 @@ if (base64) { ByteArrayOutputStream out = new ByteArrayOutputStream(); Base64.decode(value, out); - value = new String(out.toByteArray(), "UTF-8"); + value = new String(out.toByteArray(), StandardCharsets.UTF_8); } return value; } @@ -161,7 +162,7 @@ private Reader openReader() throws IOException { return new InputStreamReader( - new BufferedInputStream(new FileInputStream(tmpFile)), "UTF-8"); + new BufferedInputStream(new FileInputStream(tmpFile)), StandardCharsets.UTF_8); } /** @@ -199,7 +200,7 @@ TransientFileFactory fileFactory = TransientFileFactory.getInstance(); tmpFile = fileFactory.createTransientFile("txt", null, null); BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(tmpFile)); - writer = new OutputStreamWriter(fout, "UTF-8"); + writer = new OutputStreamWriter(fout, StandardCharsets.UTF_8); writer.write(buffer.toString()); writer.write(chars, start, len); // reset the in-memory buffer Index: jackrabbit-data/src/main/java/org/apache/jackrabbit/core/data/AbstractDataStore.java =================================================================== --- jackrabbit-data/src/main/java/org/apache/jackrabbit/core/data/AbstractDataStore.java (revision 1800801) +++ jackrabbit-data/src/main/java/org/apache/jackrabbit/core/data/AbstractDataStore.java (working copy) @@ -16,6 +16,7 @@ */ package org.apache.jackrabbit.core.data; +import java.nio.charset.StandardCharsets; import java.security.SecureRandom; import javax.crypto.Mac; @@ -98,7 +99,7 @@ Mac mac = Mac.getInstance(ALGORITHM); mac.init(new SecretKeySpec(getReferenceKey(), ALGORITHM)); - byte[] hash = mac.doFinal(id.getBytes("UTF-8")); + byte[] hash = mac.doFinal(id.getBytes(StandardCharsets.UTF_8)); return id + ':' + encodeHexString(hash); } catch (Exception e) { Index: jackrabbit-data/src/main/java/org/apache/jackrabbit/core/data/CachingDataStore.java =================================================================== --- jackrabbit-data/src/main/java/org/apache/jackrabbit/core/data/CachingDataStore.java (revision 1800801) +++ jackrabbit-data/src/main/java/org/apache/jackrabbit/core/data/CachingDataStore.java (working copy) @@ -23,6 +23,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.lang.ref.WeakReference; import java.security.DigestOutputStream; import java.security.MessageDigest; @@ -680,12 +681,8 @@ } @Override - protected byte[] getOrCreateReferenceKey() throws DataStoreException { - try { - return secret.getBytes("UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new DataStoreException(e); - } + protected byte[] getOrCreateReferenceKey() { + return secret.getBytes(StandardCharsets.UTF_8); } public Set getPendingUploads() { Index: jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ToXmlContentHandler.java =================================================================== --- jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ToXmlContentHandler.java (revision 1800801) +++ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/xml/ToXmlContentHandler.java (working copy) @@ -22,6 +22,7 @@ import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; +import java.nio.charset.StandardCharsets; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; @@ -98,13 +99,8 @@ * @param stream XML output stream */ public ToXmlContentHandler(OutputStream stream) { - try { - this.writer = new OutputStreamWriter(stream, "UTF-8"); - this.declaration = "version=\"1.0\" encoding=\"UTF-8\""; - } catch (UnsupportedEncodingException e) { - // should never happen - throw new IllegalStateException("UTF-8 is not supported!"); - } + this.writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8); + this.declaration = "version=\"1.0\" encoding=\"UTF-8\""; } /** Index: jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Base64.java =================================================================== --- jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Base64.java (revision 1800801) +++ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Base64.java (working copy) @@ -25,6 +25,7 @@ import java.io.Reader; import java.io.StringWriter; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.io.BufferedWriter; /** @@ -32,9 +33,6 @@ */ public class Base64 { - // charset used for base64 encoded data (7-bit ASCII) - private static final String CHARSET = "US-ASCII"; - // encoding table (the 64 valid base64 characters) private static final char[] BASE64CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray(); @@ -168,7 +166,7 @@ */ public static void encode(InputStream in, OutputStream out) throws IOException { - Writer writer = new BufferedWriter(new OutputStreamWriter(out, CHARSET)); + Writer writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.US_ASCII)); try { encode(in, writer); } finally { @@ -239,7 +237,7 @@ public static String encode(String data) { try { StringWriter buffer = new StringWriter(); - byte[] b = data.getBytes("UTF-8"); + byte[] b = data.getBytes(StandardCharsets.UTF_8); encode(b, 0, b.length, buffer); return buffer.toString(); } catch (IOException e) { // should never happen @@ -261,7 +259,7 @@ try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); decode(data, buffer); - return new String(buffer.toByteArray(), "UTF-8"); + return new String(buffer.toByteArray(), StandardCharsets.UTF_8); } catch (IllegalArgumentException e) { return data; } catch (IOException e) { // should never happen @@ -296,7 +294,7 @@ */ public static void decode(InputStream in, OutputStream out) throws IOException { - decode(new InputStreamReader(in, CHARSET), out); + decode(new InputStreamReader(in, StandardCharsets.US_ASCII), out); } /** Index: jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java =================================================================== --- jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java (revision 1800801) +++ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java (working copy) @@ -18,6 +18,7 @@ import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; @@ -350,24 +351,20 @@ * @throws NullPointerException if string is null. */ public static String escape(String string, char escape, boolean isPath) { - try { - BitSet validChars = isPath ? URISaveEx : URISave; - byte[] bytes = string.getBytes("utf-8"); - StringBuilder out = new StringBuilder(bytes.length); - for (byte aByte : bytes) { - int c = aByte & 0xff; - if (validChars.get(c) && c != escape) { - out.append((char) c); - } else { - out.append(escape); - out.append(hexTable[(c >> 4) & 0x0f]); - out.append(hexTable[(c) & 0x0f]); - } + BitSet validChars = isPath ? URISaveEx : URISave; + byte[] bytes = string.getBytes(StandardCharsets.UTF_8); + StringBuilder out = new StringBuilder(bytes.length); + for (byte aByte : bytes) { + int c = aByte & 0xff; + if (validChars.get(c) && c != escape) { + out.append((char) c); + } else { + out.append(escape); + out.append(hexTable[(c >> 4) & 0x0f]); + out.append(hexTable[(c) & 0x0f]); } - return out.toString(); - } catch (UnsupportedEncodingException e) { - throw new InternalError(e.toString()); } + return out.toString(); } /** @@ -413,31 +410,26 @@ * escape character */ public static String unescape(String string, char escape) { - try { - byte[] utf8 = string.getBytes("utf-8"); + byte[] utf8 = string.getBytes(StandardCharsets.UTF_8); - // Check whether escape occurs at invalid position - if ((utf8.length >= 1 && utf8[utf8.length - 1] == escape) || - (utf8.length >= 2 && utf8[utf8.length - 2] == escape)) { - throw new IllegalArgumentException("Premature end of escape sequence at end of input"); - } + // Check whether escape occurs at invalid position + if ((utf8.length >= 1 && utf8[utf8.length - 1] == escape) || + (utf8.length >= 2 && utf8[utf8.length - 2] == escape)) { + throw new IllegalArgumentException("Premature end of escape sequence at end of input"); + } - ByteArrayOutputStream out = new ByteArrayOutputStream(utf8.length); - for (int k = 0; k < utf8.length; k++) { - byte b = utf8[k]; - if (b == escape) { - out.write((decodeDigit(utf8[++k]) << 4) + decodeDigit(utf8[++k])); - } - else { - out.write(b); - } + ByteArrayOutputStream out = new ByteArrayOutputStream(utf8.length); + for (int k = 0; k < utf8.length; k++) { + byte b = utf8[k]; + if (b == escape) { + out.write((decodeDigit(utf8[++k]) << 4) + decodeDigit(utf8[++k])); } + else { + out.write(b); + } + } - return new String(out.toByteArray(), "utf-8"); - } - catch (UnsupportedEncodingException e) { - throw new InternalError(e.toString()); - } + return new String(out.toByteArray(), StandardCharsets.UTF_8); } /** Index: jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/BaseValue.java =================================================================== --- jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/BaseValue.java (revision 1800801) +++ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/BaseValue.java (working copy) @@ -27,6 +27,7 @@ import java.io.UnsupportedEncodingException; import java.io.IOException; import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; import java.util.Calendar; /** @@ -156,14 +157,9 @@ return stream; } - try { - // convert via string - stream = new ByteArrayInputStream(getInternalString().getBytes(DEFAULT_ENCODING)); - return stream; - } catch (UnsupportedEncodingException e) { - throw new RepositoryException(DEFAULT_ENCODING - + " not supported on this platform", e); - } + // convert via string + stream = new ByteArrayInputStream(getInternalString().getBytes(StandardCharsets.UTF_8)); + return stream; } /** @@ -174,10 +170,7 @@ RepositoryException { try { // convert via string - return new BinaryImpl(new ByteArrayInputStream(getInternalString().getBytes(DEFAULT_ENCODING))); - } catch (UnsupportedEncodingException e) { - throw new RepositoryException(DEFAULT_ENCODING - + " not supported on this platform", e); + return new BinaryImpl(new ByteArrayInputStream(getInternalString().getBytes(StandardCharsets.UTF_8))); } catch (IOException e) { throw new RepositoryException("failed to create Binary instance", e); } Index: jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/BinaryValue.java =================================================================== --- jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/BinaryValue.java (revision 1800801) +++ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/BinaryValue.java (working copy) @@ -24,6 +24,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; /** * A BinaryValue provides an implementation @@ -134,10 +135,7 @@ try { byte[] bytes = new byte[(int) bin.getSize()]; bin.read(bytes, 0); - text = new String(bytes, DEFAULT_ENCODING); - } catch (UnsupportedEncodingException e) { - throw new RepositoryException(DEFAULT_ENCODING - + " not supported on this platform", e); + text = new String(bytes, StandardCharsets.UTF_8); } catch (IOException e) { throw new RepositoryException("failed to retrieve binary data", e); } @@ -156,12 +154,7 @@ if (bin != null) { stream = bin.getStream(); } else { - try { - stream = new ByteArrayInputStream(text.getBytes(DEFAULT_ENCODING)); - } catch (UnsupportedEncodingException e) { - throw new RepositoryException(DEFAULT_ENCODING - + " not supported on this platform", e); - } + stream = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)); } } @@ -177,10 +170,7 @@ if (bin == null) { try { - bin = new BinaryImpl(new ByteArrayInputStream(text.getBytes(DEFAULT_ENCODING))); - } catch (UnsupportedEncodingException e) { - throw new RepositoryException(DEFAULT_ENCODING - + " not supported on this platform", e); + bin = new BinaryImpl(new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8))); } catch (IOException e) { throw new RepositoryException("failed to retrieve binary data", e); } Index: jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/ValueHelper.java =================================================================== --- jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/ValueHelper.java (revision 1800801) +++ jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/value/ValueHelper.java (working copy) @@ -52,6 +52,7 @@ import java.io.OutputStream; import java.io.BufferedOutputStream; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -738,7 +739,7 @@ } else { String textVal = value.getString(); if (enforceBase64) { - byte bytes[] = textVal.getBytes("UTF-8"); + byte bytes[] = textVal.getBytes(StandardCharsets.UTF_8); Base64.encode(bytes, 0, bytes.length, writer); } else { Index: jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/value/AbstractValue.java =================================================================== --- jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/value/AbstractValue.java (revision 1800801) +++ jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/value/AbstractValue.java (working copy) @@ -23,6 +23,7 @@ import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; import java.util.Calendar; import javax.jcr.Binary; @@ -97,31 +98,27 @@ * by {@link #getString()}. Subclasses */ public Binary getBinary() throws RepositoryException { - try { - final byte[] value = getString().getBytes("UTF-8"); - return new Binary() { - public int read(byte[] b, long position) { - if (position >= value.length) { - return -1; - } else { - int p = (int) position; - int n = Math.min(b.length, value.length - p); - System.arraycopy(value, p, b, 0, n); - return n; - } + final byte[] value = getString().getBytes(StandardCharsets.UTF_8); + return new Binary() { + public int read(byte[] b, long position) { + if (position >= value.length) { + return -1; + } else { + int p = (int) position; + int n = Math.min(b.length, value.length - p); + System.arraycopy(value, p, b, 0, n); + return n; } - public InputStream getStream() { - return new ByteArrayInputStream(value); - } - public long getSize() { - return value.length; - } - public void dispose() { - } - }; - } catch (UnsupportedEncodingException e) { - throw new RepositoryException("UTF-8 is not supported", e); - } + } + public InputStream getStream() { + return new ByteArrayInputStream(value); + } + public long getSize() { + return value.length; + } + public void dispose() { + } + }; } /** Index: jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/value/BinaryValue.java =================================================================== --- jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/value/BinaryValue.java (revision 1800801) +++ jackrabbit-jcr-rmi/src/main/java/org/apache/jackrabbit/rmi/value/BinaryValue.java (working copy) @@ -22,6 +22,7 @@ import java.io.Reader; import java.io.Serializable; import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; import java.util.Calendar; import javax.jcr.Binary; @@ -72,7 +73,7 @@ try { InputStream stream = value.getStream(); try { - Reader reader = new InputStreamReader(stream, "UTF-8"); + Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8); StringBuilder builder = new StringBuilder(); char[] buffer = new char[1024]; int n = reader.read(buffer); Index: jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/Base64.java =================================================================== --- jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/Base64.java (revision 1800801) +++ jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/Base64.java (working copy) @@ -23,6 +23,7 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; +import java.nio.charset.StandardCharsets; /** * Base64 provides Base64 encoding/decoding of strings and streams. @@ -30,8 +31,6 @@ * class to not introduce a dependency to jackrabbit from the test classes. */ public class Base64 { - // charset used for base64 encoded data (7-bit ASCII) - private static final String CHARSET = "US-ASCII"; // encoding table (the 64 valid base64 characters) private static final char[] BASE64CHARS = @@ -111,7 +110,7 @@ */ public static void encode(InputStream in, OutputStream out) throws IOException { - Writer writer = new OutputStreamWriter(out, CHARSET); + Writer writer = new OutputStreamWriter(out, StandardCharsets.US_ASCII); encode(in, writer); } @@ -185,7 +184,7 @@ * @param out stream where the decoded data should be written to */ public static void decode(InputStream in, OutputStream out) throws IOException { - decode(new InputStreamReader(in, CHARSET), out); + decode(new InputStreamReader(in, StandardCharsets.US_ASCII), out); } /** Index: jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/BooleanPropertyTest.java =================================================================== --- jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/BooleanPropertyTest.java (revision 1800801) +++ jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/BooleanPropertyTest.java (working copy) @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.BufferedInputStream; import java.io.InputStream; +import java.nio.charset.StandardCharsets; /** * Tests a boolean property. If the workspace does not contain a node with a @@ -121,7 +122,7 @@ BufferedInputStream in = new BufferedInputStream(val.getStream()); Value otherVal = PropertyUtil.getValue(prop); InputStream ins = null; - byte[] utf8bytes = otherVal.getString().getBytes(UTF8); + byte[] utf8bytes = otherVal.getString().getBytes(StandardCharsets.UTF_8); // if yet utf-8 encoded these bytes should be equal // to the ones received from the stream int i = 0; Index: jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/AbstractQValue.java =================================================================== --- jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/AbstractQValue.java (revision 1800801) +++ jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/AbstractQValue.java (working copy) @@ -37,6 +37,7 @@ import java.io.Serializable; import java.io.StringWriter; import java.io.Writer; +import java.nio.charset.StandardCharsets; /** * AbstractQValue... @@ -345,7 +346,7 @@ try { InputStream stream = getStream(); try { - Reader reader = new InputStreamReader(stream, "UTF-8"); + Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8); Writer writer = new StringWriter(); char[] buffer = new char[1024]; int n = reader.read(buffer); Index: jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/AbstractQValueFactory.java =================================================================== --- jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/AbstractQValueFactory.java (revision 1800801) +++ jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/AbstractQValueFactory.java (working copy) @@ -20,8 +20,9 @@ import java.util.UUID; import java.math.BigDecimal; import java.net.URI; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; + import javax.jcr.PropertyType; import javax.jcr.RepositoryException; import javax.jcr.ValueFormatException; @@ -42,12 +43,12 @@ * AbstractQValueFactory... */ public abstract class AbstractQValueFactory implements QValueFactory { - /** - * the default encoding + * the default encoding - unused */ public static final String DEFAULT_ENCODING = "UTF-8"; + protected static final PathFactory PATH_FACTORY = PathFactoryImpl.getInstance(); protected static final NameFactory NAME_FACTORY = NameFactoryImpl.getInstance(); @@ -108,14 +109,12 @@ case PropertyType.WEAKREFERENCE: return createReference(value, true); case PropertyType.BINARY: - return create(value.getBytes(DEFAULT_ENCODING)); + return create(value.getBytes(StandardCharsets.UTF_8)); // default: invalid type specified -> see below. } } catch (IllegalArgumentException ex) { // given String value cannot be converted to Long/Double/Path/Name throw new ValueFormatException(ex); - } catch (UnsupportedEncodingException ex) { - throw new RepositoryException(ex); } // invalid type specified: Index: jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/DefaultQValue.java =================================================================== --- jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/DefaultQValue.java (revision 1800801) +++ jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/DefaultQValue.java (working copy) @@ -22,6 +22,7 @@ import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.Calendar; import javax.jcr.RepositoryException; @@ -83,13 +84,8 @@ * @see QValue#getStream() */ public InputStream getStream() throws RepositoryException { - try { - // convert via string - return new ByteArrayInputStream(getString().getBytes( - AbstractQValueFactory.DEFAULT_ENCODING)); - } catch (UnsupportedEncodingException e) { - throw new RepositoryException(QValueFactoryImpl.DEFAULT_ENCODING + - " is not supported encoding on this platform", e); - } + // convert via string + return new ByteArrayInputStream(getString().getBytes( + StandardCharsets.UTF_8)); } } Index: jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueValue.java =================================================================== --- jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueValue.java (revision 1800801) +++ jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/value/QValueValue.java (working copy) @@ -21,6 +21,7 @@ import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; import java.util.Calendar; import javax.jcr.Binary; @@ -105,31 +106,28 @@ if (getType() == PropertyType.NAME || getType() == PropertyType.PATH) { // qualified name/path value needs to be resolved, // delegate conversion to getString() method - try { - final byte[] value = getString().getBytes("UTF-8"); - return new Binary() { - public int read(byte[] b, long position) { - if (position >= value.length) { - return -1; - } else { - int p = (int) position; - int n = Math.min(b.length, value.length - p); - System.arraycopy(value, p, b, 0, n); - return n; - } + + final byte[] value = getString().getBytes(StandardCharsets.UTF_8); + return new Binary() { + public int read(byte[] b, long position) { + if (position >= value.length) { + return -1; + } else { + int p = (int) position; + int n = Math.min(b.length, value.length - p); + System.arraycopy(value, p, b, 0, n); + return n; } - public InputStream getStream() { - return new ByteArrayInputStream(value); - } - public long getSize() { - return value.length; - } - public void dispose() { - } - }; - } catch (UnsupportedEncodingException ex) { - throw new RepositoryException("UTF-8 is not supported", ex); - } + } + public InputStream getStream() { + return new ByteArrayInputStream(value); + } + public long getSize() { + return value.length; + } + public void dispose() { + } + }; } else { return qvalue.getBinary(); } @@ -163,11 +161,7 @@ if (stream == null) { if (getType() == PropertyType.NAME || getType() == PropertyType.PATH) { // qualified name/path value needs to be resolved - try { - stream = new ByteArrayInputStream(getString().getBytes("UTF-8")); - } catch (UnsupportedEncodingException ex) { - throw new RepositoryException("UTF-8 is not supported", ex); - } + stream = new ByteArrayInputStream(getString().getBytes(StandardCharsets.UTF_8)); } else { stream = qvalue.getStream(); } Index: jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/WebdavResponseImpl.java =================================================================== --- jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/WebdavResponseImpl.java (revision 1800801) +++ jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/WebdavResponseImpl.java (working copy) @@ -42,6 +42,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.util.Locale; /** @@ -141,7 +142,7 @@ // JCR-2636: Need to use an explicit OutputStreamWriter // instead of relying on the built-in UTF-8 serialization // to avoid problems with surrogate pairs on Sun JRE 1.5. - Writer writer = new OutputStreamWriter(out, "UTF-8"); + Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8); DomUtil.transformDocument(doc, writer); writer.flush(); Index: jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/util/EncodeUtil.java =================================================================== --- jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/util/EncodeUtil.java (revision 1800801) +++ jackrabbit-webdav/src/main/java/org/apache/jackrabbit/webdav/util/EncodeUtil.java (working copy) @@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.BitSet; /** @@ -126,24 +127,20 @@ * @throws NullPointerException if string is null. */ private static String escape(String string, char escape, boolean isPath) { - try { - BitSet validChars = isPath ? URISaveEx : URISave; - byte[] bytes = string.getBytes("utf-8"); - StringBuffer out = new StringBuffer(bytes.length); - for (byte aByte : bytes) { - int c = aByte & 0xff; - if (validChars.get(c) && c != escape) { - out.append((char) c); - } else { - out.append(escape); - out.append(hexTable[(c >> 4) & 0x0f]); - out.append(hexTable[(c) & 0x0f]); - } + BitSet validChars = isPath ? URISaveEx : URISave; + byte[] bytes = string.getBytes(StandardCharsets.UTF_8); + StringBuffer out = new StringBuffer(bytes.length); + for (byte aByte : bytes) { + int c = aByte & 0xff; + if (validChars.get(c) && c != escape) { + out.append((char) c); + } else { + out.append(escape); + out.append(hexTable[(c >> 4) & 0x0f]); + out.append(hexTable[(c) & 0x0f]); } - return out.toString(); - } catch (UnsupportedEncodingException e) { - throw new InternalError(e.toString()); } + return out.toString(); } /** @@ -178,31 +175,26 @@ * escape character */ private static String unescape(String string, char escape) { - try { - byte[] utf8 = string.getBytes("utf-8"); + byte[] utf8 = string.getBytes(StandardCharsets.UTF_8); - // Check whether escape occurs at invalid position - if ((utf8.length >= 1 && utf8[utf8.length - 1] == escape) || - (utf8.length >= 2 && utf8[utf8.length - 2] == escape)) { - throw new IllegalArgumentException("Premature end of escape sequence at end of input"); - } + // Check whether escape occurs at invalid position + if ((utf8.length >= 1 && utf8[utf8.length - 1] == escape) || + (utf8.length >= 2 && utf8[utf8.length - 2] == escape)) { + throw new IllegalArgumentException("Premature end of escape sequence at end of input"); + } - ByteArrayOutputStream out = new ByteArrayOutputStream(utf8.length); - for (int k = 0; k < utf8.length; k++) { - byte b = utf8[k]; - if (b == escape) { - out.write((decodeDigit(utf8[++k]) << 4) + decodeDigit(utf8[++k])); - } - else { - out.write(b); - } + ByteArrayOutputStream out = new ByteArrayOutputStream(utf8.length); + for (int k = 0; k < utf8.length; k++) { + byte b = utf8[k]; + if (b == escape) { + out.write((decodeDigit(utf8[++k]) << 4) + decodeDigit(utf8[++k])); } + else { + out.write(b); + } + } - return new String(out.toByteArray(), "utf-8"); - } - catch (UnsupportedEncodingException e) { - throw new InternalError(e.toString()); - } + return new String(out.toByteArray(), StandardCharsets.UTF_8); } private static byte decodeDigit(byte b) {