Here you can find the source of string(ByteBuffer buffer, int position, int length, Charset charset)
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 |
public static String string(ByteBuffer buffer, int position, int length, Charset charset) throws CharacterCodingException
//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(); } }