Path: blob/master/test/functional/cmdLineTests/cmdLineTest_J9tests/src/GetConsoleCharset.java
6004 views
/*******************************************************************************1* Copyright (c) 2021, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/2122import java.io.*;23import java.lang.reflect.*;24import java.nio.charset.*;2526/**27* @file GetConsoleCharset.java28* @brief Compare the System.err or System.out Charset name to a provided name and29* return the result via the exit code, 0 meaning there is a match.30*/3132public class GetConsoleCharset {33public static void main(String[] args) throws Exception {34OutputStream stream;35if (args.length < 2) {36System.out.println("Provide two arguments, the first is either 'out' or 'err', and the second is the expected Charset name.");37System.exit(1);38}39if ("out".equals(args[0])) {40stream = System.out;41} else {42stream = System.err;43}44Field charOutField = PrintStream.class.getDeclaredField("charOut");45charOutField.setAccessible(true);46String encoding = ((OutputStreamWriter)charOutField.get(stream)).getEncoding();47System.out.println("System." + args[0] + " encoding: " + encoding);48if (args.length > 2) {49FileOutputStream out = new FileOutputStream("GetConsoleCharset-result.txt");50out.write(encoding.getBytes("ISO-8859-1"));51out.close();52}53if (encoding.equals(args[1])) {54System.out.println("SUCCESS");55System.exit(0);56}57System.out.println("FAILED");58System.exit(1);59}60}616263