1 package org.sentrysoftware.ssh; 2 3 /*- 4 * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲ 5 * SSH Java Client 6 * ჻჻჻჻჻჻ 7 * Copyright 2023 Sentry Software 8 * ჻჻჻჻჻჻ 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱ 21 */ 22 23 import java.nio.charset.Charset; 24 import java.nio.charset.IllegalCharsetNameException; 25 import java.nio.charset.StandardCharsets; 26 import java.nio.charset.UnsupportedCharsetException; 27 28 29 /** 30 * Utility Class (static), to be used anywhere in Matsya, including in 31 * standalone JARs (in CLI mode) 32 * 33 * @author Bertrand 34 */ 35 public class Utils { 36 37 private Utils() { } 38 39 /** 40 * Returns the proper Charset that can decode/encode the specified locale 41 * 42 * @param locale The locale we're dealing with, as formatted in the LANG environment variable (e.g. zh_CN.utf8) 43 * @param defaultCharset The default Charset to use if the specified locale doesn't match any supported Charset 44 * @return The appropriate Charset instance 45 */ 46 public static Charset getCharsetFromLocale(final String locale, final Charset defaultCharset) { 47 48 // What charset will we be dealing with coming from and going to the PATROL Agent 49 Charset charset = defaultCharset; 50 if (locale != null && !locale.isEmpty()) { 51 final String[] localeElements = locale.split("\\."); 52 if (localeElements.length > 0) { 53 String charsetName = localeElements[localeElements.length - 1]; 54 if (charsetName != null) { 55 charsetName = charsetName.trim(); 56 if (!charsetName.isEmpty() && !"c".equalsIgnoreCase(charsetName)) { 57 if ("gb".equalsIgnoreCase(charsetName)) { 58 charsetName = "GBK"; 59 } 60 try { 61 charset = Charset.forName(charsetName); 62 } catch (IllegalCharsetNameException | UnsupportedCharsetException e) { 63 /* Unfortunately, there is nothing we can do here, as debug is not even set yet */ 64 } 65 } 66 } 67 } 68 } 69 70 return charset; 71 } 72 73 /** 74 * Returns the proper Charset that can decode/encode the specified locale, or UTF-8 if specified locale 75 * cannot be converted to a Charset. 76 * 77 * @param locale The locale we're dealing with, as formatted in the LANG environment variable (e.g. zh_CN.utf8) 78 * @return The appropriate Charset instance 79 */ 80 public static Charset getCharsetFromLocale(final String locale) { 81 return getCharsetFromLocale(locale, StandardCharsets.UTF_8); 82 } 83 84 /** 85 * Check if the required field is not null. 86 * 87 * @param field 88 * @param name 89 * @throws IllegalStateException if the argument is null 90 */ 91 public static <T> void checkNonNullField(final T field, final String name) { 92 if (field == null) { 93 throw new IllegalStateException(name + " must not be null."); 94 } 95 } 96 97 98 /** 99 * Check if the required argument is not negative or zero. 100 * 101 * @param argument 102 * @param name 103 * @throws IllegalArgumentException if the argument is null 104 */ 105 public static void checkArgumentNotZeroOrNegative(final long argument, final String name) { 106 if (argument <= 0) { 107 throw new IllegalArgumentException(String.format("%s=%d must not be negative or zero.", name, argument)); 108 } 109 } 110 111 112 }