static void toHexString(String header, ByteBuffer buf) { sb.delete(0, sb.length()); for (int index = 0; index < buf.limit(); index++) { String hex = Integer.toHexString(0x0100 + (buf.get(index) & 0x00FF)).substring(1); sb.append((hex.length() < 2 ? "0" : "") + hex + " "); } LOG.debug( "hex->" + header + ": position,limit,capacity " + buf.position() + "," + buf.limit() + "," + buf.capacity()); LOG.debug("hex->" + sb.toString()); }
static byte[] extractByteArray(ByteBuffer buf) { int len = buf.getInt(); if (len > 0) { byte[] a = new byte[len]; buf.get(a, 0, len); return a; } else return new byte[0]; }
static void insertString(String str, ByteBuffer buf) throws java.io.UnsupportedEncodingException { if (str != null && str.length() > 0) { buf.putInt(str.length() + 1); buf.put(str.getBytes("UTF-8"), 0, str.length()); buf.put((byte) 0); } else { // buffer is null or length 0 buf.putInt(0); } }
static void printBBInfo(ByteBuffer buf) { LOG.debug( "Info : position,limit,capacity " + buf.position() + "," + buf.limit() + "," + buf.capacity()); }
static void insertByteString(byte[] array, ByteBuffer buf) throws java.io.UnsupportedEncodingException { if (array != null && array.length > 0) { buf.putInt(array.length); buf.put(array, 0, array.length); buf.put((byte) 0); } else { // buffer is null or length 0 buf.putInt(0); } }
static byte[] extractByteString(ByteBuffer buf) { int len = buf.getInt(); byte[] str = null; if (len > 0) { str = new byte[len]; buf.get(str, 0, len); buf.get(); // trailing null } else str = new byte[0]; return str; }
static String extractString(ByteBuffer buf) throws java.io.UnsupportedEncodingException { int len = buf.getInt(); byte[] str = null; if (len > 0) { str = new byte[len - 1]; ; buf.get(str, 0, len - 1); buf.get(); // trailing null } else str = new byte[0]; return new String(str, "UTF-8"); }
static void insertByteArray(byte[] array, ByteBuffer buf) { if (array != null && array.length > 0) { buf.putInt(array.length); buf.put(array, 0, array.length); } else buf.putInt(0); }