1 4 package com.tc.util.io; 5 6 import java.io.IOException ; 7 import java.nio.ByteBuffer ; 8 import java.nio.channels.GatheringByteChannel ; 9 10 13 public class MockGatheringByteChannel extends MockWritableByteChannel implements GatheringByteChannel { 14 15 public synchronized long write(ByteBuffer [] srcs, int offset, int length) throws IOException { 16 checkOpen(); 17 if (srcs == null) { throw new IOException ("null ByteBuffer[] passed in to write(ByteBuffer[], int, int)"); } 18 checkNull(srcs); 19 throw new IOException ("Not yet implemented"); 20 } 21 22 public synchronized long write(ByteBuffer [] srcs) throws IOException { 23 checkOpen(); 24 if (srcs == null) { throw new IOException ("null ByteBuffer[] passed in to write(ByteBuffer[])"); } 25 checkNull(srcs); 26 long bytesWritten = 0; 27 for (int pos = 0; pos < srcs.length && bytesWritten < getMaxWriteCount(); ++pos) { 28 ByteBuffer buffer = srcs[pos]; 29 while (buffer.hasRemaining() && bytesWritten < getMaxWriteCount()) { 30 buffer.get(); 31 ++bytesWritten; 32 } 33 } 34 return bytesWritten; 35 } 36 37 private void checkNull(ByteBuffer [] srcs) throws IOException { 38 for (int pos = 0; pos < srcs.length; ++pos) { 39 if (srcs[pos] == null) { throw new IOException ("Null ByteBuffer at array position[" + pos + "]"); } 40 } 41 } 42 } 43 | Popular Tags |