Java ByteBuffer Put putCharSequence(ByteBuffer buf, Charset charset, CharSequence value)

Here you can find the source of putCharSequence(ByteBuffer buf, Charset charset, CharSequence value)

Description

put Char Sequence

License

Apache License

Declaration

public static void putCharSequence(ByteBuffer buf, Charset charset, CharSequence value) 

Method Source Code


//package com.java2s;
/*/*  w ww  .  j a v  a  2s  .  c  o m*/
 * Copyright (C) 2008-2010 Wayne Meissner
 *
 * This file is part of the JNR project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.*;

public class Main {
    public static void putCharSequence(ByteBuffer buf, Charset charset, CharSequence value) {
        putCharSequence(buf, charset.newEncoder(), value);
    }

    public static void putCharSequence(ByteBuffer buf, CharsetEncoder encoder, CharSequence value) {
        // 
        // Convert any CharSequence implementor (String, etc) into a native
        // C string.
        //
        encoder.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE)
                .encode(CharBuffer.wrap(value), buf, true);
        encoder.flush(buf);
        final int nulSize = Math.round(encoder.maxBytesPerChar());
        // NUL terminate the string
        if (nulSize == 4) {
            buf.putInt(0);
        } else if (nulSize == 2) {
            buf.putShort((short) 0);
        } else if (nulSize == 1) {
            buf.put((byte) 0);
        }
    }
}

Related

  1. putByte(ByteBuffer byteBuffer, int value)
  2. putByteArray(ByteBuffer byteBuffer, byte[] array)
  3. putByteAsString(ByteBuffer buffer, byte value)
  4. putByteBuffer(ByteBuffer source, ByteBuffer target)
  5. putBytesUnescaped(ByteBuffer buffer, byte[] data)
  6. putDecInt(ByteBuffer buffer, int n)
  7. putInt(ByteBuffer bb, long value)
  8. putInt(ByteBuffer buffer, int val, ByteOrder order)
  9. putInt(final ByteBuffer buffer, int value)