jdk/test/sun/nio/cs/TestISCII91.java
changeset 10372 2f6d68f22eae
parent 10321 64f7ee2f31dd
parent 10371 7da2112e4236
child 10373 d4c5e59b82f8
equal deleted inserted replaced
10321:64f7ee2f31dd 10372:2f6d68f22eae
     1 /*
       
     2  * Copyright (c) 2008, 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 6431650
       
    26    @summary Check charset ISCII91 and C2B/B2CISCII91 yield same encoding/decoding result
       
    27  */
       
    28 
       
    29 
       
    30 import java.nio.*;
       
    31 import java.nio.charset.*;
       
    32 import sun.io.*;
       
    33 
       
    34 public class TestISCII91 {
       
    35     public static void main(String[] args) throws Throwable{
       
    36         CharToByteConverter c2b = new CharToByteISCII91();
       
    37         ByteToCharConverter b2c = new ByteToCharISCII91();
       
    38         Charset cs = Charset.forName("ISCII91");
       
    39         String charsToEncode = getCharsForEncoding("ISCII91");
       
    40 
       
    41         byte [] c2bBytes = c2b.convertAll(charsToEncode.toCharArray());
       
    42         byte [] csBytes = cs.encode(charsToEncode).array();
       
    43         for (int i = 0; i < c2bBytes.length; ++i) {
       
    44             if (c2bBytes[i] != csBytes[i])
       
    45                 throw new RuntimeException("ISCII91 encoding failed!");
       
    46         }
       
    47 
       
    48         char[] c2bChars = b2c.convertAll(c2bBytes);
       
    49         char[] csChars = cs.decode(ByteBuffer.wrap(csBytes)).array();
       
    50         for (int i = 0; i < c2bChars.length; ++i) {
       
    51             if (c2bChars[i] != csChars[i])
       
    52                 throw new RuntimeException("ISCII91 decoding failed!");
       
    53         }
       
    54     }
       
    55 
       
    56 
       
    57     static String getCharsForEncoding(String encodingName)
       
    58         throws CharacterCodingException{
       
    59         Charset set = Charset.forName(encodingName);
       
    60         CharBuffer chars = CharBuffer.allocate(300);
       
    61         CharsetEncoder encoder = set.newEncoder();
       
    62         for (int c = 0; chars.remaining() > 0 && c < Character.MAX_VALUE; ++c) {
       
    63             if (Character.isDefined((char) c) && !Character.isISOControl((char) c) && encoder.canEncode((char) c)) {
       
    64                 chars.put((char) c);
       
    65             }
       
    66         }
       
    67         chars.limit(chars.position());
       
    68         chars.rewind();
       
    69         return chars.toString();
       
    70     }
       
    71 }