Java Code Examples for java.nio.charset.Charset#encode()

The following examples show how to use java.nio.charset.Charset#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UrlEncodedUtils.java    From jdcloud-sdk-java with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 2
Source File: LFCRLFCharsetChooserValues.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 3
Source File: MultipartForm.java    From iaf with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 4
Source File: MessageBytes.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/** 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());
}
 
Example 5
Source File: DkCrypto.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: HttpMultipart.java    From android-open-project-demo with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: DkCrypto.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: ColumnImpl.java    From jackcess with Apache License 2.0 5 votes vote down vote up
/**
 * @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);
}
 
Example 9
Source File: PasswordAuthPlugin.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: StringUtils.java    From Komondor with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: DkCrypto.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 12
Source File: DkCrypto.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: VTDLoader.java    From translationstudio8 with GNU General Public License v2.0 5 votes vote down vote up
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());
}
 
Example 14
Source File: DkCrypto.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 15
Source File: CharsetTest.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@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]);
}
 
Example 16
Source File: PacketWriter.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void writeLenString(String s, Charset encoder) {
    if (s == null) {
        writeLength(0);
        return;
    }
    ByteBuffer bb = encoder.encode(s);
    writeLength(bb.remaining());
    writeBytes(bb);
}
 
Example 17
Source File: Utf7.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 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());
}
 
Example 18
Source File: PBKDF2KeyImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 19
Source File: Converter.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
static public byte[] convertCharToByteUTF(char[] from) {
    Charset c = CDM.utf8Charset;
    ByteBuffer output = c.encode(CharBuffer.wrap(from));
    return output.array();
}
 
Example 20
Source File: WrappedByteBuffer.java    From particle-android with Apache License 2.0 3 votes vote down vote up
/**
 * 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;
}