java.nio.charset.Charset#forName ( )源码实例Demo

下面列出了java.nio.charset.Charset#forName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: OpenMapKitAndroid   文件: DataLoadingUtils.java
/**
 * 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;
}
 
源代码2 项目: jdk8u_nashorn   文件: Source.java
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();
}
 
源代码3 项目: netty-cookbook   文件: StringUtil.java
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();
}
 
源代码4 项目: xtext-extras   文件: ExternalAntlrLexerFragment.java
@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);
}
 
源代码5 项目: webcurator   文件: BrowseController.java
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;
}
 
源代码6 项目: pumpernickel   文件: JavaParser.java
@Override
public void parse(InputStream in, Receiver<Token> receiver)
		throws IOException {
	try (Reader reader = new InputStreamReader(in, Charset.forName("UTF-8"))) {
		parse(reader, receiver);
	}
}
 
源代码7 项目: nifi   文件: PutHive_1_1QL.java
@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));
}
 
源代码8 项目: tagme   文件: RedirectMap.java
@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;

}
 
源代码9 项目: HtmlUnit-Android   文件: WebRequest.java
private void readObject(final ObjectInputStream ois) throws ClassNotFoundException, IOException {
    ois.defaultReadObject();
    final String charsetName = (String) ois.readObject();
    if (charsetName != null) {
        charset_ = Charset.forName(charsetName);
    }
}
 
源代码10 项目: j2objc   文件: CharsetEncoderTest.java
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));
}
 
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();
}
 
源代码12 项目: kylin   文件: BaseTestDistributedScheduler.java
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;
}
 
源代码13 项目: japicmp   文件: JApiCmpMojo.java
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);
	}
}
 
源代码14 项目: albert   文件: WebUtils.java
/**
 * 获取参数
 * 
 * @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;
}
 
源代码15 项目: Jpom   文件: SshController.java
@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, "操作成功");
}
 
源代码16 项目: nifi   文件: ConsumeKafkaRecord_1_0.java
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;
    }
}
 
源代码17 项目: ethdroid   文件: SolidityUtils.java
public static String hexToUtf8(String hex) {
    byte bytes[] = hexStringToBytes(hex);
    return new String(bytes, Charset.forName("UTF-8"));
}
 
源代码18 项目: tagme   文件: WikipediaArticleParser.java
protected void start() throws IOException
{
	reader = new FastBufferedReader(new InputStreamReader(new FileInputStream(input), Charset.forName("UTF-8")));
	state = State.IDLE;
}
 
源代码19 项目: sync-android   文件: AttachmentManager.java
private static String keyToString(byte[] key) {
    return new String(new Hex().encode(key), Charset.forName("UTF-8"));
}
 
源代码20 项目: components   文件: BulkResultSetTest.java
@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) {}
}