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

下面列出了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();
}
 
源代码3 项目: jdk8u_jdk   文件: PBKDF2KeyImpl.java

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;
}
 
源代码4 项目: iaf   文件: MultipartForm.java

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;
}
 
源代码5 项目: alfresco-repository   文件: Utf7.java

/**
 * 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());
}
 
源代码6 项目: antsdb   文件: PacketWriter.java

public void writeLenString(String s, Charset encoder) {
    if (s == null) {
        writeLength(0);
        return;
    }
    ByteBuffer bb = encoder.encode(s);
    writeLength(bb.remaining());
    writeBytes(bb);
}
 
源代码7 项目: Bytecoder   文件: CharsetTest.java

@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]);
}
 
源代码8 项目: jdk8u-dev-jdk   文件: DkCrypto.java

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;
}
 
源代码9 项目: translationstudio8   文件: VTDLoader.java

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());
}
 
源代码10 项目: openjdk-jdk8u-backup   文件: DkCrypto.java

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;
}
 
源代码11 项目: jdk8u-jdk   文件: DkCrypto.java

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;
}
 
源代码12 项目: Komondor   文件: StringUtils.java

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;
}
 
源代码13 项目: dble   文件: PasswordAuthPlugin.java

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;
}
 
源代码14 项目: jackcess   文件: ColumnImpl.java

/**
 * @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);
}
 
源代码15 项目: jdk8u-jdk   文件: DkCrypto.java

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;
}
 
源代码17 项目: hottub   文件: DkCrypto.java

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;
}
 
源代码18 项目: tomcatsrc   文件: MessageBytes.java

/** Do a char-&gt;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());
}
 
源代码19 项目: MeteoInfo   文件: Converter.java

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;
}