Java ByteBuffer Write writeFully(FileChannel channel, long offset, ByteBuffer buf)

Here you can find the source of writeFully(FileChannel channel, long offset, ByteBuffer buf)

Description

write Fully

License

Open Source License

Declaration

static void writeFully(FileChannel channel, long offset, ByteBuffer buf) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;
import java.nio.file.StandardOpenOption;

public class Main {
    static void writeFully(FileChannel channel, long offset, ByteBuffer buf) throws IOException {
        int remaining = buf.limit() - buf.position();

        while (remaining > 0) {
            int written = channel.write(buf, offset);
            if (written < 0)
                throw new EOFException();
            remaining -= written;//from w  w  w. j a v  a 2s .c  o  m
        }
    }

    static void writeFully(FileChannel channel, long offset, File from) throws IOException {
        long fromLen = from.length();
        FileChannel fromc = FileChannel.open(from.toPath(), StandardOpenOption.READ);

        long remaining = fromLen;

        while (remaining > 0) {
            long written = channel.transferFrom(fromc, offset + fromLen - remaining, remaining);
            if (written < 0)
                throw new EOFException();
            remaining -= written;
        }
        fromc.close();
    }
}

Related

  1. writeFully(@Nonnull final FileChannel dst, @Nonnull final ByteBuffer src, @Nonnegative final long position)
  2. writeFully(ByteBuffer buf, WritableByteChannel out)
  3. writeFully(ByteBuffer buffer, WritableByteChannel channel)
  4. writeFully(FileChannel channel, ByteBuffer fileInfosBuffer)
  5. writeFully(FileChannel channel, ByteBuffer src)
  6. writeFully(FileChannel fc, ByteBuffer buf, long offset)
  7. writeFully(FileChannel fileChannel, long filePosition, ByteBuffer buffer)
  8. writeFully(final FileChannel channel, final ByteBuffer dst, final long position)
  9. writeFully(final WritableByteChannel channel, final ByteBuffer buf)