New utility function convers input ByteBuf to byte array,
encode it with Base64 and convert to string.
The function can be useful when converting NLRI-based
route key to string representation.
Change-Id: I2adbb98da0d0841a2c4eafed841c457e05367c2e
Signed-off-by: Milos Fabian <milfabia@cisco.com>
(cherry picked from commit
cef0f088696ca9053f0a6b9b41db2818bd138d4a)
package org.opendaylight.protocol.util;
import com.google.common.base.Preconditions;
+import com.google.common.io.BaseEncoding;
import io.netty.buffer.ByteBuf;
import java.io.File;
import java.io.FileInputStream;
return Arrays.toString(bytes);
}
}
+
+ /**
+ * Encode input ByteBuf with Base64 to string format.
+ *
+ * @param buffer Input ByteBuf
+ * @return String representation of encoded ByteBuf.
+ */
+ public static String encodeBase64(final ByteBuf buffer) {
+ return BaseEncoding.base64().encode(ByteArray.readAllBytes(buffer));
+ }
}
}
}
+ @Test
+ public void testEncodeBase64() {
+ final String result = ByteArray.encodeBase64(Unpooled.wrappedBuffer("abc123".getBytes()));
+ assertEquals("YWJjMTIz", result);
+ }
+
}