下面列出了java.nio.charset.Charset#encode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean accept(Charset charset) {
if (charset.canEncode()) {
try {
ByteBuffer bf = charset.encode("\n");
if (bf.limit() != 1 || bf.get() != (byte) '\n') {
return false;
}
bf = charset.encode("\r");
if (bf.limit() != 1 || bf.get() != (byte) '\r') {
return false;
}
} catch (Exception ex) {
return false;
}
} else {
return false;
}
return true;
}
private static String urlEncode(
final String content,
final Charset charset,
final BitSet safechars,
final boolean blankAsPlus) {
if (content == null) {
return null;
}
final StringBuilder buf = new StringBuilder();
final ByteBuffer bb = charset.encode(content);
while (bb.hasRemaining()) {
final int b = bb.get() & 0xff;
if (safechars.get(b)) {
buf.append((char) b);
} else if (blankAsPlus && b == ' ') {
buf.append('+');
} else {
buf.append("%");
final char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
final char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
buf.append(hex1);
buf.append(hex2);
}
}
return buf.toString();
}
private static byte[] getPasswordBytes(char[] passwd) {
Charset utf8 = Charset.forName("UTF-8");
CharBuffer cb = CharBuffer.wrap(passwd);
ByteBuffer bb = utf8.encode(cb);
int len = bb.limit();
byte[] passwdBytes = new byte[len];
bb.get(passwdBytes, 0, len);
return passwdBytes;
}
private static ByteArrayBuffer encode(
final Charset charset, final String string) {
final ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
final ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
bab.append(encoded.array(), encoded.position(), encoded.remaining());
return bab;
}
/**
* Convert string to UTF-7 characters
*
* @param string Input string for decoding
* @return Encoded string
*/
public static String encode(String string, String charsetName)
{
if (string.length() <= 1)
{
return string;
}
CharsetProvider provider = new CharsetProvider();
Charset charset = provider.charsetForName(charsetName);
ByteBuffer byteBuffer = charset.encode(string);
return new String(byteBuffer.array()).substring(0, byteBuffer.limit());
}
public void writeLenString(String s, Charset encoder) {
if (s == null) {
writeLength(0);
return;
}
ByteBuffer bb = encoder.encode(s);
writeLength(bb.remaining());
writeBytes(bb);
}
@Test
@Ignore
public void testUTF8() {
final Charset cs = StandardCharsets.UTF_8;
final ByteBuffer bf = cs.encode("Münster");
final byte[] result = Arrays.copyOf(bf.array(), bf.limit());
System.out.println(result.length);
for (int i=0;i<result.length;i++) {
System.out.println(result[i] & 0xff);
}
Assert.assertEquals(8, result.length);
Assert.assertEquals(77, result[0] & 0xff);
Assert.assertEquals(195, result[1] & 0xff);
Assert.assertEquals(188, result[2] & 0xff);
Assert.assertEquals(110, result[3] & 0xff);
Assert.assertEquals(115, result[4] & 0xff);
Assert.assertEquals(116, result[5] & 0xff);
Assert.assertEquals(101, result[6] & 0xff);
Assert.assertEquals(114, result[7] & 0xff);
Assert.assertEquals(77, result[0]);
Assert.assertEquals(-61, result[1]);
Assert.assertEquals(-68, result[2]);
Assert.assertEquals(110, result[3]);
Assert.assertEquals(115, result[4]);
Assert.assertEquals(116, result[5]);
Assert.assertEquals(101, result[6]);
Assert.assertEquals(114, result[7]);
}
static byte[] charToUtf8(char[] chars) {
Charset utf8 = Charset.forName("UTF-8");
CharBuffer cb = CharBuffer.wrap(chars);
ByteBuffer bb = utf8.encode(cb);
int len = bb.limit();
byte[] answer = new byte[len];
bb.get(answer, 0, len);
return answer;
}
private static byte[] getBytes(char[] chars, int offset, int length) {
Charset cs = Charset.defaultCharset();
CharBuffer cb = CharBuffer.wrap(chars, offset, length);
ByteBuffer bb = cs.encode(cb);
byte[] ba = bb.array();
return Arrays.copyOf(ba, bb.limit());
}
static byte[] charToUtf8(char[] chars) {
Charset utf8 = Charset.forName("UTF-8");
CharBuffer cb = CharBuffer.wrap(chars);
ByteBuffer bb = utf8.encode(cb);
int len = bb.limit();
byte[] answer = new byte[len];
bb.get(answer, 0, len);
return answer;
}
static byte[] charToUtf16(char[] chars) {
Charset utf8 = Charset.forName("UTF-16LE");
CharBuffer cb = CharBuffer.wrap(chars);
ByteBuffer bb = utf8.encode(cb);
int len = bb.limit();
byte[] answer = new byte[len];
bb.get(answer, 0, len);
return answer;
}
public static byte[] getBytesNullTerminated(String value, String encoding) throws UnsupportedEncodingException {
Charset cs = findCharset(encoding);
ByteBuffer buf = cs.encode(value);
int encodedLen = buf.limit();
byte[] asBytes = new byte[encodedLen + 1];
buf.get(asBytes, 0, encodedLen);
asBytes[encodedLen] = 0;
return asBytes;
}
public static byte[] getBytesNullTerminated(String value, String encoding) {
// Charset cs = findCharset(encoding);
Charset cs = StandardCharsets.UTF_8;
ByteBuffer buf = cs.encode(value);
int encodedLen = buf.limit();
byte[] asBytes = new byte[encodedLen + 1];
buf.get(asBytes, 0, encodedLen);
asBytes[encodedLen] = 0;
return asBytes;
}
/**
* @param text Text to encode
* @param charset database charset
* @return A buffer with the text encoded
* @usage _advanced_method_
*/
public static ByteBuffer encodeUncompressedText(CharSequence text,
Charset charset)
{
CharBuffer cb = ((text instanceof CharBuffer) ?
(CharBuffer)text : CharBuffer.wrap(text));
return charset.encode(cb);
}
static byte[] charToUtf8(char[] chars) {
Charset utf8 = Charset.forName("UTF-8");
CharBuffer cb = CharBuffer.wrap(chars);
ByteBuffer bb = utf8.encode(cb);
int len = bb.limit();
byte[] answer = new byte[len];
bb.get(answer, 0, len);
return answer;
}
private static ByteArrayBuffer encode(
final Charset charset, final String string) {
ByteBuffer encoded = charset.encode(CharBuffer.wrap(string));
ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
bab.append(encoded.array(), encoded.position(), encoded.remaining());
return bab;
}
static byte[] charToUtf16(char[] chars) {
Charset utf8 = Charset.forName("UTF-16LE");
CharBuffer cb = CharBuffer.wrap(chars);
ByteBuffer bb = utf8.encode(cb);
int len = bb.limit();
byte[] answer = new byte[len];
bb.get(answer, 0, len);
return answer;
}
/** Do a char->byte conversion.
*/
public void toBytes() {
if (!byteC.isNull()) {
type=T_BYTES;
return;
}
toString();
type=T_BYTES;
Charset charset = byteC.getCharset();
ByteBuffer result = charset.encode(strValue);
byteC.setBytes(result.array(), result.arrayOffset(), result.limit());
}
static public byte[] convertCharToByteUTF(char[] from) {
Charset c = CDM.utf8Charset;
ByteBuffer output = c.encode(CharBuffer.wrap(from));
return output.array();
}
/**
* Puts a string into the buffer at the current position, using the character set to encode the string as bytes.
*
* @param v
* the string
* @param cs
* the character set
*
* @return the buffer
*/
public WrappedByteBuffer putString(String v, Charset cs) {
java.nio.ByteBuffer strBuf = cs.encode(v);
_autoExpand(strBuf.limit());
_buf.put(strBuf);
return this;
}