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

com.ardikars.jxpacket.common.UnknownPacket Maven / Gradle / Ivy

There is a newer version: 1.2.6.RELEASE
Show newest version
/**
 * Copyright (C) 2017-2018  Ardika Rommy Sanjaya 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package com.ardikars.jxpacket.common;

import com.ardikars.common.util.NamedNumber;
import io.netty.buffer.ByteBuf;

/**
 * Unknown packet.
 * @author Ardika Rommy Sanjaya
 * @since 1.5.0
 */
public class UnknownPacket extends AbstractPacket {

	private final UnknownPacket.Header header;

	private UnknownPacket(final Builder builder) {
		this.header = new UnknownPacket.Header(builder);
	}

	public static UnknownPacket newPacket(final ByteBuf buffer) {
		return new UnknownPacket.Builder().build(buffer);
	}

	@Override
	public UnknownPacket.Header getHeader() {
		return this.header;
	}

	@Override
	public Packet getPayload() {
		return null;
	}

	public static final class Header implements Packet.Header {

		final private ByteBuf buffer;

		public Header(final Builder builder) {
			this.buffer = builder.payloadBuffer;
		}

		@Override
		public int getLength() {
			return buffer.capacity();
		}

		@Override
		public ByteBuf getBuffer() {
			return this.buffer;
		}

		@Override
		public  T getPayloadType() {
			return null;
		}

		@Override
		public String toString() {
			return new StringBuilder()
					.append("\tbuffer: ").append(buffer).append('\n')
					.toString();
		}

	}

	@Override
	public String toString() {
		return new StringBuilder("[ UnknownPacket Header (").append(getHeader().getLength()).append(" bytes) ]")
				.append('\n').append(header).toString();
	}

	public static final class Builder implements Packet.Builder {

		private ByteBuf payloadBuffer;

		public Builder payloadBuffer(final ByteBuf buffer) {
			this.payloadBuffer = buffer;
			return this;
		}

		@Override
		public UnknownPacket build() {
			return new UnknownPacket(this);
		}

		@Override
		public UnknownPacket build(ByteBuf buffer) {
			Builder builder = new Builder()
					.payloadBuffer(buffer);
			release(buffer);
			return new UnknownPacket(builder);
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy