/* write a chunk data to the region file at specified sector number */ private void write(int sectorNumber, byte[] data, int length) throws IOException { debugln(" " + sectorNumber); file.seek(sectorNumber * SECTOR_BYTES); file.writeInt(length + 1); // chunk length file.writeByte(VERSION_DEFLATE); // chunk version number file.write(data, 0, length); // chunk data }
public RegionFile(File path) { offsets = new int[SECTOR_INTS]; chunkTimestamps = new int[SECTOR_INTS]; fileName = path; debugln("REGION LOAD " + fileName); sizeDelta = 0; try { if (path.exists()) { lastModified = path.lastModified(); } file = new RandomAccessFile(path, "rw"); if (file.length() < SECTOR_BYTES) { /* we need to write the chunk offset table */ for (int i = 0; i < SECTOR_INTS; ++i) { file.writeInt(0); } // write another sector for the timestamp info for (int i = 0; i < SECTOR_INTS; ++i) { file.writeInt(0); } sizeDelta += SECTOR_BYTES * 2; } if ((file.length() & 0xfff) != 0) { /* the file size is not a multiple of 4KB, grow it */ for (int i = 0; i < (file.length() & 0xfff); ++i) { file.write((byte) 0); } } /* set up the available sector map */ int nSectors = (int) file.length() / SECTOR_BYTES; sectorFree = new ArrayList<Boolean>(nSectors); for (int i = 0; i < nSectors; ++i) { sectorFree.add(true); } sectorFree.set(0, false); // chunk offset table sectorFree.set(1, false); // for the last modified info file.seek(0); for (int i = 0; i < SECTOR_INTS; ++i) { int offset = file.readInt(); offsets[i] = offset; if (offset != 0 && (offset >> 8) + (offset & 0xFF) <= sectorFree.size()) { for (int sectorNum = 0; sectorNum < (offset & 0xFF); ++sectorNum) { sectorFree.set((offset >> 8) + sectorNum, false); } } } for (int i = 0; i < SECTOR_INTS; ++i) { int lastModValue = file.readInt(); chunkTimestamps[i] = lastModValue; } } catch (IOException e) { e.printStackTrace(); } }
private void setTimestamp(int x, int z, int value) throws IOException { chunkTimestamps[x + z * 32] = value; file.seek(SECTOR_BYTES + (x + z * 32) * 4); file.writeInt(value); }
private void setOffset(int x, int z, int offset) throws IOException { offsets[x + z * 32] = offset; file.seek((x + z * 32) * 4); file.writeInt(offset); }