下面列出了怎么用java.nio.charset.Charset的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testJvmDecoder() {
CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
int testCount = 0;
try {
for (Utf8TestCase testCase : TEST_CASES) {
doTest(decoder, testCase, testCase.flagsJvm);
testCount++;
}
} finally {
System.err.println("Workarounds added to " + workAroundCount +
" tests to account for known JVM bugs");
if (testCount < TEST_CASES.size()) {
System.err.println("Executed " + testCount + " of " +
TEST_CASES.size() + " UTF-8 tests before " +
"encountering a failure");
}
}
}
@Test
public void testShiftJisRoundtrip() throws Exception {
String input =
"<html>"
+ "<head>"
+ "<meta http-equiv=\"content-type\" content=\"text/html; charset=Shift_JIS\" />"
+ "</head>"
+ "<body>"
+ "before after"
+ "</body>"
+ "</html>";
InputStream is = new ByteArrayInputStream(input.getBytes(Charset.forName("ASCII")));
Document doc = Jsoup.parse(is, null, "http://example.com");
doc.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
String output = new String(doc.html().getBytes(doc.outputSettings().charset()), doc.outputSettings().charset());
assertFalse("Should not have contained a '?'.", output.contains("?"));
assertTrue("Should have contained a ' ' or a ' '.",
output.contains(" ") || output.contains(" "));
}
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
SiteToSiteClientConfig clientConfig = new SiteToSiteClient.Builder()
.url("http://localhost:8080/nifi")
.portName("Data for Flink")
.requestBatchCount(5)
.buildConfig();
SourceFunction<NiFiDataPacket> nifiSource = new NiFiSource(clientConfig);
DataStream<NiFiDataPacket> streamSource = env.addSource(nifiSource).setParallelism(2);
DataStream<String> dataStream = streamSource.map(new MapFunction<NiFiDataPacket, String>() {
@Override
public String map(NiFiDataPacket value) throws Exception {
return new String(value.getContent(), Charset.defaultCharset());
}
});
dataStream.print();
env.execute();
}
@Override
public void updateSignatureHash(MessageDigest digester, Object val) throws AtlasException {
if( !(val instanceof ITypedReferenceableInstance)) {
throw new IllegalArgumentException("Unexpected value type " + val.getClass().getSimpleName() + ". Expected instance of ITypedStruct");
}
digester.update(getName().getBytes(Charset.forName("UTF-8")));
if(fieldMapping.fields != null && val != null) {
IReferenceableInstance typedValue = (IReferenceableInstance) val;
if(fieldMapping.fields.values() != null) {
for (AttributeInfo aInfo : fieldMapping.fields.values()) {
Object attrVal = typedValue.get(aInfo.name);
if (attrVal != null) {
aInfo.dataType().updateSignatureHash(digester, attrVal);
}
}
}
}
}
/**
* Returns the immutable list of tiles specified in {@param geojson} that intersect {@param
* bounds}.
*/
public ImmutableList<Tile> intersectingTiles(LatLngBounds bounds, File file) {
try {
String fileContents = FileUtils.readFileToString(file, Charset.forName(JSON_SOURCE_CHARSET));
// TODO: Separate parsing and intersection checks, make asyc (single, completable).
JSONObject geoJson = new JSONObject(fileContents);
// TODO: Make features constant.
JSONArray features = geoJson.getJSONArray(FEATURES_KEY);
return stream(toArrayList(features))
.map(GeoJsonTile::new)
.filter(tile -> tile.boundsIntersect(bounds))
.map(this::jsonToTile)
.collect(toImmutableList());
} catch (JSONException | IOException e) {
Log.e(TAG, "Unable to parse JSON", e);
}
return ImmutableList.of();
}
@Override
public List<Record> loadFromMetaData(List<RecordMetaData> recordMetaDatas) throws IOException {
List<Record> out = new ArrayList<>();
for (RecordMetaData meta : recordMetaDatas) {
URI uri = meta.getURI();
List<Writable> list;
try(InputStream s = streamCreatorFn.apply(uri)) {
list = loadFromStream(uri, s, Charset.forName(charset));
} catch (IOException e){
throw new RuntimeException("Error reading from stream for URI: " + uri);
}
out.add(new org.datavec.api.records.impl.Record(list, meta));
}
return out;
}
/**
* 计算消费日志占用的长度
* <br>
* (byte[]:messageId, int:brokerId, byte[16]:clientIP, long:consumeTime, String:app(变长), )
*
* @param consumeLog
* @return
*/
private static int consumeLogSize(ConsumeLog consumeLog) {
int size = 0;
// messageId
size += consumeLog.getBytesMessageId().length;
// brokerId
size += 4;
// clientIp
size += 16;
// consumeTime
size += 8;
// app长度
size += 2;
// app
size += consumeLog.getApp().getBytes(Charset.forName("utf-8")).length;
return size;
}
@Test
public void test_load_plugins_folder_contains_jar_file() throws Exception {
final File pluginsFolder = temporaryFolder.newFolder("extension");
final File pluginFolder = temporaryFolder.newFolder("extension", "plugin1");
final File file = new File(pluginFolder, "extension.jar");
FileUtils.writeStringToFile(pluginFolder.toPath().resolve("hivemq-extension.xml").toFile(),
validPluginXML1,
Charset.defaultCharset());
final JavaArchive javaArchive = ShrinkWrap.create(JavaArchive.class).
addAsServiceProviderAndClasses(ExtensionMain.class, TestExtensionMainImpl.class);
javaArchive.as(ZipExporter.class).exportTo(file);
when(classServiceLoader.load(eq(ExtensionMain.class), any(ClassLoader.class))).thenReturn(new ArrayList<>());
pluginLoader.loadPlugins(pluginsFolder.toPath(), false, ExtensionMain.class);
//Let's verify that the extension was loaded
verify(classServiceLoader).load(any(Class.class), any(ClassLoader.class));
}
@Override
public void setContentType(String type) {
if (isCommitted()) {
return;
}
if (hasWriter()) {
return;
}
if (null == type) {
contentType = null;
return;
}
MediaType mediaType = MediaType.parse(type);
Optional<Charset> charset = mediaType.charset();
if (charset.isPresent()) {
setCharacterEncoding(charset.get().name());
}
contentType = mediaType.type() + '/' + mediaType.subtype();
}
@Test
public void testUpload() throws Exception {
final DefaultUploadFeature<VersionId> m = new DefaultUploadFeature<VersionId>(new GoogleStorageWriteFeature(session));
final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
final Path test = new Path(container, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
final String random = new RandomStringGenerator.Builder().build().generate(1000);
final OutputStream out = local.getOutputStream(false);
IOUtils.write(random, out, Charset.defaultCharset());
out.close();
final TransferStatus status = new TransferStatus();
status.setLength(random.getBytes().length);
m.upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED),
new DisabledStreamListener(), status, new DisabledLoginCallback());
assertTrue(new GoogleStorageFindFeature(session).find(test));
final PathAttributes attributes = new GoogleStorageListService(session).list(container,
new DisabledListProgressListener()).get(test).attributes();
assertEquals(random.getBytes().length, attributes.getSize());
new GoogleStorageDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
local.delete();
}
/**
* Determines the CQL statement that should be executed for the given FlowFile
*
* @param session the session that can be used to access the given FlowFile
* @param flowFile the FlowFile whose CQL statement should be executed
* @return the CQL that is associated with the given FlowFile
*/
private String getCQL(final ProcessSession session, final FlowFile flowFile, final Charset charset) {
// Read the CQL from the FlowFile's content
final byte[] buffer = new byte[(int) flowFile.getSize()];
session.read(flowFile, new InputStreamCallback() {
@Override
public void process(final InputStream in) throws IOException {
StreamUtils.fillBuffer(in, buffer);
}
});
// Create the PreparedStatement string to use for this FlowFile.
return new String(buffer, charset);
}
public ResponseEntity<InputStreamResource> executeQuery(final String sql, boolean json, final boolean passthroughAuth)
throws SQLException {
Connection conn = null;
try {
conn = getConnection();
Statement statement = conn.createStatement();
final boolean hasResultSet = statement.execute(sql);
Object result = null;
if (hasResultSet) {
ResultSet rs = statement.getResultSet();
if (rs.next()) {
result = rs.getObject(1);
} else {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,"Only result producing procedures are allowed");
}
}
InputStream is = handleResult(Charset.defaultCharset().name(), result);
InputStreamResource inputStreamResource = new InputStreamResource(is);
HttpHeaders httpHeaders = new HttpHeaders();
return new ResponseEntity<InputStreamResource>(inputStreamResource, httpHeaders, HttpStatus.OK);
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
}
}
}
public void testConstructException() {
try (Stream<String> s = Files.lines(testFolder.resolve("notExist"), Charset.forName("UTF-8"))) {
s.forEach(l -> fail("File is not even exist!"));
} catch (IOException ioe) {
assertTrue(ioe instanceof NoSuchFileException);
}
}
@POST
@Path("fileecho")
@Consumes("text/plain")
@Produces("text/plain")
public String fileWrite(String body) throws InterruptedException, IOException {
String returnStr = "";
java.nio.file.Path tempfile = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
Files.write(tempfile, body.getBytes(Charset.defaultCharset()),
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING);
returnStr = new String(Files.readAllBytes(tempfile), Charset.defaultCharset());
Files.delete(tempfile);
return returnStr;
}
private String base64Sha256Hash(byte[] data) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] sha256Bytes = digest.digest(data);
return new String(
Base64.encode(sha256Bytes, Base64.NO_WRAP | Base64.URL_SAFE),
Charset.forName("UTF-8"));
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("SHA-256 not supported on this platform!");
}
}
@Test
public void testQueryStringPath() throws Exception {
HttpRequestEncoder encoder = new HttpRequestEncoder();
ByteBuf buffer = Unpooled.buffer(64);
encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.GET, "/?url=http://example.com"));
String req = buffer.toString(Charset.forName("US-ASCII"));
assertEquals("GET /?url=http://example.com HTTP/1.1\r\n", req);
}
public static Observable<String> createRequest(String host, int port) {
/**
* Creates an HTTP client bound to the provided host and port.
*/
return RxNetty.createHttpClient(host, port)
/* Submit an HTTP GET request with uri "/hello" */
.submit(HttpClientRequest.createGet("/hello"))
/* Print the HTTP initial line and headers. Return the content.*/
.flatMap(response -> {
/**
* Printing the HTTP initial line.
*/
System.out.println(response.getHttpVersion().text() + ' ' + response.getStatus().code()
+ ' ' + response.getStatus().reasonPhrase());
/**
* Printing HTTP headers.
*/
for (Map.Entry<String, String> header : response.getHeaders().entries()) {
System.out.println(header.getKey() + ": " + header.getValue());
}
// Line break after the headers.
System.out.println();
return response.getContent();
})
/* Convert the ByteBuf for each content chunk into a string. */
.map(byteBuf -> byteBuf.toString(Charset.defaultCharset()));
}
public static void compareLines(Path path, List<String> strings,
String encoding, boolean trim)
throws FileNotFoundException, IOException, ResourcesNotEqualException {
Charset charset = encoding != null ?
Charset.forName(encoding) :
defaultCharset;
List<String> list = Files.readAllLines(path, charset);
compareLines(list, strings, trim);
}
public String getHttpContent (String url) throws URISyntaxException, UnknownHostException, IOException, IllegalCharsetNameException {
if (StringUtils.isEmpty(url)){
return "";
}
String encodedUrl = getEncodedUrl(url);
CloseableHttpClient httpClient = getHttpClient(encodedUrl);
HttpGet get = new HttpGet(encodedUrl);
try {
LOGGER.debug("executing request to retrieve content on " + get.getURI());
HttpResponse response = httpClient.execute(get);
LOGGER.debug("received " + response.getStatusLine().getStatusCode() + " from get request");
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
LOGGER.debug("status == HttpStatus.SC_OK " );
return EntityUtils.toString(response.getEntity(), Charset.defaultCharset());
} else {
LOGGER.debug("status != HttpStatus.SC_OK " );
return "";
}
} catch (NullPointerException ioe) {
LOGGER.debug("NullPointerException");
return "";
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
get.releaseConnection();
LOGGER.debug("finally");
httpClient.close();
}
}
private void writePropertiesFile(File parent, String name, Properties properties)
throws IOException {
File propertiesFile = new File(parent, name);
try (Writer writer = Files.newWriter(propertiesFile, Charset.forName("UTF-8"))) {
properties.store(writer, null);
}
}
/**
* Always meant to be run against HEAD.
*/
@Test
public void errorCodesMatchCppDefinitions() throws Exception {
final NamedErrorCodes codes = new NamedErrorCodes();
enumerateAllDocumentProtocolErrorCodes(codes);
final String javaGoldenFile = TestFileUtil.getPath("HEAD-java-golden-error-codes.txt");
final String javaGoldenData = codes.toSortedKeyValueString();
TestFileUtil.writeToFile(javaGoldenFile, javaGoldenData);
final String cppGoldenFile = TestFileUtil.getPath("HEAD-cpp-golden-error-codes.txt");
final String cppGoldenData = new String(TestFileUtil.readFile(cppGoldenFile), Charset.forName("UTF-8"));
assertEquals(javaGoldenData, cppGoldenData);
}
@BeforeClass
public static void before() throws Throwable {
// initialize the scheduler & rm with a custom authorized script folder
scriptsFolder = folder.newFolder("scripts");
File schedulerConfigFile = generateConfigFile(originalSchedulerConfigFile.toURI(), "schedulerConfig");
File rmConfigFile = generateConfigFile(originalRMConfigFile.toURI(), "rmConfig");
// create authorized and unauthorized scripts
String tempFolderPathEscaped = folder.getRoot().getAbsolutePath().replace("\\", "\\\\");
authorizedForkScriptContent = "new File('" + tempFolderPathEscaped + "','fork_auth.out').write('ok')";
authorizedCleanScriptContent = "new File('" + tempFolderPathEscaped + "','clean_auth.out').write('ok')";
authorizedSelectionScriptContent = "new File('" + tempFolderPathEscaped +
"','selection_auth.out').write('ok'); selected=true";
File forkScript = new File(scriptsFolder, "forkScript");
FileUtils.write(forkScript, authorizedForkScriptContent, Charset.defaultCharset(), false);
File cleanScript = new File(scriptsFolder, "cleanScript");
FileUtils.write(cleanScript, authorizedCleanScriptContent, Charset.defaultCharset(), false);
File selectionScript = new File(scriptsFolder, "selectionScript");
FileUtils.write(selectionScript, authorizedSelectionScriptContent, Charset.defaultCharset(), false);
unauthorizedForkScriptContent = "new File('" + tempFolderPathEscaped + "','fork.out').write('ko')";
unauthorizedCleanScriptContent = "new File('" + tempFolderPathEscaped + "','clean.out').write('ko')";
unauthorizedSelectionScriptContent = "new File('" + tempFolderPathEscaped +
"','selection.out').write('ko'); selected=true";
// start the configured scheduler
schedulerHelper = new SchedulerTHelper(true,
schedulerConfigFile.getAbsolutePath(),
rmConfigFile.getAbsolutePath(),
null);
}
protected ServletInputStream asServletInputStream(final String data) {
return new ServletInputStream() {
ByteArrayInputStream byteInputStream =
new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8")));
int b;
@Override
public boolean isFinished() {
return b == -1;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read() throws IOException {
this.b = byteInputStream.read();
return b;
}
};
}
@Test
public void passesIndexTemplateToClient() {
//given
HCHttp factory = Mockito.spy(HCHttpTest.createDefaultHttpObjectFactoryBuilder().build());
HttpClient httpClient = mockedHttpClient(factory);
AtomicReference<ByteBuf> argCaptor = new AtomicReference<>();
mockedResult(httpClient, true, argCaptor);
IndexTemplate indexTemplate = spy(IndexTemplate.newBuilder()
.withPath("classpath:indexTemplate.json")
.withName("testName")
.build());
String expectedPayload = indexTemplate.getSource();
// when
factory.execute(indexTemplate);
// then
ArgumentCaptor<IndexTemplateRequest> requestArgumentCaptor = ArgumentCaptor.forClass(IndexTemplateRequest.class);
verify(httpClient).execute(requestArgumentCaptor.capture(), any());
assertEquals(argCaptor.get().toString(Charset.forName("UTF-8")), expectedPayload);
}
public Encoder(Charset cs)
{
super(cs);
SODesig = "$)G";
SS2Desig = "$*H";
SS3Desig = "$+I";
try {
Charset cset = Charset.forName("EUC_TW"); // CNS11643
ISOEncoder = cset.newEncoder();
} catch (Exception e) { }
}
private static String checkConvertMime2Java(String xml) {
final String jName = Encodings.convertMime2JavaEncoding(xml);
final String jCharsetName;
try {
jCharsetName = Charset.forName(jName).name();
} catch (Exception x) {
throw new Error("Unrecognized charset returned by Encodings.convertMime2JavaEncoding(\""+xml+"\")", x);
}
System.out.println("Encodings.convertMime2JavaEncoding(\""+xml+"\") = \""+jName+"\" ("+jCharsetName+")");
return jName;
}
@Test
@Ignore("Does not work on windows")
public void testSchemaFromFileSystem() throws IOException {
File schemaFile = temp.newFile("schema.avsc");
FileOutputStream out = new FileOutputStream(schemaFile);
out.write(bytesFor(SCHEMA.toString(), Charset.forName("utf8")));
out.close();
Schema schema = AbstractKiteProcessor.getSchema(
schemaFile.toString(), DefaultConfiguration.get());
Assert.assertEquals("Schema from file should match", SCHEMA, schema);
}
public void testNewWriterWritableByteChannelString() throws IOException {
this.fouts = new FileOutputStream(tmpFile);
WritableByteChannel wbChannel = Channels.newChannel(this.fouts);
Writer testWriter = Channels.newWriter(wbChannel, CODE_SET); //$NON-NLS-1$
Writer testWriter_s = Channels.newWriter(wbChannel, Charset.forName(
CODE_SET).newEncoder(), //$NON-NLS-1$
-1);
String writebuf = ""; //$NON-NLS-1$
for (int val = 0; val < this.writebufSize / 2; val++) {
writebuf = writebuf + ((char) (val + 64));
}
byte[] bit = new byte[1];
bit[0] = 80;
this.fouts.write(bit);
this.assertFileSizeSame(tmpFile, 1);
// writer continues to write after '1',what the fouts write
testWriter.write(writebuf);
testWriter.flush();
this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1);
// testwriter_s does not know if testwrite writes
testWriter_s.write(writebuf);
testWriter.flush();
this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1);
// testwriter_s even does not know if himself writes?
testWriter_s.write(writebuf);
testWriter.flush();
this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1);
// close the fouts, no longer writable for testWriter
for (int val = 0; val < this.writebufSize; val++) {
writebuf = writebuf + ((char) (val + 64));
}
this.fouts.close();
testWriter_s.write(writebuf);
testWriter.flush();
this.assertFileSizeSame(tmpFile, this.writebufSize / 2 + 1);
}
@Override
public void set(DbfRecord metadata, Polygon polygon) throws ParseException {
if (metadata != null) {
metadata.setStringCharset(Charset.defaultCharset());
polygon.setSnippet(metadata.toMap().toString());
polygon.setTitle(getSensibleTitle(polygon.getSnippet()));
}
final BoundingBox boundingBox = polygon.getBounds();
polygon.setSubDescription(boundingBox.toString());
}
private void flush() {
try {
int sizeOfBuffer = sendBuffer.position();
if (sizeOfBuffer <= 0) {
// empty buffer
return;
}
// send and reset the buffer
sendBuffer.flip();
int nbSentBytes = channel.send(sendBuffer, address);
LOG.debug("Send entirely stat :" + new String(sendBuffer.array(), Charset.forName("UTF-8")) + " to host "
+ address.getHostName() + " : " + address.getPort());
if (sizeOfBuffer != nbSentBytes) {
LOG.error("Could not send entirely stat (" + sendBuffer.toString() + ") : "
+ new String(sendBuffer.array(), Charset.forName("UTF-8")) + " to host "
+ address.getHostName() + " : " + address.getPort() + ". Only sent " + nbSentBytes
+ " out of " + sizeOfBuffer);
}
sendBuffer.limit(sendBuffer.capacity());
sendBuffer.rewind();
} catch (IOException e) {
LOG.error("Could not send entirely stat (" + sendBuffer.toString() + ") : "
+ new String(sendBuffer.array(), Charset.forName("UTF-8")) + " to host "
+ address.getHostName() + " : " + address.getPort(), e);
}
}