Java Code Examples for java.nio.charset.CharsetEncoder#canEncode()
The following examples show how to use
java.nio.charset.CharsetEncoder#canEncode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMLJ2eeEditorSupport.java From netbeans with Apache License 2.0 | 6 votes |
private boolean checkCharsetConversion(final String encoding) { boolean value = true; try { CharsetEncoder coder = Charset.forName(encoding).newEncoder(); if (!coder.canEncode(getDocument().getText(0, getDocument().getLength()))){ NotifyDescriptor nd = new NotifyDescriptor.Confirmation( NbBundle.getMessage(XMLJ2eeEditorSupport.class, "MSG_BadCharConversion", new Object [] { getDataObject().getPrimaryFile().getNameExt(), encoding}), NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.WARNING_MESSAGE); nd.setValue(NotifyDescriptor.NO_OPTION); DialogDisplayer.getDefault().notify(nd); if(nd.getValue() != NotifyDescriptor.YES_OPTION) { value = false; } } } catch (BadLocationException e){ Logger.getLogger("global").log(Level.INFO, null, e); } return value; }
Example 2
Source File: NanoHTTPD.java From VBrowser-Android with GNU General Public License v2.0 | 6 votes |
/** * 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); } }
Example 3
Source File: Util.java From Leanplum-Android-SDK with Apache License 2.0 | 5 votes |
private static boolean isValidForCharset(String id, String charsetName) { CharsetEncoder encoder = null; try { Charset charset = Charset.forName(charsetName); encoder = charset.newEncoder(); } catch (UnsupportedCharsetException e) { Log.w("Unsupported charset: " + charsetName); } if (encoder != null && !encoder.canEncode(id)) { Log.v("Invalid id (contains invalid characters): " + id); return false; } return true; }
Example 4
Source File: IndentingWriter.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
/** * Check if encode can handle the chars in this string. * */ protected boolean canEncode(String s) { final CharsetEncoder encoder = Charset.forName(System.getProperty("file.encoding")).newEncoder(); char[] chars = s.toCharArray(); for (int i=0; i<chars.length; i++) { if(!encoder.canEncode(chars[i])) { return false; } } return true; }
Example 5
Source File: FontTexture.java From lwjglbook with Apache License 2.0 | 5 votes |
private String getAllAvailableChars(String charsetName) { CharsetEncoder ce = Charset.forName(charsetName).newEncoder(); StringBuilder result = new StringBuilder(); for (char c = 0; c < Character.MAX_VALUE; c++) { if (ce.canEncode(c)) { result.append(c); } } return result.toString(); }
Example 6
Source File: FontTexture.java From lwjglbook with Apache License 2.0 | 5 votes |
private String getAllAvailableChars(String charsetName) { CharsetEncoder ce = Charset.forName(charsetName).newEncoder(); StringBuilder result = new StringBuilder(); for (char c = 0; c < Character.MAX_VALUE; c++) { if (ce.canEncode(c)) { result.append(c); } } return result.toString(); }
Example 7
Source File: HelloWorldPanel.java From netbeans with Apache License 2.0 | 5 votes |
@Override protected String validateInput() { String errorMessage = super.validateInput(); if (errorMessage == null) { // #222846 - non-ascii characters in installation path File installationFolder = new File(getDestinationPath()); CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder(); if (!encoder.canEncode(installationFolder.getAbsolutePath())) { return StringUtils.format(panel.getProperty(ERROR_CONTAINS_NON_ASCII_CHARS)); } } return errorMessage; }
Example 8
Source File: OMapContainer.java From marathonv5 with Apache License 2.0 | 5 votes |
private String sanitize(String title) { StringBuilder sb = new StringBuilder(); char[] cs = title.toCharArray(); CharsetEncoder asciiEncoder = Charset.forName("US-ASCII").newEncoder(); for (char c : cs) { if (!valid(c) || !asciiEncoder.canEncode(c)) { c = unaccent(c); } sb.append(c); } return sb.toString(); }
Example 9
Source File: FontTexture.java From lwjglbook with Apache License 2.0 | 5 votes |
private String getAllAvailableChars(String charsetName) { CharsetEncoder ce = Charset.forName(charsetName).newEncoder(); StringBuilder result = new StringBuilder(); for (char c = 0; c < Character.MAX_VALUE; c++) { if (ce.canEncode(c)) { result.append(c); } } return result.toString(); }
Example 10
Source File: FontTexture.java From lwjglbook with Apache License 2.0 | 5 votes |
private String getAllAvailableChars(String charsetName) { CharsetEncoder ce = Charset.forName(charsetName).newEncoder(); StringBuilder result = new StringBuilder(); for (char c = 0; c < Character.MAX_VALUE; c++) { if (ce.canEncode(c)) { result.append(c); } } return result.toString(); }
Example 11
Source File: FontTexture.java From lwjglbook with Apache License 2.0 | 5 votes |
private String getAllAvailableChars(String charsetName) { CharsetEncoder ce = Charset.forName(charsetName).newEncoder(); StringBuilder result = new StringBuilder(); for (char c = 0; c < Character.MAX_VALUE; c++) { if (ce.canEncode(c)) { result.append(c); } } return result.toString(); }
Example 12
Source File: DBOperator.java From tmxeditor8 with GNU General Public License v2.0 | 5 votes |
/** * 过滤查询:查询有乱码的的文本段的 主键 robert 2013-08-22 * @param srcLang * @param tgtLang * @param ignoreCase * @param ignoreTag * @return * @throws SQLException * ; */ public List<String> getWithGarbleTuPK(String srcLang, String tgtLang, boolean ignoreCase, boolean ignoreTag) throws SQLException { List<String> result = new ArrayList<String>(); String sql = dbConfig.getOperateDbSQL("getWithGarbleTuPK"); Map<Integer, Map<String, String>> rs = query(sql, new Object[] { srcLang, tgtLang }); Collection<Map<String, String>> values = rs.values(); Iterator<Map<String, String>> iterator = values.iterator(); String src = null; String tgt = null; CharsetEncoder encoder = java.nio.charset.Charset.forName("UTF-8").newEncoder(); while (iterator.hasNext()) { Map<String, String> next = iterator.next(); String id = next.get("ID"); src = next.get("SRC"); if (src != null) { if (!encoder.canEncode(src)) { result.add(id); continue; } } tgt = next.get("TGT"); if (tgt != null) { if (!encoder.canEncode(tgt)) { result.add(id); } } } return result; }
Example 13
Source File: GuavaStringUnitTest.java From tutorials with MIT License | 5 votes |
@Test public void whenRemoveCharsNotInCharset_thenRemoved() { final Charset charset = Charset.forName("cp437"); final CharsetEncoder encoder = charset.newEncoder(); final Predicate<Character> inRange = new Predicate<Character>() { @Override public boolean apply(final Character c) { return encoder.canEncode(c); } }; final String result = CharMatcher.forPredicate(inRange).retainFrom("helloは"); assertEquals("hello", result); }
Example 14
Source File: ItemID.java From mslinks with Do What The F*ck You Want To Public License | 5 votes |
private static String generateShortName( String longname ) { // assume that it is actually long, don't check it again longname = longname.replaceAll( "\\.$|^\\.", "" ); int dotIdx = longname.lastIndexOf( '.' ); String baseName = dotIdx == -1 ? longname : longname.substring( 0, dotIdx ); String ext = dotIdx == -1 ? "" : longname.substring( dotIdx + 1 ); ext = ext.replaceAll( " ", "" ).replaceAll( "[\\.\"\\/\\\\\\[\\]:;=,\\+]", "_" ); ext = ext.substring( 0, Math.min( 3, ext.length() ) ); baseName = baseName.replaceAll( " ", "" ).replaceAll( "[\\.\"\\/\\\\\\[\\]:;=,\\+]", "_" ); baseName = baseName.substring( 0, Math.min( 6, baseName.length() ) ); // well, for same short names we should use "~2", "~3" and so on, // but actual index is generated by os while creating a file and stored in filesystem // so it is not possible to get actual one StringBuilder shortname = new StringBuilder( baseName + "~1" + ( ext.isEmpty() ? "" : "." + ext ) ); // i have no idea how non-asci symbols are converted in dos names CharsetEncoder asciiEncoder = Charset.forName( "US-ASCII" ).newEncoder(); for( int i = 0; i < shortname.length(); ++i ) { if( !asciiEncoder.canEncode( shortname.charAt( i ) ) ) shortname.setCharAt( i, '_' ); } return shortname.toString().toUpperCase(); }
Example 15
Source File: Closure_34_CodeGenerator_t.java From coming with MIT License | 4 votes |
/** Helper to escape javascript string as well as regular expression */ private static String strEscape( String s, char quote, String doublequoteEscape, String singlequoteEscape, String backslashEscape, CharsetEncoder outputCharsetEncoder, boolean useSlashV) { StringBuilder sb = new StringBuilder(s.length() + 2); sb.append(quote); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); switch (c) { case '\0': sb.append("\\x00"); break; case '\u000B': if (useSlashV) { sb.append("\\v"); } else { sb.append("\\x0B"); } break; case '\n': sb.append("\\n"); break; case '\r': sb.append("\\r"); break; case '\t': sb.append("\\t"); break; case '\\': sb.append(backslashEscape); break; case '\"': sb.append(doublequoteEscape); break; case '\'': sb.append(singlequoteEscape); break; case '>': // Break --> into --\> or ]]> into ]]\> if (i >= 2 && ((s.charAt(i - 1) == '-' && s.charAt(i - 2) == '-') || (s.charAt(i - 1) == ']' && s.charAt(i - 2) == ']'))) { sb.append("\\>"); } else { sb.append(c); } break; case '<': // Break </script into <\/script final String END_SCRIPT = "/script"; // Break <!-- into <\!-- final String START_COMMENT = "!--"; if (s.regionMatches(true, i + 1, END_SCRIPT, 0, END_SCRIPT.length())) { sb.append("<\\"); } else if (s.regionMatches(false, i + 1, START_COMMENT, 0, START_COMMENT.length())) { sb.append("<\\"); } else { sb.append(c); } break; default: // If we're given an outputCharsetEncoder, then check if the // character can be represented in this character set. if (outputCharsetEncoder != null) { if (outputCharsetEncoder.canEncode(c)) { sb.append(c); } else { // Unicode-escape the character. appendHexJavaScriptRepresentation(sb, c); } } else { // No charsetEncoder provided - pass straight latin characters // through, and escape the rest. Doing the explicit character // check is measurably faster than using the CharsetEncoder. if (c > 0x1f && c < 0x7f) { sb.append(c); } else { // Other characters can be misinterpreted by some js parsers, // or perhaps mangled by proxies along the way, // so we play it safe and unicode escape them. appendHexJavaScriptRepresentation(sb, c); } } } } sb.append(quote); return sb.toString(); }
Example 16
Source File: 1_CodeGenerator.java From SimFix with GNU General Public License v2.0 | 4 votes |
/** Helper to escape javascript string as well as regular expression */ static String strEscape(String s, char quote, String doublequoteEscape, String singlequoteEscape, String backslashEscape, CharsetEncoder outputCharsetEncoder) { StringBuilder sb = new StringBuilder(s.length() + 2); sb.append(quote); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); switch (c) { case '\0': sb.append("\\0"); break; case '\n': sb.append("\\n"); break; case '\r': sb.append("\\r"); break; case '\t': sb.append("\\t"); break; case '\\': sb.append(backslashEscape); break; case '\"': sb.append(doublequoteEscape); break; case '\'': sb.append(singlequoteEscape); break; case '>': // Break --> into --\> or ]]> into ]]\> if (i >= 2 && ((s.charAt(i - 1) == '-' && s.charAt(i - 2) == '-') || (s.charAt(i - 1) == ']' && s.charAt(i - 2) == ']'))) { sb.append("\\>"); } else { sb.append(c); } break; case '<': // Break </script into <\/script final String END_SCRIPT = "/script"; // Break <!-- into <\!-- final String START_COMMENT = "!--"; if (s.regionMatches(true, i + 1, END_SCRIPT, 0, END_SCRIPT.length())) { sb.append("<\\"); } else if (s.regionMatches(false, i + 1, START_COMMENT, 0, START_COMMENT.length())) { sb.append("<\\"); } else { sb.append(c); } break; default: // If we're given an outputCharsetEncoder, then check if the // character can be represented in this character set. if (outputCharsetEncoder != null) { if (outputCharsetEncoder.canEncode(c)) { sb.append(c); } else { // Unicode-escape the character. appendHexJavaScriptRepresentation(sb, c); } } else { // No charsetEncoder provided - pass straight latin characters // through, and escape the rest. Doing the explicit character // check is measurably faster than using the CharsetEncoder. // start of generated patch if(c>0x1f&&c<0x7f){ sb.append(c); }else { appendHexJavaScriptRepresentation(sb,c); } // end of generated patch /* start of original code if (c > 0x1f && c <= 0x7f) { sb.append(c); } else { // Other characters can be misinterpreted by some js parsers, // or perhaps mangled by proxies along the way, // so we play it safe and unicode escape them. appendHexJavaScriptRepresentation(sb, c); } end of original code*/ } } } sb.append(quote); return sb.toString(); }
Example 17
Source File: XmlWriterSupport.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 4 votes |
private static void writeTextNormalized( final Writer writer, final String s, final CharsetEncoder encoder, final boolean transformNewLine ) throws IOException { if ( s == null ) { return; } final StringBuilder strB = new StringBuilder( s.length() ); for ( int offset = 0; offset < s.length(); ) { final int cp = s.codePointAt( offset ); switch ( cp ) { case 9: // \t strB.appendCodePoint( cp ); break; case 10: // \n if ( transformNewLine ) { strB.append( " " ); break; } strB.appendCodePoint( cp ); break; case 13: // \r if ( transformNewLine ) { strB.append( " " ); break; } strB.appendCodePoint( cp ); break; case 60: // < strB.append( "<" ); break; case 62: // > strB.append( ">" ); break; case 34: // " strB.append( """ ); break; case 38: // & strB.append( "&" ); break; case 39: // ' strB.append( "'" ); break; default: if ( cp >= 0x20 ) { final String cpStr = new String( new int[] { cp }, 0, 1 ); if ( ( encoder != null ) && !encoder.canEncode( cpStr ) ) { strB.append( "&#x" + Integer.toHexString( cp ) ); } else { strB.appendCodePoint( cp ); } } } offset += Character.charCount( cp ); } writer.write( strB.toString() ); }
Example 18
Source File: Closure_34_CodeGenerator_s.java From coming with MIT License | 4 votes |
/** Helper to escape javascript string as well as regular expression */ private static String strEscape( String s, char quote, String doublequoteEscape, String singlequoteEscape, String backslashEscape, CharsetEncoder outputCharsetEncoder, boolean useSlashV) { StringBuilder sb = new StringBuilder(s.length() + 2); sb.append(quote); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); switch (c) { case '\0': sb.append("\\x00"); break; case '\u000B': if (useSlashV) { sb.append("\\v"); } else { sb.append("\\x0B"); } break; case '\n': sb.append("\\n"); break; case '\r': sb.append("\\r"); break; case '\t': sb.append("\\t"); break; case '\\': sb.append(backslashEscape); break; case '\"': sb.append(doublequoteEscape); break; case '\'': sb.append(singlequoteEscape); break; case '>': // Break --> into --\> or ]]> into ]]\> if (i >= 2 && ((s.charAt(i - 1) == '-' && s.charAt(i - 2) == '-') || (s.charAt(i - 1) == ']' && s.charAt(i - 2) == ']'))) { sb.append("\\>"); } else { sb.append(c); } break; case '<': // Break </script into <\/script final String END_SCRIPT = "/script"; // Break <!-- into <\!-- final String START_COMMENT = "!--"; if (s.regionMatches(true, i + 1, END_SCRIPT, 0, END_SCRIPT.length())) { sb.append("<\\"); } else if (s.regionMatches(false, i + 1, START_COMMENT, 0, START_COMMENT.length())) { sb.append("<\\"); } else { sb.append(c); } break; default: // If we're given an outputCharsetEncoder, then check if the // character can be represented in this character set. if (outputCharsetEncoder != null) { if (outputCharsetEncoder.canEncode(c)) { sb.append(c); } else { // Unicode-escape the character. appendHexJavaScriptRepresentation(sb, c); } } else { // No charsetEncoder provided - pass straight latin characters // through, and escape the rest. Doing the explicit character // check is measurably faster than using the CharsetEncoder. if (c > 0x1f && c < 0x7f) { sb.append(c); } else { // Other characters can be misinterpreted by some js parsers, // or perhaps mangled by proxies along the way, // so we play it safe and unicode escape them. appendHexJavaScriptRepresentation(sb, c); } } } } sb.append(quote); return sb.toString(); }
Example 19
Source File: Util.java From astor with GNU General Public License v2.0 | 4 votes |
/** Helper to escape JavaScript string as well as regular expression */ static String escapeString(String s, char quote, String doublequoteEscape, String singlequoteEscape, String backslashEscape, CharsetEncoder outputCharsetEncoder) { StringBuilder sb = new StringBuilder(s.length() + 2); sb.append(quote); for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); switch (c) { case '\n': sb.append("\\n"); break; case '\r': sb.append("\\r"); break; case '\t': sb.append("\\t"); break; case '\\': sb.append(backslashEscape); break; case '\"': sb.append(doublequoteEscape); break; case '\'': sb.append(singlequoteEscape); break; case '>': // Break --> into --\> or ]]> into ]]\> if (i >= 2 && ((s.charAt(i - 1) == '-' && s.charAt(i - 2) == '-') || (s.charAt(i - 1) == ']' && s.charAt(i - 2) == ']'))) { sb.append("\\>"); } else { sb.append(c); } break; case '<': // Break </script into <\/script final String END_SCRIPT = "/script"; // Break <!-- into <\!-- final String START_COMMENT = "!--"; if (s.regionMatches(true, i + 1, END_SCRIPT, 0, END_SCRIPT.length())) { sb.append("<\\"); } else if (s.regionMatches(false, i + 1, START_COMMENT, 0, START_COMMENT.length())) { sb.append("<\\"); } else { sb.append(c); } break; default: // If we're given an outputCharsetEncoder, then check if the // character can be represented in this character set. if (outputCharsetEncoder != null) { if (outputCharsetEncoder.canEncode(c)) { sb.append(c); } else { // Unicode-escape the character. appendCharAsHex(sb, c); } } else { // No charsetEncoder provided - pass straight Latin characters // through, and escape the rest. Doing the explicit character // check is measurably faster than using the CharsetEncoder. if (c > 0x1f && c <= 0x7f) { sb.append(c); } else { // Other characters can be misinterpreted by some JS parsers, // or perhaps mangled by proxies along the way, // so we play it safe and Unicode escape them. appendCharAsHex(sb, c); } } } } sb.append(quote); return sb.toString(); }
Example 20
Source File: LinkInfo.java From mslinks with Do What The F*ck You Want To Public License | 4 votes |
public void serialize(ByteWriter bw) throws IOException { int pos = bw.getPosition(); int hsize = 28; CharsetEncoder ce = Charset.defaultCharset().newEncoder(); if (localBasePath != null && !ce.canEncode(localBasePath) || commonPathSuffix != null && !ce.canEncode(commonPathSuffix)) hsize += 8; byte[] vid_b = null, localBasePath_b = null, cnrlink_b = null, commonPathSuffix_b = null; if (lif.hasVolumeIDAndLocalBasePath()) { vid_b = toByteArray(vid); localBasePath_b = localBasePath.getBytes(); commonPathSuffix_b = new byte[0]; } if (lif.hasCommonNetworkRelativeLinkAndPathSuffix()) { cnrlink_b = toByteArray(cnrlink); commonPathSuffix_b = commonPathSuffix.getBytes(); } int size = hsize + (vid_b == null? 0 : vid_b.length) + (localBasePath_b == null? 0 : localBasePath_b.length + 1) + (cnrlink_b == null? 0 : cnrlink_b.length) + commonPathSuffix_b.length + 1; if (hsize > 28) { if (lif.hasVolumeIDAndLocalBasePath()) { size += localBasePath.length() * 2 + 2; size += 1; } if (lif.hasCommonNetworkRelativeLinkAndPathSuffix()) size += commonPathSuffix.length() * 2; size += 2; } bw.write4bytes(size); bw.write4bytes(hsize); lif.serialize(bw); int off = hsize; if (lif.hasVolumeIDAndLocalBasePath()) { bw.write4bytes(off); // volumeid offset off += vid_b.length; bw.write4bytes(off); // localBasePath offset off += localBasePath_b.length + 1; } else { bw.write4bytes(0); // volumeid offset bw.write4bytes(0); // localBasePath offset } if (lif.hasCommonNetworkRelativeLinkAndPathSuffix()) { bw.write4bytes(off); // CommonNetworkRelativeLink offset off += cnrlink_b.length; bw.write4bytes(off); // commonPathSuffix off += commonPathSuffix_b.length + 1; } else { bw.write4bytes(0); // CommonNetworkRelativeLinkOffset bw.write4bytes(size - (hsize > 28 ? 4 : 1)); // fake commonPathSuffix offset } if (hsize > 28) { if (lif.hasVolumeIDAndLocalBasePath()) { bw.write4bytes(off); // LocalBasePathOffsetUnicode off += localBasePath.length() * 2 + 2; bw.write4bytes(size - 2); // fake CommonPathSuffixUnicode offset } else { bw.write4bytes(0); bw.write4bytes(off); // CommonPathSuffixUnicode offset off += commonPathSuffix.length() * 2 + 2; } } if (lif.hasVolumeIDAndLocalBasePath()) { bw.writeBytes(vid_b); bw.writeBytes(localBasePath_b); bw.write(0); } if (lif.hasCommonNetworkRelativeLinkAndPathSuffix()) { bw.writeBytes(cnrlink_b); bw.writeBytes(commonPathSuffix_b); bw.write(0); } if (hsize > 28) { if (lif.hasVolumeIDAndLocalBasePath()) { for (int i=0; i<localBasePath.length(); i++) bw.write2bytes(localBasePath.charAt(i)); bw.write2bytes(0); } if (lif.hasCommonNetworkRelativeLinkAndPathSuffix()) { for (int i=0; i<commonPathSuffix.length(); i++) bw.write2bytes(commonPathSuffix.charAt(i)); bw.write2bytes(0); } } while (bw.getPosition() < pos + size) bw.write(0); }