/** * Set the length of the file. This will expand or shrink the file. * * @param newLength the new file size */ public void setLength(long newLength) { if (SysProperties.CHECK && newLength % Constants.FILE_BLOCK_SIZE != 0) { DbException.throwInternalError("unaligned setLength " + name + " pos " + newLength); } checkPowerOff(); checkWritingAllowed(); try { if (newLength > fileLength) { long pos = filePos; file.position(newLength - 1); FileUtils.writeFully(file, ByteBuffer.wrap(new byte[1])); file.position(pos); } else { file.truncate(newLength); } fileLength = newLength; } catch (IOException e) { closeFileSilently(); throw DbException.convertIOException(e, name); } }
/** * Write a number of bytes. * * @param b the source buffer * @param off the offset * @param len the number of bytes to write */ public void write(byte[] b, int off, int len) { if (SysProperties.CHECK && (len < 0 || len % Constants.FILE_BLOCK_SIZE != 0)) { DbException.throwInternalError("unaligned write " + name + " len " + len); } checkWritingAllowed(); checkPowerOff(); try { FileUtils.writeFully(file, ByteBuffer.wrap(b, off, len)); } catch (IOException e) { closeFileSilently(); throw DbException.convertIOException(e, name); } filePos += len; fileLength = Math.max(filePos, fileLength); }