ScrapExplorer - Template.java

Home / lab / decomplab / src Lines: 1 | Size: 3572 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1package lab.decomplab.src; 2 3import java.io.*; 4import java.nio.*; 5import java.nio.charset.StandardCharsets; 6import java.nio.file.*; 7 8public class Template { 9 10 public class BinaryAsciiUtils { 11 12 public static String readAscii(ByteBuffer buf, int len) { 13 byte[] bytes = new byte[len]; 14 buf.get(bytes); 15 return new String(bytes, StandardCharsets.US_ASCII); 16 } 17 18 public static String shortToAscii(short value) { 19 byte low = (byte) (value & 0xFF); 20 byte high = (byte) ((value >> 8) & 0xFF); 21 return new String( 22 new byte[] { low, high }, 23 StandardCharsets.US_ASCII 24 ); 25 } 26 27 public static String intToAscii(int value) { 28 byte b0 = (byte) (value & 0xFF); 29 byte b1 = (byte) ((value >> 8) & 0xFF); 30 byte b2 = (byte) ((value >> 16) & 0xFF); 31 byte b3 = (byte) ((value >> 24) & 0xFF); 32 return new String( 33 new byte[] { b0, b1, b2, b3 }, 34 StandardCharsets.US_ASCII 35 ); 36 } 37 38 public static String longToAscii(long value) { 39 byte b0 = (byte) (value & 0xFF); 40 byte b1 = (byte) ((value >> 8) & 0xFF); 41 byte b2 = (byte) ((value >> 16) & 0xFF); 42 byte b3 = (byte) ((value >> 24) & 0xFF); 43 byte b4 = (byte) ((value >> 32) & 0xFF); 44 byte b5 = (byte) ((value >> 40) & 0xFF); 45 byte b6 = (byte) ((value >> 48) & 0xFF); 46 byte b7 = (byte) ((value >> 56) & 0xFF); 47 return new String( 48 new byte[] { b0, b1, b2, b3, b4, b5, b6, b7 }, 49 StandardCharsets.US_ASCII 50 ); 51 } 52 } 53 54 static class HeaderA { 55 56 byte flag; 57 short version; 58 int dataSize; 59 long timestamp; 60 61 void read(ByteBuffer buf) { 62 flag = buf.get(); 63 version = buf.getShort(); 64 dataSize = buf.getInt(); 65 timestamp = buf.getLong(); 66 } 67 68 void print() { 69 System.out.println("HeaderA:"); 70 System.out.println(" flag: " + flag); 71 System.out.println(" version: " + version); 72 System.out.println(" dataSize: " + dataSize); 73 System.out.println( 74 " timestamp: " + BinaryAsciiUtils.longToAscii(timestamp) 75 ); 76 } 77 } 78 79 static class HeaderB { 80 81 char label; 82 int count; 83 short mode; 84 byte active; 85 86 void read(ByteBuffer buf) { 87 label = (char) buf.get(); 88 count = buf.getInt(); 89 mode = buf.getShort(); 90 active = buf.get(); 91 } 92 93 void print() { 94 System.out.println("HeaderB:"); 95 System.out.println(" label: " + label); 96 System.out.println(" count: " + count); 97 System.out.println(" mode: " + mode); 98 System.out.println(" active: " + active); 99 } 100 } 101 102 public static void main(String[] args) throws IOException { 103 if (args.length != 1) { 104 System.out.println("Usage: java Template <file>"); 105 return; 106 } 107 108 byte[] data = Files.readAllBytes(Path.of(args[0])); 109 ByteBuffer buf = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN); 110 111 HeaderA headA = new HeaderA(); 112 headA.read(buf); 113 headA.print(); 114 115 buf.position(buf.position() + 10); 116 117 HeaderB headB = new HeaderB(); 118 headB.read(buf); 119 headB.print(); 120 } 121} 122
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.