java.nio.charset.Charset.encode()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(236)

本文整理了Java中java.nio.charset.Charset.encode()方法的一些代码示例,展示了Charset.encode()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Charset.encode()方法的具体详情如下:
包路径:java.nio.charset.Charset
类名称:Charset
方法名:encode

Charset.encode介绍

[英]Returns a new ByteBuffer containing the bytes encoding the characters from s. This method uses CodingErrorAction.REPLACE.

Applications should generally create a CharsetEncoder using #newEncoderfor performance.
[中]返回一个新的ByteBuffer,其中包含对来自s的字符进行编码的字节。此方法使用CodingErrorAction。代替
应用程序通常应使用#newencoder创建CharsetEncoder以提高性能。

代码示例

代码示例来源:origin: jenkinsci/jenkins

@Override
public void write(char cbuf[], int off, int len) throws IOException {
  final CharBuffer charBuffer = CharBuffer.wrap(cbuf, off, len);
  ByteBuffer byteBuffer = charset.encode(charBuffer);
  channel.write(byteBuffer);
}

代码示例来源:origin: k9mail/k-9

private static byte[] encode(String text, Charset charset) {
  ByteBuffer buffer = charset.encode(text);
  byte[] bytes = new byte[buffer.limit()];
  buffer.get(bytes);
  return bytes;
}

代码示例来源:origin: stackoverflow.com

InputStream is = new ByteArrayInputStream(Charset.forName("UTF-16").encode(myString()).array());

代码示例来源:origin: geoserver/geoserver

/** Converts a char array to a byte array. */
public static byte[] toBytes(char[] ch, Charset charset) {
  ByteBuffer buff = charset.encode(CharBuffer.wrap(ch));
  byte[] tmp = new byte[buff.limit()];
  buff.get(tmp);
  return tmp;
}

代码示例来源:origin: stanfordnlp/CoreNLP

@Override
 public void handle(HttpExchange httpExchange) throws IOException {
  httpExchange.getResponseHeaders().set("Content-type", this.contentType);
  ByteBuffer buffer = Charset.forName("UTF-8").encode(content);
  byte[] bytes = new byte[buffer.remaining()];
  buffer.get(bytes);
  httpExchange.sendResponseHeaders(HTTP_OK, bytes.length);
  httpExchange.getResponseBody().write(bytes);
  httpExchange.close();
 }
} // end static class FileHandler

代码示例来源:origin: stackoverflow.com

private byte[] toBytes(char[] chars) {
  CharBuffer charBuffer = CharBuffer.wrap(chars);
  ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
  byte[] bytes = Arrays.copyOfRange(byteBuffer.array(),
      byteBuffer.position(), byteBuffer.limit());
  Arrays.fill(charBuffer.array(), '\u0000'); // clear sensitive data
  Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data
  return bytes;
}

代码示例来源:origin: apache/hive

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
 String value = PrimitiveObjectInspectorUtils.getString(arguments[0].get(), stringOI);
 if (value == null) {
  return null;
 }
 ByteBuffer encoded;
 if (encoder != null){
  try {
   encoded = encoder.encode(CharBuffer.wrap(value));
  } catch (CharacterCodingException e) {
   throw new HiveException(e);
  }
 } else {
  encoded = Charset.forName(
    PrimitiveObjectInspectorUtils.getString(arguments[1].get(), charsetOI)).encode(value);
 }
 result.setSize(encoded.limit());
 encoded.get(result.getBytes(), 0, encoded.limit());
 return result;
}

代码示例来源:origin: jersey/jersey

private static void appendUTF8EncodedCharacter(final StringBuilder sb, final int codePoint) {
  final CharBuffer chars = CharBuffer.wrap(Character.toChars(codePoint));
  final ByteBuffer bytes = UTF_8_CHARSET.encode(chars);
  while (bytes.hasRemaining()) {
    appendPercentEncodedOctet(sb, bytes.get() & 0xFF);
  }
}

代码示例来源:origin: oracle/opengrok

@Test
public void utf16WithBOM() throws IOException {
  final ByteBuffer utf16str = Charset.forName("UTF-16").encode("hello");
  byte[] bytes = new byte[utf16str.remaining()];
  utf16str.get(bytes, 0, bytes.length);
  new TestableTextAnalyzer().analyze(new Document(),
      getStreamSource(bytes), null);
  assertEquals("UTF-16", encoding);
  assertEquals("hello", contents);
}

代码示例来源:origin: commonsguy/cw-omnibus

static byte[] toBytes(char[] chars) {
  CharBuffer charBuffer=CharBuffer.wrap(chars);
  ByteBuffer byteBuffer=Charset.forName("UTF-8").encode(charBuffer);
  byte[] bytes=Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(),
   byteBuffer.limit());

//    Arrays.fill(charBuffer.array(), '\u0000'); // clear the cleartext
  Arrays.fill(byteBuffer.array(), (byte) 0); // clear the ciphertext

  return bytes;
 }

代码示例来源:origin: apache/drill

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
 String value = PrimitiveObjectInspectorUtils.getString(arguments[0].get(), stringOI);
 if (value == null) {
  return null;
 }
 ByteBuffer encoded;
 if (encoder != null){
  try {
   encoded = encoder.encode(CharBuffer.wrap(value));
  } catch (CharacterCodingException e) {
   throw new HiveException(e);
  }
 } else {
  encoded = Charset.forName(
    PrimitiveObjectInspectorUtils.getString(arguments[1].get(), charsetOI)).encode(value);
 }
 result.setSize(encoded.limit());
 encoded.get(result.getBytes(), 0, encoded.limit());
 return result;
}

代码示例来源:origin: stackoverflow.com

Charset utf8charset = Charset.forName("UTF-8");
Charset iso88591charset = Charset.forName("ISO-8859-1");

ByteBuffer inputBuffer = ByteBuffer.wrap(new byte[]{(byte)0xC3, (byte)0xA2});

// decode UTF-8
CharBuffer data = utf8charset.decode(inputBuffer);

// encode ISO-8559-1
ByteBuffer outputBuffer = iso88591charset.encode(data);
byte[] outputData = outputBuffer.array();

代码示例来源:origin: k9mail/k-9

public String encode(String folderName) {
  ByteBuffer byteBuffer = modifiedUtf7Charset.encode(folderName);
  byte[] bytes = new byte[byteBuffer.limit()];
  byteBuffer.get(bytes);
  return new String(bytes, asciiCharset);
}

代码示例来源:origin: AsyncHttpClient/async-http-client

public static ByteBuffer charSequence2ByteBuffer(CharSequence cs, Charset charset) {
 return charset.encode(CharBuffer.wrap(cs));
}

代码示例来源:origin: jersey/jersey

private static void appendUTF8EncodedCharacter(final StringBuilder sb, final int codePoint) {
  final CharBuffer chars = CharBuffer.wrap(Character.toChars(codePoint));
  final ByteBuffer bytes = UTF_8_CHARSET.encode(chars);
  while (bytes.hasRemaining()) {
    appendPercentEncodedOctet(sb, bytes.get() & 0xFF);
  }
}

代码示例来源:origin: oracle/opengrok

@Test
public void utf16WithBOMAlternate() throws IOException {
  final ByteBuffer utf16str = Charset.forName("UTF-16").encode("hello");
  byte[] bytes = new byte[utf16str.remaining()];
  utf16str.get(bytes, 0, bytes.length);
  for (int i = 0; i < bytes.length; i += 2) {
    byte b = bytes[i];
    bytes[i] = bytes[i + 1];
    bytes[i + 1] = b;
  }
  new TestableTextAnalyzer().analyze(new Document(),
      getStreamSource(bytes), null);
  assertEquals("UTF-16", encoding);
  assertEquals("hello", contents);
}

代码示例来源:origin: apache/nifi

/**
   * Returns a {@code byte[]} containing the value of the provided {@code char[]} without using {@code new String(chars).getBytes()} which would put sensitive data (the password) in the String pool.
   *
   * @param chars the characters to convert
   * @return the byte[]
   */
  private static byte[] convertCharsToBytes(char[] chars) {
    CharBuffer charBuffer = CharBuffer.wrap(chars);
    ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
    return Arrays.copyOfRange(byteBuffer.array(),
        byteBuffer.position(), byteBuffer.limit());
  }
}

代码示例来源:origin: stackoverflow.com

char[] c = "aaaaaaaaaa".toCharArray();
ByteBuffer bb = Charset.forName("UTF-8").encode(CharBuffer.wrap(c));
byte[] b = new byte[bb.remaining()];
bb.get(b);
System.out.println(Arrays.toString(b));

代码示例来源:origin: jphp-group/jphp

@FastMethod
@Signature({
    @Arg("string"),
    @Arg("charset")
})
public static Memory encode(Environment env, Memory... args) {
  Charset charset;
  try {
    charset = Charset.forName(args[1].toString());
  } catch (Exception e) {
    return Memory.FALSE;
  }
  ByteBuffer buffer = charset.encode(args[0].toString());
  return new BinaryMemory(Arrays.copyOf(buffer.array(), buffer.limit()));
}

代码示例来源:origin: Atmosphere/atmosphere

private static void appendUTF8EncodedCharacter(StringBuilder sb, char c) {
  final ByteBuffer bb = UTF_8_CHARSET.encode("" + c);
  while (bb.hasRemaining()) {
    appendPercentEncodedOctet(sb, bb.get() & 0xFF);
  }
}
private static final String[] SCHEME = {"0-9", "A-Z", "a-z", "+", "-", "."};

相关文章