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/* 25 * @test 26 * @bug 6419791 27 * @summary 28 * @author Martin Buchholz 29 */ 30 31import java.io.*; 32import java.util.*; 33import java.nio.charset.*; 34import java.nio.*; 35 36public class ISO8859x { 37 final static byte[] lowBytes = new byte[0xa0]; 38 final static char[] lowChars = new char[0xa0]; 39 final static String lowString; 40 static { 41 for (int i = 0; i < 0xa0; i++) { 42 lowBytes[i] = (byte) i; 43 lowChars[i] = (char) i; 44 } 45 lowString = new String(lowChars); 46 } 47 48 private static void testCharset(Charset cs) throws Throwable { 49 String csn = cs.name(); 50 System.out.println(csn); 51 52 check(cs.canEncode()); 53 check(Arrays.equals(lowString.getBytes(csn), lowBytes)); 54 check(new String(lowBytes, csn).equals(lowString)); 55 56 CharsetEncoder encoder = cs.newEncoder(); 57 CharsetDecoder decoder = cs.newDecoder(); 58 decoder.onUnmappableCharacter(CodingErrorAction.REPORT) 59 .onMalformedInput(CodingErrorAction.REPORT); 60 encoder.onUnmappableCharacter(CodingErrorAction.REPORT) 61 .onMalformedInput(CodingErrorAction.REPORT); 62 63 byte[] bytes = new byte[1]; 64 for (int c = 0xa0; c < 0x100; c++) { 65 try { 66 bytes[0] = (byte) c; 67 char[] chars; 68 try { chars = decoder.decode(ByteBuffer.wrap(bytes)).array(); } 69 catch (UnmappableCharacterException x) { continue; } 70 equal(chars.length, 1); 71 byte[] bytes2 = encoder.encode(CharBuffer.wrap(chars)).array(); 72 check(Arrays.equals(bytes2, bytes)); 73 } catch (Throwable t) { 74 System.out.printf("cs=%s c=%02x%n", cs, c); 75 unexpected(t); 76 } 77 } 78 } 79 80 private static void realMain(String[] args) throws Throwable { 81 for (Map.Entry<String,Charset> e 82 : Charset.availableCharsets().entrySet()) { 83 String csn = e.getKey(); 84 Charset cs = e.getValue(); 85 if (csn.matches(".*(8859).*")) 86 try { testCharset(cs); } 87 catch (Throwable t) { unexpected(t); } 88 } 89 } 90 91 //--------------------- Infrastructure --------------------------- 92 static volatile int passed = 0, failed = 0; 93 static void pass() {passed++;} 94 static void fail() {failed++; Thread.dumpStack();} 95 static void fail(String msg) {System.out.println(msg); fail();} 96 static void unexpected(Throwable t) {failed++; t.printStackTrace();} 97 static void check(boolean cond) {if (cond) pass(); else fail();} 98 static void equal(Object x, Object y) { 99 if (x == null ? y == null : x.equals(y)) pass(); 100 else fail(x + " not equal to " + y);} 101 public static void main(String[] args) throws Throwable { 102 try {realMain(args);} catch (Throwable t) {unexpected(t);} 103 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 104 if (failed > 0) throw new AssertionError("Some tests failed");} 105} 106