Java ByteBuffer to String asString(Map headers)

Here you can find the source of asString(Map headers)

Description

as String

License

Open Source License

Declaration

public static String asString(Map<ByteBuffer, ByteBuffer> headers) 

Method Source Code

//package com.java2s;
/**//w  w w  .j  av  a  2 s . c o m
 * Copyright 2007-2016, Kaazing Corporation. All rights reserved.
 *
 * 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.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static final Charset UTF_8 = Charset.forName("UTF-8");

    public static String asString(Map<ByteBuffer, ByteBuffer> headers) {
        if (headers == null) {
            return null;
        }

        Iterator<Entry<ByteBuffer, ByteBuffer>> i = headers.entrySet()
                .iterator();
        if (!i.hasNext()) {
            return "{}";
        }

        StringBuilder sb = new StringBuilder();
        sb.append('{');
        for (;;) {
            Entry<ByteBuffer, ByteBuffer> e = i.next();
            ByteBuffer key = e.getKey();
            ByteBuffer value = e.getValue();
            sb.append(key == headers ? "(this Map)" : asString(key));
            sb.append('=');
            sb.append(value == headers ? "(this Map)" : asString(value));
            if (!i.hasNext()) {
                return sb.append('}').toString();
            }
            sb.append(", ");
        }
    }

    /**
     * @param  buf         UTF-8 encoded bytes
     * @return String      UTF-8 decoded result
     */
    public static String asString(ByteBuffer buf) {
        if (buf == null) {
            return null;
        }

        if (buf.hasArray()) {
            return new String(buf.array(), buf.arrayOffset()
                    + buf.position(), buf.remaining(), UTF_8);
        }

        try {
            // Direct buffer
            CharsetDecoder decoder = UTF_8.newDecoder();
            CharBuffer charBuf = decoder.decode(buf.duplicate());
            return charBuf.toString();
        } catch (CharacterCodingException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. asStr(ByteBuffer buff)
  2. asString(ByteBuffer buffer, int length)
  3. asString(final ByteBuffer buffer)
  4. bbToString(ByteBuffer bb)
  5. buffer2String(ByteBuffer buff, String charsetName)
  6. bufferToString(ByteBuffer buf)
  7. bufferToString(ByteBuffer buf)