@@ -112,18 +112,20 @@ protected void mapIt(long offset, long byteCount, boolean clearNew) {
112
112
if (byteCount <= capacity ())
113
113
return ;
114
114
115
- int i = 0 ;
116
- int segmentsToMap = (int ) (byteCount / segmentSizeInBytes );
115
+ long longSegmentSize = segmentSizeInBytes ;
116
+ int segmentsToMap = (int ) (byteCount / longSegmentSize );
117
117
if (segmentsToMap < 0 )
118
118
throw new IllegalStateException ("Too many segments needs to be allocated. Increase segmentSize." );
119
119
120
- if (byteCount % segmentSizeInBytes != 0 )
120
+ if (byteCount % longSegmentSize != 0 )
121
121
segmentsToMap ++;
122
122
if (segmentsToMap == 0 )
123
123
throw new IllegalStateException ("0 segments are not allowed." );
124
124
125
125
long bufferStart = offset ;
126
126
int newSegments ;
127
+ int i = 0 ;
128
+ long newFileLength = offset + segmentsToMap * longSegmentSize ;
127
129
try {
128
130
// ugly remapping
129
131
// http://stackoverflow.com/q/14011919/194609
@@ -135,19 +137,20 @@ protected void mapIt(long offset, long byteCount, boolean clearNew) {
135
137
// This approach is probably problematic but a bit faster if done often.
136
138
// Here we rely on the OS+file system that increasing the file
137
139
// size has no effect on the old mappings!
138
- bufferStart += segments .size () * segmentSizeInBytes ;
140
+ bufferStart += segments .size () * longSegmentSize ;
139
141
newSegments = segmentsToMap - segments .size ();
140
142
}
141
- raFile .setLength (offset + segmentsToMap * segmentSizeInBytes );
143
+ raFile .setLength (newFileLength );
142
144
for (; i < newSegments ; i ++) {
143
- segments .add (newByteBuffer (bufferStart , segmentSizeInBytes ));
144
- bufferStart += segmentSizeInBytes ;
145
+ segments .add (newByteBuffer (bufferStart , longSegmentSize ));
146
+ bufferStart += longSegmentSize ;
145
147
}
146
148
} catch (IOException ex ) {
147
149
// we could get an exception here if buffer is too small and area too large
148
150
// e.g. I got an exception for the 65421th buffer (probably around 2**16 == 65536)
149
151
throw new RuntimeException ("Couldn't map buffer " + i + " of " + segmentsToMap
150
- + " at position " + bufferStart + " for " + byteCount + " bytes with offset " + offset , ex );
152
+ + " at position " + bufferStart + " for " + byteCount
153
+ + " bytes with offset " + offset + ", new fileLength:" + newFileLength , ex );
151
154
}
152
155
}
153
156
@@ -206,7 +209,7 @@ public boolean loadExisting() {
206
209
try {
207
210
long byteCount = readHeader (raFile );
208
211
if (byteCount < 0 )
209
- return false ;
212
+ return false ;
210
213
mapIt (HEADER_OFFSET , byteCount - HEADER_OFFSET , false );
211
214
return true ;
212
215
} catch (IOException ex ) {
0 commit comments