1 package org.openflow.util;
3 import java.io.UnsupportedEncodingException;
4 import java.nio.ByteBuffer;
5 import java.nio.charset.Charset;
6 import java.util.Arrays;
8 public class StringByteSerializer {
9 public static String readFrom(ByteBuffer data, int length) {
10 byte[] stringBytes = new byte[length];
11 data.get(stringBytes);
12 // find the first index of 0
14 for (byte b : stringBytes) {
19 return new String(Arrays.copyOf(stringBytes, index),
20 Charset.forName("ascii"));
23 public static void writeTo(ByteBuffer data, int length, String value) {
25 byte[] name = value.getBytes("ASCII");
26 if (name.length < length) {
28 for (int i = name.length; i < length; ++i) {
32 data.put(name, 0, length-1);
35 } catch (UnsupportedEncodingException e) {
36 throw new RuntimeException(e);