Java ByteBuffer to String string(ByteBuffer buffer, int position, int length, Charset charset)

Here you can find the source of string(ByteBuffer buffer, int position, int length, Charset charset)

Description

Decode a String representation.

License

Apache License

Parameter

Parameter Description
buffer a byte buffer holding the string representation
position the starting position in buffer to start decoding from
length the number of bytes from buffer to use
charset the String encoding charset

Return

the decoded string

Declaration

public static String string(ByteBuffer buffer, int position, int length, Charset charset)
        throws CharacterCodingException 

Method Source Code

//package com.java2s;
/*//  w w w . j  a v  a2  s .co m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.charset.CharacterCodingException;
import java.nio.charset.Charset;

public class Main {
    /**
     * Decode a String representation.
     * 
     * @param buffer
     *            a byte buffer holding the string representation
     * @param position
     *            the starting position in {@code buffer} to start decoding from
     * @param length
     *            the number of bytes from {@code buffer} to use
     * @param charset
     *            the String encoding charset
     * @return the decoded string
     */
    public static String string(ByteBuffer buffer, int position, int length, Charset charset)
            throws CharacterCodingException {
        ByteBuffer copy = buffer.duplicate();
        copy.position(position);
        copy.limit(copy.position() + length);
        return string(copy, charset);
    }

    /**
     * Decode a String representation.
     * 
     * @param buffer
     *            a byte buffer holding the string representation
     * @param charset
     *            the String encoding charset
     * @return the decoded string
     */
    public static String string(ByteBuffer buffer, Charset charset) throws CharacterCodingException {
        return charset.newDecoder().decode(buffer.duplicate()).toString();
    }
}

Related

  1. readString(ByteBuffer logBuf)
  2. readString(final ByteBuffer buffer, final String encoding)
  3. string(ByteBuffer b, Charset charset)
  4. string(ByteBuffer buffer)
  5. string(ByteBuffer buffer)
  6. string(ByteBuffer bytes)
  7. toString(@Nonnull ByteBuffer buf, int len)
  8. toString(ByteBuffer b, String separator)
  9. toString(ByteBuffer bb)