Java Code Examples for java.nio.MappedByteBuffer#order()

The following examples show how to use java.nio.MappedByteBuffer#order() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BufferUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static ByteBuffer createByteBufferFile(int size) {
    try {
        if (tmpDir != null && logger.isLoggable(Level.INFO)) {
            logger.log(Level.INFO, "tmpDir = {0}", tmpDir.getAbsoluteFile());
        }
        File tmpFile = File.createTempFile("pmd","tmp", tmpDir);
        if (logger.isLoggable(Level.INFO)) {
            logger.log(Level.INFO, "tmpFile = {0}", tmpFile.getAbsoluteFile());
        }
        RandomAccessFile os = new RandomAccessFile(tmpFile, "rw");
        os.seek(size);
        os.write(0);
        FileChannel ch = os.getChannel();
        MappedByteBuffer  bb = ch.map(MapMode.READ_WRITE, 0, size);
        os.close();
        ch.close();
        tmpFile.delete();
        bb.order(ByteOrder.nativeOrder());
        return bb;
    } catch(IOException ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 2
Source File: Crc32AppendingFileWriterTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void write() throws Exception {
    byte output[] = new byte[data.length];
    int crc;

    try (FileChannel fd = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
        try (FileWriter writer = new Crc32AppendingFileWriter(new FileChannelWriter(fd, 0), 4)) {
            ByteBuffer buf = ByteBuffer.wrap(data);
            while (buf.hasRemaining())
                writer.write(buf);
        }

        MappedByteBuffer mapped = fd.map(FileChannel.MapMode.READ_ONLY, 0, fd.size());
        mapped.order(ByteOrder.BIG_ENDIAN);
        mapped.get(output);
        for (int i = 0; i < padLen; ++i)
            assertEquals((byte)0, mapped.get());
        crc = mapped.getInt();
        assertFalse(mapped.hasRemaining());
    }

    assertArrayEquals(data, output);
    assertEquals(expectedCrc, crc);
}
 
Example 3
Source File: XdrEncodingFileWriterTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void writeBytes() throws Exception {
    byte output[] = new byte[bytes.length];

    try (FileChannel fd = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
        try (XdrEncodingFileWriter writer = new XdrEncodingFileWriter(new FileChannelWriter(fd, 0))) {
            writer.beginEncoding();
            writer.xdrEncodeOpaque(bytes, bytes.length);
            writer.endEncoding();
        }

        MappedByteBuffer map = fd.map(FileChannel.MapMode.READ_ONLY, 0, fd.size());
        map.order(ByteOrder.BIG_ENDIAN);
        map.get(output);

        assertFalse(map.hasRemaining());
    }

    assertArrayEquals(bytes, output);
}
 
Example 4
Source File: XdrEncodingFileWriterTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void writeInts() throws Exception {
    int output[] = new int[ints.length];

    try (FileChannel fd = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
        try (XdrEncodingFileWriter writer = new XdrEncodingFileWriter(new FileChannelWriter(fd, 0))) {
            writer.beginEncoding();
            writer.xdrEncodeIntFixedVector(ints, ints.length);
            writer.endEncoding();
        }

        MappedByteBuffer map = fd.map(FileChannel.MapMode.READ_ONLY, 0, fd.size());
        map.order(ByteOrder.BIG_ENDIAN);
        for (int i = 0; i < output.length; ++i)
            output[i] = map.getInt();

        assertFalse(map.hasRemaining());
    }

    assertArrayEquals(ints, output);
}
 
Example 5
Source File: FileSerializationBenchmark.java    From imhotep with Apache License 2.0 5 votes vote down vote up
@Override
public int[] deserialize(File file) throws IOException {
    MappedByteBuffer buffer = Files.map(file, FileChannel.MapMode.READ_ONLY, file.length());
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    IntBuffer intBuffer = buffer.asIntBuffer();
    int[] ret = new int[(int)(file.length() / 4)];
    intBuffer.get(ret);
    return ret;
}
 
Example 6
Source File: FileSerializationBenchmark.java    From imhotep with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(int[] a, File file) throws IOException {
    MappedByteBuffer buffer = Files.map(file, FileChannel.MapMode.READ_WRITE, a.length * 4);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    IntBuffer intBuffer = buffer.asIntBuffer();
    for (int i = 0; i < a.length; ++i) {
        intBuffer.put(i, a[i]);
    }
}
 
Example 7
Source File: FileSerializationBenchmark.java    From imhotep with Apache License 2.0 5 votes vote down vote up
@Override
public int[] deserialize(File file) throws IOException {
    MappedByteBuffer buffer = Files.map(file, FileChannel.MapMode.READ_ONLY, file.length());
    buffer.order(ByteOrder.BIG_ENDIAN);
    IntBuffer intBuffer = buffer.asIntBuffer();
    int[] ret = new int[(int)(file.length() / 4)];
    intBuffer.get(ret);
    return ret;
}
 
Example 8
Source File: FileSerializationBenchmark.java    From imhotep with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(int[] a, File file) throws IOException {
    MappedByteBuffer buffer = Files.map(file, FileChannel.MapMode.READ_WRITE, a.length * 4);
    buffer.order(ByteOrder.BIG_ENDIAN);
    IntBuffer intBuffer = buffer.asIntBuffer();
    for (int i = 0; i < a.length; ++i) {
        intBuffer.put(i, a[i]);
    }
}
 
Example 9
Source File: AntiCrashCrimeScene.java    From antsdb with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void open(boolean write) throws IOException {
    if (this.raf != null) {
        return;
    }
    
    // rename the existing file so we dont accidently lose previous crash scene
    if (write && file.exists()) {
        String filename = file.getName();
        filename = System.currentTimeMillis() + "-" + filename;
        File backup = new File(file.getParentFile(), filename);
        file.renameTo(backup);
    }
    
    // open it
    this.raf = new RandomAccessFile(file, write ? "rw" : "r");
    this.ch = this.raf.getChannel();
    
    // map units if this is read only
    if (!write) {
        int nbuffers = (int)(this.file.length() / Unit.SIZE); 
        MappedByteBuffer mmf = raf.getChannel().map(MapMode.READ_ONLY, 0, Unit.SIZE * nbuffers);
        this.mmfs.add(mmf);
        mmf.order(ByteOrder.nativeOrder());
        for (int i=0; i<nbuffers; i++) {
            Unit ii = new Unit();
            mmf.position(i * Unit.SIZE);
            mmf.limit(mmf.position() + Unit.SIZE);
            ii.buf = mmf.slice();
            ii.addr = UberUtil.getAddress(ii.buf);
            this.units.add(ii);
        }
        this.ch.close();
        this.raf.close();
    }
}
 
Example 10
Source File: Zip.java    From turbine with Apache License 2.0 5 votes vote down vote up
private byte[] getBytes(
    long offset, int nameLength, int cenExtLength, long size, boolean deflate) {
  if (size > Integer.MAX_VALUE) {
    throw new IllegalArgumentException("unsupported zip entry size: " + size);
  }
  try {
    MappedByteBuffer fc =
        chan.map(
            MapMode.READ_ONLY,
            offset,
            Math.min(
                LOCHDR + nameLength + cenExtLength + size + EXTRA_FIELD_SLACK,
                chan.size() - offset));
    fc.order(ByteOrder.LITTLE_ENDIAN);
    checkSignature(path, fc, /* index= */ 0, 3, 4, "LOCSIG");
    int locExtLength = fc.getChar(LOCEXT);
    if (locExtLength > cenExtLength + EXTRA_FIELD_SLACK) {
      // If the local header's extra fields don't match the central directory and we didn't
      // leave enough slac, re-map the data section with the correct extra field length.
      fc = chan.map(MapMode.READ_ONLY, offset + LOCHDR + nameLength + locExtLength, size);
      fc.order(ByteOrder.LITTLE_ENDIAN);
    } else {
      // Otherwise seek past the local header, name, and extra fields to the data.
      fc.position(LOCHDR + nameLength + locExtLength);
      fc.limit((int) (LOCHDR + nameLength + locExtLength + size));
    }
    byte[] bytes = new byte[(int) size];
    fc.get(bytes);
    if (deflate) {
      bytes =
          ByteStreams.toByteArray(
              new InflaterInputStream(
                  new ByteArrayInputStream(bytes), new Inflater(/*nowrap=*/ true)));
    }
    return bytes;
  } catch (IOException e) {
    throw new IOError(e);
  }
}
 
Example 11
Source File: ByteBufferUtil.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
public static ByteBuffer mapFile(@NonNull File f, long offset, @NonNull ByteOrder byteOrder) throws IOException {
  FileInputStream dataFile = new FileInputStream(f);
  try {
    FileChannel fc = dataFile.getChannel();
    MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, offset, f.length() - offset);
    buffer.order(byteOrder);
    return buffer;
  } finally {
    dataFile.close(); // this *also* closes the associated channel, fc
  }
}
 
Example 12
Source File: MemoryAlignedFileReader.java    From letv with Apache License 2.0 5 votes vote down vote up
private ccrcrr bь044C044C044C044C044C(int i) throws IOException, NotEnoughHapticBytesAvailableException {
    try {
        this.bЭ042DЭЭЭ042D.startTiming();
        if (i < this.b042D042DЭЭЭ042D) {
            int i2 = this.bЭЭ042DЭЭ042D + i;
            int bщ04490449щщщ = bщ04490449щщщ();
            int i3 = (i + 1024) + bщ04490449щщщ <= this.b042D042DЭЭЭ042D ? bщ04490449щщщ + 1024 : this.b042D042DЭЭЭ042D - i;
            try {
                if (i + i3 > this.bЭЭЭ042D042DЭ) {
                    throw new NotEnoughHapticBytesAvailableException("Not enough bytes available yet.");
                } else {
                    MappedByteBuffer map = this.bЭЭ042DЭ042DЭ.map(MapMode.READ_ONLY, (long) i2, (long) i3);
                    if (map != null) {
                        map.order(ByteOrder.BIG_ENDIAN);
                        ccrcrr rrrrrr_ccrcrr = new ccrcrr();
                        rrrrrr_ccrcrr.mMappedByteBuffer = map;
                        if (((bц04460446ц04460446() + bцц0446ц04460446) * bц04460446ц04460446()) % bццц044604460446 != b044604460446ц04460446) {
                            b04460446цц04460446 = bц04460446ц04460446();
                            b044604460446ц04460446 = 10;
                        }
                        rrrrrr_ccrcrr.mHapticDataOffset = i;
                        return rrrrrr_ccrcrr;
                    }
                }
            } catch (Exception e) {
                throw e;
            }
        }
        return null;
    } catch (Exception e2) {
        throw e2;
    }
}
 
Example 13
Source File: MemoryMappedFileManager.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
public static MappedByteBuffer mmap(final FileChannel fileChannel, final String fileName, final long start,
        final int size) throws IOException {
    for (int i = 1;; i++) {
        try {
            LOGGER.debug("MMapAppender remapping {} start={}, size={}", fileName, start, size);

            final long startNanos = System.nanoTime();
            final MappedByteBuffer map = fileChannel.map(FileChannel.MapMode.READ_WRITE, start, size);
            map.order(ByteOrder.nativeOrder());

            final float millis = (float) ((System.nanoTime() - startNanos) / NANOS_PER_MILLISEC);
            LOGGER.debug("MMapAppender remapped {} OK in {} millis", fileName, millis);

            return map;
        } catch (final IOException e) {
            if (e.getMessage() == null || !e.getMessage().endsWith("user-mapped section open")) {
                throw e;
            }
            LOGGER.debug("Remap attempt {}/{} failed. Retrying...", i, MAX_REMAP_COUNT, e);
            if (i < MAX_REMAP_COUNT) {
                Thread.yield();
            } else {
                try {
                    Thread.sleep(1);
                } catch (final InterruptedException ignored) {
                    Thread.currentThread().interrupt();
                    throw e;
                }
            }
        }
    }
}
 
Example 14
Source File: Zip.java    From turbine with Apache License 2.0 4 votes vote down vote up
public ZipIterable(Path path) throws IOException {
  this.path = path;
  this.chan = FileChannel.open(path, StandardOpenOption.READ);
  // Locate the EOCD
  long size = chan.size();
  if (size < ENDHDR) {
    throw new ZipException("invalid zip archive");
  }
  long eocdOffset = size - ENDHDR;
  MappedByteBuffer eocd = chan.map(MapMode.READ_ONLY, eocdOffset, ENDHDR);
  eocd.order(ByteOrder.LITTLE_ENDIAN);
  int index = 0;
  int commentSize = 0;
  if (!isSignature(eocd, 0, 5, 6)) {
    // The archive may contain a zip file comment; keep looking for the EOCD.
    long start = Math.max(0, size - ENDHDR - 0xFFFF);
    eocd = chan.map(MapMode.READ_ONLY, start, (size - start));
    eocd.order(ByteOrder.LITTLE_ENDIAN);
    index = (int) ((size - start) - ENDHDR);
    while (index > 0) {
      index--;
      eocd.position(index);
      if (isSignature(eocd, index, 5, 6)) {
        commentSize = (int) ((size - start) - ENDHDR) - index;
        eocdOffset = start + index;
        break;
      }
    }
  }
  checkSignature(path, eocd, index, 5, 6, "ENDSIG");
  int totalEntries = eocd.getChar(index + ENDTOT);
  long cdsize = UnsignedInts.toLong(eocd.getInt(index + ENDSIZ));
  int actualCommentSize = eocd.getChar(index + ENDCOM);
  if (commentSize != actualCommentSize) {
    throw new ZipException(
        String.format(
            "zip file comment length was %d, expected %d", commentSize, actualCommentSize));
  }
  // If the number of entries is 0xffff, check if the archive has a zip64 EOCD locator.
  if (totalEntries == ZIP64_MAGICCOUNT) {
    // Assume the zip64 EOCD has the usual size; we don't support zip64 extensible data sectors.
    long zip64eocdOffset = size - ENDHDR - ZIP64_LOCHDR - ZIP64_ENDHDR;
    MappedByteBuffer zip64eocd = chan.map(MapMode.READ_ONLY, zip64eocdOffset, ZIP64_ENDHDR);
    zip64eocd.order(ByteOrder.LITTLE_ENDIAN);
    // Note that zip reading is necessarily best-effort, since an archive could contain 0xFFFF
    // entries and the last entry's data could contain a ZIP64_ENDSIG. Some implementations
    // read the full EOCD records and compare them.
    if (zip64eocd.getInt(0) == ZIP64_ENDSIG) {
      cdsize = zip64eocd.getLong(ZIP64_ENDSIZ);
      eocdOffset = zip64eocdOffset;
    }
  }
  this.cd = chan.map(MapMode.READ_ONLY, eocdOffset - cdsize, cdsize);
  cd.order(ByteOrder.LITTLE_ENDIAN);
}
 
Example 15
Source File: ANNIndex.java    From Indra with MIT License 4 votes vote down vote up
private void load(final String filename) throws IOException {
    memoryMappedFile = new RandomAccessFile(filename, "r");
    long fileSize = memoryMappedFile.length();
    if (fileSize == 0L) {
        throw new IOException("Index is a 0-byte file?");
    }

    int numNodes = (int) (fileSize / NODE_SIZE);
    int buffIndex =  (numNodes - 1) / MAX_NODES_IN_BUFFER;
    int rest = (int) (fileSize % BLOCK_SIZE);
    int blockSize = (rest > 0 ? rest : BLOCK_SIZE);
    // Two valid relations between dimension and file size:
    // 1) rest % NODE_SIZE == 0 makes sure either everything fits into buffer or rest is a multiple of NODE_SIZE;
    // 2) (file_size - rest) % NODE_SIZE == 0 makes sure everything else is a multiple of NODE_SIZE.
    if (rest % NODE_SIZE != 0 || (fileSize - rest) % NODE_SIZE != 0) {
        throw new RuntimeException("ANNIndex initiated with wrong dimension size");
    }
    long position = fileSize - blockSize;
    buffers = new MappedByteBuffer[buffIndex + 1];
    boolean process = true;
    int m = -1;
    long index = fileSize;
    while (position >= 0) {
        MappedByteBuffer annBuf = memoryMappedFile.getChannel().map(
                FileChannel.MapMode.READ_ONLY, position, blockSize);
        annBuf.order(ByteOrder.LITTLE_ENDIAN);

        buffers[buffIndex--] = annBuf;

        for (int i = blockSize - (int) NODE_SIZE; process && i >= 0; i -= NODE_SIZE) {
            index -= NODE_SIZE;
            int k = annBuf.getInt(i);  // node[i].n_descendants
            if (m == -1 || k == m) {
                roots.add(index);
                m = k;
            } else {
                process = false;
            }
        }
        blockSize = BLOCK_SIZE;
        position -= blockSize;
    }
}
 
Example 16
Source File: ReadOnlyMemMap.java    From sparkey-java with Apache License 2.0 4 votes vote down vote up
private MappedByteBuffer createChunk(final long offset, final long size) throws IOException {
  final MappedByteBuffer map = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, offset, size);
  map.order(ByteOrder.LITTLE_ENDIAN);
  return map;
}
 
Example 17
Source File: ANNIndex.java    From annoy-java with Apache License 2.0 4 votes vote down vote up
private void load(final String filename) throws IOException {
  memoryMappedFile = new RandomAccessFile(filename, "r");
  long fileSize = memoryMappedFile.length();
  if (fileSize == 0L) {
    throw new IOException("Index is a 0-byte file?");
  }

  int numNodes = (int) (fileSize / NODE_SIZE);
  int buffIndex =  (numNodes - 1) / MAX_NODES_IN_BUFFER;
  int rest = (int) (fileSize % BLOCK_SIZE);
  int blockSize = (rest > 0 ? rest : BLOCK_SIZE);
  // Two valid relations between dimension and file size:
  // 1) rest % NODE_SIZE == 0 makes sure either everything fits into buffer or rest is a multiple of NODE_SIZE;
  // 2) (file_size - rest) % NODE_SIZE == 0 makes sure everything else is a multiple of NODE_SIZE.
  if (rest % NODE_SIZE != 0 || (fileSize - rest) % NODE_SIZE != 0) {
    throw new RuntimeException("ANNIndex initiated with wrong dimension size");
  }
  long position = fileSize - blockSize;
  buffers = new MappedByteBuffer[buffIndex + 1];
  boolean process = true;
  int m = -1;
  long index = fileSize;
  while (position >= 0) {
    MappedByteBuffer annBuf = memoryMappedFile.getChannel().map(
            FileChannel.MapMode.READ_ONLY, position, blockSize);
    annBuf.order(ByteOrder.LITTLE_ENDIAN);

    buffers[buffIndex--] = annBuf;

    for (int i = blockSize - (int) NODE_SIZE; process && i >= 0; i -= NODE_SIZE) {
      index -= NODE_SIZE;
      int k = annBuf.getInt(i);  // node[i].n_descendants
      if (m == -1 || k == m) {
        roots.add(index);
        m = k;
      } else {
        process = false;
      }
    }
    blockSize = BLOCK_SIZE;
    position -= blockSize;
  }
}
 
Example 18
Source File: GeoWorldLoader.java    From aion-germany with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("resource")
public static Map<String, Spatial> loadMeshs(String fileName) throws IOException {
	Map<String, Spatial> geoms = new HashMap<String, Spatial>();
	File geoFile = new File(fileName);
	FileChannel roChannel = null;
	MappedByteBuffer geo = null;
	roChannel = new RandomAccessFile(geoFile, "r").getChannel();
	int size = (int) roChannel.size();
	geo = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
	geo.order(ByteOrder.LITTLE_ENDIAN);
	while (geo.hasRemaining()) {
		short namelenght = geo.getShort();
		byte[] nameByte = new byte[namelenght];
		geo.get(nameByte);
		String name = new String(nameByte).intern();
		Node node = new Node(DEBUG ? name : null);
		byte intentions = 0;
		byte singleChildMaterialId = -1;
		int modelCount = geo.getShort();
		for (int c = 0; c < modelCount; c++) {
			Mesh m = new Mesh();

			int vectorCount = (geo.getInt()) * 3;
			ByteBuffer floatBuffer = ByteBuffer.allocateDirect(vectorCount * 4);
			FloatBuffer vertices = floatBuffer.asFloatBuffer();
			for (int x = 0; x < vectorCount; x++) {
				vertices.put(geo.getFloat());
			}

			int triangles = geo.getInt();
			ByteBuffer shortBuffer = ByteBuffer.allocateDirect(triangles * 2);
			ShortBuffer indexes = shortBuffer.asShortBuffer();
			for (int x = 0; x < triangles; x++) {
				indexes.put(geo.getShort());
			}

			Geometry geom = null;
			m.setCollisionFlags(geo.getShort());
			if ((m.getIntentions() & CollisionIntention.MOVEABLE.getId()) != 0) {
				// TODO: skip moveable collisions (ships, shugo boxes), not handled yet
				continue;
			}
			intentions |= m.getIntentions();
			m.setBuffer(VertexBuffer.Type.Position, 3, vertices);
			m.setBuffer(VertexBuffer.Type.Index, 3, indexes);
			m.createCollisionData();

			if ((intentions & CollisionIntention.DOOR.getId()) != 0 && (intentions & CollisionIntention.PHYSICAL.getId()) != 0) {
				if (!GeoDataConfig.GEO_DOORS_ENABLE) {
					continue;
				}
				geom = new DoorGeometry(name, m);
				// what if doors have few models ?
			}
			else {
				MaterialTemplate mtl = DataManager.MATERIAL_DATA.getTemplate(m.getMaterialId());
				geom = new Geometry(null, m);
				if (mtl != null || m.getMaterialId() == 11) {
					node.setName(name);
				}
				if (modelCount == 1) {
					geom.setName(name);
					singleChildMaterialId = geom.getMaterialId();
				}
				else {
					geom.setName(("child" + c + "_" + name).intern());
				}
				node.attachChild(geom);
			}
			geoms.put(geom.getName(), geom);
		}
		node.setCollisionFlags((short) (intentions << 8 | singleChildMaterialId & 0xFF));
		if (!node.getChildren().isEmpty()) {
			geoms.put(name, node);
		}
	}
	destroyDirectByteBuffer(geo);
	return geoms;

}
 
Example 19
Source File: GeoEngine.java    From L2jOrg with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Loads geodata from a file. When file does not exist, is corrupted or not consistent, loads none geodata.
 *
 *
 * @param filePath : The Geodata File Path
 * @param regionX : Geodata file region X coordinate.
 * @param regionY : Geodata file region Y coordinate.
 * @return boolean : True, when geodata file was loaded without problem.
 */
private boolean loadGeoBlocks(Path filePath, int regionX, int regionY) {

    // standard load
    try (RandomAccessFile raf = new RandomAccessFile(filePath.toAbsolutePath().toString(), "r");
         FileChannel fc = raf.getChannel()) {
        // initialize file buffer
        MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).load();
        buffer.order(ByteOrder.LITTLE_ENDIAN);

        // get block indexes
        final int blockX = (regionX - World.TILE_X_MIN) * GeoStructure.REGION_BLOCKS_X;
        final int blockY = (regionY - World.TILE_Y_MIN) * GeoStructure.REGION_BLOCKS_Y;

        // loop over region blocks
        for (int ix = 0; ix < GeoStructure.REGION_BLOCKS_X; ix++) {
            for (int iy = 0; iy < GeoStructure.REGION_BLOCKS_Y; iy++) {
                // get block type
                final byte type = buffer.get();

                // load block according to block type
                blocks[blockX + ix][blockY + iy] = switch (type) {
                    case GeoStructure.TYPE_FLAT_L2D -> new BlockFlat(buffer, GeoFormat.L2D);
                    case GeoStructure.TYPE_COMPLEX_L2D -> new BlockComplex(buffer, GeoFormat.L2D);
                    case GeoStructure.TYPE_MULTILAYER_L2D -> new BlockMultilayer(buffer, GeoFormat.L2D);
                    default -> throw new IllegalArgumentException("Unknown block type: " + type);
                };
            }
        }

        // check data consistency
        if (buffer.remaining() > 0) {
            LOGGER.warn("GeoEngine: Region file {} can be corrupted, remaining {} bytes to read.", filePath, buffer.remaining());
        }

        // loading was successful
        return true;
    } catch (Exception e) {
        LOGGER.error("Error while loading {} region file.", filePath);
        LOGGER.error(e.getMessage());

        // replace whole region file with null blocks
        loadNullBlocks(regionX, regionY);

        // loading was not successful
        return false;
    }
}
 
Example 20
Source File: StackLogDecoder.java    From unidbg with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    File stackLog = new File("target/stack-logs.78490.2000.unidbg.zcmkle.index");
    FileInputStream inputStream = new FileInputStream(stackLog);
    FileChannel channel = inputStream.getChannel();
    MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, stackLog.length());
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    int i = 0;
    while (buffer.remaining() >= 16) {
        long size = buffer.getInt() & 0xffffffffL;
        long addr = (buffer.getInt() & 0xffffffffL) ^ 0x00005555;
        long offset_and_flags_l = buffer.getInt() & 0xffffffffL;
        long offset_and_flags_h = buffer.getInt() & 0xffffffffL;
        int flag = (int) ((offset_and_flags_h & 0xff000000) >> 24);
        long stackId = ((offset_and_flags_h & 0x00ffffff) << 32) | offset_and_flags_l;
        String action = "OTHER";
        boolean isFree = false;
        switch (flag) {
            case MALLOC_LOG_TYPE_ALLOCATE:
                action = "ALLOC";
                isFree = false;
                break;
            case MALLOC_LOG_TYPE_DEALLOCATE:
                action = "FREE ";
                isFree = true;
                break;
            case stack_logging_type_vm_allocate:
                action = "MMAP ";
                isFree = false;
                break;
            case stack_logging_type_vm_deallocate:
                action = "UNMAP";
                isFree = true;
                break;
            default:
                if ((flag & stack_logging_type_mapped_file_or_shared_mem) != 0 && (flag & stack_logging_type_vm_allocate) != 0) {
                    action = "MMAPF";
                    isFree = false;
                    break;
                }

                System.err.println(flag);
                break;
        }
        String msg = String.format("[%08d]: %s, stackId=0x%014x, address=0x%08x, size=0x%x", i++, action, stackId, addr, size);
        if (isFree) {
            System.err.println(msg);
        } else {
            System.out.println(msg);
        }
    }
    channel.close();
    inputStream.close();
}