/** * Sets the length of the file in blocks. The file will not be expanded if it * is already large enough. The file will be truncated to the correct length * when {@link #close} is called. * * @param nrBlocks the length of the file in blocks. * @throws IOException if an I/O error occurs. */ public void setNrBlocks(long nrBlocks) throws IOException { if (nrBlocks == this.nrBlocks) return; long prevNrBlocks = this.nrBlocks; super.setNrBlocks(nrBlocks); if (nrBlocks <= prevNrBlocks) return; // Call mapFile() if the file must grow in size. int nrRegions = (nrBlocks > 0) ? (int) ((((nrBlocks - 1) * blockSize) / stride) + 1) : 0; if (nrRegions > nrMappedRegions) { mapFile(nrRegions); } }
/** * Sets the length of the file in blocks. * * @param nrBlocks The number of blocks in the file. * @throws IOException if an I/O error occurs. */ public void setNrBlocks(long nrBlocks) throws IOException { if (nrBlocks == this.nrBlocks) return; super.setNrBlocks(nrBlocks); if (nrBlocks <= allocatedNrBlocks) return; allocatedNrBlocks = nrBlocks - (nrBlocks % allocationSize) + allocationSize; for (;;) { try { raf.setLength(allocatedNrBlocks * blockSize); break; } catch (ClosedChannelException ex) { // The Channel may have been inadvertently closed by another thread // being interrupted. Attempt to reopen the channel. if (!ensureOpen()) { throw ex; } // Loop back and retry the setLength(). } } }