Java ByteBuffer Write writeRemaining(WritableByteChannel channel, ByteBuffer buffer)

Here you can find the source of writeRemaining(WritableByteChannel channel, ByteBuffer buffer)

Description

write Remaining

License

Apache License

Declaration

public static void writeRemaining(WritableByteChannel channel, ByteBuffer buffer) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.FileChannel;

import java.nio.channels.WritableByteChannel;

public class Main {
    private final static int MAX_NOOP_TRIALS = 1024;

    public static void writeRemaining(WritableByteChannel channel, ByteBuffer buffer) throws IOException {

        int noopCountDown = MAX_NOOP_TRIALS;

        while (buffer.hasRemaining()) {
            if (channel.write(buffer) == 0) {
                if (--noopCountDown == 0)
                    throw new IOException("Write failed after " + MAX_NOOP_TRIALS + " trials");
                continue;
            }//  w  w w.  j  a va 2  s  . c  o  m
            noopCountDown = MAX_NOOP_TRIALS;
        }
    }

    public static void writeRemaining(FileChannel file, long position, ByteBuffer buffer) throws IOException {

        int noopCountDown = MAX_NOOP_TRIALS;

        while (buffer.hasRemaining()) {
            int amountWritten = file.write(buffer, position);
            position += amountWritten;
            if (amountWritten == 0) {
                if (--noopCountDown == 0)
                    throw new IOException("Write failed after " + MAX_NOOP_TRIALS + " trials");
                continue;
            }
            noopCountDown = MAX_NOOP_TRIALS;
        }
    }
}

Related

  1. writeLong(long num, ByteBuffer out)
  2. writeLong(long v, ByteBuffer buffer)
  3. writeLTriad(int triad, ByteBuffer bb)
  4. writeNullTerminatedString(ByteBuffer buf, String s, String encoding)
  5. writePackageName(ByteBuffer buffer, String packageName)
  6. writeResBit15(int value, ByteBuffer toBuffer)
  7. writeShortString(ByteBuffer buffer, String s)
  8. writeSign(ByteBuffer bb)
  9. writeSignedVarint(ByteBuffer buffer, int val)