IOUtils { public static void writeFully(WritableByteChannel bc, ByteBuffer buf) throws IOException { do { bc.write(buf); } while (buf.remaining() > 0); }  static void copyBytes(InputStream in, OutputStream out, int buffSize, boolean close); static void copyBytes(InputStream in, OutputStream out, int buffSize); static void copyBytes(InputStream in, OutputStream out, Configuration conf); static void copyBytes(InputStream in, OutputStream out, Configuration conf, boolean close); static void copyBytes(InputStream in, OutputStream out, long count,
      boolean close); static int wrappedReadForCompressedData(InputStream is, byte[] buf,
      int off, int len); static void readFully(InputStream in, byte buf[],
      int off, int len); static void skipFully(InputStream in, long len); static void cleanup(Log log, java.io.Closeable... closeables); static void closeStream(java.io.Closeable stream); static void closeSocket(Socket sock); static void writeFully(WritableByteChannel bc, ByteBuffer buf); static void writeFully(FileChannel fc, ByteBuffer buf,
      long offset); static List<String> listDirectory(File dir, FilenameFilter filter);  }
@Test public void testWriteFully() throws IOException { final int INPUT_BUFFER_LEN = 10000; final int HALFWAY = 1 + (INPUT_BUFFER_LEN / 2); byte[] input = new byte[INPUT_BUFFER_LEN]; for (int i = 0; i < input.length; i++) { input[i] = (byte)(i & 0xff); } byte[] output = new byte[input.length]; try { RandomAccessFile raf = new RandomAccessFile(TEST_FILE_NAME, "rw"); FileChannel fc = raf.getChannel(); ByteBuffer buf = ByteBuffer.wrap(input); IOUtils.writeFully(fc, buf); raf.seek(0); raf.read(output); for (int i = 0; i < input.length; i++) { assertEquals(input[i], output[i]); } buf.rewind(); IOUtils.writeFully(fc, buf, HALFWAY); for (int i = 0; i < HALFWAY; i++) { assertEquals(input[i], output[i]); } raf.seek(0); raf.read(output); for (int i = HALFWAY; i < input.length; i++) { assertEquals(input[i - HALFWAY], output[i]); } } finally { File f = new File(TEST_FILE_NAME); if (f.exists()) { f.delete(); } } }