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);
}
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());
}
}
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);
}
}
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;
}
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());
}
Aggregations