0

I am having quite the difficult time parsing this .dat file using Java. The format of the .dat file which I am trying to parse is shown below. These are actually packets for a packet generator program I am working on. Each packet is delineated with a new line between each block. I want each byte in the packet to be stored in a single byte []. I have been using the BufferedReader readline() method to go line by line but still have had little success in storing an entire packet. Thanks in advance for any help you can provide.

00 04 75 8d 49 c7 00 01 03 cd 50 3c 08 00 45 00 
00 30 08 0d 40 00 80 06 00 00 c0 a8 ec 20 c0 a8 
ec 1e 04 16 00 50 b8 63 45 fd 00 00 00 00 70 02 
fa f0 2b d7 00 00 02 04 05 b4 01 01 04 02 

00 01 03 cd 50 3c 00 01 03 dd 4c 2d 08 00 45 00 
00 30 00 00 40 00 40 06 e1 37 c0 a8 ec 1e c0 a8 
ec 20 00 50 04 16 e9 bb 68 bf b8 63 45 fe 70 12 
16 d0 bd 6b 00 00 02 04 05 b4 01 01 04 02 

00 04 75 8d 49 c7 00 01 03 cd 50 3c 08 00 45 00 
00 28 08 0f 40 00 80 06 00 00 c0 a8 ec 20 c0 a8 
ec 1e 04 16 00 50 b8 63 45 fe e9 bb 68 c0 50 10 
fa f0 59 ab 00 00 

00 04 75 8d 49 c7 00 01 03 cd 50 3c 08 00 45 00 
00 ed 08 10 40 00 80 06 00 00 c0 a8 ec 20 c0 a8 
ec 1e 04 16 00 50 b8 63 45 fe e9 bb 68 c0 50 18 
fa f0 5a 70 00 00 47 45 54 20 2f 20 48 54 54 50 
2f 31 2e 31 0d 0a 41 63 63 65 70 74 3a 20 2a 2f 
2a 0d 0a 41 63 63 65 70 74 2d 4c 61 6e 67 75 61 
67 65 3a 20 65 6e 2d 75 73 0d 0a 41 63 63 65 70 
74 2d 45 6e 63 6f 64 69 6e 67 3a 20 67 7a 69 70 
2c 20 64 65 66 6c 61 74 65 0d 0a 55 73 65 72 2d 
41 67 65 6e 74 3a 20 4d 6f 7a 69 6c 6c 61 2f 34 
2e 30 20 28 63 6f 6d 70 61 74 69 62 6c 65 3b 20 
4d 53 49 45 20 36 2e 30 3b 20 57 69 6e 64 6f 77 
73 20 4e 54 20 35 2e 30 29 0d 0a 48 6f 73 74 3a 
20 31 39 32 2e 31 36 38 2e 32 33 36 2e 33 30 0d 
0a 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 4b 65 65 
70 2d 41 6c 69 76 65 0d 0a 0d 0a 

1 Answers1

0

An array is fixed length, and it appears you don't know how many bytes you're going to have until you hit a blank line and you don't know how many total packets you're going to have.

I would suggest using an ArrayList<ArrayList<Byte>> to hold all the packets - this will grow as needed.

Create an initial ArrayList<Byte> Then it's a simple loop:

  • While readline() isn't null
  • check for blank line
  • if not blank
    • parse the String into a String[] using String.split()
    • iterate through String[] and convert each hex pair into a byte and add to the ArrayList<Btye>
  • if blank
    • add ArrayList<Byte> to ArrayList<ArrayList<Byte>>
    • create new ArrayList<Byte>

When readline() returns null the loop exits and your ArrayList<ArrayList<Byte>> contains all the packets.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161