ScrapExplorer - FileConstructor.java

Home / lab / FileConstruct Lines: 1 | Size: 8385 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* FileConstruct 2 * Construct custom files with ease. 3 * 4 * FileConstructor.java 5 * 6 * COPYRIGHT NOTICE 7 * Copyright (C) 2024 0x4248 and contributors 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the license is not changed. 10 * 11 * This software is free and open source. Licensed under the GNU general 12 * public license version 3.0 as published by the Free Software Foundation. 13 */ 14 15package lab.FileConstruct; 16 17import java.util.Arrays; 18 19public class FileConstructor { 20 21 public byte[] data; 22 public String filename; 23 public int size; 24 public boolean verbose; 25 26 /** 27 * Constructor object for creating a file 28 */ 29 public FileConstructor() { 30 data = new byte[0]; 31 size = 0; 32 filename = ""; 33 verbose = false; 34 } 35 36 /** 37 * PutByte - Place a single byte into the file 38 */ 39 public void PutByte(byte b) { 40 if (verbose) { 41 String message = "[ FC ] PLACE: " + b + " LOCATION: " + size; 42 } 43 byte[] newData = new byte[data.length + 1]; 44 System.arraycopy(data, 0, newData, 0, data.length); 45 newData[data.length] = b; 46 size += 1; 47 data = newData; 48 } 49 50 /** 51 * PutBytes - Place an array of bytes into the file 52 */ 53 public void PutBytes(byte[] b) { 54 if (verbose) { 55 String message = 56 "[ FC ] PUTBYTES: " + Arrays.toString(b) + " LOCATION: " + size; 57 System.out.println(message); 58 } 59 60 byte[] newData = new byte[data.length + b.length]; 61 System.arraycopy(data, 0, newData, 0, data.length); 62 System.arraycopy(b, 0, newData, data.length, b.length); 63 size += b.length; 64 data = newData; 65 } 66 67 /** 68 * PutASCII - Place a string into the file 69 */ 70 public void PutASCII(String s) { 71 if (verbose) { 72 String message = "[ FC ] PUTASCII: " + s + " LOCATION: " + size; 73 System.out.println(message); 74 } 75 size += s.length(); 76 PutBytes(s.getBytes()); 77 } 78 79 /** 80 * PutString - Place a string into the file 81 */ 82 public void PutString(String s) { 83 if (verbose) { 84 String message = "[ FC ] PUTSTRING: " + s + " LOCATION: " + size; 85 System.out.println(message); 86 } 87 88 PutBytes(s.getBytes()); 89 } 90 91 /** 92 * PutInt - Place an integer into the file 93 */ 94 public void PutInt(int i) { 95 if (verbose) { 96 String message = "[ FC ] PUTINT: " + i + " LOCATION: " + size; 97 System.out.println(message); 98 } 99 PutBytes( 100 new byte[] { 101 (byte) (i >> 24), 102 (byte) (i >> 16), 103 (byte) (i >> 8), 104 (byte) i, 105 } 106 ); 107 } 108 109 /** 110 * PutBool - Place a boolean into the file (as a bit) 111 */ 112 public void PutBool(boolean b) { 113 if (verbose) { 114 String message = "[ FC ] PUTBOOL: " + b + " LOCATION: " + size; 115 System.out.println(message); 116 } 117 PutByte((byte) (b ? 1 : 0)); 118 } 119 120 /** 121 * PutBit - Place a bit into the file 122 */ 123 public void PutBit(int b) { 124 PutByte((byte) (b & 1)); 125 } 126 127 /** 128 * PutHexStr - Place a hex string into the file 129 * Example: "55 AA" -> 0x55, 0xAA 130 */ 131 public void PutHexStr(String s) { 132 if (verbose) { 133 String message = "[ FC ] PUTHEXSTR: " + s + " LOCATION: " + size; 134 System.out.println(message); 135 } 136 String[] hex = s.split(" "); 137 for (String h : hex) { 138 PutByte((byte) Integer.parseInt(h, 16)); 139 } 140 } 141 142 /** 143 * Fill - Fill the file with a byte array pattern 144 */ 145 public void Fill(int length, byte[] b) { 146 if (verbose) { 147 String message = 148 "[ FC ] FILL: " + 149 Arrays.toString(b) + 150 " LENGTH: " + 151 length + 152 " LOCATION: " + 153 size; 154 System.out.println(message); 155 } 156 byte[] newData = new byte[data.length + length]; 157 System.arraycopy(data, 0, newData, 0, data.length); 158 for (int i = 0; i < length; i++) { 159 newData[data.length + i] = b[i % b.length]; 160 } 161 size += length; 162 data = newData; 163 } 164 165 /** 166 * ReplaceByte - Replace a byte at a specific index 167 */ 168 public void ReplaceByte(int index, byte b) { 169 if (verbose) { 170 String message = "[ FC ] REPLACEBYTE: " + b + " LOCATION: " + index; 171 System.out.println(message); 172 } 173 data[index] = b; 174 } 175 176 /** 177 * ReplaceBytes - Replace a byte array at a specific index 178 */ 179 public void ReplaceBytes(int index, byte[] b) { 180 if (verbose) { 181 String message = 182 "[ FC ] REPLACEBYTES: " + 183 Arrays.toString(b) + 184 " LOCATION: " + 185 index; 186 System.out.println(message); 187 } 188 for (int i = 0; i < b.length; i++) { 189 data[index + i] = b[i]; 190 } 191 } 192 193 /** 194 * ReplaceASCII - Replace a string at a specific index 195 */ 196 public void ReplaceASCII(int index, String s) { 197 if (verbose) { 198 String message = 199 "[ FC ] REPLACEASCII: " + s + " LOCATION: " + index; 200 System.out.println(message); 201 } 202 ReplaceBytes(index, s.getBytes()); 203 } 204 205 /** 206 * ReplaceString - Replace a string at a specific index 207 */ 208 public void ReplaceString(int index, String s) { 209 if (verbose) { 210 String message = 211 "[ FC ] REPLACESTRING: " + s + " LOCATION: " + index; 212 System.out.println(message); 213 } 214 ReplaceBytes(index, s.getBytes()); 215 } 216 217 /** 218 * ReplaceInt - Replace an integer at a specific index 219 */ 220 public void ReplaceInt(int index, int i) { 221 if (verbose) { 222 String message = "[ FC ] REPLACEINT: " + i + " LOCATION: " + index; 223 System.out.println(message); 224 } 225 ReplaceBytes( 226 index, 227 new byte[] { 228 (byte) (i >> 24), 229 (byte) (i >> 16), 230 (byte) (i >> 8), 231 (byte) i, 232 } 233 ); 234 } 235 236 /** 237 * ReplaceBool - Replace a boolean at a specific index 238 */ 239 public void ReplaceBool(int index, boolean b) { 240 if (verbose) { 241 String message = "[ FC ] REPLACEBOOL: " + b + " LOCATION: " + index; 242 System.out.println(message); 243 } 244 ReplaceByte(index, (byte) (b ? 1 : 0)); 245 } 246 247 /** 248 * ReplaceBit - Replace a bit at a specific index 249 */ 250 public void ReplaceBit(int index, int b) { 251 if (verbose) { 252 String message = "[ FC ] REPLACEBIT: " + b + " LOCATION: " + index; 253 System.out.println(message); 254 } 255 ReplaceByte(index, (byte) (b & 1)); 256 } 257 258 /** 259 * Dump - Write the file to disk 260 */ 261 public void Dump() { 262 if (verbose) { 263 String message = "[ FC ] DUMP: " + filename; 264 System.out.println(message); 265 } 266 try { 267 java.io.FileOutputStream fos = new java.io.FileOutputStream( 268 filename 269 ); 270 fos.write(data); 271 fos.close(); 272 if (verbose) { 273 String message = 274 "[ FC ] WROTE: " + size + " BYTES TO: " + filename; 275 System.out.println(message); 276 } 277 } catch (java.io.IOException e) { 278 e.printStackTrace(); 279 } 280 } 281 282 /** 283 * Load - Load a file from disk 284 */ 285 public void Load(String filename) { 286 if (verbose) { 287 String message = "[ FC ] LOAD: " + filename; 288 System.out.println(message); 289 } 290 try { 291 java.io.FileInputStream fis = new java.io.FileInputStream(filename); 292 data = fis.readAllBytes(); 293 size = data.length; 294 fis.close(); 295 if (verbose) { 296 String message = 297 "[ FC ] LOADED: " + size + " BYTES FROM: " + filename; 298 System.out.println(message); 299 } 300 } catch (java.io.IOException e) { 301 e.printStackTrace(); 302 } 303 } 304} 305
[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.