List of usage examples for java.io UnsupportedEncodingException getMessage
public String getMessage()
From source file:com.alibaba.otter.node.etl.common.db.utils.SqlUtils.java
public static String encoding(String source, int sqlType, String sourceEncoding, String targetEncoding) { switch (sqlType) { case Types.CHAR: case Types.VARCHAR: case Types.LONGVARCHAR: case Types.NCHAR: case Types.NVARCHAR: case Types.LONGNVARCHAR: case Types.CLOB: case Types.NCLOB: if (false == StringUtils.isEmpty(source)) { String fromEncoding = StringUtils.isBlank(sourceEncoding) ? "UTF-8" : sourceEncoding; String toEncoding = StringUtils.isBlank(targetEncoding) ? "UTF-8" : targetEncoding; // if (false == StringUtils.equalsIgnoreCase(fromEncoding, // toEncoding)) { try { return new String(source.getBytes(fromEncoding), toEncoding); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e.getMessage(), e); }/* w ww . j a v a 2 s . c o m*/ // } } } return source; }
From source file:ch.cyberduck.core.HostParser.java
public static Host parse(final ProtocolFactory factory, final Protocol scheme, final String url) { final String input = url.trim(); int begin = 0; int cut;/*from w w w. jav a 2 s . c om*/ Protocol protocol = null; if (input.indexOf("://", begin) != -1) { cut = input.indexOf("://", begin); protocol = factory.forName(input.substring(begin, cut)); begin += cut - begin + 3; } if (null == protocol) { protocol = scheme; } String username; String password = null; if (protocol.isAnonymousConfigurable()) { username = preferences.getProperty("connection.login.anon.name"); } else { username = preferences.getProperty("connection.login.name"); } if (input.indexOf('@', begin) != -1) { if (-1 == input.indexOf(Path.DELIMITER, begin) || input.indexOf('@', begin) < input.indexOf(Path.DELIMITER, begin)) { cut = input.indexOf('@', begin); // Handle at sign in username while (cut < input.lastIndexOf('@')) { if (input.indexOf(Path.DELIMITER, begin) != -1 && input.indexOf('@', cut + 1) > input.indexOf(Path.DELIMITER, begin)) { // At sign is part of the path break; } cut = input.indexOf('@', cut + 1); } if (input.indexOf(':', begin) != -1 && cut > input.indexOf(':', begin)) { // ':' is not for the port number but username:pass separator username = input.substring(begin, input.indexOf(':', begin)); begin += username.length() + 1; cut = input.indexOf('@', begin); try { username = URLDecoder.decode(username, "UTF-8"); } catch (UnsupportedEncodingException e) { log.error(e.getMessage(), e); } password = input.substring(begin, cut); begin += password.length() + 1; } else { // No password given username = input.substring(begin, cut); begin += username.length() + 1; try { username = URLDecoder.decode(username, "UTF-8"); } catch (UnsupportedEncodingException e) { log.error(e.getMessage(), e); } } } } String hostname = preferences.getProperty("connection.hostname.default"); String path = null; int port = protocol.getDefaultPort(); // Handle IPv6 if (input.indexOf('[', begin) != -1 && input.indexOf(']', begin) != -1) { if (input.indexOf(']', begin) > input.indexOf('[', begin)) { begin = input.indexOf('[', begin) + 1; cut = input.indexOf(']', begin); String address = input.substring(begin, cut); if (isv6Address(address)) { hostname = address; begin += hostname.length(); } } } else if (input.indexOf(Path.DELIMITER, begin) != -1) { cut = input.indexOf(Path.DELIMITER, begin); String address = input.substring(begin, cut); if (isv6Address(address)) { hostname = address; begin += hostname.length(); } } else { if (isv6Address(input)) { hostname = input; begin += hostname.length(); } } if (StringUtils.isBlank(hostname)) { // Handle DNS name or IPv4 if (StringUtils.isNotBlank(input)) { if (input.indexOf(':', begin) != -1 && (input.indexOf(Path.DELIMITER, begin) == -1 || input.indexOf(':', begin) < input.indexOf(Path.DELIMITER, begin))) { cut = input.indexOf(':', begin); } else if (input.indexOf(Path.DELIMITER, begin) != -1) { cut = input.indexOf(Path.DELIMITER, begin); } else { cut = input.length(); } hostname = input.substring(begin, cut); begin += hostname.length(); } } if (input.indexOf(':', begin) != -1 && (input.indexOf(Path.DELIMITER, begin) == -1 || input.indexOf(':', begin) < input.indexOf(Path.DELIMITER, begin))) { begin = input.indexOf(':', begin) + 1; String portString; if (input.indexOf(Path.DELIMITER, begin) != -1) { cut = input.indexOf(Path.DELIMITER, begin); portString = input.substring(begin, cut); try { port = Integer.parseInt(portString); begin += portString.length(); } catch (NumberFormatException e) { log.warn("Invalid port number given"); } try { path = URLDecoder.decode(input.substring(begin, input.length()), "UTF-8"); begin += path.length(); } catch (UnsupportedEncodingException | IllegalArgumentException e) { log.error(e.getMessage(), e); } } else { portString = input.substring(begin, input.length()); try { port = Integer.parseInt(portString); begin += portString.length(); } catch (NumberFormatException e) { log.warn("Invalid port number given"); } } } if (input.indexOf(Path.DELIMITER, begin) != -1) { try { path = URLDecoder.decode(input.substring(begin, input.length()), "UTF-8"); } catch (UnsupportedEncodingException | IllegalArgumentException e) { log.error(e.getMessage(), e); } } switch (protocol.getType()) { case s3: case googlestorage: case swift: if (StringUtils.isNotBlank(protocol.getDefaultHostname())) { if (StringUtils.isNotBlank(hostname)) { // Replace with static hostname and prefix path with bucket if (StringUtils.isBlank(path)) { path = PathNormalizer.normalize(hostname); } else { path = PathNormalizer.normalize(hostname) + path; } hostname = protocol.getDefaultHostname(); } } } final Host host = new Host(protocol, hostname, port, path, new Credentials(username, password)); host.configure(); return host; }
From source file:de.unidue.inf.is.ezdl.dlcore.utils.StringUtils.java
/** * Compresses a string./*from www . java 2 s . c o m*/ * * @param s * The string to compress * @return The compressed string */ public static String compress(String s) { ByteArrayOutputStream baos = null; try { byte[] input = s.getBytes("UTF-8"); Deflater compresser = new Deflater(); compresser.setLevel(Deflater.BEST_COMPRESSION); compresser.setInput(input); compresser.finish(); baos = new ByteArrayOutputStream(); while (!compresser.finished()) { byte[] output = new byte[1024]; int compressedDataLength = compresser.deflate(output); baos.write(output, 0, compressedDataLength); } baos.flush(); return Base64.encodeBase64String(baos.toByteArray()); } catch (UnsupportedEncodingException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error(e.getMessage(), e); } finally { ClosingUtils.close(baos); } return ""; }
From source file:de.unidue.inf.is.ezdl.dlcore.utils.StringUtils.java
/** * Decompresses a string./*from ww w. ja v a2s .co m*/ * * @param s * The string to decompress * @return The decompressed string */ public static String decompress(String s) { ByteArrayOutputStream baos = null; try { Inflater ifl = new Inflater(); ifl.setInput(Base64.decodeBase64(s)); baos = new ByteArrayOutputStream(); while (!ifl.finished()) { byte[] buff = new byte[1024]; int count = ifl.inflate(buff); baos.write(buff, 0, count); } baos.flush(); byte[] output = baos.toByteArray(); return new String(output, "UTF-8"); } catch (UnsupportedEncodingException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error(e.getMessage(), e); } catch (DataFormatException e) { logger.error(e.getMessage(), e); } finally { ClosingUtils.close(baos); } return ""; }
From source file:com.cws.esolutions.security.utils.PasswordUtils.java
/** * Provides one-way (irreversible) encryption of a provided string. * * @param plainText - The plain text data to encrypt * @param salt - The salt value to utilize for the request * @param instance - The security instance to utilize * @param iterations - The number of times the value should be re-encrypted * @param encoding - The text encoding/*from www. j a v a2 s. co m*/ * @return The encrypted string * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing */ public static final String encryptText(final String plainText, final String salt, final String instance, final int iterations, final String encoding) throws SecurityException { final String methodName = PasswordUtils.CNAME + "#encryptText(final String plainText, final String salt, final String algorithm, final String instance, final int iterations, final String encoding) throws SecurityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", plainText); DEBUGGER.debug("Value: {}", salt); DEBUGGER.debug("Value: {}", instance); DEBUGGER.debug("Value: {}", iterations); DEBUGGER.debug("Value: {}", encoding); } String response = null; try { MessageDigest md = MessageDigest.getInstance(instance); md.reset(); md.update(salt.getBytes(encoding)); byte[] input = md.digest(plainText.getBytes(encoding)); for (int x = 0; x < iterations; x++) { md.reset(); input = md.digest(input); } response = Base64.getEncoder().encodeToString(input); } catch (NoSuchAlgorithmException nsx) { throw new SecurityException(nsx.getMessage(), nsx); } catch (UnsupportedEncodingException uex) { throw new SecurityException(uex.getMessage(), uex); } return response; }
From source file:com.fluidops.iwb.deepzoom.ImageLoader.java
public static String filename(String imagefile) { try {//from w ww. j a v a 2 s . c om return URLEncoder.encode( Integer.toString(imagefile.hashCode()) /*.substring(imagefile.lastIndexOf("/")+1)*/, "UTF-8"); } catch (UnsupportedEncodingException e) { logger.error(e.getMessage(), e); } return null; }
From source file:cn.bc.web.util.WebUtils.java
/** * ????????//from w ww.j av a 2 s . com * * @param isIE * @param srcFileName * ?? * @return ???? */ public static String encodeFileName(boolean isIE, String srcFileName) { String _fileName; // ??? // boolean isIE = request.getHeader("User-Agent").toUpperCase() // .indexOf("MSIE") != -1; try { if (isIE) {// IE? _fileName = URLEncoder.encode(srcFileName, "UTF-8"); if (_fileName.length() > 150) { // URLEncoder?17IE6 // IEbug??KB816868 // ?????ie6 sp1 String guessCharset = "gb2312"; // ?requestlocale???gb2312 _fileName = new String(srcFileName.getBytes(guessCharset), "ISO8859-1"); } } else {// ?IE?:?URLEncoder.encode???url? _fileName = new String(srcFileName.getBytes("UTF-8"), "ISO8859-1"); } } catch (UnsupportedEncodingException e) { logger.warn(e.getMessage()); _fileName = srcFileName; } return _fileName; }
From source file:com.cws.esolutions.security.utils.PasswordUtils.java
/** * Provides two-way (reversible) encryption of a provided string. Can be used where reversibility * is required but encryption (obfuscation, technically) is required. * * @param value - The plain text data to encrypt * @param salt - The salt value to utilize for the request * @param secretInstance - The cryptographic instance to use for the SecretKeyFactory * @param iterations - The number of times to loop through the keyspec * @param keyBits - The size of the key, in bits * @param algorithm - The algorithm to encrypt the data with * @param cipherInstance - The cipher instance to utilize * @param encoding - The text encoding/*from ww w .j a v a 2 s. co m*/ * @return The encrypted string in a reversible format * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing */ public static final String decryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException { final String methodName = PasswordUtils.CNAME + "#encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", secretInstance); DEBUGGER.debug("Value: {}", iterations); DEBUGGER.debug("Value: {}", keyBits); DEBUGGER.debug("Value: {}", algorithm); DEBUGGER.debug("Value: {}", cipherInstance); DEBUGGER.debug("Value: {}", encoding); } String decPass = null; try { String decoded = new String(Base64.getDecoder().decode(value)); String iv = decoded.split(":")[0]; String property = decoded.split(":")[1]; SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretInstance); PBEKeySpec keySpec = new PBEKeySpec(salt.toCharArray(), salt.getBytes(), iterations, keyBits); SecretKey keyTmp = keyFactory.generateSecret(keySpec); SecretKeySpec sks = new SecretKeySpec(keyTmp.getEncoded(), algorithm); Cipher pbeCipher = Cipher.getInstance(cipherInstance); pbeCipher.init(Cipher.DECRYPT_MODE, sks, new IvParameterSpec(Base64.getDecoder().decode(iv))); decPass = new String(pbeCipher.doFinal(Base64.getDecoder().decode(property)), encoding); } catch (InvalidKeyException ikx) { throw new SecurityException(ikx.getMessage(), ikx); } catch (NoSuchAlgorithmException nsx) { throw new SecurityException(nsx.getMessage(), nsx); } catch (NoSuchPaddingException npx) { throw new SecurityException(npx.getMessage(), npx); } catch (IllegalBlockSizeException ibx) { throw new SecurityException(ibx.getMessage(), ibx); } catch (BadPaddingException bpx) { throw new SecurityException(bpx.getMessage(), bpx); } catch (UnsupportedEncodingException uex) { throw new SecurityException(uex.getMessage(), uex); } catch (InvalidAlgorithmParameterException iapx) { throw new SecurityException(iapx.getMessage(), iapx); } catch (InvalidKeySpecException iksx) { throw new SecurityException(iksx.getMessage(), iksx); } return decPass; }
From source file:com.cws.esolutions.security.utils.PasswordUtils.java
/** * Provides two-way (reversible) encryption of a provided string. Can be used where reversibility * is required but encryption (obfuscation, technically) is required. * * @param value - The plain text data to encrypt * @param salt - The salt value to utilize for the request * @param secretInstance - The cryptographic instance to use for the SecretKeyFactory * @param iterations - The number of times to loop through the keyspec * @param keyBits - The size of the key, in bits * @param algorithm - The algorithm to encrypt the data with * @param cipherInstance - The cipher instance to utilize * @param encoding - The text encoding/* w w w. j a va2 s . co m*/ * @return The encrypted string in a reversible format * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing */ public static final String encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException { final String methodName = PasswordUtils.CNAME + "#encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", secretInstance); DEBUGGER.debug("Value: {}", iterations); DEBUGGER.debug("Value: {}", keyBits); DEBUGGER.debug("Value: {}", algorithm); DEBUGGER.debug("Value: {}", cipherInstance); DEBUGGER.debug("Value: {}", encoding); } String encPass = null; try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretInstance); PBEKeySpec keySpec = new PBEKeySpec(salt.toCharArray(), salt.getBytes(), iterations, keyBits); SecretKey keyTmp = keyFactory.generateSecret(keySpec); SecretKeySpec sks = new SecretKeySpec(keyTmp.getEncoded(), algorithm); Cipher pbeCipher = Cipher.getInstance(cipherInstance); pbeCipher.init(Cipher.ENCRYPT_MODE, sks); AlgorithmParameters parameters = pbeCipher.getParameters(); IvParameterSpec ivParameterSpec = parameters.getParameterSpec(IvParameterSpec.class); byte[] cryptoText = pbeCipher.doFinal(value.getBytes(encoding)); byte[] iv = ivParameterSpec.getIV(); String combined = Base64.getEncoder().encodeToString(iv) + ":" + Base64.getEncoder().encodeToString(cryptoText); encPass = Base64.getEncoder().encodeToString(combined.getBytes()); } catch (InvalidKeyException ikx) { throw new SecurityException(ikx.getMessage(), ikx); } catch (NoSuchAlgorithmException nsx) { throw new SecurityException(nsx.getMessage(), nsx); } catch (NoSuchPaddingException npx) { throw new SecurityException(npx.getMessage(), npx); } catch (IllegalBlockSizeException ibx) { throw new SecurityException(ibx.getMessage(), ibx); } catch (BadPaddingException bpx) { throw new SecurityException(bpx.getMessage(), bpx); } catch (UnsupportedEncodingException uex) { throw new SecurityException(uex.getMessage(), uex); } catch (InvalidKeySpecException iksx) { throw new SecurityException(iksx.getMessage(), iksx); } catch (InvalidParameterSpecException ipsx) { throw new SecurityException(ipsx.getMessage(), ipsx); } return encPass; }
From source file:com.mingsoft.util.proxy.Proxy.java
/** * post/*from ww w. j av a 2 s . com*/ * * @param url * ?<br> * @param header * ?Header new Header()<br> * @param params * ?<br> * @param encoding * ??<br> * @return Result */ public static Result post(String url, com.mingsoft.util.proxy.Header header, Map params, String encoding) { //log.info("?" + url); // Httpclient DefaultHttpClient client = new DefaultHttpClient(); // post HttpPost httpPost = new HttpPost(url); // cookie?() httpPost.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY); // ???? if (params != null) { List list = new ArrayList(); Iterator it = params.keySet().iterator(); while (it.hasNext()) { String temp = (String) it.next(); list.add(new BasicNameValuePair(temp, params.get(temp) + "")); } try { httpPost.setEntity(new UrlEncodedFormEntity(list, encoding)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); log.error(e.getMessage()); } } // if (null != header && header.getHeaders().size() > 0) { httpPost.setHeaders(Proxy.assemblyHeader(header.getHeaders())); } // HttpResponse response = null; try { response = client.execute(httpPost); HttpEntity entity = response.getEntity(); // // httpPost.abort(); // ?? Result result = new Result(); // ?? result.setStatusCode(response.getStatusLine().getStatusCode()); // ? result.setHeaders(response.getAllHeaders()); // cookie result.setCookie(assemblyCookie(client.getCookieStore().getCookies())); // ? result.setHttpEntity(entity); return result; } catch (ClientProtocolException e) { e.printStackTrace(); log.error(e.getMessage()); } catch (IOException e) { e.printStackTrace(); log.error(e.getMessage()); } return null; }