java.nio.charset.UnsupportedCharsetException Java Examples

The following examples show how to use java.nio.charset.UnsupportedCharsetException. 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: Encoders.java    From sql-layer with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Get the encoding for a character column.
 */
public static Encoding charEncoding(String charsetName) {
    synchronized (charEncodingMap) {
        Encoding encoding = charEncodingMap.get(charsetName);
        if (encoding == null) {
            try {
                Charset charset = Charset.forName(charsetName);
                if (charset.name().equals("UTF-8"))
                    encoding = UTF8Encoder.INSTANCE;
                else if (charset.newEncoder().maxBytesPerChar() == 1.0)
                    encoding = SBCSEncoder.INSTANCE;
            }
            catch (IllegalCharsetNameException |
                   UnsupportedCharsetException |
                   UnsupportedOperationException ex) {
                encoding = SBCSEncoder.INSTANCE;
            }
            if (encoding == null)
                encoding = new SlowMBCSEncoder(charsetName);
            charEncodingMap.put(charsetName, encoding);
        }
        return encoding;
    }
}
 
Example #2
Source File: StringUtils.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the byte[] representation of subset of the given char[] using the given encoding.
 * 
 * @param value
 *            chars
 * @param offset
 *            offset
 * @param length
 *            length
 * @param encoding
 *            java encoding
 * @return bytes
 */
public static byte[] getBytes(char[] value, int offset, int length, String encoding) {
    Charset cs;
    try {
        if (encoding == null) {
            cs = Charset.defaultCharset();
        } else {
            cs = Charset.forName(encoding);
        }
    } catch (UnsupportedCharsetException ex) {
        throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("StringUtils.0", new Object[] { encoding }), ex);
    }
    ByteBuffer buf = cs.encode(CharBuffer.wrap(value, offset, length));

    // can't simply .array() this to get the bytes especially with variable-length charsets the buffer is sometimes larger than the actual encoded data
    int encodedLen = buf.limit();
    byte[] asBytes = new byte[encodedLen];
    buf.get(asBytes, 0, encodedLen);

    return asBytes;
}
 
Example #3
Source File: Utilities.java    From tracee with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Creates a alphanumeric projection with a given length of the given object using its {@link Object#hashCode()}.
 */
public static String createAlphanumericHash(final String str, final int length) {
	try {
		final MessageDigest md = MessageDigest.getInstance("SHA-256");
		final byte[] digest = md.digest(str.getBytes(CHARSET_UTF8));
		// To human
		final StringBuilder sb = new StringBuilder();
		for (final byte b : digest) {
			if (b < 16) sb.append("0");
			sb.append(Integer.toHexString(b & 0xff));
		}
		// repeat if to small
		while (sb.length() < length) {
			sb.append(sb.toString());
		}
		// truncation and return
		return sb.delete(length, sb.length()).toString();
	} catch (NoSuchAlgorithmException | UnsupportedCharsetException e) {
		// Hashalgo. and charset is mandatory for all kinds of JDK, so this should happend. But even when, we generate a random string.
		return createRandomAlphanumeric(length);
	}
}
 
Example #4
Source File: CharsetConverterTest.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
@Test(expected=FileDecoderException.class)
public void testCharsetConverterCharsetCharsetInvalid() throws IOException, UnsupportedCharsetException, ConnectionException, RequestException, AccessException, NoSuchObjectException, ConfigException, ResourceException, URISyntaxException, FileDecoderException, FileEncoderException {
	File testResourceFile = loadFileFromClassPath(CLASS_PATH_PREFIX + "/euc-jp.txt");
	
	CharsetConverter convert = new CharsetConverter(PerforceCharsets.getP4Charset("shiftjis"), CharsetDefs.UTF8);
	
	InputStream inStream = new FileInputStream(testResourceFile);

	byte[] bytes = new byte[2048];
	int read = inStream.read(bytes);
	inStream.close();
	
	byte[] trueIn = new byte[read];
	System.arraycopy(bytes, 0, trueIn, 0, read);
	
	ByteBuffer bufIn  = ByteBuffer.wrap(bytes, 0, read);
	convert.convert(bufIn);
}
 
Example #5
Source File: XmlParserConfig.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public DataParserFactory getParserFactory(Stage.Context context) {
  DataParserFactoryBuilder builder = new DataParserFactoryBuilder(context, DataFormat.XML.getParserFormat());
  try {

    builder.setCharset(Charset.forName(charset));
  } catch (UnsupportedCharsetException ex) {
    throw new RuntimeException("It should not happen: " + ex.toString(), ex);
  }

  builder.setRemoveCtrlChars(removeCtrlChars).setMaxDataLen(-1)
      .setConfig(XmlDataParserFactory.RECORD_ELEMENT_KEY, xmlRecordElement)
      .setConfig(XmlDataParserFactory.INCLUDE_FIELD_XPATH_ATTRIBUTES_KEY, includeFieldXpathAttributes)
      .setConfig(XmlDataParserFactory.RECORD_ELEMENT_XPATH_NAMESPACES_KEY, xPathNamespaceContext)
      .setConfig(XmlDataParserFactory.USE_FIELD_ATTRIBUTES, outputFieldAttributes);
  return builder.build();
}
 
Example #6
Source File: FixLenDataFormatter.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public int writeHeader() throws IOException {
	if (header == null && sHeader != null) {
    	try {
			header = CloverBuffer.wrap(sHeader.getBytes(encoder.charset().name()));
		} catch (UnsupportedEncodingException e) {
			throw new UnsupportedCharsetException(encoder.charset().name());
		}
	}
	if (header != null) {
		dataBuffer.put(header);
		header.rewind();
		return header.remaining();
	} else 
		return 0;
}
 
Example #7
Source File: MimeHelper.java    From backlog4j with MIT License 6 votes vote down vote up
protected static String decodeRFC2231value(String value) {
    int q1 = value.indexOf('\'');
    if (q1 == -1) {
        // missing charset
        return value;
    }
    String mimeCharset = value.substring(0, q1);
    int q2 = value.indexOf('\'', q1 + 1);
    if (q2 == -1) {
        // missing language
        return value;
    }
    byte[] bytes = fromHex(value.substring(q2 + 1));
    try {
        return new String(bytes, Charset.forName(mimeCharset));
    } catch (UnsupportedCharsetException e) {
        // incorrect encoding
        return value;
    }
}
 
Example #8
Source File: SharedResourcesManager.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
private void handleFileActivity(@NotNull FileActivity activity)
    throws IOException, IllegalCharsetNameException, UnsupportedCharsetException {

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

  switch (activity.getType()) {
    case CREATED:
      handleFileCreation(activity);
      break;
    case REMOVED:
      handleFileDeletion(activity);
      break;
    case MOVED:
      handleFileMove(activity);
      break;
    default:
      throw new UnsupportedOperationException(
          "FileActivity type "
              + activity.getType()
              + " not supported. Dropped activity: "
              + activity);
  }
}
 
Example #9
Source File: TikaCharsetFinder.java    From alfresco-data-model with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected Charset detectCharsetImpl(byte[] buffer) throws Exception
{
    CharsetDetector detector = new CharsetDetector();
    detector.setText(buffer);
    CharsetMatch match = detector.detect();

    if(match != null && match.getConfidence() > threshold)
    {
        try
        {
            return Charset.forName(match.getName());
        }
        catch(UnsupportedCharsetException e)
        {
            logger.info("Charset detected as " + match.getName() + " but the JVM does not support this, detection skipped");
        }
    }
    return null;
}
 
Example #10
Source File: FastXmlSerializer.java    From HeadsUp with GNU General Public License v2.0 6 votes vote down vote up
public void setOutput(OutputStream os, String encoding) throws IOException,
        IllegalArgumentException, IllegalStateException {
    if (os == null)
        throw new IllegalArgumentException();
    if (true) {
        try {
            mCharset = Charset.forName(encoding).newEncoder();
        } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
            throw (UnsupportedEncodingException) (new UnsupportedEncodingException(
                    encoding).initCause(e));
        }
        mOutputStream = os;
    } else {
        setOutput(
                encoding == null
                        ? new OutputStreamWriter(os)
                        : new OutputStreamWriter(os, encoding));
    }
}
 
Example #11
Source File: CodeSetConversion.java    From openjdk-jdk8u-backup 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 #12
Source File: BaseJspEditorSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void saveFromKitToStream(StyledDocument doc, EditorKit kit, OutputStream stream) throws IOException, BadLocationException {
    Parameters.notNull("doc", doc);
    Parameters.notNull("kit", kit);

    String foundEncoding = (String) doc.getProperty(DOCUMENT_SAVE_ENCODING);
    String encoding = foundEncoding != null ? foundEncoding : defaulEncoding;
    Charset charset = Charset.forName("UTF-8"); //NOI18N
    try {
        charset = Charset.forName(encoding);
    } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
        LOGGER.log(Level.INFO, "Illegal charset found: {0}, defaulted to UTF-8 as warned by dialog", encoding);
    }
    writeByteOrderMark(charset, stream);
    super.saveFromKitToStream(doc, kit, stream);
}
 
Example #13
Source File: CodeSetConversion.java    From openjdk-8-source 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: TextFileReaderTest.java    From kafka-connect-fs with Apache License 2.0 6 votes vote down vote up
@ParameterizedTest
@MethodSource("fileSystemConfigProvider")
public void invalidFileEncoding(ReaderFsTestConfig fsConfig) {
    Map<String, Object> readerConfig = getReaderConfig();
    readerConfig.put(TextFileReader.FILE_READER_TEXT_FIELD_NAME_VALUE, FIELD_NAME_VALUE);
    readerConfig.put(TextFileReader.FILE_READER_TEXT_ENCODING, "invalid_charset");
    readerConfig.put(TextFileReader.FILE_READER_TEXT_COMPRESSION_TYPE, COMPRESSION_TYPE_DEFAULT);
    assertThrows(ConnectException.class, () -> getReader(fsConfig.getFs(), fsConfig.getDataFile(), readerConfig));
    assertThrows(UnsupportedCharsetException.class, () -> {
        try {
            getReader(fsConfig.getFs(), fsConfig.getDataFile(), readerConfig);
        } catch (Exception e) {
            throw e.getCause();
        }
    });
}
 
Example #15
Source File: Encodings.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns the EncodingInfo object for the specified
 * encoding.
 * <p>
 * This is not a public API.
 *
 * @param encoding The encoding
 * @return The object that is used to determine if
 * characters are in the given encoding.
 * @xsl.usage internal
 */
static EncodingInfo getEncodingInfo(String encoding)
{
    EncodingInfo ei;

    String normalizedEncoding = toUpperCaseFast(encoding);
    ei = _encodingInfos.findEncoding(normalizedEncoding);
    if (ei == null) {
        // We shouldn't have to do this, but just in case.
        try {
            // This may happen if the caller tries to use
            // an encoding that wasn't registered in the
            // (java name)->(preferred mime name) mapping file.
            // In that case we attempt to load the charset for the
            // given encoding, and if that succeeds - we create a new
            // EncodingInfo instance - assuming the canonical name
            // of the charset can be used as the mime name.
            final Charset c = Charset.forName(encoding);
            final String name = c.name();
            ei = new EncodingInfo(name, name);
            _encodingInfos.putEncoding(normalizedEncoding, ei);
        } catch (IllegalCharsetNameException | UnsupportedCharsetException x) {
            ei = new EncodingInfo(null,null);
        }
    }

    return ei;
}
 
Example #16
Source File: Converter.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private boolean isPermittedEncoding(String name) {
    try {
        Charset cs = Charset.forName(name);
        for (Charset encoding : permittedEncodings) {
            if (encoding.equals(cs))
                return true;
        }
        return false;
    } catch (UnsupportedCharsetException e) {
        return false;
    }
}
 
Example #17
Source File: Main.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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 #18
Source File: ByteString.java    From play-store-api with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a new {@code String} by decoding the bytes using the
 * specified charset.
 *
 * @param charsetName encode using this charset
 * @return new string
 * @throws UnsupportedEncodingException if charset isn't recognized
 */
public final String toString(String charsetName)
    throws UnsupportedEncodingException {
  try {
    return toString(Charset.forName(charsetName));
  } catch (UnsupportedCharsetException e) {
    UnsupportedEncodingException exception = new UnsupportedEncodingException(charsetName);
    exception.initCause(e);
    throw exception;
  }
}
 
Example #19
Source File: RowReader.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
protected String decodeField() {
    try {
        return new String(fieldBuffer, 0, fieldLength, encoding);
    }
    catch (UnsupportedEncodingException ex) {
        UnsupportedCharsetException nex = new UnsupportedCharsetException(encoding);
        nex.initCause(ex);
        throw nex;
    }
}
 
Example #20
Source File: Formatter.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a charset object for the given charset name.
 * @throws NullPointerException          is csn is null
 * @throws UnsupportedEncodingException  if the charset is not supported
 */
private static Charset toCharset(String csn)
    throws UnsupportedEncodingException
{
    Objects.requireNonNull(csn, "charsetName");
    try {
        return Charset.forName(csn);
    } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
        // UnsupportedEncodingException should be thrown
        throw new UnsupportedEncodingException(csn);
    }
}
 
Example #21
Source File: Formatter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a charset object for the given charset name.
 * @throws NullPointerException          is csn is null
 * @throws UnsupportedEncodingException  if the charset is not supported
 */
private static Charset toCharset(String csn)
    throws UnsupportedEncodingException
{
    Objects.requireNonNull(csn, "charsetName");
    try {
        return Charset.forName(csn);
    } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
        // UnsupportedEncodingException should be thrown
        throw new UnsupportedEncodingException(csn);
    }
}
 
Example #22
Source File: Formatter.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a charset object for the given charset name.
 * @throws NullPointerException          is csn is null
 * @throws UnsupportedEncodingException  if the charset is not supported
 */
private static Charset toCharset(String csn)
    throws UnsupportedEncodingException
{
    Objects.requireNonNull(csn, "charsetName");
    try {
        return Charset.forName(csn);
    } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
        // UnsupportedEncodingException should be thrown
        throw new UnsupportedEncodingException(csn);
    }
}
 
Example #23
Source File: LocaleInfo.java    From common-utils with GNU General Public License v2.0 5 votes vote down vote up
/** 判断charset是否被支持。 */
public LocaleInfo assertCharsetSupported() throws UnsupportedCharsetException {
    if (charset instanceof UnknownCharset) {
        throw new UnsupportedCharsetException(charset.name());
    }

    return this;
}
 
Example #24
Source File: PrintWriter.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a charset object for the given charset name.
 * @throws NullPointerException          is csn is null
 * @throws UnsupportedEncodingException  if the charset is not supported
 */
private static Charset toCharset(String csn)
    throws UnsupportedEncodingException
{
    Objects.requireNonNull(csn, "charsetName");
    try {
        return Charset.forName(csn);
    } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
        // UnsupportedEncodingException should be thrown
        throw new UnsupportedEncodingException(csn);
    }
}
 
Example #25
Source File: DataTransferer.java    From openjdk-jdk8u 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 #26
Source File: DefaultHttpRequestMetaData.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private static String urlEncode(String s) {
    try {
        return encode(s, REQUEST_TARGET_CHARSET.name());
    } catch (final UnsupportedEncodingException e) {
        throw new UnsupportedCharsetException(REQUEST_TARGET_CHARSET.name());
    }
}
 
Example #27
Source File: Formatter.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a charset object for the given charset name.
 * @throws NullPointerException          is csn is null
 * @throws UnsupportedEncodingException  if the charset is not supported
 */
private static Charset toCharset(String csn)
    throws UnsupportedEncodingException
{
    Objects.requireNonNull(csn, "charsetName");
    try {
        return Charset.forName(csn);
    } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
        // UnsupportedEncodingException should be thrown
        throw new UnsupportedEncodingException(csn);
    }
}
 
Example #28
Source File: SnifferTestCase.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static ByteBuffer makeByteBuffer(String xmlDeclEncoding, String xmlDecl) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {
        byte[] bytes = xmlDecl.getBytes(Charset.forName(xmlDeclEncoding));
        os.write(bytes, 0, bytes.length);
    } catch (UnsupportedCharsetException e) {
        return null;
    }
    return ByteBuffer.wrap(os.toByteArray());
}
 
Example #29
Source File: DataTransferer.java    From jdk8u-dev-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;
    }
}
 
Example #30
Source File: StandardValidators.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult validate(final String subject, final String value, final ValidationContext context) {
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(value)) {
        final ResultType resultType = context.newExpressionLanguageCompiler().getResultType(value);
        if (!resultType.equals(ResultType.STRING)) {
            return new ValidationResult.Builder()
                    .subject(subject)
                    .input(value)
                    .valid(false)
                    .explanation("Expected Attribute Query to return type " + ResultType.STRING + " but query returns type " + resultType)
                    .build();
        }

        return new ValidationResult.Builder().subject(subject).input(value).explanation("Expression Language Present").valid(true).build();
    }

    String reason = null;
    try {
        if (!Charset.isSupported(value)) {
            reason = "Character Set is not supported by this JVM.";
        }
    } catch (final UnsupportedCharsetException uce) {
        reason = "Character Set is not supported by this JVM.";
    } catch (final IllegalArgumentException iae) {
        reason = "Character Set value cannot be null.";
    }

    return new ValidationResult.Builder().subject(subject).input(value).explanation(reason).valid(reason == null).build();
}