KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > io > MockGatheringByteChannel


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util.io;
5
6 import java.io.IOException JavaDoc;
7 import java.nio.ByteBuffer JavaDoc;
8 import java.nio.channels.GatheringByteChannel JavaDoc;
9
10 /**
11  * dev-null implementation of a gathering byte channel.
12  */

13 public class MockGatheringByteChannel extends MockWritableByteChannel implements GatheringByteChannel JavaDoc {
14
15   public synchronized long write(ByteBuffer JavaDoc[] srcs, int offset, int length) throws IOException JavaDoc {
16     checkOpen();
17     if (srcs == null) { throw new IOException JavaDoc("null ByteBuffer[] passed in to write(ByteBuffer[], int, int)"); }
18     checkNull(srcs);
19     throw new IOException JavaDoc("Not yet implemented");
20   }
21
22   public synchronized long write(ByteBuffer JavaDoc[] srcs) throws IOException JavaDoc {
23     checkOpen();
24     if (srcs == null) { throw new IOException JavaDoc("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 JavaDoc 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 JavaDoc[] srcs) throws IOException JavaDoc {
38     for (int pos = 0; pos < srcs.length; ++pos) {
39       if (srcs[pos] == null) { throw new IOException JavaDoc("Null ByteBuffer at array position[" + pos + "]"); }
40     }
41   }
42 }
43
Popular Tags