Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ninjacave.rabrg.map;
- import java.io.File;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.nio.MappedByteBuffer;
- import java.nio.channels.FileChannel;
- public final class WorldMapFactory {
- public static WorldMap createMap(final String directory) throws IOException {
- return createMap(new File(directory));
- }
- public static WorldMap createMap(final File directory) throws IOException {
- try (final RandomAccessFile file = new RandomAccessFile(directory, "r"); final FileChannel channel = file.getChannel()) {
- final MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
- final int width = buffer.getShort();
- final int height = buffer.getShort();
- final int[][] map = new int[width][height];
- for (int offset = 0; offset < buffer.remaining(); offset++) {
- map[offset / width][offset % width] = buffer.getShort();
- }
- return createMap(map);
- }
- }
- public static WorldMap createMap(final int[][] map) {
- return new WorldMap(map);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement