java.nio.charset.Charset#canEncode ( )源码实例Demo

下面列出了java.nio.charset.Charset#canEncode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。


public static void main(String[] args) throws IOException {
    SortedMap<String, Charset> map = Charset.availableCharsets();
    for (String name : map.keySet()) {
        Charset charset = map.get(name);
        if (charset.canEncode() && !charset.name().equals("x-COMPOUND_TEXT")) {
            testNormalSurrogate(charset, NORMAL_SURROGATE);
            testMalformedSurrogate(charset, MALFORMED_SURROGATE);
            testMalformedSurrogate(charset, REVERSED_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_HIGH_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_LOW_SURROGATE);
            testSurrogateWithReplacement(charset, NORMAL_SURROGATE);
            testSurrogateWithReplacement(charset, MALFORMED_SURROGATE);
            testSurrogateWithReplacement(charset, REVERSED_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_HIGH_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_LOW_SURROGATE);
        }
    }
}
 
源代码2 项目: ghidra   文件: StringDataInstance.java

private byte[] getOriginalBytes(boolean isByteToStringCharEquiv, int charOffset, int codePoint,
		byte[] stringBytes, AdjustedCharsetInfo aci) {

	if (isByteToStringCharEquiv) {
		byte[] originalCharBytes = new byte[charSize];
		System.arraycopy(stringBytes, charOffset * charSize + aci.byteStartOffset,
			originalCharBytes, 0, charSize);
		return originalCharBytes;
	}

	// can't get original bytes, cheat and run the codePoint through the charset
	// to get what should be the same as the original bytes.
	String singleCharStr = new String(new int[] { codePoint }, 0, 1);
	Charset cs = Charset.isSupported(aci.charsetName) ? Charset.forName(aci.charsetName) : null;
	if (cs == null || !cs.canEncode()) {
		return null;
	}
	return singleCharStr.getBytes(cs);
}
 

public static void main(String[] args) throws IOException {
    SortedMap<String, Charset> map = Charset.availableCharsets();
    for (String name : map.keySet()) {
        Charset charset = map.get(name);
        if (charset.canEncode() && !charset.name().equals("x-COMPOUND_TEXT")) {
            testNormalSurrogate(charset, NORMAL_SURROGATE);
            testMalformedSurrogate(charset, MALFORMED_SURROGATE);
            testMalformedSurrogate(charset, REVERSED_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_HIGH_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_LOW_SURROGATE);
            testSurrogateWithReplacement(charset, NORMAL_SURROGATE);
            testSurrogateWithReplacement(charset, MALFORMED_SURROGATE);
            testSurrogateWithReplacement(charset, REVERSED_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_HIGH_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_LOW_SURROGATE);
        }
    }
}
 

public static void main(String[] args) throws IOException {
    SortedMap<String, Charset> map = Charset.availableCharsets();
    for (String name : map.keySet()) {
        Charset charset = map.get(name);
        if (charset.canEncode() && !charset.name().equals("x-COMPOUND_TEXT")) {
            testNormalSurrogate(charset, NORMAL_SURROGATE);
            testMalformedSurrogate(charset, MALFORMED_SURROGATE);
            testMalformedSurrogate(charset, REVERSED_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_HIGH_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_LOW_SURROGATE);
            testSurrogateWithReplacement(charset, NORMAL_SURROGATE);
            testSurrogateWithReplacement(charset, MALFORMED_SURROGATE);
            testSurrogateWithReplacement(charset, REVERSED_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_HIGH_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_LOW_SURROGATE);
        }
    }
}
 

public static void main(String[] args) throws IOException {
    SortedMap<String, Charset> map = Charset.availableCharsets();
    for (String name : map.keySet()) {
        Charset charset = map.get(name);
        if (charset.canEncode() && !charset.name().equals("x-COMPOUND_TEXT")) {
            testNormalSurrogate(charset, NORMAL_SURROGATE);
            testMalformedSurrogate(charset, MALFORMED_SURROGATE);
            testMalformedSurrogate(charset, REVERSED_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_HIGH_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_LOW_SURROGATE);
            testSurrogateWithReplacement(charset, NORMAL_SURROGATE);
            testSurrogateWithReplacement(charset, MALFORMED_SURROGATE);
            testSurrogateWithReplacement(charset, REVERSED_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_HIGH_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_LOW_SURROGATE);
        }
    }
}
 
源代码6 项目: jdk8u_jdk   文件: MalformedSurrogates.java

public static void main(String[] args) throws IOException {
    SortedMap<String, Charset> map = Charset.availableCharsets();
    for (String name : map.keySet()) {
        Charset charset = map.get(name);
        if (charset.canEncode() && !charset.name().equals("x-COMPOUND_TEXT")) {
            testNormalSurrogate(charset, NORMAL_SURROGATE);
            testMalformedSurrogate(charset, MALFORMED_SURROGATE);
            testMalformedSurrogate(charset, REVERSED_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_HIGH_SURROGATE);
            testMalformedSurrogate(charset, SOLITARY_LOW_SURROGATE);
            testSurrogateWithReplacement(charset, NORMAL_SURROGATE);
            testSurrogateWithReplacement(charset, MALFORMED_SURROGATE);
            testSurrogateWithReplacement(charset, REVERSED_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_HIGH_SURROGATE);
            testSurrogateWithReplacement(charset, SOLITARY_LOW_SURROGATE);
        }
    }
}
 

@Override
public boolean accept(Charset charset) {
  if (charset.canEncode()) {
    try {
      ByteBuffer bf = charset.encode("\n");
      if (bf.limit() != 1 || bf.get() != (byte) '\n') {
        return false;
      }
      bf = charset.encode("\r");
      if (bf.limit() != 1 || bf.get() != (byte) '\r') {
        return false;
      }
    } catch (Exception ex) {
      return false;
    }
  } else {
    return false;
  }
  return true;
}
 
源代码8 项目: j2objc   文件: CharsetTest.java

public void test_allAvailableCharsets() throws Exception {
  // Check that we can instantiate every Charset, CharsetDecoder, and CharsetEncoder.
  for (String charsetName : Charset.availableCharsets().keySet()) {
    if (charsetName.equals("UTF-32")) {
      // Our UTF-32 is broken. http://b/2702411
      // TODO: remove this hack when UTF-32 is fixed.
      continue;
    }

    Charset cs = Charset.forName(charsetName);
    assertNotNull(cs.newDecoder());
    if (cs.canEncode()) {
      CharsetEncoder enc = cs.newEncoder();
      assertNotNull(enc);
      assertNotNull(enc.replacement());
    }
  }
}
 
源代码9 项目: consulo   文件: PathUtilRt.java

/**
 * Checks whether a file with the given name can be created on a platform specified by given parameters.
 * <p>
 * Platform restrictions:<br>
 * {@code Platform.UNIX} prohibits empty names, traversals (".", ".."), and names containing '/' or '\' characters.<br>
 * {@code Platform.WINDOWS} prohibits empty names, traversals (".", ".."), reserved names ("CON", "NUL", "COM1" etc.),
 * and names containing any of characters {@code <>:"/\|?*} or control characters (range 0..31)
 * (<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx">more info</a>).
 *
 * @param os     specifies a platform.
 * @param strict prohibits names containing any of characters {@code <>:"/\|?*;} and control characters (range 0..31).
 * @param cs     prohibits names which cannot be encoded by this charset (optional).
 */
public static boolean isValidFileName(@Nonnull String name, @Nonnull Platform os, boolean strict, @javax.annotation.Nullable Charset cs) {
  if (name.length() == 0 || name.equals(".") || name.equals("..")) {
    return false;
  }

  for (int i = 0; i < name.length(); i++) {
    if (!isValidFileNameChar(name.charAt(i), os, strict)) {
      return false;
    }
  }

  if (os == Platform.WINDOWS && name.length() >= 3 && name.length() <= 4 && WINDOWS_NAMES.contains(name.toUpperCase(Locale.US))) {
    return false;
  }

  if (cs != null && !(cs.canEncode() && cs.newEncoder().canEncode(name))) {
    return false;
  }

  return true;
}
 
源代码10 项目: consulo   文件: PathUtilRt.java

/**
 * Checks whether a file with the given name can be created on a platform specified by given parameters.
 * <p>
 * Platform restrictions:<br>
 * {@code Platform.UNIX} prohibits empty names, traversals (".", ".."), and names containing '/' or '\' characters.<br>
 * {@code Platform.WINDOWS} prohibits empty names, traversals (".", ".."), reserved names ("CON", "NUL", "COM1" etc.),
 * and names containing any of characters {@code <>:"/\|?*} or control characters (range 0..31)
 * (<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx">more info</a>).
 *
 * @param os     specifies a platform.
 * @param strict prohibits names containing any of characters {@code <>:"/\|?*;} and control characters (range 0..31).
 * @param cs     prohibits names which cannot be encoded by this charset (optional).
 */
public static boolean isValidFileName(@Nonnull String name, @Nonnull Platform os, boolean strict, @javax.annotation.Nullable Charset cs) {
  if (name.length() == 0 || name.equals(".") || name.equals("..")) {
    return false;
  }

  for (int i = 0; i < name.length(); i++) {
    if (!isValidFileNameChar(name.charAt(i), os, strict)) {
      return false;
    }
  }

  if (os == Platform.WINDOWS && name.length() >= 3 && name.length() <= 4 && WINDOWS_NAMES.contains(name.toUpperCase(Locale.US))) {
    return false;
  }

  if (cs != null && !(cs.canEncode() && cs.newEncoder().canEncode(name))) {
    return false;
  }

  return true;
}
 
源代码11 项目: rembulan   文件: StringByteString.java

StringByteString(String s, Charset charset) {
	this.string = Objects.requireNonNull(s);
	this.charset = Objects.requireNonNull(charset);
	if (!charset.canEncode()) {
		throw new IllegalArgumentException("Charset cannot encode: " + charset.name());
	}
	this.byteHashCode = 0;
	this.byteLength = string.isEmpty() ? 0 : -1;
}
 
源代码12 项目: OkapiBarcode   文件: EciMode.java

public static EciMode of(String data, String charsetName, int mode) {
    try {
        Charset charset = Charset.forName(charsetName);
        if (charset.canEncode() && charset.newEncoder().canEncode(data)) {
            return new EciMode(mode, charset);
        } else {
            return NONE;
        }
    } catch (UnsupportedCharsetException e) {
        return NONE;
    }
}
 
源代码13 项目: pentaho-reporting   文件: CleanFunction.java

private String pruneUnprintableChars( final TypeRegistry typeRegistry,
                                      final String result,
                                      final Type typeEncoding,
                                      final Object valueEncoding )
  throws EvaluationException {
  final String encoding = typeRegistry.convertToText( typeEncoding, valueEncoding );
  if ( StringUtils.isEmpty( encoding ) ) {
    return result;
  }

  if ( !Charset.isSupported( encoding ) ) {
    throw EvaluationException.getInstance( LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE );
  }

  final Charset charset = Charset.forName( encoding );
  if ( charset.canEncode() ) {
    final CharsetEncoder charsetEncoder = charset.newEncoder();
    final char[] chars = result.toCharArray();
    final StringBuffer b = new StringBuffer( chars.length );
    for ( int i = 0; i < chars.length; i++ ) {
      final char c = chars[ i ];
      if ( charsetEncoder.canEncode( c ) ) {
        b.append( c );
      }
    }
    return b.toString();
  }
  return result;
}
 
源代码14 项目: consulo   文件: LoadTextUtil.java

@Nullable
private static byte[] isSupported(@Nonnull Charset charset, @Nonnull String str) {
  try {
    if (!charset.canEncode()) return null;
    byte[] bytes = str.getBytes(charset);
    if (!str.equals(new String(bytes, charset))) {
      return null;
    }

    return bytes;
  }
  catch (Exception e) {
    return null;//wow, some charsets throw NPE inside .getBytes() when unable to encode (JIS_X0212-1990)
  }
}
 
源代码15 项目: rembulan   文件: ByteStringBuilder.java

/**
 * Appends a char sequence {@code charSequence} interpreted as a sequence
 * of bytes using the specified {@code Charset}.
 *
 * @param charSequence  the char sequence to append, must not be {@code null}
 * @param charset  the charset to use for encoding, must not be {@code null}
 * @return  this builder
 *
 * @throws NullPointerException  if {@code string} is {@code null}
 * @throws IllegalArgumentException  if {@code charset} cannot does not provide encoding
 *                                   capability (see {@link Charset#canEncode()})
 */
public ByteStringBuilder append(CharSequence charSequence, Charset charset) {
	if (!charset.canEncode()) {
		throw new IllegalArgumentException("Charset cannot encode: " + charset.name());
	}

	// FIXME: inefficient, could be done more directly
	append(ByteString.of(charSequence.toString(), charset));
	return this;
}