private static boolean canEncode(final CoreCharset charset, final char c, final CharsetEncoder fallback) { // todo add more charset tests if impacted by Android's bad perf in canEncode switch (charset) { case ascii: return c < 0x80; case utf: return true; // real is:!(Character.isLowSurrogate(c) || Character.isHighSurrogate(c)); - but already check above default: return fallback.canEncode(c); } }
@Override protected boolean requireEscaping(int ch) { // control characters if (ch < 0x20 && " \t\r\n".indexOf(ch) == -1) { return true; } // ASCII chars if (ch < 0x80) { return false; } return !encoder.canEncode((char) ch); } };
private static void verifyProperty(Optional<String> catalog, String name, String value) { checkArgument(!catalog.isPresent() || !catalog.get().isEmpty(), "Invalid session property: %s.%s:%s", catalog, name, value); checkArgument(!name.isEmpty(), "Session property name is empty"); CharsetEncoder charsetEncoder = US_ASCII.newEncoder(); checkArgument(catalog.orElse("").indexOf('=') < 0, "Session property catalog must not contain '=': %s", name); checkArgument(charsetEncoder.canEncode(catalog.orElse("")), "Session property catalog is not US_ASCII: %s", name); checkArgument(name.indexOf('=') < 0, "Session property name must not contain '=': %s", name); checkArgument(charsetEncoder.canEncode(name), "Session property name is not US_ASCII: %s", name); checkArgument(charsetEncoder.canEncode(value), "Session property value is not US_ASCII: %s", value); }
/** * Tests whether the given character can be encoded by this encoder. * * <p>Note that this method may change the internal state of this encoder, so * it should not be called when another encoding process is ongoing, * otherwise it will throw an <code>IllegalStateException</code>. * * @throws IllegalStateException if another encode process is ongoing. */ public boolean canEncode(char c) { return canEncode(CharBuffer.wrap(new char[] { c })); }
/** * Set the value of the {@linkplain #AUTHORIZATION Authorization} header to * Basic Authentication based on the given username and password. * @param username the username * @param password the password * @param charset the charset to use to convert the credentials into an octet * sequence. Defaults to {@linkplain StandardCharsets#ISO_8859_1 ISO-8859-1}. * @throws IllegalArgumentException if {@code username} or {@code password} * contains characters that cannot be encoded to the given charset * @since 5.1 * @see <a href="https://tools.ietf.org/html/rfc7617">RFC 7617</a> */ public void setBasicAuth(String username, String password, @Nullable Charset charset) { Assert.notNull(username, "Username must not be null"); Assert.notNull(password, "Password must not be null"); if (charset == null) { charset = StandardCharsets.ISO_8859_1; } CharsetEncoder encoder = charset.newEncoder(); if (!encoder.canEncode(username) || !encoder.canEncode(password)) { throw new IllegalArgumentException( "Username or password contains characters that cannot be encoded to " + charset.displayName()); } String credentialsString = username + ":" + password; byte[] encodedBytes = Base64.getEncoder().encode(credentialsString.getBytes(charset)); String encodedCredentials = new String(encodedBytes, charset); set(AUTHORIZATION, "Basic " + encodedCredentials); }
/** * @see org.apache.tools.zip.ZipEncoding#canEncode(java.lang.String) */ public boolean canEncode(final String name) { final CharsetEncoder enc = this.charset.newEncoder(); enc.onMalformedInput(CodingErrorAction.REPORT); enc.onUnmappableCharacter(CodingErrorAction.REPORT); return enc.canEncode(name); }
/** * @see * com.alimama.mdrill.utils.zip.ZipEncoding#canEncode(java.lang.String) */ public boolean canEncode(String name) { CharsetEncoder enc = this.charset.newEncoder(); enc.onMalformedInput(CodingErrorAction.REPORT); enc.onUnmappableCharacter(CodingErrorAction.REPORT); return enc.canEncode(name); }
/** * Adds a session property (experimental). */ public void setSessionProperty(String name, String value) { requireNonNull(name, "name is null"); requireNonNull(value, "value is null"); checkArgument(!name.isEmpty(), "name is empty"); CharsetEncoder charsetEncoder = US_ASCII.newEncoder(); checkArgument(name.indexOf('=') < 0, "Session property name must not contain '=': %s", name); checkArgument(charsetEncoder.canEncode(name), "Session property name is not US_ASCII: %s", name); checkArgument(charsetEncoder.canEncode(value), "Session property value is not US_ASCII: %s", value); sessionProperties.put(name, value); }
public SocksAuthRequest(String username, String password) { super(SocksRequestType.AUTH); if (username == null) { throw new NullPointerException("username"); } if (password == null) { throw new NullPointerException("username"); } if (!asciiEncoder.canEncode(username) || !asciiEncoder.canEncode(password)) { throw new IllegalArgumentException( "username: " + username + " or password: **** values should be in pure ascii"); } if (username.length() > 255) { throw new IllegalArgumentException("username: " + username + " exceeds 255 char limit"); } if (password.length() > 255) { throw new IllegalArgumentException("password: **** exceeds 255 char limit"); } this.username = username; this.password = password; }
@Nullable private String charsetFix(String s) { if (s != null && !enc.canEncode(s)) { // Some whacky characters are in this string (e.g. \uD900). These are problematic because they are decodeable // by new String(...) but will not encode into the same character. This dance here will replace these // characters with something more sane. return StringUtils.fromUtf8(StringUtils.toUtf8(s)); } else { return s; } }
/** * @see ZipEncoding#canEncode(java.lang.String) */ @Override public boolean canEncode(final String name) { final CharsetEncoder enc = newEncoder(); return enc.canEncode(name); }
/** * Set the value of the {@linkplain #AUTHORIZATION Authorization} header to * Basic Authentication based on the given username and password. * @param username the username * @param password the password * @param charset the charset to use to convert the credentials into an octet * sequence. Defaults to {@linkplain StandardCharsets#ISO_8859_1 ISO-8859-1}. * @throws IllegalArgumentException if {@code username} or {@code password} * contains characters that cannot be encoded to the given charset * @since 5.1 * @see <a href="https://tools.ietf.org/html/rfc7617">RFC 7617</a> */ public void setBasicAuth(String username, String password, @Nullable Charset charset) { Assert.notNull(username, "Username must not be null"); Assert.notNull(password, "Password must not be null"); if (charset == null) { charset = StandardCharsets.ISO_8859_1; } CharsetEncoder encoder = charset.newEncoder(); if (!encoder.canEncode(username) || !encoder.canEncode(password)) { throw new IllegalArgumentException( "Username or password contains characters that cannot be encoded to " + charset.displayName()); } String credentialsString = username + ":" + password; byte[] encodedBytes = Base64.getEncoder().encode(credentialsString.getBytes(charset)); String encodedCredentials = new String(encodedBytes, charset); set(AUTHORIZATION, "Basic " + encodedCredentials); }
checkArgument(!entry.getKey().isEmpty(), "Resource name is empty"); checkArgument(entry.getKey().indexOf('=') < 0, "Resource name must not contain '=': %s", entry.getKey()); checkArgument(charsetEncoder.canEncode(entry.getKey()), "Resource name is not US_ASCII: %s", entry.getKey()); checkArgument(!entry.getKey().isEmpty(), "Session property name is empty"); checkArgument(entry.getKey().indexOf('=') < 0, "Session property name must not contain '=': %s", entry.getKey()); checkArgument(charsetEncoder.canEncode(entry.getKey()), "Session property name is not US_ASCII: %s", entry.getKey()); checkArgument(charsetEncoder.canEncode(entry.getValue()), "Session property value is not US_ASCII: %s", entry.getValue());
import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; public class StringUtils { static CharsetEncoder asciiEncoder = Charset.forName("US-ASCII").newEncoder(); // or "ISO-8859-1" for ISO Latin 1 public static boolean isPureAscii(String v) { return asciiEncoder.canEncode(v); } public static void main (String args[]) throws Exception { String test = "Réal"; System.out.println(test + " isPureAscii() : " + StringUtils.isPureAscii(test)); test = "Real"; System.out.println(test + " isPureAscii() : " + StringUtils.isPureAscii(test)); /* * output : * Réal isPureAscii() : false * Real isPureAscii() : true */ } }
private static String encodeText(String string) { StringBuilder sb = new StringBuilder(); int index = 0; final int len = string.length(); while (index < len) { int point = string.codePointAt(index); int count = Character.charCount(point); if (isValidCharCode(point)) { if (encoder.canEncode(string.substring(index, index + count))) { String value = quote(point); if (value != null) { sb.append(value); } else { sb.appendCodePoint(point); } } else { sb.append("&#x"); sb.append(Integer.toString(point, 16)); sb.append(';'); } index += count; } else { sb.append(encodeCode(string.charAt(index))); index++; } } return sb.toString(); } }
checkArgument(!entry.getKey().isEmpty(), "Resource name is empty"); checkArgument(entry.getKey().indexOf('=') < 0, "Resource name must not contain '=': %s", entry.getKey()); checkArgument(charsetEncoder.canEncode(entry.getKey()), "Resource name is not US_ASCII: %s", entry.getKey()); checkArgument(!entry.getKey().isEmpty(), "Session property name is empty"); checkArgument(entry.getKey().indexOf('=') < 0, "Session property name must not contain '=': %s", entry.getKey()); checkArgument(charsetEncoder.canEncode(entry.getKey()), "Session property name is not US_ASCII: %s", entry.getKey()); checkArgument(charsetEncoder.canEncode(entry.getValue()), "Session property value is not US_ASCII: %s", entry.getValue());
@Override public Object apply(JsonNode node) { if (node == null || node.isMissingNode() || node.isNull()) { return null; } if (node.isIntegralNumber()) { if (node.canConvertToLong()) { return node.asLong(); } else { return node.asDouble(); } } if (node.isFloatingPointNumber()) { return node.asDouble(); } final String s = node.asText(); final CharsetEncoder enc = StandardCharsets.UTF_8.newEncoder(); if (s != null && !enc.canEncode(s)) { // Some whacky characters are in this string (e.g. \uD900). These are problematic because they are decodeable // by new String(...) but will not encode into the same character. This dance here will replace these // characters with something more sane. return StringUtils.fromUtf8(StringUtils.toUtf8(s)); } else { return s; } } };
public static byte[] userRealmPasswordDigest(MessageDigest messageDigest, String username, String realm, char[] password) { CharsetEncoder latin1Encoder = StandardCharsets.ISO_8859_1.newEncoder(); latin1Encoder.reset(); boolean bothLatin1 = latin1Encoder.canEncode(username); latin1Encoder.reset(); if (bothLatin1) { for (char c: password) { bothLatin1 = bothLatin1 && latin1Encoder.canEncode(c); } } Charset chosenCharset = StandardCharsets.UTF_8; // bothLatin1 ? StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8; ByteStringBuilder urp = new ByteStringBuilder(); // username:realm:password urp.append(username.getBytes(chosenCharset)); urp.append(':'); if (realm != null) { urp.append(realm.getBytes((chosenCharset))); } else { urp.append(""); } urp.append(':'); urp.append(new String(password).getBytes((chosenCharset))); return messageDigest.digest(urp.toArray()); }
/** * Create a text response with known length. */ public static Response newFixedLengthResponse(IStatus status, String mimeType, String txt) { ContentType contentType = new ContentType(mimeType); if (txt == null) { return newFixedLengthResponse(status, mimeType, new ByteArrayInputStream(new byte[0]), 0); } else { byte[] bytes; try { CharsetEncoder newEncoder = Charset.forName(contentType.getEncoding()).newEncoder(); if (!newEncoder.canEncode(txt)) { contentType = contentType.tryUTF8(); } bytes = txt.getBytes(contentType.getEncoding()); } catch (UnsupportedEncodingException e) { NanoHTTPD.LOG.log(Level.SEVERE, "encoding problem, responding nothing", e); bytes = new byte[0]; } return newFixedLengthResponse(status, contentType.getContentTypeHeader(), new ByteArrayInputStream(bytes), bytes.length); } }
private void checkCodePage(String value) { int cp = getCodepage(); if (cp == -1) { cp = Property.DEFAULT_CODEPAGE; } if (cp == CodePageUtil.CP_UNICODE) { return; } String cps = ""; try { cps = CodePageUtil.codepageToEncoding(cp, false); } catch (UnsupportedEncodingException e) { LOG.log(POILogger.ERROR, "Codepage '"+cp+"' can't be found."); } if (!cps.isEmpty() && Charset.forName(cps).newEncoder().canEncode(value)) { return; } LOG.log(POILogger.DEBUG, "Charset '"+cps+"' can't encode '"+value+"' - switching to unicode."); setCodepage(CodePageUtil.CP_UNICODE); } }