Here you can find the source of readShortString(ByteBuffer buffer)
Parameter | Description |
---|---|
buffer | data buffer |
public static String readShortString(ByteBuffer buffer)
//package com.java2s; //License from project: Open Source License import java.io.*; import java.nio.ByteBuffer; public class Main { /**//from w ww .jav a 2 s. c o m * read bytes with a short sign prefix(mark the size of bytes) * * @param buffer data buffer * @return string result(encoding with UTF-8) * @see #writeShortString(ByteBuffer, String) */ public static String readShortString(ByteBuffer buffer) { short size = buffer.getShort(); if (size < 0) { return null; } byte[] bytes = new byte[size]; buffer.get(bytes); return fromBytes(bytes); } public static String fromBytes(byte[] b) { return fromBytes(b, "UTF-8"); } public static String fromBytes(byte[] b, String encoding) { try { return new String(b, encoding); } catch (UnsupportedEncodingException e) { return new String(b); } } }