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

The following examples show how to use java.nio.charset.Charset#forName() . 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: DataLoadingUtils.java    From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
/**
 * Load GeoJSON from URL (in synchronous manner) and return GeoJSON FeatureCollection
 * @param url URL of GeoJSON data
 * @return Remote GeoJSON parsed into Library objects
 * @throws IOException
 * @throws JSONException
 */
public static FeatureCollection loadGeoJSONFromUrl(final String url) throws IOException, JSONException {
    if (TextUtils.isEmpty(url)) {
        throw new NullPointerException("No GeoJSON URL passed in.");
    }

    if (UtilConstants.DEBUGMODE) {
        Log.d(DataLoadingUtils.class.getCanonicalName(), "Mapbox SDK downloading GeoJSON URL: " + url);
    }

    InputStream is;
    if (url.toLowerCase(Locale.US).indexOf("http") == 0) {
        is = NetworkUtils.getHttpURLConnection(new URL(url)).getInputStream();
    } else {
        is = new URL(url).openStream();
    }
    BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
    String jsonText = readAll(rd);

    FeatureCollection parsed = (FeatureCollection) GeoJSON.parse(jsonText);
    if (UtilConstants.DEBUGMODE) {
        Log.d(DataLoadingUtils.class.getCanonicalName(), "Parsed GeoJSON with " + parsed.getFeatures().size() + " features.");
    }

    return parsed;
}
 
Example 2
Source File: Source.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
private static char[] byteToCharArray(final byte[] bytes) {
    Charset cs = StandardCharsets.UTF_8;
    int start = 0;
    // BOM detection.
    if (bytes.length > 1 && bytes[0] == (byte) 0xFE && bytes[1] == (byte) 0xFF) {
        start = 2;
        cs = StandardCharsets.UTF_16BE;
    } else if (bytes.length > 1 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE) {
        if (bytes.length > 3 && bytes[2] == 0 && bytes[3] == 0) {
            start = 4;
            cs = Charset.forName("UTF-32LE");
        } else {
            start = 2;
            cs = StandardCharsets.UTF_16LE;
        }
    } else if (bytes.length > 2 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) {
        start = 3;
        cs = StandardCharsets.UTF_8;
    } else if (bytes.length > 3 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == (byte) 0xFE && bytes[3] == (byte) 0xFF) {
        start = 4;
        cs = Charset.forName("UTF-32BE");
    }

    return new String(bytes, start, bytes.length - start, cs).toCharArray();
}
 
Example 3
Source File: JsonPoweredTestHelper.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
private static String getFileAsString(final File file) throws IOException {
    StringBuilder stringBuilder = new StringBuilder();
    String line;
    String ls = System.getProperty("line.separator");
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")));
    try {
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
            stringBuilder.append(ls);
        }
    } finally {
        reader.close();
    }
    return stringBuilder.toString();
}
 
Example 4
Source File: JApiCmpMojo.java    From japicmp with Apache License 2.0 5 votes vote down vote up
private void writeToFile(String output, File outputfile) throws MojoFailureException, IOException {
	try (OutputStreamWriter fileWriter = new OutputStreamWriter(new FileOutputStream(outputfile), Charset.forName("UTF-8"))) {
		fileWriter.write(output);
		getLog().info("Written file '" + outputfile.getAbsolutePath() + "'.");
	} catch (Exception e) {
		throw new MojoFailureException(String.format("Failed to write diff file: %s", e.getMessage()), e);
	}
}
 
Example 5
Source File: BaseTestDistributedScheduler.java    From kylin with Apache License 2.0 5 votes vote down vote up
String getServerName(String segName) {
    String lockPath = DistributedScheduler.getLockPath(segName);
    String serverName = null;
    if (zkClient.getState().equals(CuratorFrameworkState.STARTED)) {
        try {
            if (zkClient.checkExists().forPath(lockPath) != null) {
                byte[] data = zkClient.getData().forPath(lockPath);
                serverName = new String(data, Charset.forName("UTF-8"));
            }
        } catch (Exception e) {
            logger.error("get the serverName failed", e);
        }
    }
    return serverName;
}
 
Example 6
Source File: StringUtil.java    From netty-cookbook with Apache License 2.0 5 votes vote down vote up
public static String toString(ByteBuffer buffer){
	/// Create a StringBuffer so that we can convert the bytes to a String
	StringBuffer response = new StringBuffer();
	
	// Create a CharSet that knows how to encode and decode standard text (UTF-8)
	Charset charset = Charset.forName("UTF-8");

	// Decode the buffer to a String using the CharSet and append it to our buffer
	response.append( charset.decode( buffer ) );
	buffer.flip();
	return response.toString();
}
 
Example 7
Source File: CharsetEncoderTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testSurrogatePairAllAtOnce() throws Exception {
    // okay: surrogate pair seen all at once is decoded to U+20b9f.
    Charset cs = Charset.forName("UTF-32BE");
    CharsetEncoder e = cs.newEncoder();
    ByteBuffer bb = ByteBuffer.allocate(128);
    CoderResult cr = e.encode(CharBuffer.wrap(new char[] { '\ud842', '\udf9f' }), bb, false);
    assertEquals(CoderResult.UNDERFLOW, cr);
    assertEquals(4, bb.position());
    assertEquals((byte) 0x00, bb.get(0));
    assertEquals((byte) 0x02, bb.get(1));
    assertEquals((byte) 0x0b, bb.get(2));
    assertEquals((byte) 0x9f, bb.get(3));
}
 
Example 8
Source File: WebRequest.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private void readObject(final ObjectInputStream ois) throws ClassNotFoundException, IOException {
    ois.defaultReadObject();
    final String charsetName = (String) ois.readObject();
    if (charsetName != null) {
        charset_ = Charset.forName(charsetName);
    }
}
 
Example 9
Source File: RedirectMap.java    From tagme with Apache License 2.0 5 votes vote down vote up
@Override
protected Int2IntMap parseSet() throws IOException
{
	final Object2IntMap<String> titles = new TitlesToWIDMap(lang).getDataset();
	final Int2IntOpenHashMap map = new Int2IntOpenHashMap(3000000);
	SQLWikiParser parser = new SQLWikiParser(log, "Titles NF") {
		@Override
		public boolean compute(ArrayList<String> values) throws IOException
		{
			int ns = Integer.parseInt(values.get(SQLWikiParser.REDIRECT_NS));
			if (ns == SQLWikiParser.NS_ARTICLE)
			{
				int idFrom = Integer.parseInt(values.get(SQLWikiParser.REDIRECT_ID_FROM));
				int idTo = titles.getInt(cleanPageName(values.get(SQLWikiParser.REDIRECT_TITLE_TO)));
				if (idTo >= 0)
					map.put(idFrom, idTo);
				else this.updateItem(0);
				
				return true;
			} else return false;
		}
	};

	File input = WikipediaFiles.REDIRECTS.getSourceFile(lang);
	InputStreamReader in = new InputStreamReader(new FileInputStream(input), Charset.forName("UTF-8"));
	parser.compute(in);
	in.close();
	
	map.defaultReturnValue(-1);
	map.trim();
	
	return map;

}
 
Example 10
Source File: PutHive_1_1QL.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void onTrigger(ProcessContext context, ProcessSessionFactory sessionFactory) throws ProcessException {
    final Boolean rollbackOnFailure = context.getProperty(RollbackOnFailure.ROLLBACK_ON_FAILURE).asBoolean();
    final Charset charset = Charset.forName(context.getProperty(CHARSET).getValue());
    final String statementDelimiter = context.getProperty(STATEMENT_DELIMITER).getValue();
    final FunctionContext functionContext = new FunctionContext(rollbackOnFailure, charset, statementDelimiter);
    RollbackOnFailure.onTrigger(context, sessionFactory, functionContext, getLogger(), session -> process.onTrigger(context, session, functionContext));
}
 
Example 11
Source File: JavaParser.java    From pumpernickel with MIT License 5 votes vote down vote up
@Override
public void parse(InputStream in, Receiver<Token> receiver)
		throws IOException {
	try (Reader reader = new InputStreamReader(in, Charset.forName("UTF-8"))) {
		parse(reader, receiver);
	}
}
 
Example 12
Source File: BrowseController.java    From webcurator with Apache License 2.0 5 votes vote down vote up
private Charset loadCharset(String charset) {
	Charset cs = CHARSET_LATIN_1;
	if (charset != null) {
		try {
			cs = Charset.forName(charset);
		} catch (Exception ex) {
			log.warn("Could not load desired charset " + charset
					+ "; using ISO-8859-1");
		}
	}
	
	return cs;
}
 
Example 13
Source File: ExternalAntlrLexerFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void generate(Grammar grammar, XpandExecutionContext ctx) {
	super.generate(grammar, ctx);
	String srcGen = Generator.SRC_GEN;
	String src = Generator.SRC;
	if (contentAssist || highlighting) {
		srcGen = Generator.SRC_GEN_IDE;
		src = Generator.SRC_IDE;
	}
	String srcGenPath = ctx.getOutput().getOutlet(srcGen).getPath();
	String srcPath = ctx.getOutput().getOutlet(src).getPath();
	String grammarFile = srcPath + "/" + getLexerGrammar().replace('.', '/') + ".g";
	String generateTo = "";
	if (lexerGrammar.lastIndexOf('.') != -1) {
		generateTo = lexerGrammar.substring(0, lexerGrammar.lastIndexOf('.'));
	}
	generateTo = srcGenPath + "/" + generateTo.replace('.', '/');
	addAntlrParam("-fo");
	addAntlrParam(generateTo);
	final String encoding = getEncoding(ctx, srcGen);
	getAntlrTool().runWithEncodingAndParams(grammarFile, encoding, getAntlrParams());
	Charset charset = Charset.forName(encoding);
	String javaFile = srcGenPath+"/"+getLexerGrammar().replace('.', '/')+".java";
	splitLexerIfEnabled(javaFile, charset);
	suppressWarningsImpl(javaFile, charset);
	normalizeLineDelimiters(javaFile, charset);
	normalizeTokens(javaFile, charset);
}
 
Example 14
Source File: WebUtils.java    From albert with MIT License 4 votes vote down vote up
/**
 * 获取参数
 * 
 * @param queryString
 *            查询字符串
 * @param encoding
 *            编码格式
 * @return 参数
 */
public static Map<String, String[]> getParameterMap(String queryString, String encoding) {
	Map<String, String[]> parameterMap = new HashMap<String, String[]>();
	Charset charset = Charset.forName(encoding);
	if (StringUtils.isNotEmpty(queryString)) {
		byte[] bytes = queryString.getBytes(charset);
		if (bytes != null && bytes.length > 0) {
			int ix = 0;
			int ox = 0;
			String key = null;
			String value = null;
			while (ix < bytes.length) {
				byte c = bytes[ix++];
				switch ((char) c) {
				case '&':
					value = new String(bytes, 0, ox, charset);
					if (key != null) {
						putMapEntry(parameterMap, key, value);
						key = null;
					}
					ox = 0;
					break;
				case '=':
					if (key == null) {
						key = new String(bytes, 0, ox, charset);
						ox = 0;
					} else {
						bytes[ox++] = c;
					}
					break;
				case '+':
					bytes[ox++] = (byte) ' ';
					break;
				case '%':
					bytes[ox++] = (byte) ((convertHexDigit(bytes[ix++]) << 4) + convertHexDigit(bytes[ix++]));
					break;
				default:
					bytes[ox++] = c;
				}
			}
			if (key != null) {
				value = new String(bytes, 0, ox, charset);
				putMapEntry(parameterMap, key, value);
			}
		}
	}
	return parameterMap;
}
 
Example 15
Source File: SshController.java    From Jpom with MIT License 4 votes vote down vote up
@RequestMapping(value = "save.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@OptLog(UserOperateLogV1.OptType.EditSsh)
@ResponseBody
@Feature(method = MethodFeature.EDIT)
public String save(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "ssh名称不能为空") String name,
                   @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "host不能为空") String host,
                   @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "user不能为空") String user,
                   String password,
                   SshModel.ConnectType connectType,
                   String privateKey,
                   @ValidatorItem(value = ValidatorRule.POSITIVE_INTEGER, msg = "port错误") int port,
                   String charset, String fileDirs,
                   String id, String type) {
    SshModel sshModel;
    if ("edit".equals(type)) {
        sshModel = sshService.getItem(id);
        if (sshModel == null) {
            return JsonMessage.getString(500, "不存在对应ssh");
        }
    } else {
        sshModel = new SshModel();
    }
    if (connectType == SshModel.ConnectType.PASS && StrUtil.isEmpty(password)) {
        return JsonMessage.getString(405, "请填写登录密码");
    }
    if (connectType == SshModel.ConnectType.PUBKEY && StrUtil.isEmpty(privateKey)) {
        return JsonMessage.getString(405, "请填写证书内容");
    }
    // 目录
    if (StrUtil.isEmpty(fileDirs)) {
        sshModel.setFileDirs(null);
    } else {
        List<String> list = StrSpliter.splitTrim(fileDirs, StrUtil.LF, true);
        sshModel.setFileDirs(list);
    }
    sshModel.setHost(host);
    sshModel.setPassword(password);
    sshModel.setPort(port);
    sshModel.setUser(user);
    sshModel.setName(name);
    sshModel.setConnectType(connectType);
    sshModel.setPrivateKey(privateKey);
    Charset.forName(charset);
    sshModel.setCharset(charset);
    try {
        Session session = SshService.getSession(sshModel);
        JschUtil.close(session);
    } catch (Exception e) {
        return JsonMessage.getString(505, "ssh连接失败:" + e.getMessage());
    }
    if ("add".equalsIgnoreCase(type)) {
        sshService.addItem(sshModel);
    } else {
        sshService.updateItem(sshModel);
    }
    return JsonMessage.getString(200, "操作成功");
}
 
Example 16
Source File: ConsumeKafkaRecord_1_0.java    From nifi with Apache License 2.0 4 votes vote down vote up
protected ConsumerPool createConsumerPool(final ProcessContext context, final ComponentLog log) {
    final int maxLeases = context.getMaxConcurrentTasks();
    final long maxUncommittedTime = context.getProperty(MAX_UNCOMMITTED_TIME).asTimePeriod(TimeUnit.MILLISECONDS);

    final Map<String, Object> props = new HashMap<>();
    KafkaProcessorUtils.buildCommonKafkaProperties(context, ConsumerConfig.class, props);
    props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class.getName());
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class.getName());
    final String topicListing = context.getProperty(ConsumeKafkaRecord_1_0.TOPICS).evaluateAttributeExpressions().getValue();
    final String topicType = context.getProperty(ConsumeKafkaRecord_1_0.TOPIC_TYPE).evaluateAttributeExpressions().getValue();
    final List<String> topics = new ArrayList<>();
    final String securityProtocol = context.getProperty(KafkaProcessorUtils.SECURITY_PROTOCOL).getValue();
    final String bootstrapServers = context.getProperty(KafkaProcessorUtils.BOOTSTRAP_SERVERS).evaluateAttributeExpressions().getValue();

    final RecordReaderFactory readerFactory = context.getProperty(RECORD_READER).asControllerService(RecordReaderFactory.class);
    final RecordSetWriterFactory writerFactory = context.getProperty(RECORD_WRITER).asControllerService(RecordSetWriterFactory.class);
    final boolean honorTransactions = context.getProperty(HONOR_TRANSACTIONS).asBoolean();

    final String charsetName = context.getProperty(MESSAGE_HEADER_ENCODING).evaluateAttributeExpressions().getValue();
    final Charset charset = Charset.forName(charsetName);

    final String headerNameRegex = context.getProperty(HEADER_NAME_REGEX).getValue();
    final Pattern headerNamePattern = headerNameRegex == null ? null : Pattern.compile(headerNameRegex);

    if (topicType.equals(TOPIC_NAME.getValue())) {
        for (final String topic : topicListing.split(",", 100)) {
            final String trimmedName = topic.trim();
            if (!trimmedName.isEmpty()) {
                topics.add(trimmedName);
            }
        }

        return new ConsumerPool(maxLeases, readerFactory, writerFactory, props, topics, maxUncommittedTime, securityProtocol,
            bootstrapServers, log, honorTransactions, charset, headerNamePattern);
    } else if (topicType.equals(TOPIC_PATTERN.getValue())) {
        final Pattern topicPattern = Pattern.compile(topicListing.trim());
        return new ConsumerPool(maxLeases, readerFactory, writerFactory, props, topicPattern, maxUncommittedTime, securityProtocol,
            bootstrapServers, log, honorTransactions, charset, headerNamePattern);
    } else {
        getLogger().error("Subscription type has an unknown value {}", new Object[] {topicType});
        return null;
    }
}
 
Example 17
Source File: SolidityUtils.java    From ethdroid with MIT License 4 votes vote down vote up
public static String hexToUtf8(String hex) {
    byte bytes[] = hexStringToBytes(hex);
    return new String(bytes, Charset.forName("UTF-8"));
}
 
Example 18
Source File: WikipediaArticleParser.java    From tagme with Apache License 2.0 4 votes vote down vote up
protected void start() throws IOException
{
	reader = new FastBufferedReader(new InputStreamReader(new FileInputStream(input), Charset.forName("UTF-8")));
	state = State.IDLE;
}
 
Example 19
Source File: AttachmentManager.java    From sync-android with Apache License 2.0 4 votes vote down vote up
private static String keyToString(byte[] key) {
    return new String(new Hex().encode(key), Charset.forName("UTF-8"));
}
 
Example 20
Source File: BulkResultSetTest.java    From components with Apache License 2.0 3 votes vote down vote up
@Test(expected = IOException.class)
public void testResultSetIOError() throws IOException {

    InputStream in = mock(InputStream.class);
    doThrow(new IOException("I/O ERROR")).when(in).read();
    when(in.read(any(byte[].class))).thenThrow(new IOException("I/O ERROR"));

    CsvReader csvReader = new CsvReader(in, ',', Charset.forName("UTF-8"));

    BulkResultSet resultSet = new BulkResultSet(csvReader, Arrays.asList("fieldA", "fieldB", "fieldC"));

    while (resultSet.next() != null) {}
}