44import java .io .File ;
55import java .io .IOException ;
66import java .io .RandomAccessFile ;
7- import java .util .ArrayList ;
87import java .util .Arrays ;
9- import java .util .List ;
8+ import java .util .BitSet ;
109
1110import static engine .world .chunk .storage .RegionConstants .*;
1211
@@ -21,30 +20,28 @@ public class RegionFile implements AutoCloseable {
2120
2221 private final RandomAccessFile file ;
2322 private final int [] chunkStartSectors ;
24- private final List <Boolean > usedSectors ;
23+ private final BitSet usedSectors ;
24+ private int sectorCount ;
2525
2626 public RegionFile (File file ) throws IOException {
2727 this .file = new RandomAccessFile (file , "rw" );
2828 this .chunkStartSectors = new int [REGION_SIZE ];
2929 Arrays .fill (chunkStartSectors , -1 );
30+ this .usedSectors = new BitSet ();
3031
3132 if (this .file .length () < REGION_HEADER_SIZE ) { // Initialize empty region file
3233 this .file .seek (0 );
3334 for (int i = 0 ; i < REGION_SIZE ; i ++) {
3435 this .file .writeInt (-1 );
3536 }
36- usedSectors = new ArrayList <>() ;
37+ sectorCount = 0 ;
3738 return ;
3839 }
3940
4041 // Initialize exists region file
4142 loadChunkStartSectors ();
4243
43- int sectorCount = getSectorCount (this .file .length ()) - 4 ;
44- usedSectors = new ArrayList <>(sectorCount );
45- for (int i = 0 ; i < sectorCount ; i ++) {
46- usedSectors .add (false );
47- }
44+ sectorCount = getSectorCount (this .file .length ()) - 4 ;
4845 initUsedSectors ();
4946 }
5047
@@ -61,8 +58,8 @@ private void initUsedSectors() throws IOException {
6158 continue ;
6259 }
6360
64- int sectorCount = getSectorCount (getChunkDataLength (startSector ));
65- useSectors (startSector , startSector + sectorCount );
61+ int chunkSectorCount = getSectorCount (getChunkDataLength (startSector ));
62+ useSectors (startSector , startSector + chunkSectorCount );
6663 }
6764 }
6865
@@ -128,15 +125,11 @@ private int getChunkIndex(int chunkX, int chunkY, int chunkZ) {
128125 }
129126
130127 private void useSectors (int start , int end ) {
131- for (int i = start ; i < end ; i ++) {
132- usedSectors .set (i , true );
133- }
128+ usedSectors .set (start , end );
134129 }
135130
136131 private void freeSectors (int start , int end ) {
137- for (int i = start ; i < end ; i ++) {
138- usedSectors .set (i , false );
139- }
132+ usedSectors .clear (start , end );
140133 }
141134
142135 private void setStartSector (int chunkIndex , int startSector ) throws IOException {
@@ -148,7 +141,7 @@ private void setStartSector(int chunkIndex, int startSector) throws IOException
148141 private int allocateSectors (int count ) {
149142 int startSector = -1 ;
150143 int allocatedSector = 0 ;
151- for (int i = 0 ; i < usedSectors . size () ; i ++) { // find unused sectors
144+ for (int i = 0 ; i < sectorCount ; i ++) {
152145 if (usedSectors .get (i )) {
153146 startSector = -1 ;
154147 allocatedSector = 0 ;
@@ -166,16 +159,14 @@ private int allocateSectors(int count) {
166159 }
167160
168161 if (startSector == -1 ) { // if no found sector
169- startSector = usedSectors . size () ;
162+ startSector = sectorCount ;
170163 }
171164
172- useSectors (startSector , startSector + allocatedSector );
173-
165+ useSectors (startSector , startSector + count );
174166 if (allocatedSector < count ) { // add enough sectors
175- for (int i = 0 , size = count - allocatedSector ; i < size ; i ++) {
176- usedSectors .add (true );
177- }
167+ sectorCount += count - allocatedSector ;
178168 }
169+
179170 return startSector ;
180171 }
181172
0 commit comments