use of java.nio.CharBuffer in project bazel by bazelbuild.
the class NSString method toBinary.
@Override
public void toBinary(BinaryPropertyListWriter out) throws IOException {
CharBuffer charBuf = CharBuffer.wrap(content);
int kind;
ByteBuffer byteBuf;
synchronized (NSString.class) {
if (asciiEncoder == null)
asciiEncoder = Charset.forName("ASCII").newEncoder();
else
asciiEncoder.reset();
if (asciiEncoder.canEncode(charBuf)) {
// standard ASCII
kind = 0x5;
byteBuf = asciiEncoder.encode(charBuf);
} else {
if (utf16beEncoder == null)
utf16beEncoder = Charset.forName("UTF-16BE").newEncoder();
else
utf16beEncoder.reset();
// UTF-16-BE
kind = 0x6;
byteBuf = utf16beEncoder.encode(charBuf);
}
}
byte[] bytes = new byte[byteBuf.remaining()];
byteBuf.get(bytes);
out.writeIntHeader(kind, content.length());
out.write(bytes);
}
use of java.nio.CharBuffer in project grails-core by grails.
the class BoundedCharsAsEncodedBytesCounter method update.
public void update(char[] buf, int off, int len) {
if (calculationActive && len > 0) {
try {
CharBuffer cb = CharBuffer.wrap(buf, off, len);
ce.reset();
CoderResult cr = ce.encode(cb, bb, true);
if (!cr.isUnderflow()) {
terminateCalculation();
return;
}
cr = ce.flush(bb);
if (!cr.isUnderflow()) {
terminateCalculation();
return;
}
} catch (BufferOverflowException e) {
terminateCalculation();
} catch (Exception x) {
terminateCalculation();
}
}
}
use of java.nio.CharBuffer in project buck by facebook.
the class LineIteratingTest method inputBufferReusedForCallbacksIfNothingLeft.
@Test
public void inputBufferReusedForCallbacksIfNothingLeft() {
final CharBuffer buf = CharBuffer.wrap("foo\nbar\nbaz\n");
LineIterating.CharLineHandler reuseLineHandler = new LineIterating.CharLineHandler(20) {
@Override
public boolean handleLine(CharBuffer line) {
assertThat(line, is(buf));
return true;
}
};
LineIterating.iterateByLines(buf, reuseLineHandler);
}
use of java.nio.CharBuffer in project buck by facebook.
the class LineIteratingTest method inputBufferNotReusedForCallbacksIfDataLeftInLineHandler.
@Test
public void inputBufferNotReusedForCallbacksIfDataLeftInLineHandler() {
final CharBuffer buf = CharBuffer.wrap("bar\n");
LineIterating.CharLineHandler reuseLineHandler = new LineIterating.CharLineHandler(20) {
@Override
public boolean handleLine(CharBuffer line) {
assertThat(line, not(is(buf)));
return true;
}
};
LineIterating.iterateByLines("no-newline-yet", reuseLineHandler);
LineIterating.iterateByLines(buf, reuseLineHandler);
}
use of java.nio.CharBuffer in project gocd by gocd.
the class NokogiriHelpers method convertEncoding.
public static byte[] convertEncoding(Charset output_charset, String input_string) throws CharacterCodingException {
CharsetEncoder encoder = output_charset.newEncoder();
CharBuffer charBuffer = CharBuffer.wrap(input_string);
ByteBuffer byteBuffer = encoder.encode(charBuffer);
byte[] buffer = new byte[byteBuffer.remaining()];
byteBuffer.get(buffer);
return buffer;
}
Aggregations