Advertisement
Chiddix

WorldMapFactory

May 19th, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. package com.ninjacave.rabrg.map;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.RandomAccessFile;
  6. import java.nio.MappedByteBuffer;
  7. import java.nio.channels.FileChannel;
  8.  
  9. public final class WorldMapFactory {
  10.  
  11.     public static WorldMap createMap(final String directory) throws IOException {
  12.         return createMap(new File(directory));
  13.     }
  14.  
  15.     public static WorldMap createMap(final File directory) throws IOException {
  16.         try (final RandomAccessFile file = new RandomAccessFile(directory, "r"); final FileChannel channel = file.getChannel()) {
  17.             final MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
  18.             final int width = buffer.getShort();
  19.             final int height = buffer.getShort();
  20.             final int[][] map = new int[width][height];
  21.             for (int offset = 0; offset < buffer.remaining(); offset++) {
  22.                 map[offset / width][offset % width] = buffer.getShort();
  23.             }
  24.             return createMap(map);
  25.         }
  26.     }
  27.  
  28.     public static WorldMap createMap(final int[][] map) {
  29.         return new WorldMap(map);
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement