Returns the offset to a free slot for writing an entry with the specified length. Fills the original space with 0xFF to facilitate future write operations. @param pos original offset @param size size of new text entry @return new offset to store text
(final long pos, final int size)
| 341 | * @return new offset to store text |
| 342 | */ |
| 343 | public long free(final long pos, final int size) { |
| 344 | // old text size (available space) |
| 345 | int os = readNum(pos) + (int) (cursor() - pos); |
| 346 | |
| 347 | // extend available space by subsequent zero-bytes |
| 348 | cursor(pos + os); |
| 349 | for(; pos + os < length && os < size && read() == 0xFF; os++); |
| 350 | |
| 351 | long o = pos; |
| 352 | if(pos + os == length) { |
| 353 | // entry is placed last: reset file length (discard last entry) |
| 354 | length(pos); |
| 355 | } else { |
| 356 | int t = size; |
| 357 | if(os < size) { |
| 358 | // gap is too small for new entry... |
| 359 | // reset cursor to overwrite entry |
| 360 | cursor(pos); |
| 361 | t = 0; |
| 362 | // place new entry after last entry |
| 363 | o = length; |
| 364 | } else { |
| 365 | // gap is large enough: set cursor to overwrite remaining bytes |
| 366 | cursor(pos + size); |
| 367 | } |
| 368 | // fill gap with 0xFF for future updates |
| 369 | while(t++ < os) write(0xFF); |
| 370 | } |
| 371 | return o; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Sets the file length. |