Java Byte Array Encode encodeDataWithCharsetInMetaTag(byte[] data)

Here you can find the source of encodeDataWithCharsetInMetaTag(byte[] data)

Description

encode Data With Charset In Meta Tag

License

Open Source License

Declaration

private static String encodeDataWithCharsetInMetaTag(byte[] data) throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/**/*from  w  ww  .j a va  2 s .  co m*/
 *
 * Copyright (C) 2011-2017 KSFX. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /** default character encoding (ISO-8859-1) */
    public static final String DEFAULT_CHARACTER_ENCODING = "windows-1252";
    private static Pattern characterEncodingPattern = Pattern.compile("charset=([.[^; ]]*)\"");
    private static Logger logger = Logger.getLogger("");

    private static String encodeDataWithCharsetInMetaTag(byte[] data) throws UnsupportedEncodingException {
        Matcher m = characterEncodingPattern.matcher(new String(data, "ISO-8859-1"));
        if (m.find()) {
            String foundCharset = m.group(1);
            if (foundCharset.toLowerCase().equals("iso-8859-1")) {
                logger.info("ISO-8859-1 found in meta tag, usind windows-1252 instead");
                foundCharset = DEFAULT_CHARACTER_ENCODING;
            }
            return new String(data, foundCharset);
        } else {
            throw new UnsupportedEncodingException();
        }
    }
}

Related

  1. encodeAsUtf8(byte[] bytes)
  2. encodeBitString(byte[] in, OutputStream os)
  3. encodeByteArray(DataOutputStream out, byte[] bytes)
  4. encodeBytes(byte[] source, int off, int len)
  5. encodeCompositeAlways(List bytesList)
  6. encodeHex(byte[] bytes)
  7. encodeHex(byte[] bytes)