当前位置: 首页>>代码示例>>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;未经允许,请勿转载。