Java Code Examples for java.nio.charset.Charset.isSupported()

The following are Jave code examples for showing how to use isSupported() of the java.nio.charset.Charset class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: FJ-VDMJ   File: VDMJ.java   View Source Code Vote up 6 votes
private static String validateCharset(String cs)
{
	if (!Charset.isSupported(cs))
	{
		println("Charset " + cs + " is not supported\n");
		println("Available charsets:");
		println("Default = " + Charset.defaultCharset());
		Map<String,Charset> available = Charset.availableCharsets();

		for (Entry<String, Charset> entry: available.entrySet())
		{
			println(entry.getKey() + " " + available.get(entry.getValue()).aliases());
		}

		println("");
		usage("Charset " + cs + " is not supported");
	}

	return cs;
}
 
Example 2
Project: OpenDiabetes   File: JDBCClobFile.java   View Source Code Vote up 6 votes
protected static Charset charsetForName(final String charsetName)
throws UnsupportedEncodingException {

    String csn = charsetName;

    if (csn == null) {
        csn = Charset.defaultCharset().name();
    }

    try {
        if (Charset.isSupported(csn)) {
            return Charset.forName(csn);
        }
    } catch (IllegalCharsetNameException x) {}

    throw new UnsupportedEncodingException(csn);
}
 
Example 3
Project: marathonv5   File: WriterOutputStream.java   View Source Code Vote up 6 votes
/**
 * Creates WriterOutputStream from given java.io.Writer object with a
 * specified encoding.
 *
 * @param writer
 *            java.io.Writer object to be converted to.
 */
public WriterOutputStream(Writer writer, String encoding) {
    this.writer = writer;
    if (encoding == null && writer instanceof OutputStreamWriter) {
        // this encoding might be null when writer has been closed
        encoding = ((OutputStreamWriter) writer).getEncoding();
    }
    if (encoding == null) {
        encoding = Charset.defaultCharset().name();
    } else if (!Charset.isSupported(encoding)) {
        throw new IllegalArgumentException(encoding + " is not supported");
    }
    decoder = Charset.forName(encoding).newDecoder();
    decoder.onMalformedInput(CodingErrorAction.REPLACE);
    decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
}
 
Example 4
Project: spark-project   File: StringUtils.java   View Source Code Vote up 6 votes
public static String toString(byte[] bytes, String encoding) {
    String str;

    if (encoding != null && Charset.isSupported(encoding)) {
        try {
            str = new String(bytes, encoding);
        } catch (UnsupportedEncodingException e) {
            // Uses same func as Charset.isSupported (cannot happen)
            str = new String(bytes);
        }
    } else {
        str = new String(bytes);
    }

    return str;
}
 
Example 5
Project: jdk8u-jdk   File: LauncherHelper.java   View Source Code Vote up 6 votes
static String makePlatformString(boolean printToStderr, byte[] inArray) {
    initOutput(printToStderr);
    if (encoding == null) {
        encoding = System.getProperty(encprop);
        isCharsetSupported = Charset.isSupported(encoding);
    }
    try {
        String out = isCharsetSupported
                ? new String(inArray, encoding)
                : new String(inArray);
        return out;
    } catch (UnsupportedEncodingException uee) {
        abort(uee, null);
    }
    return null; // keep the compiler happy
}
 
Example 6
Project: ipack   File: LocalizedMessage.java   View Source Code Vote up 6 votes
/**
 * Constructs a new LocalizedMessage using <code>resource</code> as the base name for the 
 * RessourceBundle and <code>id</code> as the message bundle id the resource file. 
 * @param resource base name of the resource file 
 * @param id the id of the corresponding bundle in the resource file
 * @param encoding the encoding of the resource file
 * @param arguments an array containing the arguments for the message
 * @throws NullPointerException if <code>resource</code> or <code>id</code> is <code>null</code>
 * @throws UnsupportedEncodingException if the encoding is not supported
 */
public LocalizedMessage(String resource, String id, String encoding, Object[] arguments) throws NullPointerException, UnsupportedEncodingException
{
    if (resource == null || id == null || arguments == null)
    {
        throw new NullPointerException();
    }
    this.id = id;
    this.resource = resource;
    this.arguments = new FilteredArguments(arguments);
    if (!Charset.isSupported(encoding))
    {
        throw new UnsupportedEncodingException("The encoding \"" + encoding + "\" is not supported.");
    }
    this.encoding = encoding;
}
 
Example 7
Project: openjdk-jdk10   File: StreamSOAPCodec.java   View Source Code Vote up 6 votes
public void decode(InputStream in, String contentType, Packet packet, AttachmentSet att ) throws IOException {
    List<String> expectedContentTypes = getExpectedContentTypes();
    if (contentType != null && !isContentTypeSupported(contentType,expectedContentTypes)) {
        throw new UnsupportedMediaException(contentType, expectedContentTypes);
    }
    com.oracle.webservices.internal.api.message.ContentType pct = packet.getInternalContentType();
    ContentTypeImpl cti = (pct != null && pct instanceof ContentTypeImpl) ?
            (ContentTypeImpl)pct : new ContentTypeImpl(contentType);
    String charset = cti.getCharSet();
    if (charset != null && !Charset.isSupported(charset)) {
        throw new UnsupportedMediaException(charset);
    }
    if (charset != null) {
        packet.invocationProperties.put(DECODED_MESSAGE_CHARSET, charset);
    } else {
        packet.invocationProperties.remove(DECODED_MESSAGE_CHARSET);
    }
    packet.setMessage(streamDecoder.decode(in, charset, att, soapVersion));
}
 
Example 8
Project: dev-courses   File: JDBCClobClient.java   View Source Code Vote up 6 votes
protected static Charset charsetForName(
        final String charsetName) throws SQLException {

    String csn = charsetName;

    if (csn == null) {
        csn = Charset.defaultCharset().name();
    }

    try {
        if (Charset.isSupported(csn)) {
            return Charset.forName(csn);
        }
    } catch (IllegalCharsetNameException x) {
    }

    throw JDBCUtil.sqlException(new UnsupportedEncodingException(csn));
}
 
Example 9
Project: OpenJSharp   File: StreamSOAPCodec.java   View Source Code Vote up 6 votes
public void decode(InputStream in, String contentType, Packet packet, AttachmentSet att ) throws IOException {
    List<String> expectedContentTypes = getExpectedContentTypes();
    if (contentType != null && !isContentTypeSupported(contentType,expectedContentTypes)) {
        throw new UnsupportedMediaException(contentType, expectedContentTypes);
    }
    com.oracle.webservices.internal.api.message.ContentType pct = packet.getInternalContentType();
    ContentTypeImpl cti = (pct != null && pct instanceof ContentTypeImpl) ?
            (ContentTypeImpl)pct : new ContentTypeImpl(contentType);
    String charset = cti.getCharSet();
    if (charset != null && !Charset.isSupported(charset)) {
        throw new UnsupportedMediaException(charset);
    }
    if (charset != null) {
        packet.invocationProperties.put(DECODED_MESSAGE_CHARSET, charset);
    } else {
        packet.invocationProperties.remove(DECODED_MESSAGE_CHARSET);
    }
    packet.setMessage(streamDecoder.decode(in, charset, att, soapVersion));
}
 
Example 10
Project: openjdk-jdk10   File: CharsetTest.java   View Source Code Vote up 5 votes
private static void testNegative(String csn) {
    if (Charset.isSupported(csn))
        fail(csn, "Supported");
    if (available.containsKey(csn))
        fail(csn, "Available");
    try {
        Charset.forName(csn);
    } catch (UnsupportedCharsetException x) {
        out.println(csn + " not supported, as expected");
        return;
    }
    fail(csn, "Lookup succeeded");
}
 
Example 11
Project: Reer   File: DefaultManifest.java   View Source Code Vote up 5 votes
@Override
public void setContentCharset(String contentCharset) {
    if (contentCharset == null) {
        throw new InvalidUserDataException("contentCharset must not be null");
    }
    if (!Charset.isSupported(contentCharset)) {
        throw new InvalidUserDataException(String.format("Charset for contentCharset '%s' is not supported by your JVM", contentCharset));
    }
    this.contentCharset = contentCharset;
}
 
Example 12
Project: Reer   File: DefaultManifestMergeSpec.java   View Source Code Vote up 5 votes
@Override
public void setContentCharset(String contentCharset) {
    if (contentCharset == null) {
        throw new InvalidUserDataException("contentCharset must not be null");
    }
    if (!Charset.isSupported(contentCharset)) {
        throw new InvalidUserDataException(String.format("Charset for contentCharset '%s' is not supported by your JVM", contentCharset));
    }
    this.contentCharset = contentCharset;
}
 
Example 13
Project: openjdk-jdk10   File: DataFlavorUtil.java   View Source Code Vote up 5 votes
/**
 * Determines whether this JRE can both encode and decode text in the
 * specified encoding.
 */
private static boolean isEncodingSupported(String encoding) {
    if (encoding == null) {
        return false;
    }
    try {
        return Charset.isSupported(encoding);
    } catch (IllegalCharsetNameException icne) {
        return false;
    }
}
 
Example 14
Project: CSS-Editor-FX   File: Options.java   View Source Code Vote up 5 votes
private static Charset safeCharSet(String charsetName) {
  if (Charset.isSupported(charsetName)) {
    return Charset.forName(charsetName);
  } else {
    return Charset.defaultCharset();
  }
}
 
Example 15
Project: openjdk-jdk10   File: MtomCodec.java   View Source Code Vote up 5 votes
@Override
protected void decode(MimeMultipartParser mpp, Packet packet) throws IOException {
    //TODO shouldn't we check for SOAP1.1/SOAP1.2 and throw
    //TODO UnsupportedMediaException like StreamSOAPCodec
    String charset = null;
    String ct = mpp.getRootPart().getContentType();
    if (ct != null) {
        charset = new ContentTypeImpl(ct).getCharSet();
    }
    if (charset != null && !Charset.isSupported(charset)) {
        throw new UnsupportedMediaException(charset);
    }

    if (charset != null) {
        packet.invocationProperties.put(DECODED_MESSAGE_CHARSET, charset);
    } else {
        packet.invocationProperties.remove(DECODED_MESSAGE_CHARSET);
    }

    // we'd like to reuse those reader objects but unfortunately decoder may be reused
    // before the decoded message is completely used.
    XMLStreamReader mtomReader = new MtomXMLStreamReaderEx( mpp,
        XMLStreamReaderFactory.create(null, mpp.getRootPart().asInputStream(), charset, true)
    );

    packet.setMessage(codec.decode(mtomReader, new MimeAttachmentSet(mpp)));
    packet.setMtomFeature(mtomFeature);
    packet.setContentType(mpp.getContentType());
}
 
Example 16
Project: jdk8u-jdk   File: Main.java   View Source Code Vote up 5 votes
private static Charset lookupCharset(String csName) {
    if (Charset.isSupported(csName)) {
       try {
            return Charset.forName(csName);
       } catch (UnsupportedCharsetException x) {
            throw new Error(x);
       }
    }
    return null;
}
 
Example 17
Project: jdk8u-jdk   File: DataTransferer.java   View Source Code Vote up 5 votes
/**
 * Determines whether this JRE can both encode and decode text in the
 * specified encoding.
 */
public static boolean isEncodingSupported(String encoding) {
    if (encoding == null) {
        return false;
    }
    try {
        return Charset.isSupported(encoding);
    } catch (IllegalCharsetNameException icne) {
        return false;
    }
}
 
Example 18
Project: Phoenix-for-VK   File: TitleExtractor.java   View Source Code Vote up 5 votes
private static Charset getCharset(ContentType contentType) {
    if (contentType != null && contentType.charsetName != null && Charset.isSupported(contentType.charsetName)) {
        return Charset.forName(contentType.charsetName);
    } else {
        return null;
    }
}
 
Example 19
Project: tomcat7   File: AddDefaultCharsetFilter.java   View Source Code Vote up 5 votes
@Override
public void init(FilterConfig filterConfig) throws ServletException {
    super.init(filterConfig);
    if (encoding == null || encoding.length() == 0 ||
            encoding.equalsIgnoreCase("default")) {
        encoding = DEFAULT_ENCODING;
    } else if (encoding.equalsIgnoreCase("system")) {
        encoding = Charset.defaultCharset().name();
    } else if (!Charset.isSupported(encoding)) {
        throw new IllegalArgumentException(sm.getString(
                "addDefaultCharset.unsupportedCharset", encoding));
    }
}
 
Example 20
Project: java-debug   File: LaunchRequestHandler.java   View Source Code Vote up 4 votes
@Override
public CompletableFuture<Response> handle(Command command, Arguments arguments, Response response, IDebugAdapterContext context) {
    LaunchArguments launchArguments = (LaunchArguments) arguments;
    if (StringUtils.isBlank(launchArguments.mainClass)
            || ArrayUtils.isEmpty(launchArguments.modulePaths) && ArrayUtils.isEmpty(launchArguments.classPaths)) {
        return AdapterUtils.createAsyncErrorResponse(response, ErrorCode.ARGUMENT_MISSING,
                   String.format("Failed to launch debuggee VM. Missing mainClass or modulePaths/classPaths options in launch configuration"));
    }

    context.setAttached(false);
    context.setSourcePaths(launchArguments.sourcePaths);
    context.setVmStopOnEntry(launchArguments.stopOnEntry);
    context.setMainClass(parseMainClassWithoutModuleName(launchArguments.mainClass));
    context.setStepFilters(launchArguments.stepFilters);

    if (StringUtils.isBlank(launchArguments.encoding)) {
        context.setDebuggeeEncoding(StandardCharsets.UTF_8);
    } else {
        if (!Charset.isSupported(launchArguments.encoding)) {
            return AdapterUtils.createAsyncErrorResponse(response, ErrorCode.INVALID_ENCODING,
                        String.format("Failed to launch debuggee VM. 'encoding' options in the launch configuration is not recognized."));
        }

        context.setDebuggeeEncoding(Charset.forName(launchArguments.encoding));
    }

    if (StringUtils.isBlank(launchArguments.vmArgs)) {
        launchArguments.vmArgs = String.format("-Dfile.encoding=%s", context.getDebuggeeEncoding().name());
    } else {
        // if vmArgs already has the file.encoding settings, duplicate options for jvm will not cause an error, the right most value wins
        launchArguments.vmArgs = String.format("%s -Dfile.encoding=%s", launchArguments.vmArgs, context.getDebuggeeEncoding().name());
    }

    return launch(launchArguments, response, context).thenCompose(res -> {
        if (res.success) {
            ISourceLookUpProvider sourceProvider = context.getProvider(ISourceLookUpProvider.class);
            Map<String, Object> options = new HashMap<>();
            options.put(Constants.DEBUGGEE_ENCODING, context.getDebuggeeEncoding());
            if (launchArguments.projectName != null) {
                options.put(Constants.PROJECTNAME, launchArguments.projectName);
            }
            sourceProvider.initialize(context, options);
            IEvaluationProvider evaluationProvider = context.getProvider(IEvaluationProvider.class);
            evaluationProvider.initialize(context, options);
            IHotCodeReplaceProvider hcrProvider = context.getProvider(IHotCodeReplaceProvider.class);
            hcrProvider.initialize(context, options);

            // Send an InitializedEvent to indicate that the debugger is ready to accept configuration requests
            // (e.g. SetBreakpointsRequest, SetExceptionBreakpointsRequest).
            context.getProtocolServer().sendEvent(new Events.InitializedEvent());
        }
        return CompletableFuture.completedFuture(res);
    });
}