@@ -0,0 +1,3020 @@
+/*%0A* Licensed to the Apache Software Foundation (ASF) under one or more%0A* contributor license agreements.  See the NOTICE file distributed with%0A* this work for additional information regarding copyright ownership.%0A* The ASF licenses this file to You under the Apache License, Version 2.0%0A* (the %22License%22); you may not use this file except in compliance with%0A* the License.  You may obtain a copy of the License at%0A*%0A*     http://www.apache.org/licenses/LICENSE-2.0%0A*%0A* Unless required by applicable law or agreed to in writing, software%0A* distributed under the License is distributed on an %22AS IS%22 BASIS,%0A* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A* See the License for the specific language governing permissions and%0A* limitations under the License.%0A*/%0Apackage org.apache.jackrabbit.oak.plugins.document.util;%0A%0Aimport java.io.IOException;%0Aimport java.nio.ByteBuffer;%0Aimport java.nio.CharBuffer;%0Aimport java.nio.charset.CharsetEncoder;%0Aimport java.nio.charset.CodingErrorAction;%0A%0Aimport java.nio.charset.StandardCharsets;%0A%0A/**%0A * Utility class related to encoding characters into (UTF-8) byte sequences.%0A */%0Apublic class UTF8Encoder %7B%0A%0A    private UTF8Encoder() %7B%0A    %7D%0A%0A    /**%0A     * Get an encoder suitable for UTF-8, using the proper error handling flags.%0A     * %0A     * Note that the instance is kept in a thread local to avoid the cost of%0A     * constructing an encoder for every call. This actually mimics the%0A     * internals of %7B@link String#getBytes(String)%7D.%0A     */%0A    private static ThreadLocal%3CCharsetEncoder%3E CSE = new ThreadLocal%3CCharsetEncoder%3E() %7B%0A        @Override%0A        protected CharsetEncoder initialValue() %7B%0A            CharsetEncoder e = StandardCharsets.UTF_8.newEncoder();%0A            e.onUnmappableCharacter(CodingErrorAction.REPORT);%0A            e.onMalformedInput(CodingErrorAction.REPORT);%0A            return e;%0A        %7D%0A    %7D;%0A%0A    /**%0A     * Like %7B@link String#getBytes(java.nio.charset.Charset)%7D (with %22UTF-8%22),%0A     * except that invalid character sequences (such as unpaired surrogates) are%0A     * reported as exceptions (see %7B@link CodingErrorAction#REPORT%7D, instead of%0A     * being silently replaced by a replacement character as it would happen%0A     * otherwise.%0A     * %0A     * @param input%0A     *            String to encode%0A     * @return String encoded using %7B@link StandardCharsets#UTF_8%7D%0A     * @throws IOException%0A     *             on encoding error%0A     */%0A    public static byte%5B%5D encodeAsByteArray(String input) throws IOException %7B%0A        CharsetEncoder e = CSE.get();%0A        e.reset();%0A        return bytes(e.encode(CharBuffer.wrap(input.toCharArray())));%0A    %7D%0A%0A    /**%0A     * @see %7B@link CharsetEncoder#canEncode(CharSequence)%0A     */%0A    public static boolean canEncode(CharSequence input) %7B%0A        CharsetEncoder e = CSE.get();%0A        e.reset();%0A        return e.canEncode(input);%0A    %7D%0A%0A    private static byte%5B%5D bytes(ByteBuffer b) %7B%0A        byte%5B%5D a = new byte%5Bb.remaining()%5D;%0A        b.get(a);%0A        return a;%0A    %7D%0A%7D%0A

Create charset encoding utility that detects malformed input