本文整理汇总了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();
}
示例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();
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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());
}
示例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;
}
示例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;
}
示例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;
}
示例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();
}
示例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;
}
示例13: toBytes
import java.nio.charset.Charset; //导入方法依赖的package包/类
/**
* 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());
}
示例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;
}
示例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;
}