當前位置: 首頁>>代碼示例>>Java>>正文


Java Charset.encode方法代碼示例

本文整理匯總了Java中java.nio.charset.Charset.encode方法的典型用法代碼示例。如果您正苦於以下問題:Java Charset.encode方法的具體用法?Java Charset.encode怎麽用?Java Charset.encode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.nio.charset.Charset的用法示例。


在下文中一共展示了Charset.encode方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: urlencode

import java.nio.charset.Charset; //導入方法依賴的package包/類
private static String urlencode(
        final String content,
        final Charset charset,
        final BitSet safechars,
        final boolean blankAsPlus) {
    if (content == null) {
        return null;
    }
    StringBuilder buf = new StringBuilder();
    ByteBuffer bb = charset.encode(content);
    while (bb.hasRemaining()) {
        int b = bb.get() & 0xff;
        if (safechars.get(b)) {
            buf.append((char) b);
        } else if (blankAsPlus && b == ' ') {
            buf.append('+');
        } else {
            buf.append("%");
            char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, RADIX));
            char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, RADIX));
            buf.append(hex1);
            buf.append(hex2);
        }
    }
    return buf.toString();
}
 
開發者ID:SavorGit,項目名稱:Hotspot-master-devp,代碼行數:27,代碼來源:URLEncodedUtils.java

示例2: urlEncode

import java.nio.charset.Charset; //導入方法依賴的package包/類
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();
}
 
開發者ID:rocye,項目名稱:wx-idk,代碼行數:23,代碼來源:HttpRequestTools.java

示例3: getBytes

import java.nio.charset.Charset; //導入方法依賴的package包/類
public static byte[] getBytes(String value, int offset, int length, String encoding) throws UnsupportedEncodingException {
    // Some CharsetEncoders (e.g. CP942, CP943, CP948, CP950, CP1381, CP1383, x-COMPOUND_TEXT or ISO-2022-JP) can't
    // handle correctly when encoding directly from its methods while calling the encoder from String object works
    // just fine. Most of these problems occur only in Java 1.5.
    // CharsetEncoder#encode() may be used in Java 1.6+ but only the method that receives a char[] as argument as
    // the one that receives a String argument doesn't always behaves correctly.
    if (!Util.isJdbc4()) {
        if (offset != 0 || length != value.length()) {
            return value.substring(offset, offset + length).getBytes(encoding);
        }
        return value.getBytes(encoding);
    }

    Charset cs = findCharset(encoding);

    ByteBuffer buf = cs.encode(CharBuffer.wrap(value.toCharArray(), offset, length));

    // can't simply .array() this to get the bytes especially with variable-length charsets the buffer is sometimes larger than the actual encoded data
    int encodedLen = buf.limit();
    byte[] asBytes = new byte[encodedLen];
    buf.get(asBytes, 0, encodedLen);

    return asBytes;
}
 
開發者ID:bragex,項目名稱:the-vigilantes,代碼行數:25,代碼來源:StringUtils.java

示例4: getBytes

import java.nio.charset.Charset; //導入方法依賴的package包/類
public static byte[] getBytes(char[] value, int offset, int length, String encoding) throws UnsupportedEncodingException {
    Charset cs = findCharset(encoding);

    ByteBuffer buf = cs.encode(CharBuffer.wrap(value, offset, length));

    // can't simply .array() this to get the bytes especially with variable-length charsets the buffer is sometimes larger than the actual encoded data
    int encodedLen = buf.limit();
    byte[] asBytes = new byte[encodedLen];
    buf.get(asBytes, 0, encodedLen);

    return asBytes;
}
 
開發者ID:rafallis,項目名稱:BibliotecaPS,代碼行數:13,代碼來源:StringUtils.java

示例5: charToUtf16

import java.nio.charset.Charset; //導入方法依賴的package包/類
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;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:11,代碼來源:DkCrypto.java

示例6: charToUtf8

import java.nio.charset.Charset; //導入方法依賴的package包/類
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;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:11,代碼來源:DkCrypto.java

示例7: encode

import java.nio.charset.Charset; //導入方法依賴的package包/類
/**
 * 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());
}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:18,代碼來源:Utf7.java

示例8: encode

import java.nio.charset.Charset; //導入方法依賴的package包/類
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;
}
 
開發者ID:SavorGit,項目名稱:Hotspot-master-devp,代碼行數:8,代碼來源:HttpMultipart.java

示例9: getBytesNullTerminated

import java.nio.charset.Charset; //導入方法依賴的package包/類
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;
}
 
開發者ID:bragex,項目名稱:the-vigilantes,代碼行數:13,代碼來源:StringUtils.java

示例10: getPasswordBytes

import java.nio.charset.Charset; //導入方法依賴的package包/類
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;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:12,代碼來源:PBKDF2KeyImpl.java

示例11: getBytes

import java.nio.charset.Charset; //導入方法依賴的package包/類
private static byte[] getBytes(char[] chars) {
    Charset cs = Charset.forName("UTF-8");
    CharBuffer cb = CharBuffer.allocate(chars.length);
    cb.put(chars);
    cb.flip();
    ByteBuffer bb = cs.encode(cb);
    return bb.array();
}
 
開發者ID:rpgmakervx,項目名稱:slardar,代碼行數:9,代碼來源:IOUtils.java

示例12: putPrefixedString

import java.nio.charset.Charset; //導入方法依賴的package包/類
/**
 * Puts a string into the buffer at the specified index, using the character set to encode the string as bytes.
 * 
 * @param fieldSize
 *            the width in bytes of the prefixed length field
 * @param v
 *            the string
 * @param cs
 *            the character set
 * 
 * @return the buffer
 */
public WrappedByteBuffer putPrefixedString(int fieldSize, String v, Charset cs) {
    if (fieldSize == 0) {
        return this;
    }
    boolean utf16 = cs.name().startsWith("UTF-16");

    if (utf16 && (fieldSize == 1)) {
        throw new IllegalArgumentException("fieldSize is not even for UTF-16 character set");
    }

    java.nio.ByteBuffer strBuf = cs.encode(v);
    _autoExpand(fieldSize + strBuf.limit());

    int len = strBuf.remaining();
    switch (fieldSize) {
    case 1:
        put((byte) len);
        break;
    case 2:
        putShort((short) len);
        break;
    case 4:
        putInt(len);
        break;
    default:
        throw new IllegalArgumentException("Illegal argument, field size should be 1,2 or 4 and fieldSize is: " + fieldSize);
    }

    _buf.put(strBuf);
    return this;
}
 
開發者ID:Datatellit,項目名稱:xlight_android_native,代碼行數:44,代碼來源:WrappedByteBuffer.java

示例13: toBytes

import java.nio.charset.Charset; //導入方法依賴的package包/類
/**
 * 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());
}
 
開發者ID:how2j,項目名稱:lazycat,代碼行數:15,代碼來源:MessageBytes.java

示例14: encode

import java.nio.charset.Charset; //導入方法依賴的package包/類
private static byte[] encode(String text, Charset charset) {
    ByteBuffer buffer = charset.encode(text);
    byte[] bytes = new byte[buffer.limit()];
    buffer.get(bytes);
    return bytes;
}
 
開發者ID:philipwhiuk,項目名稱:q-mail,代碼行數:7,代碼來源:EncoderUtil.java

示例15: encode

import java.nio.charset.Charset; //導入方法依賴的package包/類
private static ByteArrayBuffer encode(Charset charset, 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;
}
 
開發者ID:JackChan1999,項目名稱:boohee_v5.6,代碼行數:7,代碼來源:HttpMultipart.java


注:本文中的java.nio.charset.Charset.encode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。