/** * Writes the given byte to this file at position {@code pos}. {@code pos} may be greater than * the current size of this file, in which case this file is resized and all bytes between the * current size and {@code pos} are set to 0. Returns the number of bytes written. * * @throws IOException if the file needs more blocks but the disk is full */ public int write(long pos, byte b) throws IOException { prepareForWrite(pos, 1); byte[] block = blocks[blockIndex(pos)]; int off = offsetInBlock(pos); block[off] = b; if (pos >= size) { size = pos + 1; } return 1; }
/** * Writes all available bytes from buffer {@code buf} to this file starting at position {@code * pos}. {@code pos} may be greater than the current size of this file, in which case this file * is resized and all bytes between the current size and {@code pos} are set to 0. Returns the * number of bytes written. * * @throws IOException if the file needs more blocks but the disk is full */ public int write(long pos, ByteBuffer buf) throws IOException { int len = buf.remaining(); prepareForWrite(pos, len); if (len == 0) { return 0; } int blockIndex = blockIndex(pos); byte[] block = blocks[blockIndex]; int off = offsetInBlock(pos); put(block, off, buf); while (buf.hasRemaining()) { block = blocks[++blockIndex]; put(block, 0, buf); } long endPos = pos + len; if (endPos > size) { size = endPos; } return len; }
prepareForWrite(pos, 0); // don't assume the full count bytes will be written
prepareForWrite(pos, len);
/** * Writes the given byte to this file at position {@code pos}. {@code pos} may be greater than * the current size of this file, in which case this file is resized and all bytes between the * current size and {@code pos} are set to 0. Returns the number of bytes written. * * @throws IOException if the file needs more blocks but the disk is full */ public int write(long pos, byte b) throws IOException { prepareForWrite(pos, 1); byte[] block = blocks[blockIndex(pos)]; int off = offsetInBlock(pos); block[off] = b; if (pos >= size) { size = pos + 1; } return 1; }
/** * Writes all available bytes from buffer {@code buf} to this file starting at position {@code * pos}. {@code pos} may be greater than the current size of this file, in which case this file * is resized and all bytes between the current size and {@code pos} are set to 0. Returns the * number of bytes written. * * @throws IOException if the file needs more blocks but the disk is full */ public int write(long pos, ByteBuffer buf) throws IOException { int len = buf.remaining(); prepareForWrite(pos, len); if (len == 0) { return 0; } int blockIndex = blockIndex(pos); byte[] block = blocks[blockIndex]; int off = offsetInBlock(pos); put(block, off, buf); while (buf.hasRemaining()) { block = blocks[++blockIndex]; put(block, 0, buf); } long endPos = pos + len; if (endPos > size) { size = endPos; } return len; }
prepareForWrite(pos, 0); // don't assume the full count bytes will be written