本文整理匯總了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;
}