1 /*
   2  * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @bug 6379808
  26  * @summary Check all Cp933 SBCS characters are not supported in Cp834
  27  * @modules jdk.charsets
  28  */
  29 
  30 import java.io.*;
  31 import java.nio.*;
  32 import java.nio.charset.*;
  33 
  34 public class TestCp834_SBCS {
  35     public static void main(String args[]) throws Exception {
  36         // The correctness of 1:1 mapping is Coverted by CoderTest.java
  37         // and TestConv.java, we only need to verify that SBCS characters
  38         // are not supported by this charset.
  39         CharsetEncoder enc834 = Charset.forName("Cp834")
  40                                        .newEncoder()
  41                                        .onUnmappableCharacter(CodingErrorAction.REPLACE)
  42                                        .onMalformedInput(CodingErrorAction.REPLACE);
  43 
  44         CharsetDecoder dec834 = Charset.forName("Cp834")
  45                                        .newDecoder()
  46                                        .onUnmappableCharacter(CodingErrorAction.REPLACE)
  47                                        .onMalformedInput(CodingErrorAction.REPLACE);
  48 
  49         CharsetDecoder dec933 = Charset.forName("Cp933")
  50                                        .newDecoder()
  51                                        .onUnmappableCharacter(CodingErrorAction.REPLACE)
  52                                        .onMalformedInput(CodingErrorAction.REPLACE);
  53         byte[] ba = new byte[1];
  54         byte[] ba2 = new byte[2];
  55         ByteBuffer dbb = ByteBuffer.allocateDirect(10);
  56         char[] ca = new char[1];
  57         char c;
  58         for (int i = 0; i <= 0xff; i++) {
  59             if (i != 0xe && i != 0xf) {   // no SI/SO
  60                 ba[0] = (byte)i;
  61                 CharBuffer cb = dec933.decode(ByteBuffer.wrap(ba));
  62                 if ((c = cb.get()) != '\ufffd') {
  63                     // OK, this is a SBCS character in Cp933
  64                     if (dec834.decode(ByteBuffer.wrap(ba)).get() != '\ufffd')
  65                         throw new Exception("SBCS is supported in IBM834 decoder");
  66 
  67                     if (enc834.canEncode(c))
  68                         throw new Exception("SBCS can be encoded in IBM834 encoder");
  69 
  70                     ca[0] = c;
  71                     ByteBuffer bb = enc834.encode(CharBuffer.wrap(ca));
  72                     if (bb.get() != (byte)0xfe || bb.get() != (byte)0xfe)
  73                         throw new Exception("SBCS is supported in IBM834 encoder");
  74                 }
  75             }
  76         }
  77     }
  78 }