Search in sources :

Example 96 with CharBuffer

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);
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 97 with CharBuffer

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();
        }
    }
}
Also used : CharBuffer(java.nio.CharBuffer) BufferOverflowException(java.nio.BufferOverflowException) BufferOverflowException(java.nio.BufferOverflowException) IOException(java.io.IOException) CoderResult(java.nio.charset.CoderResult)

Example 98 with CharBuffer

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);
}
Also used : CharBuffer(java.nio.CharBuffer) Test(org.junit.Test)

Example 99 with CharBuffer

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);
}
Also used : CharBuffer(java.nio.CharBuffer) Test(org.junit.Test)

Example 100 with CharBuffer

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;
}
Also used : CharBuffer(java.nio.CharBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CharBuffer (java.nio.CharBuffer)387 ByteBuffer (java.nio.ByteBuffer)143 CoderResult (java.nio.charset.CoderResult)81 CharsetDecoder (java.nio.charset.CharsetDecoder)45 IOException (java.io.IOException)41 Charset (java.nio.charset.Charset)28 Test (org.junit.Test)23 CharacterCodingException (java.nio.charset.CharacterCodingException)12 CharsetEncoder (java.nio.charset.CharsetEncoder)12 FileInputStream (java.io.FileInputStream)11 IntBuffer (java.nio.IntBuffer)10 Reader (java.io.Reader)9 BufferOverflowException (java.nio.BufferOverflowException)9 DoubleBuffer (java.nio.DoubleBuffer)9 FloatBuffer (java.nio.FloatBuffer)9 LongBuffer (java.nio.LongBuffer)9 ShortBuffer (java.nio.ShortBuffer)9 BufferUnderflowException (java.nio.BufferUnderflowException)7 ValueWrapper (org.apache.geode.internal.memcached.ValueWrapper)7 ReadableByteChannel (java.nio.channels.ReadableByteChannel)6