Path: blob/master/test/jdk/sun/nio/cs/Decode.java
41149 views
/*1* Copyright (c) 2008, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**/2425public class Decode {26private static boolean isAscii(char c) {27return c < '\u0080';28}2930private static boolean isPrintable(char c) {31return ('\u0020' < c) && (c < '\u007f');32}3334public static void main(String[] args) throws Throwable {35if (args.length < 2)36throw new Exception("Usage: java Decode CHARSET BYTE [BYTE ...]");37String cs = args[0];38byte[] bytes = new byte[args.length-1];39for (int i = 1; i < args.length; i++) {40String arg = args[i];41bytes[i-1] =42(arg.length() == 1 && isAscii(arg.charAt(0))) ?43(byte) arg.charAt(0) :44arg.equals("ESC") ? 0x1b :45arg.equals("SO") ? 0x0e :46arg.equals("SI") ? 0x0f :47arg.equals("SS2") ? (byte) 0x8e :48arg.equals("SS3") ? (byte) 0x8f :49arg.matches("0x.*") ? Integer.decode(arg).byteValue() :50Integer.decode("0x"+arg).byteValue();51}52String s = new String(bytes, cs);5354for (int j = 0; j < s.length(); j++) {55if (j > 0)56System.out.print(' ');57char c = s.charAt(j);58if (isPrintable(c))59System.out.print(c);60else if (c == '\u001b') System.out.print("ESC");61else62System.out.printf("\\u%04x", (int) c);63}64System.out.print("\n");65}66}676869