All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.projectfloodlight.openflow.util.ChannelUtils Maven / Gradle / Ivy

Go to download

OpenFlowJ API supporting OpenFlow versions 1.0 through 1.5.1, generated by LoxiGen

The newest version!
package org.projectfloodlight.openflow.util;

import java.util.List;

import org.projectfloodlight.openflow.exceptions.OFParseError;
import org.projectfloodlight.openflow.protocol.OFMessageReader;
import org.projectfloodlight.openflow.protocol.Writeable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;

import io.netty.buffer.ByteBuf;

/**
 * Collection of helper functions for reading and writing into Unpooled
 *
 * @author capveg
 */

public class ChannelUtils {
    private static final Logger logger = LoggerFactory.getLogger(ChannelUtils.class);

    public static String readFixedLengthString(ByteBuf bb, int length) {
        byte[] dst = new byte[length];
        bb.readBytes(dst, 0, length);
        int validLength = 0;
        for (validLength = 0; validLength < length; validLength++) {
            if (dst[validLength] == 0)
                break;
        }
        return new String(dst, 0, validLength, Charsets.US_ASCII);
    }

    public static void writeFixedLengthString(ByteBuf bb, String string,
            int length) {
        int l = string.length();
        if (l > length) {
            throw new IllegalArgumentException("Error writing string: length="
                    + l + " > max Length=" + length);
        }
        bb.writeBytes(string.getBytes(Charsets.US_ASCII));
        if (l < length) {
            bb.writeZero(length - l);
        }
    }

    static public byte[] readBytes(final ByteBuf bb, final int length) {
        byte byteArray[] = new byte[length];
        bb.readBytes(byteArray);
        return byteArray;
    }

    static public void writeBytes(final ByteBuf bb,
            final byte byteArray[]) {
        bb.writeBytes(byteArray);
    }

    public static  List readList(ByteBuf bb, int length, OFMessageReader reader) throws OFParseError {
        int end = bb.readerIndex() + length;
        Builder builder = ImmutableList.builder();
        if(logger.isTraceEnabled())
            logger.trace("readList(length={}, reader={})", length, reader.getClass());
        while(bb.readerIndex() < end) {
            T read = reader.readFrom(bb);
            if(logger.isTraceEnabled())
                logger.trace("readList: read={}, left={}", read, end - bb.readerIndex());
            builder.add(read);
        }
        if(bb.readerIndex() != end) {
            throw new IllegalStateException("Overread length: length="+length + " overread by "+ (bb.readerIndex() - end) + " reader: "+reader);
        }
        return builder.build();
    }

    public static void writeList(ByteBuf bb, List writeables) {
        for(Writeable w: writeables)
            w.writeTo(bb);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy