MCPcopy Create free account
hub / github.com/demoth/jake2 / LoadMappedFile

Method LoadMappedFile

qcommon/src/main/java/jake2/qcommon/filesystem/FS.java:356–427  ·  view source on GitHub ↗

Filename are relative to the quake search path a null buffer will just return the file content as ByteBuffer (memory mapped) Used for big files like cinematics

(String filename)

Source from the content-addressed store, hash-verified

354 * Used for big files like cinematics
355 */
356 public static ByteBuffer LoadMappedFile(String filename) {
357
358 try {
359 // check for links first
360 for (filelink_t fs_link : fs_links) {
361 // if filename starts with fs.from
362 if (filename.startsWith(fs_link.from)) {
363 File file = new File(fs_link.to + filename.substring(fs_link.from.length()));
364 if (file.canRead()) {
365 try (FileInputStream input = new FileInputStream(file);
366 FileChannel channel = input.getChannel()) {
367 int fileLength = (int) channel.size();
368 return channel.map(FileChannel.MapMode.READ_ONLY, 0, fileLength);
369 }
370 }
371 }
372 }
373
374 //
375 // search through the path, one element at a time
376 //
377 for (SearchPath search : searchPaths) {
378 // is the element a pak file?
379 if (search.pack != null) {
380 // look through all the pak file elements
381 packfile_t entry = search.pack.files.get(filename.toLowerCase());
382
383 if (entry != null) {
384 // found it!
385 //Com.DPrintf ("PackFile: " + pak.filename + " : " +
386 // filename + '\n');
387 File file = new File(search.pack.filename);
388 if (!file.canRead())
389 Com.Error(Defines.ERR_FATAL, "Couldn't reopen "
390 + search.pack.filename);
391 if (search.pack.handle == null || !search.pack.handle.getFD().valid()) {
392 // hold the pakfile handle open
393 search.pack.handle = new RandomAccessFile(search.pack.filename, "r");
394 }
395 // open a new file on the pakfile
396 if (search.pack.mappedFileChannel == null) {
397 try (FileChannel channel = search.pack.handle.getChannel()) {
398 search.pack.mappedFileChannel = channel.map(FileChannel.MapMode.READ_ONLY, 0, search.pack.handle.length());
399 }
400 }
401 search.pack.mappedFileChannel.position(entry.filepos);
402 ByteBuffer buffer = search.pack.mappedFileChannel.slice();
403 buffer.limit(entry.filelen);
404 return buffer;
405 }
406 } else {
407 // check a file in the directory tree
408
409 File file = new File(search.filename + '/' + filename);
410 if (!file.canRead())
411 continue;
412
413 //Com.DPrintf("FindFile: " + netpath +'\n');

Callers 2

LoadPCXMethod · 0.95
PlayCinematicMethod · 0.95

Calls 3

ErrorMethod · 0.95
sizeMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected