Java ByteBuffer to String string(ByteBuffer buffer)

Here you can find the source of string(ByteBuffer buffer)

Description

Returns a string in the buffer.

License

Apache License

Parameter

Parameter Description
buffer the buffer

Return

the string

Declaration

public static String string(ByteBuffer buffer) 

Method Source Code

//package com.java2s;
/**//from w w  w  . j av  a2  s .  co m
 * Copyright 2011-2017 Asakusa Framework Team.
 *
 * 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.charset.StandardCharsets;

public class Main {
    /**
     * Returns a string in the buffer.
     * @param buffer the buffer
     * @return the string
     */
    public static String string(ByteBuffer buffer) {
        byte[] bytes = new byte[buffer.remaining()];
        buffer.get(bytes);
        return new String(bytes, StandardCharsets.UTF_8);
    }
}

Related

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