Path: blob/master/src/java.base/share/classes/sun/util/PropertyResourceBundleCharset.java
41152 views
/*1* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.util;2627import sun.nio.cs.ISO_8859_1;28import sun.nio.cs.UTF_8;2930import java.nio.ByteBuffer;31import java.nio.CharBuffer;32import java.nio.charset.Charset;33import java.nio.charset.CharsetDecoder;34import java.nio.charset.CharsetEncoder;35import java.nio.charset.CoderResult;36import java.nio.charset.CodingErrorAction;37import java.nio.charset.StandardCharsets;38import java.util.Objects;3940/**41* A Charset implementation for reading PropertyResourceBundle, in order42* for loading properties files. This first tries to load the properties43* file with UTF-8 encoding). If it fails, then load the file with ISO-8859-144*/45public class PropertyResourceBundleCharset extends Charset {4647private boolean strictUTF8 = false;4849public PropertyResourceBundleCharset(boolean strictUTF8) {50this(PropertyResourceBundleCharset.class.getCanonicalName(), null);51this.strictUTF8 = strictUTF8;52}5354public PropertyResourceBundleCharset(String canonicalName, String[] aliases) {55super(canonicalName, aliases);56}5758@Override59public boolean contains(Charset cs) {60return false;61}6263@Override64public CharsetDecoder newDecoder() {65return new PropertiesFileDecoder(this, 1.0f, 1.0f);66}6768@Override69public CharsetEncoder newEncoder() {70throw new UnsupportedOperationException("Encoding is not supported");71}7273private final class PropertiesFileDecoder extends CharsetDecoder {7475private CharsetDecoder cdUTF_8 = UTF_8.INSTANCE.newDecoder()76.onMalformedInput(CodingErrorAction.REPORT)77.onUnmappableCharacter(CodingErrorAction.REPORT);78private CharsetDecoder cdISO_8859_1 = null;7980protected PropertiesFileDecoder(Charset cs,81float averageCharsPerByte, float maxCharsPerByte) {82super(cs, averageCharsPerByte, maxCharsPerByte);83}8485protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {86if (Objects.nonNull(cdISO_8859_1)) {87return cdISO_8859_1.decode(in, out, false);88}89in.mark();90out.mark();9192CoderResult cr = cdUTF_8.decode(in, out, false);93if (cr.isUnderflow() || cr.isOverflow() ||94PropertyResourceBundleCharset.this.strictUTF8) {95return cr;96}9798// Invalid or unmappable UTF-8 sequence detected.99// Switching to the ISO 8859-1 decorder.100assert cr.isMalformed() || cr.isUnmappable();101in.reset();102out.reset();103cdISO_8859_1 = ISO_8859_1.INSTANCE.newDecoder();104return cdISO_8859_1.decode(in, out, false);105}106}107}108109110