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

The following examples show how to use java.nio.charset.Charset#encode() . These examples are extracted from open source projects. 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
@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 2
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 3
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 4
Source Project: iaf   File: MultipartForm.java    License: 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 5
/**
 * 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 6
public void writeLenString(String s, Charset encoder) {
    if (s == null) {
        writeLength(0);
        return;
    }
    ByteBuffer bb = encoder.encode(s);
    writeLength(bb.remaining());
    writeBytes(bb);
}
 
Example 7
Source Project: Bytecoder   File: CharsetTest.java    License: 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 8
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 9
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 10
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 11
Source Project: jdk8u-jdk   File: DkCrypto.java    License: 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
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 13
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 14
Source Project: jackcess   File: ColumnImpl.java    License: 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 15
Source Project: jdk8u-jdk   File: DkCrypto.java    License: 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 16
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 17
Source Project: hottub   File: DkCrypto.java    License: 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 18
Source Project: tomcatsrc   File: MessageBytes.java    License: Apache License 2.0 5 votes vote down vote up
/** 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());
}
 
Example 19
static public byte[] convertCharToByteUTF(char[] from) {
    Charset c = CDM.utf8Charset;
    ByteBuffer output = c.encode(CharBuffer.wrap(from));
    return output.array();
}
 
Example 20
/**
 * 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;
}