001package jmri.jmrix.ipocs.protocol.packets;
002
003import java.nio.ByteBuffer;
004
005/**
006 * Packet to send just about anything.
007 *
008 * @author Fredrik Elestedt Copyright (C) 2020
009 * @since 4.21.2
010 */
011@org.openide.util.lookup.ServiceProvider(service = Packet.class)
012public class ApplicationDataPacket extends Packet {
013  public final static byte IDENT = 5;
014  private short xUser;
015  private ByteBuffer data = ByteBuffer.allocate(0);
016
017  @Override
018  public byte getId() {
019    return IDENT;
020  }
021
022  @Override
023  protected void parseSpecific(ByteBuffer buffer) {
024    xUser = buffer.getShort();
025    data = ByteBuffer.allocate(getLength() - 5);
026    data.put(buffer);
027  }
028
029  @Override
030  protected byte[] serializeSpecific() {
031    ByteBuffer buffer = ByteBuffer.allocate(2 + data.capacity());
032    buffer.putShort(xUser);
033    buffer.put(data);
034    buffer.rewind();
035    return buffer.array();
036  }
037
038  public short getxUser() {
039    return xUser;
040  }
041
042  public void setxUser(short xUser) {
043    this.xUser = xUser;
044  }
045
046  public ByteBuffer getData() {
047    data.rewind();
048    return data;
049  }
050
051  public void setData(ByteBuffer data) {
052    this.data = data;
053  }
054}