Java Question What does this code do?

DonManfred

Expert
Licensed User
i´m checking a java code to implement in a project.

I found a code which looks like

B4X:
public class PacketLapData extends Packet{
  
    ////////////////////////////////////////////////////////////////
    //                                                            //
    //                      LAP DATA PACKET                       //
    //                                                            //
    ////////////////////////////////////////////////////////////////
    // STATIC VALUES
    /**
     * Packet size in bytes (w/o header size).
     */
    public static int LENGHT = 843;
    /**
     * Lap data for all cars on track (20).
     */
    public ArrayList<LapData> lapData = new ArrayList<>();
  
    /**
     * Packet Lap Data constructor.
     * @param content All datagram content in bytes.
     */
    public PacketLapData(byte[] content){
        super(Arrays.copyOfRange(content, 0, Packet.HEADER_SIZE));
        super.lenght = LENGHT;

        int from = Packet.HEADER_SIZE;
        int to = 0;
        byte[] lapcontent;
        while(from < LENGHT){
            to = from + LapData.SIZE;

            lapcontent = Arrays.copyOfRange(content, from, to);
          
            LapData lap = new LapData(lapcontent);
            lapData.add(lap);
          
            from += LapData.SIZE;
        }
    }
my question:
B4X:
 content has a length of, lets say, 1500
public PacketLapData(byte[] content){
        super(Arrays.copyOfRange(content, 0, Packet.HEADER_SIZE)); //  HEADER_SIZE is 20
        super.lenght = LENGHT;
        //
What is the content after the line super.lenght = LENGHT;?
Is it bytes 21 to 21+843 (= 843 bytes?) and the content-size is now 843?
In fact i need to parse the Bytes 21+ from the original content (which includes the header too). I need to parse 843 bytes into a PacketLapData. Starting from byte index 20

The question. Do i need to consider the header still exists in the content?
Means. Is byte index 0 of content now the byte index 20 of the original contentlength of 1500?
 
Last edited:

DonManfred

Expert
Licensed User
41 in the old UDP-Structure

To be exact (the values here was just an example)

The Header in 2019 is 22 bytes.
content has a total length of 1500

But in this paket only 843 bytes need to be interpretet

Lap Data Packet
The lap data packet gives details of all the cars in the session.
Frequency: Rate as specified in menus
Size: 843 bytes
Version: 1

B4X:
struct LapData
{
   float       m_lastLapTime;               // Last lap time in seconds
   float       m_currentLapTime;    // Current time around the lap in seconds
   float       m_bestLapTime;        // Best lap time of the session in seconds
   float       m_sector1Time;        // Sector 1 time in seconds
   float       m_sector2Time;        // Sector 2 time in seconds
   float       m_lapDistance;        // Distance vehicle is around current lap in metres – could
                   // be negative if line hasn’t been crossed yet
   float       m_totalDistance;        // Total distance travelled in session in metres – could
                   // be negative if line hasn’t been crossed yet
   float       m_safetyCarDelta;        // Delta in seconds for safety car
   uint8       m_carPosition;       // Car race position
   uint8       m_currentLapNum;        // Current lap number
   uint8       m_pitStatus;                // 0 = none, 1 = pitting, 2 = in pit area
   uint8       m_sector;                   // 0 = sector1, 1 = sector2, 2 = sector3
   uint8       m_currentLapInvalid;        // Current lap invalid - 0 = valid, 1 = invalid
   uint8       m_penalties;                // Accumulated time penalties in seconds to be added
   uint8       m_gridPosition;             // Grid position the vehicle started the race in
   uint8       m_driverStatus;             // Status of driver - 0 = in garage, 1 = flying lap
// 2 = in lap, 3 = out lap, 4 = on track
   uint8       m_resultStatus;          // Result status - 0 = invalid, 1 = inactive, 2 = active
// 3 = finished, 4 = disqualified, 5 = not classified
// 6 = retired
};


struct PacketLapData
{
   PacketHeader    m_header;              // Header

   LapData         m_lapData[20];         // Lap data for all cars on track
};
 
Top