Java Code Examples for java.nio.charset.Charset#canEncode()

The following examples show how to use java.nio.charset.Charset#canEncode() . These examples are extracted from open source projects. 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
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);
        }
    }
}
 
Example 2
Source Project: ghidra   File: StringDataInstance.java    License: Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
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);
        }
    }
}
 
Example 4
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);
        }
    }
}
 
Example 5
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);
        }
    }
}
 
Example 6
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);
        }
    }
}
 
Example 7
@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;
}
 
Example 8
Source Project: j2objc   File: CharsetTest.java    License: Apache License 2.0 6 votes vote down vote up
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());
    }
  }
}
 
Example 9
Source Project: consulo   File: PathUtilRt.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 10
Source Project: consulo   File: PathUtilRt.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source Project: rembulan   File: StringByteString.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 12
Source Project: OkapiBarcode   File: EciMode.java    License: Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 13
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;
}
 
Example 14
Source Project: consulo   File: LoadTextUtil.java    License: Apache License 2.0 5 votes vote down vote up
@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)
  }
}
 
Example 15
Source Project: rembulan   File: ByteStringBuilder.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}