Search in sources :

Example 41 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project tutorials by eugenp.

the class GuavaStringUnitTest method whenRemoveCharsNotInCharset_thenRemoved.

@Test
public void whenRemoveCharsNotInCharset_thenRemoved() {
    final Charset charset = Charset.forName("cp437");
    final CharsetEncoder encoder = charset.newEncoder();
    final Predicate<Character> inRange = new Predicate<Character>() {

        @Override
        public boolean apply(final Character c) {
            return encoder.canEncode(c);
        }
    };
    final String result = CharMatcher.forPredicate(inRange).retainFrom("helloは");
    assertEquals("hello", result);
}
Also used : Charset(java.nio.charset.Charset) Matchers.containsString(org.hamcrest.Matchers.containsString) CharsetEncoder(java.nio.charset.CharsetEncoder) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Example 42 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project beetl2.0 by javamonkey.

the class DefaultEncoder method write.

public void write(final char[] chars, final int len, final OutputStream out) throws IOException {
    if (chars != null && len != 0) {
        final CharsetEncoder encoder;
        final ByteBuffer bb;
        byte[] buffer = this.localBuffer.getByteBuffer((int) (len * this.expansionFactor));
        char[] charBuffer = this.localBuffer.getCharBuffer(len);
        (encoder = this.charsetEncoder).reset().encode(CharBuffer.wrap(chars, 0, len), bb = ByteBuffer.wrap(buffer), true);
        encoder.flush(bb);
        out.write(buffer, 0, bb.position());
    }
}
Also used : CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 43 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project vertexium by visallo.

the class KeyHelper method getColumnQualifierFromPropertyMetadataColumnQualifier.

public static Text getColumnQualifierFromPropertyMetadataColumnQualifier(String propertyName, String propertyKey, String visibilityString, String metadataKey, NameSubstitutionStrategy nameSubstitutionStrategy) {
    String name = nameSubstitutionStrategy.deflate(propertyName);
    String key = nameSubstitutionStrategy.deflate(propertyKey);
    metadataKey = nameSubstitutionStrategy.deflate(metadataKey);
    KeyBase.assertNoValueSeparator(name);
    KeyBase.assertNoValueSeparator(key);
    KeyBase.assertNoValueSeparator(visibilityString);
    KeyBase.assertNoValueSeparator(metadataKey);
    int charCount = name.length() + key.length() + visibilityString.length() + metadataKey.length() + 3;
    CharBuffer qualifierChars = (CharBuffer) CharBuffer.allocate(charCount).put(name).put(KeyBase.VALUE_SEPARATOR).put(key).put(KeyBase.VALUE_SEPARATOR).put(visibilityString).put(KeyBase.VALUE_SEPARATOR).put(metadataKey).flip();
    CharsetEncoder encoder = ENCODER_FACTORY.get();
    encoder.reset();
    try {
        ByteBuffer encodedQualifier = encoder.encode(qualifierChars);
        Text result = new Text();
        result.set(encodedQualifier.array(), 0, encodedQualifier.limit());
        return result;
    } catch (CharacterCodingException cce) {
        throw new RuntimeException("This should never happen", cce);
    }
}
Also used : CharBuffer(java.nio.CharBuffer) Text(org.apache.hadoop.io.Text) CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Example 44 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project bson4jackson by michel-kraemer.

the class DynamicOutputBuffer method putUTF8.

/**
 * Puts the given string as UTF-8 into the buffer at the
 * given position. This method does not increase the write position.
 * @param pos the position where to put the string
 * @param s the string to put
 * @return the number of UTF-8 bytes put
 */
public int putUTF8(int pos, String s) {
    ByteBuffer minibb = null;
    CharsetEncoder enc = getUTF8Encoder();
    CharBuffer in = CharBuffer.wrap(s);
    int pos2 = pos;
    ByteBuffer bb = getBuffer(pos2);
    int index = pos2 % _bufferSize;
    bb.position(index);
    while (in.remaining() > 0) {
        CoderResult res = enc.encode(in, bb, true);
        // flush minibb first
        if (bb == minibb) {
            bb.flip();
            while (bb.remaining() > 0) {
                putByte(pos2, bb.get());
                ++pos2;
            }
        } else {
            pos2 += bb.position() - index;
        }
        if (res.isOverflow()) {
            if (bb.remaining() > 0) {
                // exceeded buffer boundaries; write to a small temporary buffer
                if (minibb == null) {
                    minibb = ByteBuffer.allocate(4);
                }
                minibb.rewind();
                bb = minibb;
                index = 0;
            } else {
                bb = getBuffer(pos2);
                index = pos2 % _bufferSize;
                bb.position(index);
            }
        } else if (res.isError()) {
            try {
                res.throwException();
            } catch (CharacterCodingException e) {
                throw new RuntimeException("Could not encode string", e);
            }
        }
    }
    adaptSize(pos2);
    return pos2 - pos;
}
Also used : CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder) CoderResult(java.nio.charset.CoderResult)

Example 45 with CharsetEncoder

use of java.nio.charset.CharsetEncoder in project Symphony by Vazkii.

the class EncodedText method charBufferToBytes.

protected static byte[] charBufferToBytes(CharBuffer charBuffer, String characterSet) throws CharacterCodingException {
    Charset charset = Charset.forName(characterSet);
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer byteBuffer = encoder.encode(charBuffer);
    return BufferTools.copyBuffer(byteBuffer.array(), 0, byteBuffer.limit());
}
Also used : Charset(java.nio.charset.Charset) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharsetEncoder (java.nio.charset.CharsetEncoder)177 ByteBuffer (java.nio.ByteBuffer)87 Charset (java.nio.charset.Charset)50 CharBuffer (java.nio.CharBuffer)42 CharacterCodingException (java.nio.charset.CharacterCodingException)31 CoderResult (java.nio.charset.CoderResult)31 OutputStreamWriter (java.io.OutputStreamWriter)21 IOException (java.io.IOException)15 BufferedWriter (java.io.BufferedWriter)13 OutputStream (java.io.OutputStream)10 FileOutputStream (java.io.FileOutputStream)7 InputStream (java.io.InputStream)7 IStatus (org.eclipse.core.runtime.IStatus)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)6 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)6 CoreException (org.eclipse.core.runtime.CoreException)6 Status (org.eclipse.core.runtime.Status)6 ArrayEncoder (sun.nio.cs.ArrayEncoder)6 Writer (java.io.Writer)5