alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame^] | 1 | package org.projectfloodlight.openflow.util; |
| 2 | |
| 3 | import java.util.List; |
| 4 | |
| 5 | import org.jboss.netty.buffer.ChannelBuffer; |
| 6 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 7 | import org.projectfloodlight.openflow.protocol.OFMessageReader; |
| 8 | import org.projectfloodlight.openflow.protocol.Writeable; |
| 9 | import org.slf4j.Logger; |
| 10 | import org.slf4j.LoggerFactory; |
| 11 | |
| 12 | import com.google.common.base.Charsets; |
| 13 | import com.google.common.collect.ImmutableList; |
| 14 | import com.google.common.collect.ImmutableList.Builder; |
| 15 | |
| 16 | /** |
| 17 | * Collection of helper functions for reading and writing into ChannelBuffers |
| 18 | * |
| 19 | * @author capveg |
| 20 | */ |
| 21 | |
| 22 | public class ChannelUtils { |
| 23 | private static final Logger logger = LoggerFactory.getLogger(ChannelUtils.class); |
| 24 | public static String readFixedLengthString(ChannelBuffer bb, int length) { |
| 25 | byte[] dst = new byte[length]; |
| 26 | bb.readBytes(dst, 0, length); |
| 27 | int validLength = 0; |
| 28 | for (validLength = 0; validLength < length; validLength++) { |
| 29 | if (dst[validLength] == 0) |
| 30 | break; |
| 31 | } |
| 32 | return new String(dst, 0, validLength, Charsets.US_ASCII); |
| 33 | } |
| 34 | |
| 35 | public static void writeFixedLengthString(ChannelBuffer bb, String string, |
| 36 | int length) { |
| 37 | int l = string.length(); |
| 38 | if (l > length) { |
| 39 | throw new IllegalArgumentException("Error writing string: length=" |
| 40 | + l + " > max Length=" + length); |
| 41 | } |
| 42 | bb.writeBytes(string.getBytes(Charsets.US_ASCII)); |
| 43 | if (l < length) { |
| 44 | bb.writeZero(length - l); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | static public byte[] readBytes(final ChannelBuffer bb, final int length) { |
| 49 | byte byteArray[] = new byte[length]; |
| 50 | bb.readBytes(byteArray); |
| 51 | return byteArray; |
| 52 | } |
| 53 | |
| 54 | static public void writeBytes(final ChannelBuffer bb, |
| 55 | final byte byteArray[]) { |
| 56 | bb.writeBytes(byteArray); |
| 57 | } |
| 58 | |
| 59 | public static <T> List<T> readList(ChannelBuffer bb, int length, OFMessageReader<T> reader) throws OFParseError { |
| 60 | int end = bb.readerIndex() + length; |
| 61 | Builder<T> builder = ImmutableList.<T>builder(); |
| 62 | if(logger.isTraceEnabled()) |
| 63 | logger.trace("readList(length={}, reader={})", length, reader.getClass()); |
| 64 | while(bb.readerIndex() < end) { |
| 65 | T read = reader.readFrom(bb); |
| 66 | if(logger.isTraceEnabled()) |
| 67 | logger.trace("readList: read={}, left={}", read, end - bb.readerIndex()); |
| 68 | builder.add(read); |
| 69 | } |
| 70 | if(bb.readerIndex() != end) { |
| 71 | throw new IllegalStateException("Overread length: length="+length + " overread by "+ (bb.readerIndex() - end) + " reader: "+reader); |
| 72 | } |
| 73 | return builder.build(); |
| 74 | } |
| 75 | |
| 76 | public static void writeList(ChannelBuffer bb, List<? extends Writeable> writeables) { |
| 77 | for(Writeable w: writeables) |
| 78 | w.writeTo(bb); |
| 79 | } |
| 80 | } |