java.nio.charset.IllegalCharsetNameException Java Examples

The following examples show how to use java.nio.charset.IllegalCharsetNameException. 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: StringCoding.java    From jdk1.8-source-analysis with Apache License 2.0 7 votes vote down vote up
static byte[] encode(String charsetName, char[] ca, int off, int len)
    throws UnsupportedEncodingException
{
    StringEncoder se = deref(encoder);
    String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
    if ((se == null) || !(csn.equals(se.requestedCharsetName())
                          || csn.equals(se.charsetName()))) {
        se = null;
        try {
            Charset cs = lookupCharset(csn);
            if (cs != null)
                se = new StringEncoder(cs, csn);
        } catch (IllegalCharsetNameException x) {}
        if (se == null)
            throw new UnsupportedEncodingException (csn);
        set(encoder, se);
    }
    return se.encode(ca, off, len);
}
 
Example #2
Source File: CodeSetConversion.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public JavaCTBConverter(OSFCodeSetRegistry.Entry codeset,
                        int alignmentForEncoding) {

    try {
        ctb = cache.getCharToByteConverter(codeset.getName());
        if (ctb == null) {
            Charset tmpCharset = Charset.forName(codeset.getName());
            ctb = tmpCharset.newEncoder();
            cache.setConverter(codeset.getName(), ctb);
        }
    } catch(IllegalCharsetNameException icne) {

        // This can only happen if one of our Entries has
        // an invalid name.
        throw wrapper.invalidCtbConverterName(icne,codeset.getName());
    } catch(UnsupportedCharsetException ucne) {

        // This can only happen if one of our Entries has
        // an unsupported name.
        throw wrapper.invalidCtbConverterName(ucne,codeset.getName());
    }

    this.codeset = codeset;
    alignment = alignmentForEncoding;
}
 
Example #3
Source File: StringCoding.java    From jdk-1.7-annotated with Apache License 2.0 6 votes vote down vote up
static byte[] encode(String charsetName, char[] ca, int off, int len)
    throws UnsupportedEncodingException
{
    StringEncoder se = deref(encoder);
    String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
    if ((se == null) || !(csn.equals(se.requestedCharsetName())
                          || csn.equals(se.charsetName()))) {
        se = null;
        try {
            Charset cs = lookupCharset(csn);
            if (cs != null)
                se = new StringEncoder(cs, csn);
        } catch (IllegalCharsetNameException x) {}
        if (se == null)
            throw new UnsupportedEncodingException (csn);
        set(encoder, se);
    }
    return se.encode(ca, off, len);
}
 
Example #4
Source File: CodeSetConversion.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Utility method to find a CharsetDecoder in the
 * cache or create a new one if necessary.  Throws an
 * INTERNAL if the code set is unknown.
 */
protected CharsetDecoder getConverter(String javaCodeSetName) {

    CharsetDecoder result = null;
    try {
        result = cache.getByteToCharConverter(javaCodeSetName);

        if (result == null) {
            Charset tmpCharset = Charset.forName(javaCodeSetName);
            result = tmpCharset.newDecoder();
            cache.setConverter(javaCodeSetName, result);
        }

    } catch(IllegalCharsetNameException icne) {
        // This can only happen if one of our charset entries has
        // an illegal name.
        throw wrapper.invalidBtcConverterName( icne, javaCodeSetName ) ;
    }

    return result;
}
 
Example #5
Source File: DataUtil.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parse out a charset from a content type header. If the charset is not supported, returns null (so the default
 * will kick in.)
 * @param contentType e.g. "text/html; charset=EUC-JP"
 * @return "EUC-JP", or null if not found. Charset is trimmed and uppercased.
 */
static String getCharsetFromContentType(String contentType) {
    if (contentType == null) return null;
    Matcher m = charsetPattern.matcher(contentType);
    if (m.find()) {
        String charset = m.group(1).trim();
        charset = charset.replace("charset=", "");
        if (charset.length() == 0) return null;
        try {
            if (Charset.isSupported(charset)) return charset;
            charset = charset.toUpperCase(Locale.ENGLISH);
            if (Charset.isSupported(charset)) return charset;
        } catch (IllegalCharsetNameException e) {
            // if our advanced charset matching fails.... we just take the default
            return null;
        }
    }
    return null;
}
 
Example #6
Source File: StringCoding.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static char[] decode(String charsetName, byte[] ba, int off, int len)
    throws UnsupportedEncodingException
{
    StringDecoder sd = deref(decoder);
    String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
    if ((sd == null) || !(csn.equals(sd.requestedCharsetName())
                          || csn.equals(sd.charsetName()))) {
        sd = null;
        try {
            Charset cs = lookupCharset(csn);
            if (cs != null)
                sd = new StringDecoder(cs, csn);
        } catch (IllegalCharsetNameException x) {}
        if (sd == null)
            throw new UnsupportedEncodingException(csn);
        set(decoder, sd);
    }
    return sd.decode(ba, off, len);
}
 
Example #7
Source File: CodeSetConversion.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Utility method to find a CharsetDecoder in the
 * cache or create a new one if necessary.  Throws an
 * INTERNAL if the code set is unknown.
 */
protected CharsetDecoder getConverter(String javaCodeSetName) {

    CharsetDecoder result = null;
    try {
        result = cache.getByteToCharConverter(javaCodeSetName);

        if (result == null) {
            Charset tmpCharset = Charset.forName(javaCodeSetName);
            result = tmpCharset.newDecoder();
            cache.setConverter(javaCodeSetName, result);
        }

    } catch(IllegalCharsetNameException icne) {
        // This can only happen if one of our charset entries has
        // an illegal name.
        throw wrapper.invalidBtcConverterName( icne, javaCodeSetName ) ;
    }

    return result;
}
 
Example #8
Source File: StringCoding.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
static byte[] encode(String charsetName, char[] ca, int off, int len)
    throws UnsupportedEncodingException
{
    StringEncoder se = deref(encoder);
    String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
    if ((se == null) || !(csn.equals(se.requestedCharsetName())
                          || csn.equals(se.charsetName()))) {
        se = null;
        try {
            Charset cs = lookupCharset(csn);
            if (cs != null)
                se = new StringEncoder(cs, csn);
        } catch (IllegalCharsetNameException x) {}
        if (se == null)
            throw new UnsupportedEncodingException (csn);
        set(encoder, se);
    }
    return se.encode(ca, off, len);
}
 
Example #9
Source File: CodeSetConversion.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public JavaCTBConverter(OSFCodeSetRegistry.Entry codeset,
                        int alignmentForEncoding) {

    try {
        ctb = cache.getCharToByteConverter(codeset.getName());
        if (ctb == null) {
            Charset tmpCharset = Charset.forName(codeset.getName());
            ctb = tmpCharset.newEncoder();
            cache.setConverter(codeset.getName(), ctb);
        }
    } catch(IllegalCharsetNameException icne) {

        // This can only happen if one of our Entries has
        // an invalid name.
        throw wrapper.invalidCtbConverterName(icne,codeset.getName());
    } catch(UnsupportedCharsetException ucne) {

        // This can only happen if one of our Entries has
        // an unsupported name.
        throw wrapper.invalidCtbConverterName(ucne,codeset.getName());
    }

    this.codeset = codeset;
    alignment = alignmentForEncoding;
}
 
Example #10
Source File: BinaryContent.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"EmptyCatchBlock"})
@Nullable
public Document getDocument() {
  if (myDocument == null) {
    if (isBinary()) return null;

    String text = null;
    try {
      Charset charset = ObjectUtil
              .notNull(myCharset, EncodingProjectManager.getInstance(myProject).getDefaultCharset());
      text = CharsetToolkit.bytesToString(myBytes, charset);
    }
    catch (IllegalCharsetNameException e) {
    }

    //  Still NULL? only if not supported or an exception was thrown.
    //  Decode a string using the truly default encoding.
    if (text == null) text = new String(myBytes);
    text = LineTokenizer.correctLineSeparators(text);

    myDocument = EditorFactory.getInstance().createDocument(text);
    myDocument.setReadOnly(true);
  }
  return myDocument;
}
 
Example #11
Source File: StringCoding.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static byte[] encode(String charsetName, char[] ca, int off, int len)
    throws UnsupportedEncodingException
{
    StringEncoder se = deref(encoder);
    String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
    if ((se == null) || !(csn.equals(se.requestedCharsetName())
                          || csn.equals(se.charsetName()))) {
        se = null;
        try {
            Charset cs = lookupCharset(csn);
            if (cs != null)
                se = new StringEncoder(cs, csn);
        } catch (IllegalCharsetNameException x) {}
        if (se == null)
            throw new UnsupportedEncodingException (csn);
        set(encoder, se);
    }
    return se.encode(ca, off, len);
}
 
Example #12
Source File: FileActivityConsumer.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
private void handleFileActivity(FileActivity activity)
    throws CoreException, IOException, IllegalCharsetNameException, UnsupportedCharsetException {

  if (activity.isRecovery()) {
    handleFileRecovery(activity);
    return;
  }

  // TODO check if we should open / close existing editors here too
  switch (activity.getType()) {
    case CREATED:
      handleFileCreation(activity);
      break;
    case REMOVED:
      handleFileDeletion(activity);
      break;
    case MOVED:
      handleFileMove(activity);
      break;
  }
}
 
Example #13
Source File: CodeSetConversion.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public JavaCTBConverter(OSFCodeSetRegistry.Entry codeset,
                        int alignmentForEncoding) {

    try {
        ctb = cache.getCharToByteConverter(codeset.getName());
        if (ctb == null) {
            Charset tmpCharset = Charset.forName(codeset.getName());
            ctb = tmpCharset.newEncoder();
            cache.setConverter(codeset.getName(), ctb);
        }
    } catch(IllegalCharsetNameException icne) {

        // This can only happen if one of our Entries has
        // an invalid name.
        throw wrapper.invalidCtbConverterName(icne,codeset.getName());
    } catch(UnsupportedCharsetException ucne) {

        // This can only happen if one of our Entries has
        // an unsupported name.
        throw wrapper.invalidCtbConverterName(ucne,codeset.getName());
    }

    this.codeset = codeset;
    alignment = alignmentForEncoding;
}
 
Example #14
Source File: StringCoding.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
static byte[] encode(String charsetName, char[] ca, int off, int len)
    throws UnsupportedEncodingException
{
    StringEncoder se = deref(encoder);
    String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
    if ((se == null) || !(csn.equals(se.requestedCharsetName())
                          || csn.equals(se.charsetName()))) {
        se = null;
        try {
            Charset cs = lookupCharset(csn);
            if (cs != null)
                se = new StringEncoder(cs, csn);
        } catch (IllegalCharsetNameException x) {}
        if (se == null)
            throw new UnsupportedEncodingException (csn);
        set(encoder, se);
    }
    return se.encode(ca, off, len);
}
 
Example #15
Source File: StringCoding.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static byte[] encode(String charsetName, char[] ca, int off, int len)
    throws UnsupportedEncodingException
{
    StringEncoder se = deref(encoder);
    String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
    if ((se == null) || !(csn.equals(se.requestedCharsetName())
                          || csn.equals(se.charsetName()))) {
        se = null;
        try {
            Charset cs = lookupCharset(csn);
            if (cs != null)
                se = new StringEncoder(cs, csn);
        } catch (IllegalCharsetNameException x) {}
        if (se == null)
            throw new UnsupportedEncodingException (csn);
        set(encoder, se);
    }
    return se.encode(ca, off, len);
}
 
Example #16
Source File: CodeSetConversion.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public JavaCTBConverter(OSFCodeSetRegistry.Entry codeset,
                        int alignmentForEncoding) {

    try {
        ctb = cache.getCharToByteConverter(codeset.getName());
        if (ctb == null) {
            Charset tmpCharset = Charset.forName(codeset.getName());
            ctb = tmpCharset.newEncoder();
            cache.setConverter(codeset.getName(), ctb);
        }
    } catch(IllegalCharsetNameException icne) {

        // This can only happen if one of our Entries has
        // an invalid name.
        throw wrapper.invalidCtbConverterName(icne,codeset.getName());
    } catch(UnsupportedCharsetException ucne) {

        // This can only happen if one of our Entries has
        // an unsupported name.
        throw wrapper.invalidCtbConverterName(ucne,codeset.getName());
    }

    this.codeset = codeset;
    alignment = alignmentForEncoding;
}
 
Example #17
Source File: OptionsFactory.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private String createEncoding(String encoding)
		throws MojoExecutionException {
	if (encoding == null) {
		return null;
	}
	try {
		if (!Charset.isSupported(encoding)) {
			throw new MojoExecutionException(MessageFormat.format(
					"Unsupported encoding [{0}].", encoding));
		}
		return encoding;
	} catch (IllegalCharsetNameException icne) {
		throw new MojoExecutionException(MessageFormat.format(
				"Unsupported encoding [{0}].", encoding));
	}

}
 
Example #18
Source File: StringUtils.java    From Zebra with Apache License 2.0 6 votes vote down vote up
static Charset findCharset(String alias) throws UnsupportedEncodingException {
	try {
		Charset cs = charsetsByAlias.get(alias);

		if (cs == null) {
			cs = Charset.forName(alias);
			charsetsByAlias.putIfAbsent(alias, cs);
		}

		return cs;

		// We re-throw these runtimes for compatibility with java.io
	} catch (UnsupportedCharsetException uce) {
		throw new UnsupportedEncodingException(alias);
	} catch (IllegalCharsetNameException icne) {
		throw new UnsupportedEncodingException(alias);
	} catch (IllegalArgumentException iae) {
		throw new UnsupportedEncodingException(alias);
	}
}
 
Example #19
Source File: JMeterProxy.java    From jsflight with Apache License 2.0 6 votes vote down vote up
/**
 * Add the page encoding of the sample result to the Map with page encodings
 *
 * @param result the sample result to check
 * @return the page encoding found for the sample result, or null
 */
private String addPageEncoding(SampleResult result)
{
    String pageEncoding = null;
    try
    {
        pageEncoding = ConversionUtils.getEncodingFromContentType(result.getContentType());
    }
    catch (IllegalCharsetNameException ex)
    {
        LOG.warn("Unsupported charset detected in contentType:'" + result.getContentType()
                + "', will continue processing with default charset", ex);
    }
    if (pageEncoding != null)
    {
        String urlWithoutQuery = getUrlWithoutQuery(result.getURL());
        pageEncodings.put(urlWithoutQuery, pageEncoding);
    }
    return pageEncoding;
}
 
Example #20
Source File: TplDataObject.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private CoderResult handleHead(CharBuffer in, ByteBuffer out) {
    String encoding = null;
    try {
        encoding = getEncoding();
    } catch (IOException ioe) {
        Exceptions.printStackTrace(ioe);
    }
    if (encoding == null) {
        buffer = null;
        throwUnknownEncoding();
        return null;
    } else {
        Charset c;
        try {
            c = cache(Charset.forName(encoding));
        } catch (UnsupportedCharsetException | IllegalCharsetNameException e) {
            buffer = null;
            throwUnknownEncoding();
            return null;
        }
        encoder = c.newEncoder();
        return flushHead(in, out);
    }
}
 
Example #21
Source File: StringUtils.java    From r-course with MIT License 6 votes vote down vote up
static Charset findCharset(String alias) throws UnsupportedEncodingException {
    try {
        Charset cs = charsetsByAlias.get(alias);

        if (cs == null) {
            cs = Charset.forName(alias);
            Charset oldCs = charsetsByAlias.putIfAbsent(alias, cs);
            if (oldCs != null) {
                // if the previous value was recently set by another thread we return it instead of value we found here
                cs = oldCs;
            }
        }

        return cs;

        // We re-throw these runtimes for compatibility with java.io
    } catch (UnsupportedCharsetException uce) {
        throw new UnsupportedEncodingException(alias);
    } catch (IllegalCharsetNameException icne) {
        throw new UnsupportedEncodingException(alias);
    } catch (IllegalArgumentException iae) {
        throw new UnsupportedEncodingException(alias);
    }
}
 
Example #22
Source File: Security.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
static Charset findCharset(String alias) throws UnsupportedEncodingException {
    try {
        Charset cs = CHARSETSBYALIAS.get(alias);

        if (cs == null) {
            cs = Charset.forName(alias);
            Charset oldCs = CHARSETSBYALIAS.putIfAbsent(alias, cs);
            if (oldCs != null) {
                // if the previous value was recently set by another thread we return it instead of value we found here
                cs = oldCs;
            }
        }

        return cs;

        // We re-throw these runtimes for compatibility with java.io
    } catch (UnsupportedCharsetException uce) {
        throw new UnsupportedEncodingException(alias);
    } catch (IllegalCharsetNameException icne) {
        throw new UnsupportedEncodingException(alias);
    } catch (IllegalArgumentException iae) {
        throw new UnsupportedEncodingException(alias);
    }
}
 
Example #23
Source File: CodeSetConversion.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
public JavaCTBConverter(OSFCodeSetRegistry.Entry codeset,
                        int alignmentForEncoding) {

    try {
        ctb = cache.getCharToByteConverter(codeset.getName());
        if (ctb == null) {
            Charset tmpCharset = Charset.forName(codeset.getName());
            ctb = tmpCharset.newEncoder();
            cache.setConverter(codeset.getName(), ctb);
        }
    } catch(IllegalCharsetNameException icne) {

        // This can only happen if one of our Entries has
        // an invalid name.
        throw wrapper.invalidCtbConverterName(icne,codeset.getName());
    } catch(UnsupportedCharsetException ucne) {

        // This can only happen if one of our Entries has
        // an unsupported name.
        throw wrapper.invalidCtbConverterName(ucne,codeset.getName());
    }

    this.codeset = codeset;
    alignment = alignmentForEncoding;
}
 
Example #24
Source File: CodeSetConversion.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Utility method to find a CharsetDecoder in the
 * cache or create a new one if necessary.  Throws an
 * INTERNAL if the code set is unknown.
 */
protected CharsetDecoder getConverter(String javaCodeSetName) {

    CharsetDecoder result = null;
    try {
        result = cache.getByteToCharConverter(javaCodeSetName);

        if (result == null) {
            Charset tmpCharset = Charset.forName(javaCodeSetName);
            result = tmpCharset.newDecoder();
            cache.setConverter(javaCodeSetName, result);
        }

    } catch(IllegalCharsetNameException icne) {
        // This can only happen if one of our charset entries has
        // an illegal name.
        throw wrapper.invalidBtcConverterName( icne, javaCodeSetName ) ;
    }

    return result;
}
 
Example #25
Source File: ModelValidation.java    From warnings-ng-plugin with MIT License 5 votes vote down vote up
/**
 * Returns the default charset for the specified encoding string. If the default encoding is empty or {@code null},
 * or if the charset is not valid then the default encoding of the platform is returned.
 *
 * @param charset
 *         identifier of the character set
 *
 * @return the default charset for the specified encoding string
 */
public Charset getCharset(@Nullable final String charset) {
    try {
        if (StringUtils.isNotBlank(charset)) {
            return Charset.forName(charset);
        }
    }
    catch (UnsupportedCharsetException | IllegalCharsetNameException exception) {
        // ignore and return default
    }
    return Charset.defaultCharset();
}
 
Example #26
Source File: ImmutableMediaTypeTest.java    From ldp4j with Apache License 2.0 5 votes vote down vote up
@Test
public void cannotParseMediaTypesWithBadCharsetName() throws Exception {
	try {
		createParam("charset=<catepora>");
		fail("Should fail for bad-name charsets");
	} catch (final InvalidMediaTypeException e) {
		assertThat(e.getCause(),instanceOf(IllegalArgumentException.class));
		assertThat(((IllegalCharsetNameException)e.getCause().getCause()).getCharsetName(),equalTo("<catepora>"));
	}
}
 
Example #27
Source File: Main.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void initializeConverter() throws UnsupportedEncodingException {
    Charset cs = null;

    try {
        cs = (encodingString == null) ?
            lookupCharset(defaultEncoding):
            lookupCharset(encodingString);

        encoder =  (cs != null) ?
            cs.newEncoder() :
            null;
    } catch (IllegalCharsetNameException e) {
        throw new Error(e);
    }
}
 
Example #28
Source File: StringCoding.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static byte[] encode(String charsetName, byte coder, byte[] val)
    throws UnsupportedEncodingException
{
    StringEncoder se = deref(encoder);
    String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
    if ((se == null) || !(csn.equals(se.requestedCharsetName())
                          || csn.equals(se.charsetName()))) {
        se = null;
        try {
            Charset cs = lookupCharset(csn);
            if (cs != null) {
                if (cs == UTF_8) {
                    return encodeUTF8(coder, val, true);
                }
                if (cs == ISO_8859_1) {
                    return encode8859_1(coder, val);
                }
                if (cs == US_ASCII) {
                    return encodeASCII(coder, val);
                }
                se = new StringEncoder(cs, csn);
            }
        } catch (IllegalCharsetNameException x) {}
        if (se == null) {
            throw new UnsupportedEncodingException (csn);
        }
        set(encoder, se);
    }
    return se.encode(coder, val);
}
 
Example #29
Source File: DataTransferer.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts an arbitrary text encoding to its canonical name.
 */
public static String canonicalName(String encoding) {
    if (encoding == null) {
        return null;
    }
    try {
        return Charset.forName(encoding).name();
    } catch (IllegalCharsetNameException icne) {
        return encoding;
    } catch (UnsupportedCharsetException uce) {
        return encoding;
    }
}
 
Example #30
Source File: DataTransferer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Converts an arbitrary text encoding to its canonical name.
 */
public static String canonicalName(String encoding) {
    if (encoding == null) {
        return null;
    }
    try {
        return Charset.forName(encoding).name();
    } catch (IllegalCharsetNameException icne) {
        return encoding;
    } catch (UnsupportedCharsetException uce) {
        return encoding;
    }
}